Jump to content









Photo

Listener Event for Device Status change?

Listener Event

  • Please log in to reply
27 replies to this topic

#1 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 09 March 2013 - 07:23 AM

What I would like to have is an e-mail sent to me when a device's Status changes from connected to disconnected or vice-versa.  

 

I'm looking at the listener events, and they all seem to be based on data.  What is the best way to construct the feature I would like for listening for a device coming online or going offline?

 

Thank you. 



#2 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 10 March 2013 - 11:45 AM

Hello Zambiot.

 

This event is called "data_change_state" and you can bind a listener to it. We will translate those names into more descriptive ones very soon.

 

Thanks for asking!


Alvaro Everlet

Carriots CTO


#3 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 10 March 2013 - 12:37 PM

OK.  What to use for the If Express then?  Something like the following but with my device?

 

context.device == "testdevice@Carriots"



#4 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 11 March 2013 - 04:59 PM

Context will give you information for build your expressions and rules with execution-time variables.

You can check your context information by printing “context” content in any expression.

For example you can set a listener like this one:

If expression: true

Then expression:
import com.carriots.sdk.utils.Email;def email = new Email();email.to="—replace with your destination email--";email.subject="Carriots context test";email.message="Context: "+context;email.send();


Then send a stream to the associated device and you will receive an email with something like:

Context: [session:no-session, data:[mydata:1], hierarchy:[project:[id_developer:defaultProject@carriots, _id:511899da987d75576900003a], service:[id_developer:defaultService@carriots, _id:511899ec5c5d758aec8790033], device:[id_developer:defaultDevice@carriots, frequency_stream:5, _id:51189a3e593eee58a69000034, frequency_status:5, status:no_status, checksum:null], customer:[id_developer:@carriots, _id:50d45734565d75aa2345038c], group:[id_developer:defaultGroup@carriots, _id:51189a0c598655969000034]], apiKey:acccb9f633a1a69860566f78a0a06123414c23d89179a111d85b64587a4867653]

Then access your context data using dot notation. For example:
  • context.data.mydata for mydata value
  • context.hierarchy for the full hierarchy array
  • context.hierarchy.device.id_developer for the device's id_developer

You can then use it in your listener's if expressions with something like: context.hierarchy.device.id_developer=='defaultDevice@carriots'

Enjoy!

Alvaro Everlet

Carriots CTO


#5 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 11 March 2013 - 10:54 PM

Thank you Alvaro.  I'll try this and go through it.  I see now how I can use context to get a lot of data and ensure I am using the right dot notation for a specific element.



#6 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 13 March 2013 - 01:38 PM

I have gotten the above example to work for checking the context.  I took it further such that I got an e-mail when data was received.  However, I don't see, and maybe it is a naming problem how "data_change_state" is the even to be used.  I had that event, but I didn't seem to get any activity until I changed the event to "data_received".

 

In the context, and in the view, there is the Status.  I suspect this status is changed based on receiving of streams and the value set in frequency.  What I would like is an email when the Status changes.  In other words, when a device is detected coming online, I'd like an e-mail.  And then another e-mail if the device isn't detected online after a timeout.

 

What settings do you recommend for this?



#7 smartmetercarriots

smartmetercarriots

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 15 March 2013 - 12:26 PM

Hello!

 

For the context you describe you have to define a listener watching the device (or the set of devices: group, service or project) for a data_state_change event to happen. Then you can track status changes and react.

 

For example you can setup a listener with this configuration:

 

Entity:

·      Group: myListenedGroup@carriots

·      Event: data_state_change

·      If expression: true

·      Then expression: code that send an email with context data

 

Full context data will be something like:

 

 

 

[session:no-session,
data:[
            reason:[id_developer:defaultDevice@carriots, updater:carriots, enabled:true, frequency_stream:1440, status:disconnected, id_group:defaultGroup@carriots, n_str:1363278057, properties:null, checksum:null, type:Computer, id_model:null, sensor:Other, time_zone:Europe/Madrid, updated_at:1363274534, frequency_status:1440, description:null, name:defaultDevice, owner:carriots, created_at:1363274534, id_asset:null],
            device:defaultDevice@carriots
            ],
            hierarchy:[
                       project:[id_developer:defaultProject@carriots, _id:51364549da5c5d75576900003a],
                       service:[id_developer:defaultService@carriots, _id:513456c5d758a69000033],
                       device:[id_developer:defaultDevice@carriots, frequency_stream:1440, _id:514134523454c5d75985b0001de, frequency_status:1440, status:ok, checksum:null],
                       customer:[id_developer:@carriots, _id:503453615c45675aa2200038c],
                       group:[id_developer:defaultGroup@carriots, _id:513dec3455d7465450000032]
           
]

 

 

Where “reason” holds the device data that has caused the change state event. In this example the device goes to a disconnected status.

 

If you don’t have seen any activity is probably because you don’t have a “seed” stream. This stream is not a particular one, it is simply a stream (data or status) needed by Carriots to calculate the next stream expected arrival time by adding the frequency (stream or status) to the stream “at” timestamp.

So, you need to send a stream to let Carriots know when you expect the next one to arrive and fire the event if it does not occur.

 

Enjoy!



#8 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 17 March 2013 - 10:42 AM

I seem to have a listener working on the group.  I have it setup so that it send an e-mail when the Status changes by looking at context.hierarchy.status.  Well, it was working and then I changed some coding, so I am just waiting for the device to be revived to see if the changes work.



#9 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 22 March 2013 - 02:40 PM

I have a listener setup.  The following is the code.  The event is "data_change_state".  The If expression is true.  What I am seeing in the behavior is that I get the "Status is OK for..." message only after the device has been offline for a time (set by Data Stream Frequency".  I am expecting to get "Status is not OK...".  I also don't seem to get a message when the device comes back online.  Any thoughts?

 

import com.carriots.sdk.utils.Email;
def email = new Email();
email.to="email@domain.com";

if (context.hierarchy.device.status=="ok"){
email.subject="Status is OK for " + context.hierarchy.device.id_developer;}
else {
email.subject="Status is not OK for " + context.hierarchy.device.id_developer;}

email.send();


#10 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 23 March 2013 - 07:52 AM

I'm getting an error message:

 

Listener DeveloperDeviceStatus@Zambiot: html title 500 Internal Server Error title body 500 Internal Server Error body html

 

How do troubleshoot this?



#11 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 25 March 2013 - 05:05 PM

Hello.

 

Answering your first question, I think you should try with the expression if (context.data.reason.status=="ok") in your if condition instead of if (context.hierarchy.device.status=="ok")

 

The context hierarchy represents the context when the causing stream is received and the "reason" is holds the device's destination status. 

 

Regarding the second question we will need more details about it. Can you please tell us in which context you get that error? Is this message part of an alarm? Is it a query response? If it's in a listener execution, try to execute the code in the console to get the execution response message.

 

Hope it helps.


Alvaro Everlet

Carriots CTO


#12 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 26 March 2013 - 11:24 PM

The error message was listed in my alarms and was caused by a listener.

 

For the moment I've changed my two listeners, which are nearly identical, to use the if statement as you recommend.  So I'll see how that performs.  I'm looking forward to deeper details on Context.data and context.hierarchy so that I can understand them better.



#13 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 29 March 2013 - 01:26 AM

My listener is working as I would like.  Thank you for the help.  The following is summary of the complete setup so that others can make use of this knowledge.

 

This is a listener that sends an e-mail when a device goes online or offline in a Group.  That is when the "status" changes.

 

1.  Set up your device in Carriots.  Choose a value for "Data Stream Frequency".  For me, I choose a value that was slightly longer than what my device is programmed to update as there can be various network delays and tolerances.  So, say you have a device that posts data once a minute to Carriots, I'd recommend a value of 2, for 2 minutes.  If your device is posting once every 10 minutes, then maybe a value of 11 or 12 minutes.  Status of the device is based on this frequency.  Lets say your device has been logging away at 1 minute intervals.  If, your frequency is set to 2 and you device goes offline, the Status will change 2 minutes after the last data point was logged.  As soon as you device begins logging again, the status is changed again.

 

2.  Create a new listener.  The listener in this example is setup to monitor a Group, but the listener could be for a specific device, asset, project, etc.  In creating the listener, enter the name, description, entity type and ID.

 

3.  For the listener's event, use "Event Device Change State".

4.  For the "If Expression" use "true".

5.  For the "Then Expression" use the following code.  

import com.carriots.sdk.utils.Email;
def email = new Email();
email.to="myemail@mydomain.com";

if (context.data.reason.status=="ok"){
email.subject="Status is OK for " + context.hierarchy.device.id_developer;}
else {
email.subject="Status is not OK for " + context.hierarchy.device.id_developer;}

email.send();

 

There is one thing that you need to edit, it is the e-mail address.  Change "myemail@mydomain.com" to the e-mail address you would like the Status e-mail sent to.

 

Save your listener.  Next time your device comes online and Status is OK, you should get an e-mail.  If you device goes offline and Status is Not OK, you should get an e-mail.  



#14 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 29 March 2013 - 07:40 AM

Great! Thanks for sharing!

Alvaro Everlet

Carriots CTO


#15 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 11 April 2013 - 07:36 AM

Hello to all reading this post.

 

We've detected that in some cases status sent in context.hierarchy.device.status is not accurate due to the asynchronous processing of events. To prevent collecting wrong previous status from the context data we have included context.data.reason.before_status property to hold that info. Thanks sdeancos for pointing and solving it!

 

Enjoy Carriots!


Alvaro Everlet

Carriots CTO


#16 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 102 posts
  • LocationMadrid, Spain.

Posted 11 April 2013 - 07:43 AM

Hi everyone!

 

Thanks to all the development team.

 

I hope carriots's users find useful this new feature.

 

 

Enjoy Carriots!



#17 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 15 April 2013 - 05:24 AM

I'm not fully following the Groovy stuff happening here.  Just to confirm:

 

The code posted above has the following in it

if (context.data.reason.status=="ok"){

 

 

Based on the revision mentioned, this should now be as follows.  Correct?

 

 

if (context.data.reason.before_status=="ok"){

 

 

 



#18 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 15 April 2013 - 01:49 PM

Not exactly... I will try to explain a little bit this "status change" topic.

 

Some introduction topics can be useful. A device can have the following inner status:

·       Disconnected: no status stream nor data stream received in the expected time range defined by frequency_status and frequency_stream device’s properties

·       No_data: no data stream received in the expected time range defined by frequency_stream device’s property. Status stream received.

·       No_status: no status stream received in the expected time range defined by frequency_status device’s property. Data stream received.

·       OK: Data and status streams received in the expected time range defined by frequency_status and frequency_stream device’s properties.

 

When a device changes its inner status, Carriots raise an event and injects the context to be used in listeners and rules scripts. A status change implies to statuses: one before the change takes place and another when the change is done. For example, a device can be in a “no_data” status and change to “disconnected” or from “OK” to “no_status” and so on.

 

So, there is 2 status in an “change state” event context:

·       context.data.reason.before_status: device’s status BEFORE the event

·       context.data.reason.status: device’s status AFTER the event

 

This is an excerpt from the documentation we’re preparing.

 

Hope it helps!


Alvaro Everlet

Carriots CTO


#19 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 21 April 2013 - 04:03 AM

This is helpful, thank you.  I'm looking forward to this documentation as I am starting to think through the listeners I need to write.



#20 jlanton

jlanton

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 12 July 2014 - 07:04 PM

thanks!

I had to use this method because the "else Expression" doesn't work

when a change in the status occurs, the listener is triggered, the "if Expression" evaluated and the "then expression" executed if the "if" is true (normal behaviour), but if the "if expression" is false, the "Else Expression" is never executed

 

is this normal?

any idea?

 

thanks in advance







Also tagged with one or more of these keywords: Listener, Event

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users