IEdge NodeRED iAlarm Sample

From ESS-WIKI
Revision as of 03:32, 27 December 2022 by Seven.lin (talk | contribs)
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 target 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 to Holding10 and only the data from the target sensor which name is Holding10 can go through 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 iAlarmMessage

NodeRed gets target sensor after twice filter, next, set alarm message and alarm type base on the error code from sensor data. 

 

 

Publish Alarm to MQTT