This is the 5th post of the series “IoT Devices on Sofia2. Integration and Management”:
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.
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).
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.
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.
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.
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.
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.
The invocation of the ultimoDato method, will trigger:
Returning the most recent data inserted into the Ontology.
To get further information on the Sofia2’s API Manager capabilities, visit the following posts: