Hello,
I just wanted to understand, on how we can have ThingWorx push an alert to the Java SDK ?
If I am not wrong, that this can be set up on ThingWorx by tying up alerts to the Subscriptions, but how do we access these Subscriptions from the Java SDK ?
Please guide me on how this can be achieved.
Thanks !
What exactly are you trying to accomplish? You should be able to retrieve a thing's subscriptions using the following Java syntax:
myThing.getEventSubscriptions();
Hope this helps!
Whenever an alert is generated on Thingworx, I am trying to push the same on Client and show a message on Client.
myThing.getEventSubscriptions();
This code will only give me the details of Subscriptions for that thing, using Java SDK, how can I know that the alert has been generated on Thingworx?
Thanks
Hello, this has proved a little bit tricky, but I got it. Basically, you need to create a service on a local thing on the Platform and then invoke that service in the SDK. The service should contain the following Javascript (I called it GetAlerts on MyThing, which used the GenericThing template; it returns an infotable of type InfotableDS, a data shape containing three fields as seen below):
var params = {
infoTableName : "InfoTable",
dataShapeName : "InfotableDS"
};
var allAlerts = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
var myProperties = me.GetPropertyDefinitions({});
for(var i = 0; i < myProperties.length; i++) {
var params = {
property: myProperties.getRow(i).name
};
var alerts = me.GetAlertDefinitions(params);
for(var j = 0; j < alerts.length; j++) {
var newEntry = new Object();
newEntry.Value1 = alerts.getRow(j).name;
newEntry.Value2 = "";
newEntry.Value3 = "";
allAlerts.AddRow(newEntry);
}
}
var result = allAlerts;
The Java code in the SDK then looks like this:
ValueCollection nullCollection = new ValueCollection();
InfoTable myAlerts = myClient.invokeService(com.thingworx.relationships.RelationshipTypes.ThingworxEntityTypes.Things, "MyThing", "GetAlerts", nullCollection, 100);
String output = "Alerts Retrieved:";
for(int i = 0; i < myAlerts.getLength(); i++) {
output += " " + myAlerts.getRow(i).getValue("Value1");
}
System.out.println(output);
Note that you have to pass a null ValueCollection into the invokeService method. Passing in "null" will not work. See KCS Article 212321 for details on that. Let me know of further issues with this!