AgentLite-Node.js

From ESS-WIKI
Revision as of 10:57, 13 July 2020 by Eric.liang (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

AgentLite-Node.js is a light weight south-bound agent in Node.js. There are 3 main component:

  1. Net-Protocol ( ./lib/WISECore3.3.js)",
  2. main program ( AgentLite3.3.js) 
  3. plugin module ( ./module/plugin_temp.js). Developer need to implement your application in this plugin.js.

AgentLite-Node-js.jpg

 

AgentLite-Node.js

Sofware Components of AgentLite-Node.js

AgentLite-Node.js-SW-Stack.jpg

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

Item Function Description
1 bool initialize ( strName, jInfo ) To initialize the AdvDataMgt
2 bool addSensor( strPath, jsSensor, strKey )
Add a sensor data by path.
3 bool removeSensorbyPath( strPath )
Remove a sensor by path
4 bool removeSensorbyKeystrKey )
Remove a sensor by key
5 bool removeGroup( strPath ) Remove a JSON Goup
6 var getCapability ()
Get capability.​
7 bool updatebyPath( strPath, value )
Update value by path
8 bool updatebyKey( strKey, value )
Update value by key
9 var getSenseorbyPath( strPath )
Get sensor value by path
10 var getSenseorbyKey( strKey )
Get sensor value by key
11 var queryKeybyPath( strPath )
Query key by path
12 var getAllPath( outRet [ ] )
Get all of paths
13 var getAllKey( outRet [ ] )
Get all of keys
14 var getValuebyKey ( strKey ) Get value only by key
15 var getAllLatestValues() Get all latest sensor data values only 
16 bool getUpdatedValues( outRet{}, strPath ) Get latest update sensor data values, strPath = null (All), by JSON group
 17 resetUpdatedValues( strPath ) Reset latest update sensor data table, strPath: NULL ( All), by JSON Group 

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 r'emoveSensorbyPath''''( strPath )'

  • Description: Remove a sensor by 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 removeGroup( strPath )

  • Description: Remove a JSON Group by path
  • Sample Code
var strPath = 'SNMP_Handler/Net';
dataMgt.removeGroup(strPath);
console.log('remove JSON Group by path = ' + strPath );
console.log('new capability ' + JSON.stringify(dataMgt.getCapability()));
  • Result
remove JSON Group by path = SNMP_Handler/Net
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"},{"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"}]}}}

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 = 'SNMP_Handler/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"}]

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

SNMP-NMS Project

Source Code

http://advgitlab.eastasia.cloudapp.azure.com/EdgeSense-Open/agentlite-node.js