Difference between revisions of "AgentLite-Node.js"
Eric.liang (talk | contribs) |
Eric.liang (talk | contribs) |
||
| Line 212: | Line 212: | ||
'''int addSensor( strPath, jsSensor, strKey )''' | '''int addSensor( strPath, jsSensor, strKey )''' | ||
| − | *Description: | + | *Description: Add a sensor data by path. |
*Sample Code | *Sample Code | ||
<pre> var prefix = g_strPluginName+'/'; | <pre> var prefix = g_strPluginName+'/'; | ||
| Line 230: | Line 230: | ||
Result | Result | ||
<pre>{ | <pre>{ | ||
| − | + | "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"}, | { "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"}, | "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"} | "Net":{"e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100.000000,"asm":"r"}],"bn":"Net"} | ||
| − | + | } | |
}</pre> | }</pre> | ||
| + | |||
| + | '''var ''''''removeSensorbyPath''''''( strPath )''' | ||
| + | |||
| + | *Description: Remove a sensorby path | ||
| + | *Sample Code | ||
| + | <pre>var strPath = 'SNMP_Handler/SenData/Temperature'; | ||
| + | dataMgt.removeSensorbyPath(strPath); | ||
| + | console.log('remove sensor by path = ' + strPath ); | ||
| + | console.log('new capability ' + JSON.stringify(dataMgt.getCapability())); | ||
| + | </pre> | ||
| + | |||
| + | *Result | ||
| + | <pre>remove sensor by path = SNMP_Handler/SenData/Temperature | ||
| + | new capability {"SNMP_Handler":{"info":{"bn":"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"}]},"SenData":{"bn":"SenData","e":[{"n":"Count","u":"","v":0,"asm":"rw"}]},"Info":{"bn":"Info","e":[{"n":"Name","sv":"SenHub","asm":"rw"},{"n":"sw","sv":"1.0.0","asm":"r"}]},"Net":{"bn":"Net","e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100,"asm":"r"}]}}} | ||
| + | </pre> | ||
| + | |||
| + | '''var ''''''removeSensorbyKey''''''( strKey )''' | ||
| + | |||
| + | *Description: Remove a sensor by key | ||
| + | *Sample Code | ||
| + | <pre>var g_strCountKey= '172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15'; | ||
| + | dataMgt.removeSensorbyKey(g_strCountKey); | ||
| + | console.log('remove sensor by key = ' + g_strCountKey ); | ||
| + | console.log('new capability ' + JSON.stringify(dataMgt.getCapability()));</pre> | ||
| + | |||
| + | *Result | ||
| + | <pre>remove sensor by key = 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15 | ||
| + | new capability {"SNMP_Handler":{"info":{"bn":"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"}]},"SenData":{"bn":"SenData","e":[{"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"}]},"Info":{"bn":"Info","e":[{"n":"Name","sv":"SenHub","asm":"rw"},{"n":"sw","sv":"1.0.0","asm":"r"}]},"Net":{"bn":"Net","e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100,"asm":"r"}]}}}</pre> | ||
'''var getCapability ()''' | '''var getCapability ()''' | ||
| Line 245: | Line 273: | ||
'''int updatebyPath( strPath, value )''' | '''int updatebyPath( strPath, value )''' | ||
| − | *Description: | + | *Description: Update value by path |
*Sample Code | *Sample Code | ||
<pre>var strPath = 'SNMP_Handler/SenData/Count'; | <pre>var strPath = 'SNMP_Handler/SenData/Count'; | ||
| Line 253: | Line 281: | ||
'''int updatebyKey( strKey, value )''' | '''int updatebyKey( strKey, value )''' | ||
| − | *Description: | + | *Description: Update value by key |
*Sample Code | *Sample Code | ||
<pre>var strPath = 'SNMP_Handler/SenData/Count'; | <pre>var strPath = 'SNMP_Handler/SenData/Count'; | ||
var g_iCount = 500; | 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 );</pre> | dataMgt.updatebyKey( 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15, g_iCount );</pre> | ||
| + | |||
| + | '''var getSenseorbyPath( strPath )''' | ||
| + | |||
| + | *Description: Get value by path | ||
| + | *Sample Code | ||
| + | <pre>var strPath = prefix+'SenData/Temperature'; | ||
| + | console.log('get value by path= ' + strPath); | ||
| + | console.log('ret= ' + JSON.stringify(dataMgt.getSenseorbyPath(strPath)));</pre> | ||
| + | |||
| + | *Result | ||
| + | <pre>get value by path= SNMP_Handler/SenData/Temperature | ||
| + | ret= {"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"} | ||
| + | </pre> | ||
| + | |||
| + | '''var getSensrobyKey( strKey )''' | ||
| + | |||
| + | *Description: Get value by key | ||
| + | *Sample | ||
| + | <pre>var g_strCountKey = '172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15'; | ||
| + | console.log('get value by key= ' + g_strCountKey); | ||
| + | console.log('ret= ' + JSON.stringify(dataMgt.getSenseorbyKey(g_strCountKey)));</pre> | ||
| + | |||
| + | *Result | ||
| + | <pre>get value by key= 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15 | ||
| + | ret= {"n":"Count","u":"","v":0,"asm":"rw"}</pre> | ||
'''var queryKeybyPath( strPath )''' | '''var queryKeybyPath( strPath )''' | ||
| − | *Description: | + | *Description: Query key by path |
*Sample Code | *Sample Code | ||
<pre>strPath = 'SNMP_Handler/SenData/Count'; | <pre>strPath = 'SNMP_Handler/SenData/Count'; | ||
| Line 267: | Line 320: | ||
*Result | *Result | ||
| − | <pre>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</pre> | + | <pre>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</pre> | ||
'''var getAllKey( outRet [ ] )''' | '''var getAllKey( outRet [ ] )''' | ||
| − | *Description: | + | *Description: Get all keys |
*Sample Code | *Sample Code | ||
<pre>var ret = []; | <pre>var ret = []; | ||
| Line 284: | Line 338: | ||
'''var getAllPath( outRet [ ] )''' | '''var getAllPath( outRet [ ] )''' | ||
| − | *Description: | + | *Description: Get all paths |
*Sample Code | *Sample Code | ||
<pre>var ret = []; | <pre>var ret = []; | ||
Revision as of 06:49, 20 December 2018
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 do not need to modify in general
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 can help that makes it easy to compose a plugin's capability in Advantech sensor data JSON format.
Defined
g_strPluginName: Name of Plugin
- Sample Code: g_strPluginName = 'SNMP_Handler';
g_jPluginInfo: Basic informaion of this plugin, You can extend key: value
- Sample Code: g_jPluginInfo =
{
type:"Protocol",
name:g_strPluginName,
description:"This is a SNMP Manager service",
version:"v1.0.2",
};
Function
int initialize ( strName, jInfo )
- Description: To initialize the AdvDataMgt
- Sample Code
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: Add a sensor data by path.
- Sample Code
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 'removeSensorbyPath'( strPath )
- Description: Remove a sensorby path
- Sample Code
var strPath = 'SNMP_Handler/SenData/Temperature';
dataMgt.removeSensorbyPath(strPath);
console.log('remove sensor by path = ' + strPath );
console.log('new capability ' + JSON.stringify(dataMgt.getCapability()));
- Result
remove sensor by path = SNMP_Handler/SenData/Temperature
new capability {"SNMP_Handler":{"info":{"bn":"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"}]},"SenData":{"bn":"SenData","e":[{"n":"Count","u":"","v":0,"asm":"rw"}]},"Info":{"bn":"Info","e":[{"n":"Name","sv":"SenHub","asm":"rw"},{"n":"sw","sv":"1.0.0","asm":"r"}]},"Net":{"bn":"Net","e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100,"asm":"r"}]}}}
var 'removeSensorbyKey'( strKey )
- Description: Remove a sensor by key
- Sample Code
var g_strCountKey= '172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15';
dataMgt.removeSensorbyKey(g_strCountKey);
console.log('remove sensor by key = ' + g_strCountKey );
console.log('new capability ' + JSON.stringify(dataMgt.getCapability()));
- Result
remove sensor by key = 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15
new capability {"SNMP_Handler":{"info":{"bn":"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"}]},"SenData":{"bn":"SenData","e":[{"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"}]},"Info":{"bn":"Info","e":[{"n":"Name","sv":"SenHub","asm":"rw"},{"n":"sw","sv":"1.0.0","asm":"r"}]},"Net":{"bn":"Net","e":[{"n":"sw","sv":"3.0.1","asm":"r"},{"n":"Health","v":100,"asm":"r"}]}}}
var getCapability ()
- Description: Get capability.
- Sample Code
g_sendreportcbf(g_strPluginName, dataMgt.getCapability());
int updatebyPath( strPath, value )
- Description: Update value by path
- Sample Code
var strPath = 'SNMP_Handler/SenData/Count'; var g_iCount = 300; dataMgt.updatebyPath( strPath, g_iCount );
int updatebyKey( strKey, value )
- Description: Update value by key
- Sample Code
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 getSenseorbyPath( strPath )
- Description: Get value by path
- Sample Code
var strPath = prefix+'SenData/Temperature';
console.log('get value by path= ' + strPath);
console.log('ret= ' + JSON.stringify(dataMgt.getSenseorbyPath(strPath)));
- Result
get value by path= SNMP_Handler/SenData/Temperature
ret= {"n":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"}
var getSensrobyKey( strKey )
- Description: Get value by key
- Sample
var g_strCountKey = '172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15';
console.log('get value by key= ' + g_strCountKey);
console.log('ret= ' + JSON.stringify(dataMgt.getSenseorbyKey(g_strCountKey)));
- Result
get value by key= 172.22.12.49-1.3.6.1.4.1.10297.101.1.4.1.1.15
ret= {"n":"Count","u":"","v":0,"asm":"rw"}
var queryKeybyPath( strPath )
- Description: Query key by path
- Sample Code
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: Get all keys
- Sample Code
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: Get all paths
- Sample Code
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