Getting started with Labeeb IoT Business Rules Engine
Labeeb IoT Platform has an integrated Rules Engine,which gives the ability to create and apply specific rules to the incoming data sent by any connected IoT devices.
The rules built using Drools a Production Rule System that uses the rule-based approach to implement and Expert System. Expert Systems are knowledge-based systems that use knowledge representation to process acquired knowledge into a knowledge base that can be used for reasoning.
What is a Rule ?
Rules are pieces of knowledge often expressed as, "When some conditions occur, then do some tasks." , Rule Structure is :
rule <rule_name> <attribute> <value> when <conditions> then <actions> end
Conditions in Rules
A rule can contain many conditions and patterns such as:
- Data(temp== 35)
- Device(name == “Station1”)
The above conditions check if the Data temp is 35 or the Device name is “Station1”.
Variables in Rules
A variable name in Drools starts with a Dollar($) symbol.
- $data:DeviceData ( )
$data is the variable for DeviceData() class
Rules can work with all the native Java types and even Enum.
Hello World Scenario
The following example shows how to output "Hello World" when message received to Labeeb IoT platform :
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.DataService data rule "helloworld" when $messageValue:DataStreamRecord() then System.out.println("Hello World"); end
Threshold Alert Scenario
The following example shows how to send an SMS to a specific mobile number when Humidity data received by Labeeb IoT reaches a specific threshold:
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.MessengerService messenger rule "publishingsd" when $messageValue:DataStreamRecord(dataTypeName=="Humidity",java.lang.Integer.parseInt(value)>85) then messenger.sendSms("wajdi","01123456789","Humidity is higher than 85%"); end
Labeeb IoT Rules Engine Capabilities :
In addition to what can be done with rule engine, Labeeb IoT platform give you multiply special Rules and Actions such like sending an email or SMS, push data to another Mqtt broker,etc ..
JDBC Update Rule:
JDBC update rule can be used to update records in a table using JDBC application, the following example shows how to update the JDBC when a message is received:JDBC Update Rule:
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.DataService data rule "jdbc" when $messageValue:DataStreamRecord() then data.jdbcUpdate(String url, String username, String password, String sql); end
Cassandra Update Rule:
Cassandra update rule can be used to update records in Cassandra table , the following example shows how to update when a message is received:Cassandra Update Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.DataService data rule "cassandra" when $messageValue:DataStreamRecord() then data.cassandraUpdate(String address, int port, String username, String password, String cql); end
Device Data Rule:
Device data rule gives you the ability to insert data to a specific device in Labeeb IoT platform when a new message is revived, using this rule, you can filter messages or make some modification on the data before assigning it to a specific device:Device Data Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.DataService data rule "deviceData" dialect "mvel" when $dataStreamRecord:DataStreamRecord() then data.insertDeviceData(String deviceName, String modelName, String typeName, Timestamp timestamp, Timestamp receiveTimestamp, Double longitude, Double latitude, Double altitude, Boolean booleanValue, Double doubleValue, String stringValue, Timestamp timeValue); end
SMS Rule :
SMS rule can be used to send SMS message, it can be used as a notification for some events, or as warning, for example when received message contain temperature data type with value grater that 45 , send SMS message to a specific number :SMS Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.MessengerService messenger rule "publishingSMS" when $messageValue:DataStreamRecord(Float.parseFloat(value) > 45) then messenger.sendSms("name","number","Message"); end
REST rule give Labeeb IoT platform a great ability to extent and connect your data to external data bases, or trigger some actions on other platforms using REST API calls, depending on the message received by labeeb, you can decide which REST to call, for example :Rest Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.RestService rest rule "publishing" when $messageValue:DataStreamRecord() then rest.post("Valid URI","JSON String"); end
MQTT Publishing Rule:
MQTT in one of the most popular IoT communication protocol and Labeeb IoT supports pushing data through MQTT, but you are not limited to that, using this rule you can redirect received messages to another MQTT broker :MQTT Publishing Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.mqtt.MqttPublisher mqtt rule "publishing" when $messageValue:DataStreamRecord() then mqtt.publish(String mqttClientName, String mqttTopicName, String mqttServerUri, String mqttServerUsername, String mqttServerPassword, int qos, String payload); end
generating RSS using this rule give the ability of delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.the following example shows how to use this rule:RSS Rule
package org.quwic.itms.common.beans; global org.qmic.fusion.rules.RssService rss import java.util.ArrayList declare DataStreamRecord @role( event ) end rule "rss" dialect "mvel" timer (cron:0 0/1 * * * ?) when $dataStreamRecords: ArrayList() from collect(DataStreamRecord() over window:time(1m)) then System.out.println("Generating Feed with " + $dataStreamRecords.size + " records"); rss.generate(String feedTitle, String feedDescription, String feedType, List<DataStreamRecord> records, String fileName); end