Difference between revisions of "How to implement a new plugin"

From AMTech WikiDocs
Jump to: navigation, search
Line 34: Line 34:
 
     module.exports.XXXThing = XXXThing;
 
     module.exports.XXXThing = XXXThing;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== Implementing smoothing ==
 +
 +
In order to include the smoothing tool we propose, the nodejs code should ensure the following
 +
* import the smoothing handler class
 +
<syntaxhighlight>
 +
const SmoothingHandler = require("../../common/smoothingHandler.js");
 +
</syntaxhighlight>
 +
* initialize the smoothing handler in '''start''' function.
 +
<syntaxhighlight>
 +
this.smoothingHandler = new SmoothingHandler(smoothingConfig, this.logger, smoothingId);
 +
</syntaxhighlight>
 +
where ''smoothingId'' is used only for logging purpose and ''smoothingConfig'' contains the smoothing configuration (see [[#Smoothing_configuration|below]])
 +
* free the smoothing memory on plugin '''stop''' function
 +
<syntaxhighlight>
 +
this.smoothingHandler.destroy();
 +
this.smoothingHandler=undefined;
 +
</syntaxhighlight>
 +
* verify if the observation satisfy or not the smoothing criteria with the '''smooth''' function of the smoothing handler, for instance:
 +
<syntaxhighlight>
 +
if (this.smoothingHandler.smooth(deviceId, observation)){
 +
  this.sendObservation(...);
 +
}
 +
</syntaxhighlight>
 +
 +
 +
 +
=== Smoothing configuration ===

Revision as of 11:28, 2 February 2018

  • Create a thing type XXXThing (See Thing types)
  • Create the observations type the thing XXXThing produces or consumes, example XXXThingObserv1 (See Observations and observation types)
  • Associate the observation XXXThingObserv1 type to the XXXThing
  • Implement a nodejs module with the following interface
    function XXXThing() {
    }

    XXXThing.prototype.start = function ( complete) {
        try {
            complete(null);
        } catch (e) {
            complete(e);
        }
    };

    XXXThing.prototype.stop = function (complete) {
        try {
            complete(null);
        } catch (e) {
            complete(e);
        }
    };

    XXXThing.prototype.command = function (observation, complete) {
        try {
            complete(null);
        } catch (e) {
            complete(e);
        }
    };

    module.exports.XXXThing = XXXThing;

Implementing smoothing

In order to include the smoothing tool we propose, the nodejs code should ensure the following

  • import the smoothing handler class
const SmoothingHandler = require("../../common/smoothingHandler.js");
  • initialize the smoothing handler in start function.
this.smoothingHandler = new SmoothingHandler(smoothingConfig, this.logger, smoothingId);

where smoothingId is used only for logging purpose and smoothingConfig contains the smoothing configuration (see below)

  • free the smoothing memory on plugin stop function
this.smoothingHandler.destroy();
this.smoothingHandler=undefined;
  • verify if the observation satisfy or not the smoothing criteria with the smooth function of the smoothing handler, for instance:
if (this.smoothingHandler.smooth(deviceId, observation)){
   this.sendObservation(...);
}


Smoothing configuration