@Ziyad, again few options are available. For example:
- (for simple use cases) You can expose your code via a Resource with a "static" service, so that you use it like that:
var weight = Resources["MTViper"].GetImmediateReading({ host: "10.18.200.51", port: 9761 });
- (slightly more advanced) You can create a thing template instead of Resource (hostname and port are configured in thing's Configuration):
var weight = Things["MTViper-001"].GetImmediateReading();
- (probably the most advanced) The same as (2), but using RemoteThing as your base template. This will help you to avoid reconnecting for each reading, handle isConnected(), etc. So that your thing behaves like a proper remote thing:
var weight = Things["MTViper-001"].weight;
As you can imagine, (3) is the most complex and (1) is the easiest to implement. Let's consider the latter. Firstly, you need to get a copy of ThingWorx Extension SDK (make sure its version corresponds to the one of your platform). Then you'll need to create a Java class corresponding to your Resource:
package com.example;
// Imports
public class MTViperResource extends Resource {
@ThingworxServiceDefinition(name = "GetImmediateReading")
@ThingworxServiceResult(name = "result", baseType = "NUMBER")
public Double GetImmediateReading(
@ThingworxServiceParameter(name = "host", baseType = "STRING") String host,
@ThingworxServiceParameter(name = "port", baseType = "INTEGER") Integer port
) {
// ...do your TCP/IP exchange...
return 247.0;
}
}
...and a metadata.xml, which describes the extension:
<Entities>
<ExtensionPackages>
<ExtensionPackage name="MTViperExtension" vendor="My Company" packageVersion="0.1.0" minimumThingWorxVersion="6.6.0" />
</ExtensionPackages>
<Resources>
<Resource name="MTViper" className="com.example.MTViperResource">
<JarResources>
<FileResource type="JAR" file="mtviper.jar" />
</JarResources>
</Resource>
</Resources>
</Entities>
Compile your Java classes into mtviper.jar and zip it altogether like this:
mtviper-extension.zip
- metadata.xml
- lib/
- - common/
- - - mtviper.jar
Import this extension in ThingWorx and use it like I mentioned at the beginning of this post. If you need to update your code -- iterate the version in metadata.xml, re-import and restart ThingWorx. You'll find more details in ThingWorx Help and SDK package.
Regards,
Constantine