Difference between revisions of "IEdge NodeRED iAlarm Sample"

From ESS-WIKI
Jump to: navigation, search
 
(18 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
== Introduction ==
 
== Introduction ==
  
This document will introduce a NodeRed sample code for user define their own alarm flow and message in iEdge. In this example, we 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. 
+
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. 
  
[[File:IEdge NodeRed 1 Example Code.png|center|1024px]]
+
[[File:IEdge NodeRed 1 Example Code.png|center|1024px|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.
 +
<pre>context.global.ex_Err_Code = 0;</pre>
 +
 
 +
[[File:IEdge NodeRed 2 Set Golbal Variable.png|center|IEdge NodeRed 2 Set Golbal Variable.png]]
 +
 
 +
&nbsp;
 +
 
 +
== 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&nbsp;corresponding topics from&nbsp;MQTT to get the raw data.
 +
 
 +
Get a MQTT Node then&nbsp;set Topic to&nbsp;'''/cagent/admin/#&nbsp;'''and QoS to 1, click edit icon then&nbsp;set Server IP to gateway IP and port number to 1883.
 +
 
 +
[[File:IEdge NodeRed 3 Get MQTT Data.png|center|IEdge NodeRed 3 Get MQTT Data.png]]
 +
 
 +
&nbsp;
 +
 
 +
== Filter Modbus-Master Data ==
 +
 
 +
In previous step, NodeRed&nbsp;will get raw data&nbsp;from all plugin on the gateway, due to the status sensor&nbsp;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.
 +
<pre>context.global.PluginID='Modbus-Master';
 +
if( msg.topic.indexOf(context.global.PluginID)>-1 && msg.topic.indexOf('deviceinfo')>-1 ) {
 +
    return msg;
 +
}</pre>
 +
 
 +
[[File:IEdge NodeRed 4 Filter Modbus plugin.png|center|IEdge NodeRed 4 Filter Modbus plugin.png]]
 +
 
 +
&nbsp;
 +
 
 +
== 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.
 +
 
 +
[[File:IEdge NodeRed 5 Convert Json to JavaScript.png|center|IEdge NodeRed 5 Convert Json to JavaScript.png]]
 +
 
 +
&nbsp;
 +
 
 +
== Filter Target Sensor ==
 +
 
 +
After the raw data is converted to Object, taking&nbsp;a debug node behind the json node to get&nbsp;the output data&nbsp;and copy the status&nbsp;sensor path. Next step, NodeRed has to filter the target sensor from&nbsp;all Modbus sensors.
 +
 
 +
Get a function and put it hehind json node, set the target sensor id to status sensor name&nbsp;and only return the data that comes from the status&nbsp;sensor to next node.
 +
<pre>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;
 +
    }
 +
}</pre>
 +
 
 +
[[File:IEdge NodeRed 6 Filter Target Sensor.png|center|IEdge NodeRed 6 Filter Target Sensor.png]]
 +
 
 +
&nbsp;
 +
 
 +
== Define [https://hackmd.io/Bc2fdu7TSNKWB4BvLHRCqQ iAlarm] Message ==
 +
 
 +
NodeRed gets status sensor as '''msg.payload'''&nbsp;after twice filter, next, set alarm message and alarm type&nbsp;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.
 +
<pre>//context.global.Error_severity 3 is Error 4 is Warning
 +
 
 +
if( msg.payload.v !== context.global.ex_Err_Code){
 +
&nbsp; &nbsp; context.global.ex_Err_Code = parseInt(msg.payload.v);
 +
&nbsp; &nbsp; switch(context.global.ex_Err_Code){
 +
&nbsp; &nbsp; &nbsp; &nbsp; case 0:
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
 +
&nbsp; &nbsp; &nbsp; &nbsp; case 77:
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.global.Error_Code = "E:77";
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.global.Error_Msg = "Seven Modified Holding10";
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.global.Error_severity = 3;
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
 +
&nbsp; &nbsp; }
 +
&nbsp; &nbsp; if (context.global.Error_severity == 3)
 +
&nbsp; &nbsp; &nbsp; &nbsp; context.global.Type = "THRESHOLD_CHECK_ERROR";
 +
&nbsp; &nbsp; else
 +
&nbsp; &nbsp; &nbsp; &nbsp; context.global.Type = "THRESHOLD_CHECK_WARN";
 +
&nbsp; &nbsp; msg.payload = '{"Command":"trigger","SessionID":"f57c772412f18bbe4acb22cc5dc145d7","Params":{"type":"' +&nbsp;
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.global.Type + '","msg":"' + msg.payload.n +' ' + context.global.Error_Code + ' ' +&nbsp;
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.global.Error_Msg + '","severity":'+ context.global.Error_severity +'}}';
 +
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
 +
&nbsp; &nbsp; if( context.global.ex_Err_Code !== 0 ){
 +
&nbsp; &nbsp; &nbsp; &nbsp; msg.topic = "/iAlarm/" + context.global.PluginID + "/Request";
 +
&nbsp; &nbsp; &nbsp; &nbsp; return msg;
 +
&nbsp; &nbsp; }
 +
}</pre>
 +
 
 +
[[File:IEdge NodeRed 7 Define Alarm.png|center|IEdge NodeRed 7 Define Alarm.png]]
 +
 
 +
&nbsp;
 +
 
 +
== 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&nbsp;click edit icon and input gateway IP.
 +
 
 +
[[File:IEdge NodeRed 8 Send iAlarm.png|center|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.
 +
 
 +
[[File:IEdge NodeRed 9 Notification.png|center|IEdge NodeRed 9 Notification.png]]
 +
 
 +
&nbsp;

Latest revision as of 03:39, 17 April 2023

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