Difference between revisions of "AgentLite-Node.js"

From ESS-WIKI
Jump to: navigation, search
Line 173: Line 173:
 
 It is a librarie help that makes it easy to compose plugin's capability in the Advantech IPSO-like JSON format.
 
 It is a librarie help that makes it easy to compose plugin's capability in the Advantech IPSO-like JSON format.
  
'''Defined'''
+
=== '''Defined''' ===
  
 
'''g_strPluginName''': Name of Plugin 
 
'''g_strPluginName''': Name of Plugin 
  
*Example: g_strPluginName = 'SNMP_Handler';
+
*Sample Code: g_strPluginName = 'SNMP_Handler';
  
 
'''g_jPluginInfo''': Basic informaion of this plugin, You can extend key: value
 
'''g_jPluginInfo''': Basic informaion of this plugin, You can extend key: value
  
*Example: '''g_jPluginInfo ='''
+
*Sample Code: '''g_jPluginInfo ='''
 
<pre>{
 
<pre>{
 
  type:"Protocol",
 
  type:"Protocol",
Line 188: Line 188:
 
  version:"v1.0.2",
 
  version:"v1.0.2",
 
};</pre>
 
};</pre>
 +
 +
=== Function ===
  
 
'''int initialize ( strName, jInfo )'''
 
'''int initialize ( strName, jInfo )'''
  
 
*Description: To initialize the AdvDataMgt
 
*Description: To initialize the AdvDataMgt
 +
*Sample Code
 
<pre>g_jPluginInfo
 
<pre>g_jPluginInfo
 
var dataMgt = new dataMgt();
 
var dataMgt = new dataMgt();
 
dataMgt.initialize(g_strPluginName,g_jPluginInfo);</pre>
 
dataMgt.initialize(g_strPluginName,g_jPluginInfo);</pre>
  
Result
+
*Result
 
<pre>{
 
<pre>{
 
   "SNMP_Handler": {
 
   "SNMP_Handler": {
Line 212: Line 215:
  
 
*Description: To add a sensor data by path.
 
*Description: To add a sensor data by path.
*Example
+
*Sample Code
 
<pre>    var prefix = g_strPluginName+'/';     
 
<pre>    var prefix = g_strPluginName+'/';     
&nbsp;  // SenData     
+
    // SenData     
 
     var strPath = prefix+'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":"Temperature","u":"Cel","v":26.5,"min":-100,"max":200,"asm":"r"});     
 
     dataMgt.addSensor(strPath,{"n":"Count","u":"","v":0, "asm":"rw"},g_strCountKey);     
 
     dataMgt.addSensor(strPath,{"n":"Count","u":"","v":0, "asm":"rw"},g_strCountKey);     
&nbsp;  // Info
+
    // Info
&nbsp;  strPath = prefix + 'Info';     
+
    strPath = prefix + 'Info';     
 
     dataMgt.addSensor(strPath,{"n":"Name","sv":"SenHub","asm":"rw"});     
 
     dataMgt.addSensor(strPath,{"n":"Name","sv":"SenHub","asm":"rw"});     
 
     dataMgt.addSensor(strPath,{"n":"sw","sv":"1.0.0","asm":"r"});     
 
     dataMgt.addSensor(strPath,{"n":"sw","sv":"1.0.0","asm":"r"});     
&nbsp;  // Net     
+
    // Net     
&nbsp;  strPath = prefix + 'Net';     
+
    strPath = prefix + 'Net';     
 
     dataMgt.addSensor(strPath,{"n":"sw","sv":"3.0.1","asm":"r"});     
 
     dataMgt.addSensor(strPath,{"n":"sw","sv":"3.0.1","asm":"r"});     
 
     dataMgt.addSensor(strPath,{"n":"Health","v":100.000000,"asm":"r"});</pre>
 
     dataMgt.addSensor(strPath,{"n":"Health","v":100.000000,"asm":"r"});</pre>
Line 239: Line 242:
  
 
*Description: Get capability.​
 
*Description: Get capability.​
*Example
+
*Sample Code
 
<pre>g_sendreportcbf(g_strPluginName, dataMgt.getCapability());</pre>
 
<pre>g_sendreportcbf(g_strPluginName, dataMgt.getCapability());</pre>
  
Line 245: Line 248:
  
 
*Description: To update value by path
 
*Description: To update value by path
 +
*Sample Code
 
<pre>var strPath = 'SNMP_Handler/SenData/Count';   
 
<pre>var strPath = 'SNMP_Handler/SenData/Count';   
 
var g_iCount = 300;     
 
var g_iCount = 300;     
Line 252: Line 256:
  
 
*Description: To update value by key
 
*Description: To update value by key
*Example
+
*Sample Code
 
<pre>var strPath = 'SNMP_Handler/SenData/Count';
 
<pre>var strPath = 'SNMP_Handler/SenData/Count';
 
var g_iCount = 500;
 
var g_iCount = 500;
Line 260: Line 264:
  
 
*Description: To query key by path
 
*Description: To query key by path
*Example
+
*Sample Code
 
<pre>strPath = 'SNMP_Handler/SenData/Count';
 
<pre>strPath = 'SNMP_Handler/SenData/Count';
 
console.log('query key by path= ' + strPath + ' ret= ' + dataMgt.queryKeybyPath(strPath));</pre>
 
console.log('query key by path= ' + strPath + ' ret= ' + dataMgt.queryKeybyPath(strPath));</pre>
  
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>
  
Line 270: Line 274:
  
 
*Description: To get all keys
 
*Description: To get all keys
*Example
+
*Sample Code
 
<pre>var ret = [];
 
<pre>var ret = [];
 
if ( dataMgt.getAllKey( ret ) === true )
 
if ( dataMgt.getAllKey( ret ) === true )
 
   console.log( 'Number of all key: ' + ret.length + ' result = ' + JSON.stringify( ret ));</pre>
 
   console.log( 'Number of all key: ' + ret.length + ' result = ' + JSON.stringify( ret ));</pre>
  
Reslut
+
*Reslut
 
<pre>Numberof all key: 1
 
<pre>Numberof all key: 1
 
result =  
 
result =  
Line 283: Line 287:
  
 
*Description: To get all paths
 
*Description: To get all paths
*Example
+
*Sample Code
 
<pre>var ret = [];
 
<pre>var ret = [];
 
if( dataMgt.getAllPath( ret ) === true )
 
if( dataMgt.getAllPath( ret ) === true )
 
  console.log( 'Number of all Path: ' + ret.length + ' result = ' + JSON.stringify( ret ));</pre>
 
  console.log( 'Number of all Path: ' + ret.length + ' result = ' + JSON.stringify( ret ));</pre>
  
Result
+
*Result
 
<pre>Number of all Path: 6
 
<pre>Number of all Path: 6
 
result =
 
result =

Revision as of 08:03, 19 December 2018

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). Develop need to implement a 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 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 

  • 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: To 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 getCapability ()

  • Description: Get capability.​
  • Sample Code
g_sendreportcbf(g_strPluginName, dataMgt.getCapability());

int updatebyPath( strPath, value )

  • Description: To 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: To 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 queryKeybyPath( strPath )

  • Description: To 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: To 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: To 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