IoT Devices on Sofia2. Integration and Management (V. Playing with Data)

This is the 5th post of the series “IoT Devices on Sofia2. Integration and Management”:

 

 

Part I. Overview

Part II. User and Ontology creation

Part III. Connecting to the Device

Part IV. Data Visualization

 

 

The previous post talked about Sofia2’s capabilities regarding data visualization for either raw data or simple transformed data. This post takes the next step by showing you how to configure a business rule depending on incoming data, and also how to configure a custom API to provide a controlled interface to access the data.

Real-time rules configuration

For our scenario with a SensorTag’s measures received on Sofia2, we will monitor one of the magnitudes to stay below a determined threshold. If the magnitude does exceed the threshold, the rule will send an SMS alert to the administrator.

First, we will select an Script Rule Ontology. This kind of rule will be executed every time a new data is inserted in the Ontology demoDispositivos_RTFrame. To find this option you should navigate to the Wizard New Rules, located under the Rules menu.

 

en-menu-script

 

It is necessary to fill the fields for name, timeout (mandatory), rule’s class (Ontology-based), Ontology, and script language (we are using Groovy in this scenario).

 

en-creacion-script

 

 

To ease the code, we will place the value monitoring code chunk into the IF tab. If the evaluation is true, it will continue executing the code of the THEN tab, but if it evaluates to false, the code of the ELSE tab will be executed. The ERROR tab will normally host error catching code.

For this scenario we will just use the IF and THEN tabs.

Inside the IF tab (image below), we first start importing the required libraries and declaring the variables for the associated ThinKP instance and token. Then we load the Ontology onto the ontologyJson variable and evaluate the humidity value. If the value exceeds the defined threshold (30), the script will resume its execution on the THEN tab.

 

if-script

 

Inside the THEN tab, we place the code related to the SMS alert composition and delivery. We opted for a public SMS provider that allows for SMS sending via HTTP GET.

 

then-script

 

So simple! Once the script is saved and activated, it will run upon new data insertion on the Ontology.

Before taking a deep dive into Sofia2 scripting, you will find good use of these tricks:

  • Keeping the cursor on the script’s editor, press F11 to go full-screen so you do not miss a thing!
  • To verify whether the execution has been successful, navigate to the Tools menu and then to the Processes Status Visualization There you will have a detailed log on how your scripts are doing.

 

 

Data-access APIs

Now we are going to define interfaces to access the gathered SensorTag data. To do so, Sofia2 allows for REST services configuration through Sofia2’s API Manager. Using the manager you can define operations for reading, writing (POST), update (PUT), delete (DELETE), basic or advanced search (GET).

To create a set of operations on the (already well-known!) demoDispositivos_RTFrame Ontology, navigate to the New API option under the My APIs menu.

 

 

en-creation-apis

 

 

By default, the form proposes a configuration for the external API. In our scenario, we will uncheck this setting to be able to select our Ontology in the combo-box. This action will suggest as a name for the API (demoDispositivo_RTFrames) but feel free to change this name to your choice.

To enable all the available operations (GET, POST, PUT, DELETE…), just select them from the list and complete them with a description. In our case, we defined a simple example for the operation CUSTOM QUERY.

 

 

en-custom-query

 

 

The invocation of the ultimoDato method, will trigger:

db.demoDispositivos_RTFrame.find().sort({\’demoDispositivos_RTFrame.date\’:1}).sort({\’contextData.timestamp\’:-1})

 

Returning the most recent data inserted into the Ontology.

To get further information on the Sofia2’s API Manager capabilities, visit the following posts:

 

 

 

Happy scripting!

 

IoT Devices on Sofia2. Integration and Management (V. Playing with Data)

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s