Difference between revisions of "HelloWorld plugin"
From AMTech WikiDocs
(→Implement plugin HelloWorld interface) |
(→bridgeConfig.json) |
||
(30 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | === HelloWorld plugin flow === | ||
+ | *Create HelloWorld thing type | ||
+ | *Create observation(s) type | ||
+ | *Implement plugin node.js interface | ||
+ | *Configuration to test plugin | ||
+ | **Create amtechM2MBridge instance | ||
+ | **Create HelloWorld instance | ||
+ | **Create activity HelloWorld (See [[Hello_World_activity_flow|Hello World activity flow]]) | ||
+ | |||
=== Create thing type HelloWorld === | === Create thing type HelloWorld === | ||
− | * | + | *Add supported property type string hwgreetingLabel (See [[Activities#Thing types|Thing types]]) |
− | ** | + | **String without space validation regexp ^[^\s]+$ |
− | * | + | *Add supported property type integer hwSayHelloFrequency |
=== Create observation type obsrvHelloWorld === | === Create observation type obsrvHelloWorld === | ||
− | * | + | *Add supported property type string hwgreetingLabel (See [[Sensor's network#Observations and observation types|Observations and observation types]]) |
− | * | + | *Clone obsrvHelloWorld with name commandHelloWorld |
− | === Define observation types produce or consume by | + | === Define observation types produce or consume by HelloWorld === |
− | * | + | *Add obsrvHelloWorldobservation to Observation Production configuration list (See [[Activities#Thing types|Thing types]]) |
− | * | + | *Add commandHelloWorldobservation to Observation Production configuration list |
− | * | + | *Add observationresourcecrud to Observation Production configuration list |
+ | **Enables listen per configuration changes | ||
=== Implement plugin HelloWorld interface === | === Implement plugin HelloWorld interface === | ||
− | * | + | *Create a directory with the name HelloWorld at (See [https://github.com/AMTechMX/M2MBridge/tree/master/plugins plugin directory] ) |
− | * | + | *At the directory HelloWorld create a javascript file HelloWorld.js |
− | * | + | *Implement the interface (See [[How to implement a new plugin|How to implement a new plugin]]) |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
function HelloWorld() { | function HelloWorld() { | ||
Line 48: | Line 58: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | === | + | === What helloWorld does === |
− | * | + | *Send an observation obsrvHelloWorldobservation with the frequency set by the property hwSayHelloFrequency |
− | * | + | *Change hwgreetingLabel value when receive command obsrvHelloWorld observation |
− | * | + | *Implement placeholder #{greetingLabel} |
− | * | + | *Start method |
− | ** | + | **Create an observation type obsrvHelloWorld at /amtech/things/observations (See [[Simulator|Simulator]]) |
− | **copy observation instance jsonld | + | **Tip copy observation instance jsonld using jsonld Viewer (See [[Tree editor|Json Viewer]]) |
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var clone = require('clone'); | var clone = require('clone'); | ||
Line 118: | Line 128: | ||
=== Configure amtechM2MBridge template === | === Configure amtechM2MBridge template === | ||
*(See [[M2M Bridge#Cloud Configuration|Cloud Configuration]]) | *(See [[M2M Bridge#Cloud Configuration|Cloud Configuration]]) | ||
− | * | + | *Create [[Activities|activity]] helloWorld |
− | * | + | *Create helloWord [[Notifications|notification]] |
− | ** | + | **Add subject with [[Sensor%27s_network#Placeholders_substitution|placeholder]] #{label} |
− | ** | + | **Add subject with [[Sensor%27s_network#Placeholders_substitution|placeholder]] #{producer} and #{label} |
− | * | + | *Create helloWorld actor with access [[Access_control#Policies|polices]] (See [[Access control#Actors|Actors]]) |
− | ** | + | **AmtechM2MBridge thing type |
**HelloWord thing type | **HelloWord thing type | ||
**helloWord notification | **helloWord notification | ||
− | ** | + | **Associate [[Activities#Actors|helloWorld actor]] with helloWorld activity |
− | * | + | *Configure [[Observation_production_config|observation production]] |
− | ** | + | ** Thing type HelloWorld |
− | *** | + | ***Observation type obsrvHelloWorld |
− | **** | + | ****Topic /helloWorld/#{greetingLabel} |
− | **** | + | ****Producer #{thingId} |
****guestTeants add m2mcreator to shared the observation | ****guestTeants add m2mcreator to shared the observation | ||
***observation type observationresourcecrud | ***observation type observationresourcecrud | ||
Line 140: | Line 150: | ||
****topic /helloWorld/asynch | ****topic /helloWorld/asynch | ||
****producer #{thingId} | ****producer #{thingId} | ||
− | + | *publishing activity (See [[Activities#Publishing|Publishing]]) | |
− | + | **validate activity | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | *publishing activity (See [[Publishing|Publishing]]) | + | |
− | **validate | + | |
− | + | ||
*invite a user as helloWorld actor (See [[Access control#Actors|Invite user]]) | *invite a user as helloWorld actor (See [[Access control#Actors|Invite user]]) | ||
− | |||
*configure helloWorld m2mBridge template | *configure helloWorld m2mBridge template | ||
− | ** create amtechM2MBridge instance with name helloWorld (See [[ | + | ** create amtechM2MBridge instance with name helloWorld (See [[Things#Thing_instances|Instances]]) |
− | ***it will be the value of templateId at bridgeConfig.json | + | ***it will be the value of templateId at [[Edge_Configuration|bridgeConfig.json]] |
**create HelloWorld type instace with name helloWorld | **create HelloWorld type instace with name helloWorld | ||
**associate HelloWorld instance to amtechM2MBridge instance propety '''bridgeInstances''' | **associate HelloWorld instance to amtechM2MBridge instance propety '''bridgeInstances''' | ||
− | **give guest access to helloWorld M2MBridge instance to tenant m2mfollower and user smartsensor@amtech.mx | + | **give guest access to helloWorld M2MBridge instance to tenant m2mfollower and user smartsensor@amtech.mx (if check user id set in [[Access_control#Policies|policies]]) |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
=== bridgeConfig.json === | === bridgeConfig.json === | ||
Line 171: | Line 165: | ||
{ | { | ||
"description": "AMTech M2M Bridge Helloworld demo", | "description": "AMTech M2M Bridge Helloworld demo", | ||
− | |||
"dap":{ | "dap":{ | ||
"dapUrl": "https://dapdev.amtech.mx", | "dapUrl": "https://dapdev.amtech.mx", | ||
Line 180: | Line 173: | ||
"templateId":"helloWorld", | "templateId":"helloWorld", | ||
"bridgeIdPrefix":"helloWorldDemo", | "bridgeIdPrefix":"helloWorldDemo", | ||
− | "location: | + | "children": { |
+ | "location" : "inherit", | ||
+ | "description": "inherit" | ||
+ | }, | ||
"address" :{ | "address" :{ | ||
"country" : "USA", | "country" : "USA", | ||
Line 192: | Line 188: | ||
*node-inspector & | *node-inspector & | ||
**[https://greenido.wordpress.com/2013/08/27/debug-nodejs-like-a-pro/ Debug NodeJS Like A Pro] | **[https://greenido.wordpress.com/2013/08/27/debug-nodejs-like-a-pro/ Debug NodeJS Like A Pro] | ||
+ | ***visit http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 | ||
**[https://mattdesl.svbtle.com/debugging-nodejs-in-chrome-devtools Debugging Node.js in Chrome DevTools] | **[https://mattdesl.svbtle.com/debugging-nodejs-in-chrome-devtools Debugging Node.js in Chrome DevTools] | ||
− | * | + | **[[How_to_install_%26_execute#Remote_debugging|Remote Debugging]] |
Latest revision as of 12:52, 11 October 2017
Contents
HelloWorld plugin flow
- Create HelloWorld thing type
- Create observation(s) type
- Implement plugin node.js interface
- Configuration to test plugin
- Create amtechM2MBridge instance
- Create HelloWorld instance
- Create activity HelloWorld (See Hello World activity flow)
Create thing type HelloWorld
- Add supported property type string hwgreetingLabel (See Thing types)
- String without space validation regexp ^[^\s]+$
- Add supported property type integer hwSayHelloFrequency
Create observation type obsrvHelloWorld
- Add supported property type string hwgreetingLabel (See Observations and observation types)
- Clone obsrvHelloWorld with name commandHelloWorld
Define observation types produce or consume by HelloWorld
- Add obsrvHelloWorldobservation to Observation Production configuration list (See Thing types)
- Add commandHelloWorldobservation to Observation Production configuration list
- Add observationresourcecrud to Observation Production configuration list
- Enables listen per configuration changes
Implement plugin HelloWorld interface
- Create a directory with the name HelloWorld at (See plugin directory )
- At the directory HelloWorld create a javascript file HelloWorld.js
- Implement the interface (See How to implement a new plugin)
function HelloWorld() {
}
HelloWorld.prototype.start = function (complete) {
try {
complete(null);
} catch (e) {
complete(e);
}
};
HelloWorld.prototype.stop = function (complete) {
try {
complete(null);
} catch (e) {
complete(e);
}
};
HelloWorld.prototype.command = function (observation, complete) {
try {
complete(null);
} catch (e) {
complete(e);
}
};
module.exports.HelloWorld = HelloWorld;
What helloWorld does
- Send an observation obsrvHelloWorldobservation with the frequency set by the property hwSayHelloFrequency
- Change hwgreetingLabel value when receive command obsrvHelloWorld observation
- Implement placeholder #{greetingLabel}
- Start method
- Create an observation type obsrvHelloWorld at /amtech/things/observations (See Simulator)
- Tip copy observation instance jsonld using jsonld Viewer (See Json Viewer)
var clone = require('clone');
var util = require('util');
function HelloWorld() {
}
HelloWorld.prototype.start = function (complete) {
var self = this;
try {
self.sayHelloInterval = setInterval(function(){
var obsrvHelloWorld = clone({
"proximityarea": "",
"topic": "",
"guestusers": [],
"targetthings": "[]",
"location": "",
"@type": "/amtech/linkeddata/types/composite/observation/obsrvHelloWorld",
"hwgreetingLabel": "",
"creationDate": "2016-04-20T03:24:54.099Z",
"guesttenants": [],
"description": "",
"producer": ""
});
obsrvHelloWorld.hwgreetingLabel = self.hwgreetingLabel;
var ph = {'greetingLabel': self.hwgreetingLabel};
self.sendObservation(self,obsrvHelloWorld, ph );
}, self.hwSayHelloFrequency);
complete(null);
} catch (e) {
complete(e);
}
};
HelloWorld.prototype.stop = function (complete) {
try {
if (this.sayHelloInterval) {
clearInterval(this.sayHelloInterval);
}
complete(null);
} catch (e) {
complete(e);
}
};
HelloWorld.prototype.command = function (observation, complete) {
try {
if(observation['@type'] === "/amtech/linkeddata/types/composite/observation/commandHelloWorld"){
this.hwgreetingLabel = observation.hwgreetingLabel;
complete(null);
}else{
complete(new Error(util.format("HelloWorld %s support commands of observations type commandHelloWorld", this._name)));
}
} catch (e) {
complete(e);
}
};
module.exports.HelloWorld = HelloWorld;
Configure amtechM2MBridge template
- (See Cloud Configuration)
- Create activity helloWorld
- Create helloWord notification
- Add subject with placeholder #{label}
- Add subject with placeholder #{producer} and #{label}
- Create helloWorld actor with access polices (See Actors)
- AmtechM2MBridge thing type
- HelloWord thing type
- helloWord notification
- Associate helloWorld actor with helloWorld activity
- Configure observation production
- Thing type HelloWorld
- Observation type obsrvHelloWorld
- Topic /helloWorld/#{greetingLabel}
- Producer #{thingId}
- guestTeants add m2mcreator to shared the observation
- observation type observationresourcecrud
- topic /helloWorld/asynch
- producer #{thingId}
- Observation type obsrvHelloWorld
- thing type amtechM2mBridge
- observation type observationresourcecrud
- topic /helloWorld/asynch
- producer #{thingId}
- observation type observationresourcecrud
- Thing type HelloWorld
- publishing activity (See Publishing)
- validate activity
- invite a user as helloWorld actor (See Invite user)
- configure helloWorld m2mBridge template
- create amtechM2MBridge instance with name helloWorld (See Instances)
- it will be the value of templateId at bridgeConfig.json
- create HelloWorld type instace with name helloWorld
- associate HelloWorld instance to amtechM2MBridge instance propety bridgeInstances
- give guest access to helloWorld M2MBridge instance to tenant m2mfollower and user smartsensor@amtech.mx (if check user id set in policies)
- create amtechM2MBridge instance with name helloWorld (See Instances)
bridgeConfig.json
- (See Edge Configuration)
{
"description": "AMTech M2M Bridge Helloworld demo",
"dap":{
"dapUrl": "https://dapdev.amtech.mx",
"userId": "smartsensor@amtech.mx",
"tenant" : "m2mfollower",
"password" :"xxxxxxxx"
},
"templateId":"helloWorld",
"bridgeIdPrefix":"helloWorldDemo",
"children": {
"location" : "inherit",
"description": "inherit"
},
"address" :{
"country" : "USA",
"city": "New York City",
"road" : "97th street transverse"
}
}