Last Event Cache in Watson IoT Platform

Image source: Mike Nottebrock / pixelio.de

Sometimes a client is offline and when it becomes online again it should be up to date instantly. The key question is "what was the last value of...?" The solution is provided by the Last Event Cache in Watson IoT Platform. It does exactly what the name says: provide the last event that a device has sent - regardless if the device is currently online or offline. The event must not be older than 365 days. Last Event Cache ist simple to use as I will show you in this article.

In Theory...

Last Event Cache can be called using the REST API with the following pattern:

GET https://<org-id>.internetofthings.ibmcloud.com/api/v0002/device/types/<device-type>/devices/<device-id>/events/<event-id>

If you are interested in all events that the device has sent regardless of the event ID, then simply use:

GET https://<org-id>.internetofthings.ibmcloud.com/api/v0002/device/types/<device-type>/devices/<device-id>/events

To ensure that data is only accessible by those who are authorized you have to authenticate using basic auth. You need to create an API key in Watson IoT Platform. The key is used as username and the authentication token is the password.

...and practice

Example needed? I want to get the last values for my thermostat and use the following call:

GET https://<my-org>.internetofthings.ibmcloud.com/api/v0002/device/types/Thermostat/devices/ThermostatKidsroom/events

And I get this JSON strcuture as result:

[
{
"typeId": "Thermostat",
"deviceId": "ThermostatKidsroom",
"eventId": "event",
"format": "json",
"timestamp": "2017-04-03T21:39:12.673+0000",
"payload": "eyJkIjp7Imh1bWlkaXR5Ijo0MiwidGVtcGVyYXR1cmUiOjIyLjl9fQ=="
}
]

Please note: this is an array containing an object. Would my device send different events with different event-IDs, the array would contain one object with the latest value for each event-ID. Probably the most used fields are the last two: timestamp shows when Watson IoT Platform has received the message from the device, payload conatins the device data in a base64 encoded string. This can be converted back to the original device data with a simple Node-RED flow:

Node-RED Flow for reading Last Event Cache

Starting point is the HTTP Request as depicted above (e.g. triggered by an inject-node for testing).  Next we extract the object from the array:

msg.payload = msg.payload[0].payload;
return msg;

Then we decode the string using the base64 node and convert it into an utf8 string:

msg.payload = msg.payload.toString('utf8');
return msg;

Using the JSON node we construct a JSON-object out of the string. This makes it easier to work with the data in the following course of the flow. Using the above example I receive the following msg.payload:

{
d: {

(Visited 135 times, 1 visits today)

Don't want to miss anything?

Newsletter Subscribe Content

* This field is required

About René Auberger 12 Articles
René is an IT Architect for Internet of Things, Industry 4.0, Analytics and Cognitive Computing. In his spare time he makes his house smarter step by step. In 2017 René founded the Internet-of-Things.blog. He is also frequently tweeting. View René's full profile...

Be the first to comment

Leave a Reply

Your email address will not be published.


*


four + four =