I'm using Apama in Cumulocity. Whenever a managed object (device) is created in Cumulocity, I would like to provide it with some initial parameters, here the required interval the device needs to report to Cumulocity before it's considered to be unavailable.
My problem is that in Apama I don't seem to have any way to distinguish between create and update events. So if I receive a managed object, add some parameters to it and send it back to the managed object channel, I end up in a loop.
I can of course do some check after the event has been received, but I would prefer to filter on only the create events of managed objects and don't perform any IF checks.
Is there any way I can filter on only create events? What is the difference between the CHANNEL and the UPDATE_CHANNEL? It doesn't seem to make a difference which one I use.
My current code looks as follows. What I want to achieve is avoid the IF statement and filter directly on create events in the listener.
monitor InitializeDevice {
action onload() {
monitor.subscribe(ManagedObject.CHANNEL);
on all ManagedObject(type = "c8y_MQTTDevice") as mo {
log "###Received managed object. Content is: " + mo.toString() at INFO;
if (mo.params.hasKey("c8y_RequiredAvailability")) {
//Assuming an interval has already been set, do nothing.
log "###Received managed object with required availability fragment. Doing nothing." at INFO;
}
else {
//Set the response interval on the managed object
dictionary<string,any> params := mo.params;
dictionary<string,any> paramssub := new dictionary<string,any>;
paramssub.add("responseInterval",3);
params.add("c8y_RequiredAvailability",paramssub);
mo.params := params;
log "###Added required interval to managed object. Content is: " + mo.toString() at INFO;
send mo to ManagedObject.UPDATE_CHANNEL;
}
}
}
}
When I execute this monitor and create a new managed object, this is what is printed to the logs:
2019-05-27 16:15:07.310 INFO [12648] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO [12648] - InitializeDevice [6] ###Added required interval to managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<string,any>,{"responseInterval":any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO [12648] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<string,any>,{"responseInterval":any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO [12648] - InitializeDevice [6] ###Received managed object with required availability fragment. Doing nothing.
2019-05-27 16:15:08.244 INFO [7868] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_Availability":any(dictionary<any,any>,{any(string,"lastMessage"):any(dictionary<any,any>,{any(string,"date"):any(integer,27),any(string,"day"):any(integer,1),any(string,"hours"):any(integer,16),any(string,"minutes"):any(integer,15),any(string,"month"):any(integer,4),any(string,"seconds"):any(integer,7),any(string,"time"):any(integer,1558966507220),any(string,"timezoneOffset"):any(integer,-120),any(string,"year"):any(integer,119)}),any(string,"status"):any(string,"AVAILABLE")}),"c8y_Connection":any(dictionary<any,any>,{any(string,"status"):any(string,"DISCONNECTED")}),"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<any,any>,{any(string,"responseInterval"):any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:08.244 INFO [7868] - InitializeDevice [6] ###Received managed object with required availability fragment. Doing nothing.
Is there any way to filter directly on create events? Why do I received two print statements after the update?
Thanks Mathias