Difference between revisions of "HelloWorld plugin"

From AMTech WikiDocs
Jump to: navigation, search
(Configure amtechM2MBridge template)
(Implement plugin HelloWorld interface)
Line 16: Line 16:
 
*create a directory with the thing type name HelloWorld at (See [https://github.com/AMTechMX/M2MBridge/tree/master/plugins plugin directory] )
 
*create a directory with the thing type name HelloWorld at (See [https://github.com/AMTechMX/M2MBridge/tree/master/plugins plugin directory] )
 
*at the directory HelloWorld create a javascript file HelloWorld.js
 
*at the directory HelloWorld create a javascript file HelloWorld.js
*implement the interface (See [[M2MBridge# How to implement a new plugin| How to implement a new plugin]])
+
*implement the interface (See [[M2MBridge#How to implement a new plugin| How to implement a new plugin]])
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
     function HelloWorld() {
 
     function HelloWorld() {

Revision as of 13:59, 27 April 2016

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

Define observation types produce or consume by the Thing

  • add obsrvHelloWorldobservation to Observation Production configuration list (See Thing types)
  • add commandHelloWorldobservation to Observation Production configuration list
  • add observationresourcecrud to Observation Production configuration list

Implement plugin HelloWorld interface

    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;

Functionality

  • 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)
    • copy observation instance jsonld
    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 (See Activities)
  • create helloWord notification (See Notifications)
    • 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 (See Observation production configuration)
    • 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}
    • thing type amtechM2mBridge
      • observation type observationresourcecrud
        • topic /helloWorld/asynch
        • producer #{thingId}
  • create observation simulation for observation type obsrvHelloWorld (to be used in observers)
  • create helloWorld observer (See Observers)
    • type observation obsrvHelloWorld
    • test observer
  • create helloWorld reazoner (See Reasoners)
    • add action send notification type helloWorld
    • add action send command
    • test and validate reasoner
  • publishing activity (See Publishing)
    • validate activity
    • deploy activity
  • invite a user as helloWorld actor (See Invite user)
    • user id smartsensor@amtech.mx
  • configure helloWorld m2mBridge template
    • create amtechM2MBridge instance with name helloWorld (See Instances)
    • 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
  • add new action send Command to reasoner helloWorld
    • thing type HelloWorld
    • observation type commandHelloWorld
    • set topic to /helloWorld/asynch
    • set target uri to observation producer (See targetthings)
    • set hwgreetingLabel holaFromCommand

bridgeConfig.json

    {
        "description": "AMTech M2M Bridge Helloworld demo",
        "description:children" : true,
        "dap":{
            "dapUrl": "https://dapdev.amtech.mx",
            "userId": "smartsensor@amtech.mx",
            "tenant" : "m2mfollower",
            "password" :"xxxxxxxx"
        }, 
        "templateId":"helloWorld",
        "bridgeIdPrefix":"helloWorldDemo",
        "location:children" : true,
        "address" :{
            "country" : "USA",
            "city": "New York City",
            "road" : "97th street transverse"
        }     
    }

Debug a plugin