Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
I have developed a ThingShape in Java as a POJO.
I place this shape on several templates, hence why it's a shape.
Once I have an instantiated Thing, is there any way I can cast that Thing as this ThingShape it uses to get access to the public services, without having to use the reflective 'processServiceRequest(serviceName, params) method?
It's not a huge deal, but seems like due to the reflective nature of how Things are loaded, this would be doable, by either extending the ThingShape in Java from Thing or by some other mechanism.
On the Thing level you will only be able to use the services that apply to an instance, so from a ThingShape perspective only the custom (My) services, properties and events you've added. If you wanted to do something that is executed against the ThingShape itself, you will need to specifically do that as ThingShapes[NameOfShape].Service/Property/Event I don't know any way around that.
But if it is something you added as a MY item, then in the context of the Thing you can use me.Service/Property/Event
Yeah I was hoping there was something I could do like this:
Thing t = EntityUtilities.findEntity("MyThingName", ThingworxRelationshipTypes.Thing);
MyThingShape mts = (MyThingShape) t;
mts.myServiceName();
Bummer
The alternative is not bad but just inconsistent with my other things. For the services internal to the shape I do this:
private Thing getMe() throws Exception {
Object meObj = ThreadLocalContext.getMeContext();
if (meObj instanceof Thing) {
return (Thing)meObj;
} else {
throw new Exception("Cannot cast me to Thing");
}
}
private void someServce() {
Thing t = getMe();
t.whateverThingmethodIneed();
someOtherService();
}
private void someOtherServce() {
//code-n-stuff
}
But then in any other code that needs to access this services defined on the thing shape, it goes something like this
Thing t = EntityUtilities.findEntity("MyThingName", ThingworxRelationshipTypes.Thing);
DeviceTools.runSomeService(t); //DeviceTools is just a wrapper that uses the reflective Thing.processServiceRequest(...)
Excuse any typos!