IEdge NodeRED iAlarm Sample

From ESS-WIKI
Jump to: navigation, search

Introduction

This document will introduce a NodeRed sample code for user define their own alarm flow and message in iEdge. In this example, iEdge use Modbus-Master plugin to collect sensor data, and if the status sensor returns 0 which means device does not any abnormal, other wise, sensor returns error code. We use NodeRed to design a work flow that will send corresponding error message which depends on the error code. 

IEdge NodeRed 1 Example Code.png

 

Set Global Variable

To avoid a large number of notifications in a short period of time, this sample code adds a variable to record previous error code. If sensor returns error code, NodeRed will send alarm once and will not notify again if getting the same code with previous time again.

Get a inject node and enable Inject once at start for initialing, get a function node and declare a global variable with initial value 0 for recording previous error code.

context.global.ex_Err_Code = 0;
IEdge NodeRed 2 Set Golbal Variable.png

 

Get Data From MQTT

At the begining, NodeRed has to get status sensor data, due to Modbus-Master plugin will collect sensor data to MQTT Broker on gateway, therefore, NodeRed can subscribe corresponding topics from MQTT to get the raw data.

Get a MQTT Node then set Topic to /cagent/admin/# and QoS to 1, click edit icon then set Server IP to gateway IP and port number to 1883.

IEdge NodeRed 3 Get MQTT Data.png

 

Filter Modbus-Master Data

In previous step, NodeRed will get raw data from all plugin on the gateway, due to the status sensor in this example is using Modbus protocol, NodeRed has to filter the raw data that is collected by Modbus-Master plugin.

Get a function node and put it behind of MQTT node, declare global variable PluginID as Modbus-Master and filter the sensor data that only comes from Modbus-Master plugin.

context.global.PluginID='Modbus-Master';
if( msg.topic.indexOf(context.global.PluginID)>-1 && msg.topic.indexOf('deviceinfo')>-1 ) {
    return msg;
}
IEdge NodeRed 4 Filter Modbus plugin.png

 

Convert Json to JavaScript

Convert raw data from Json string format to JavaScript Object format which is more convenient doing the data processing at next stage in function node.

IEdge NodeRed 5 Convert Json to JavaScript.png

 

Filter Target Sensor

After the raw data is converted to Object, taking a debug node behind the json node to get the output data and copy the status sensor path. Next step, NodeRed has to filter the target sensor from all Modbus sensors.

Get a function and put it hehind json node, set the target sensor id to status sensor name and only return the data that comes from the status sensor to next node.

context.global.SensorID='Holding10';
for( counter = 0; counter < msg.payload.susiCommData.data["Modbus-Master"].Device.EIServer.Sensor.e.length; ++counter ){
    if( msg.payload.susiCommData.data["Modbus-Master"].Device.EIServer.Sensor.e[counter].n == context.global.SensorID ){
        msg.payload = msg.payload.susiCommData.data["Modbus-Master"].Device.EIServer.Sensor.e[counter];
        return msg;
    }
}
IEdge NodeRed 6 Filter Target Sensor.png

 

Define iAlarm Message

NodeRed gets status sensor as msg.payload after twice filter, next, set alarm message and alarm type base on the error code from sensor data. In this example, if the status sensor return 77 code, NodeRed will send the "Seven Modified Holding10" message in iEdge portal.

Get a function node and put it behind the Sensor Selector node, when the data of status sensor changed, the node will combine its meaning message to iAlarm.

//context.global.Error_severity 3 is Error 4 is Warning

if( msg.payload.v !== context.global.ex_Err_Code){
    context.global.ex_Err_Code = parseInt(msg.payload.v);
    switch(context.global.ex_Err_Code){
        case 0:
            break;
        case 77:
            context.global.Error_Code = "E:77";
            context.global.Error_Msg = "Seven Modified Holding10";
            context.global.Error_severity = 3;
            break;
    }
    if (context.global.Error_severity == 3)
        context.global.Type = "THRESHOLD_CHECK_ERROR";
    else
        context.global.Type = "THRESHOLD_CHECK_WARN";
    msg.payload = '{"Command":"trigger","SessionID":"f57c772412f18bbe4acb22cc5dc145d7","Params":{"type":"' + 
                  context.global.Type + '","msg":"' + msg.payload.n +' ' + context.global.Error_Code + ' ' + 
                  context.global.Error_Msg + '","severity":'+ context.global.Error_severity +'}}';
                  
    if( context.global.ex_Err_Code !== 0 ){
        msg.topic = "/iAlarm/" + context.global.PluginID + "/Request";
        return msg;
    }
}
IEdge NodeRed 7 Define Alarm.png

 

Publish Alarm to MQTT

After combined iAlarm format message in Alarm Define node, the last step is sending it to Gateway MQTT Broker.

Get a MQTT node, set QoS to 1 then click edit icon and input gateway IP.

IEdge NodeRed 8 Send iAlarm.png

Demo

When the data of status sensor Holding10 changes to 77, iEdge will notify the user with user defined message.

IEdge NodeRed 9 Notification.png