HelloWorld plugin

From AMTech WikiDocs
Jump to: navigation, search

HelloWorld plugin flow

  • Create HelloWorld thing type
  • Create observation(s) type
  • Implement plugin node.js interface
  • Configuration to test plugin

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 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

    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
  • 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}
    • thing type amtechM2mBridge
      • observation type observationresourcecrud
        • topic /helloWorld/asynch
        • producer #{thingId}
  • 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)
    • 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)

bridgeConfig.json

    {
        "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"
        }     
    }

Debug a plugin