AgentLite-Node.js
Contents
Introduction
AgentLite-Node.js is a light weight south-bound agent in Node.js. There are 3 main component:
- Net-Protocol ( ./lib/WISECore3.3.js)",
- main program ( AgentLite3.3.js)
- plugin module ( ./module/plugin_temp.js). Develop need to implement a plugin.js.
AgentLite-Node.js
Sofware Components of AgentLite-Node.js
Configure
./config/agent_config.json : in JSON Format
{
"ServerIP":"127.0.0.1",
"ServerPort": 1883,
"DeviceName": "SNMP_Handler",
"DevType": "Service",
"version": "v1.0.1",
"plugin": "./module/Simple-plugin_test.js"
}
Plugin
A plugin.js with common interfaces. A main program can load a plugin to send and control its sensor data.
Define and Callback functions
| global variable | Description |
|---|---|
| g_strPluginName |
Name of plugin |
| g_jsCapability |
Capability of this plugin |
| g_sendcbf | reply action function point |
| g_sendreportcbf | auto report function point |
| g_sendcapabilitycbf | send capability function point |
| g_sendeventcbf | send event message function point |
| g_sendreplytopiccbf | send update data by reply action topic ( Monitor page ) function point |
| |
|
Internal Function
Developer needs to implement these functions.
var InitPlugin()
- Description: To initialize process of your plugin.
var UnInitPlugin()
- Description: To uninitialize process of your plugin.
var AutoReport()
- Description: system will call this "AutoReport" after trigging auto report. To send auto report data by "g_sendreportcbf".
var getSenseData( iCmd, strSessionid, jsRequest )
- Description: To receive a get control command and call this function to query sensor data in capability cach.
var StartMonitorData(iCmd, strSessionid, jsMsg )
- Description: To receive a get monitor command and register a timer envet to call "MonitorUpdate" function.
var MonitorStop( )
- Description: To stop a timer event of Monitor sensor data.
var MonitorUpdate()
- Description: To upload sensor data by g_sendreplytopiccbf
Export Interfaces of Plugin
Developers generally do not need to modify
var Handler_Initialize( param )
- Description: Main program calls "Handler_Initialize" to initialize this module.
| Parameter | Description |
|---|---|
| param.sendcbf | reply action function point |
| param.sendreportcbf | auto report function point |
| param.sendcapabilitycbf | send capability function point |
| param.sendeventcbf | send event message function point |
| param.sendreplytopiccbf | send update data by reply action topic ( Monitor page ) function point |
var Handler_Uninitialize( )
- Description: Main program calls "Handler_Uninitialize" to uninitialize this module.
void Handler_AutoReportStart( interval )
- Description: Main program calls the "Handler_AutoReportStart" to start auto report.
| Parameter | Description |
|---|---|
| interval | report interval in second |
var Handler_AutoReportStop()
- Description: Main program calls "'Handler_AutoReportStop'" to stop auto report.
var Handler_Get_Capability()
- Description: Main program calls "'Handler_AutoReportStop'" to get plugin's capability.
var Handler_Recv( strTopic, strMsg )
- Description: Main program calls "'Handler_Recv'" if receive a control message from others.
| Parameter | Description |
|---|---|
| strTopic | received mqtt topic |
| strMsg | received message |
AdvDataMgt
It is a librarie help that makes it easy to compose plugin's capability in the Advantech IPSO-like JSON format.
Defined
g_strPluginName: Name of Plugin
- Example: g_strPluginName = 'SNMP_Handler';
g_jPluginInfo: Basic informaion of this plugin, You can extend key: value
- Example: g_jPluginInfo =
{
type:"Protocol",
name:g_strPluginName,
description:"This is a SNMP Manager service",
version:"v1.0.2",
};
int initialize ( strName, jInfo )
- Description: To initialize the AdvDataMgt
g_jPluginInfo var dataMgt = new dataMgt(); dataMgt.initialize(g_strPluginName,g_jPluginInfo);
Result
{
"SNMP_Handler": {
"info": {
"e": [{"n": "type","sv": "Protocol","asm": "r"},
{"n": "name","sv": "SNMP_Handler","asm": "r"},
{"n": "description","sv": "This is a SNMP Manager service","asm": "r"},
{"n": "version","sv": "v1.0.2","asm": "r"}],
"bn": "info"
}
}
}
int addSensor( strPath, jsSensor, strKey )
- Description: To add a sensor data by path.
- Example
var prefix = g_strPluginName+'/';
// SenData
var strPath = prefix+'SenData';
dataMgt.addSensor(strPath,{"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"});
dataMgt.addSensor(strPath,{"n":"Count","u":"","v":0, "asm":"rw"},g_strCountKey);
// Info
strPath = prefix + 'Info';
dataMgt.addSensor(strPath,{"n":"Name","sv":"SenHub","asm":"rw"});
dataMgt.addSensor(strPath,{"n":"sw","sv":"1.0.0","asm":"r"});
// Net
strPath = prefix + 'Net';
dataMgt.addSensor(strPath,{"n":"sw","sv":"3.0.1","asm":"r"});
dataMgt.addSensor(strPath,{"n":"Health","v":100.000000,"asm":"r"});
Result
{
"SNMP_Handler":
{ "SenData":{"e":[{"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"},{"n":"Count","u":"","bv":false, "asm":"rw"}],"bn":"SenData"},
"Info":{"e":[{"n":"Name","sv":"SenHub","asm":"rw"},{"n":"sw","sv":"1.0.0","asm":"r"}],"bn":"Info"},
"Net":{"e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100.000000,"asm":"r"}],"bn":"Net"}
}
}
var getCapability ()
- Description: Get capability.
- Example
g_sendreportcbf(g_strPluginName, dataMgt.getCapability());
int updatebyPath( strPath, value )
- Description: To update value by path
var strPath = 'SNMP_Handler/SenData/Count'; var g_iCount = 300; dataMgt.updatebyPath( strPath, g_iCount );
int updatebyKey( strKey, value )
- Description: To update value by key
- Example
var strPath = 'SNMP_Handler/SenData/Count'; var g_iCount = 500; dataMgt.updatebyKey( 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15, g_iCount );
var queryKeybyPath( strPath )
- Description: To query key by path
- Example
strPath = 'SNMP_Handler/SenData/Count';
console.log('query key by path= ' + strPath + ' ret= ' + dataMgt.queryKeybyPath(strPath));
Result
query key by path= SNMP_Handler/SenData/Count ret= 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15
var getAllKey( outRet [ ] )
- Description: To get all keys
- Example
var ret = []; if ( dataMgt.getAllKey( ret ) === true ) console.log( 'Number of all key: ' + ret.length + ' result = ' + JSON.stringify( ret ));
Reslut
Numberof all key: 1
result =
[{"key":"172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15","path":"SNMP_Handler/SenData/Count"}]
var getAllPath( outRet [ ] )
- Description: To get all paths
- Example
var ret = []; if( dataMgt.getAllPath( ret ) === true ) console.log( 'Number of all Path: ' + ret.length + ' result = ' + JSON.stringify( ret ));
Result
Number of all Path: 6
result =
[{"path":"SNMP_Handler/SenData/Temperature"},{"path":"SNMP_Handler/SenData/Count","key":"172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15"},{"path":"SNMP_Handler/Info/Name"},{"path":"SNMP_Handler/Info/sw"},{"path":"SNMP_Handler/Net/sw"},{"path":"SNMP_Handler/Net/Health"}]
How To - Sample Code
Plugin
Sample Code: ./module/plugin_test.js
Template Code: ./module/plugin_temp.js
Plugin with AdvDataMgt
Sample Code: ./module/Simple-plugin_test.js
Template Code: ./module/Simple-plugin_temp.js
Release
gitlab http://advgitlab.eastasia.cloudapp.azure.com/EdgeSense/AgentLite-Node.js