Difference between revisions of "IEdge NodeRED iAlarm Sample"
Line 1: | Line 1: | ||
− | |||
− | |||
− | |||
== Introduction == | == Introduction == | ||
Line 20: | Line 17: | ||
[[File:IEdge NodeRed 2 Set Golbal Variable.png|center|IEdge NodeRed 2 Set Golbal Variable.png]] | [[File:IEdge NodeRed 2 Set Golbal Variable.png|center|IEdge NodeRed 2 Set Golbal Variable.png]] | ||
− | + | | |
− | |||
− | |||
== Get Data From MQTT == | == Get Data From MQTT == | ||
Line 28: | Line 23: | ||
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. | 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 | + | 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. |
[[File:IEdge NodeRed 3 Get MQTT Data.png|center|IEdge NodeRed 3 Get MQTT Data.png]] | [[File:IEdge NodeRed 3 Get MQTT Data.png|center|IEdge NodeRed 3 Get MQTT Data.png]] | ||
Line 34: | Line 29: | ||
| | ||
+ | == 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. | ||
+ | <pre>context.global.PluginID='Modbus-Master'; | ||
+ | if( msg.topic.indexOf(context.global.PluginID)>-1 && msg.topic.indexOf('deviceinfo')>-1 ) { | ||
+ | return msg; | ||
+ | }</pre> | ||
− | == Filter Modbus-Master | + | [[File:IEdge NodeRed 4 Filter Modbus plugin.png|center]] |
+ | |||
+ | == 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]] | ||
+ | |||
+ | == 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. | ||
+ | <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]] | ||
+ | |||
+ | == Define [[iAlarm|iAlarm]]Message == | ||
+ | |||
+ | NodeRed gets target sensor after twice filter, next, set alarm message and alarm type base on the error code from sensor data. | ||
+ | |||
+ | [[File:IEdge NodeRed 7 Set Alarm Message.png|center]] | ||
| | ||
− | + | | |
− | |||
− | |||
== Publish Alarm to MQTT == | == Publish Alarm to MQTT == |
Revision as of 04:32, 27 December 2022
Contents
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.
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;
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.
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; }
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.
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; } }
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