Handling dynamic timers
Hi all,
I have a couple of questions regarding the programatic creation of timers and subscriptions.
Context: I was implementing a mechanism to determine lost of connectivity of my devices (which they post a property change within an interval of time (e.g. 10 min) thought a PUT REST call against the Thing it represents it respectively). To do so I wanted to associate a timer for each created Thing as these timers need to be independent: each data change resets the timer preventing the Timer event from firing; when the timer is up it would mean the device did not post and hence we assume it has connection problems.
I was doing the above at the Template level so that whenever a Device is created it is automatically already setup with this lost of connection mechanism, inheriting it from the template's behaviors, without having us to worry about of creating an associated timer, subscription etc.
Hence, I configured the initialization part (creating the specific thing timer and associating a handler for it) by assigning a subscription for the ThingStart event within the respective Device Template. (since this needs to happen for each to be created Thing it seemed like a proper place to put it, but not sure if there are other places where we can put 'Thing initialization related code' )
var timerIdentifier = me.name+'Timer';
var params = {
name: timerIdentifier /* STRING */,
description: 'Data change timer' /* STRING */,
thingTemplateName: 'Timer' /* THINGTEMPLATENAME */
};
Resources["EntityServices"].CreateThing(params);
var subParams = {
thingName: timerIdentifier /* THINGNAME */,
eventName: "Timer" /* STRING */,
serviceName: TimerIsUp /* STRING */
};
me.AddDynamicSubscription(subParams);
Things[timerIdentifier].EnableTimer();
My first question would be how to pass the 'Update Rate' property of the timer at the moment of creation. Once the creation of it is done, is line #15 the correct way to start the timer?
Since I am defining this code on a template subscription, is it ok to just pass the service name on the sub params like above on line 12 or do I need to put the complete reference, e.g.: 'me.TimerIsUp' (defined on the template) ? This Template service would be the one responsible of changing the Status property to say 'OFFLINE'.
Later on, on the data change subscription handler for a certain property of the ThingTemplate I want to reset the timer and prevent the Timer event from firing.
var timer = Things[timerIdentifier];
if(timer) {
// stop and start timer.
timer.DisableTimer();
timer.EnableTimer();
}
Do I achieve this by calling sequentially: timer.DisableTimer() and timer.EnableTimer() ?
Is there a way of seeing the actual timers that get created in some sort of runtime view to monitors things? I don't seem to find them within the normal composer things view after having created manually a couple of new Devices that inherited from the mentioned Template. Hence I don't think this is actually working. At the moment I can not access or use the script log view which might be handy to debug with logger msg (will son get access).
Lastly, does this seem to be a feasible thing?
Thanks in advance for any help.

