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

From AMTech WikiDocs
Jump to: navigation, search
Line 37: Line 37:
 
== Implementing smoothing ==
 
== Implementing smoothing ==
  
In order to include the smoothing tool we propose, the nodejs code should ensure the following
+
In order to include the smoothing tool we propose, the nodejs code should :
* import the smoothing handler class
+
*[[#Import_smoothing_handler|Import the code of the smoothing handler]]
 +
*[[#Initialize_smoothing_handler|Initialize the smoothing handler with the given configuration]]
 +
*[[#Free_memory| Free the memory used by the smoothing handler]]
 +
*[[#Verify_smoothing_criteria| Verify each observation by the smoothing handler before send it]]
 +
 
 +
=== Import smoothing handler ===
 +
To import the smoothing handler class include the code belo at the beginning of the ''nodejs'' file
 
<syntaxhighlight>
 
<syntaxhighlight>
 
const SmoothingHandler = require("../../common/smoothingHandler.js");
 
const SmoothingHandler = require("../../common/smoothingHandler.js");
 
</syntaxhighlight>
 
</syntaxhighlight>
* initialize the smoothing handler in '''start''' function.  
+
 
 +
=== Initialize smoothing handler ===
 +
The smoothing handlers should be initialized on the '''start''' function of the plugin with the smoothing configuration to use (see [[#Smoothing_configuration|below]]).  
 
<syntaxhighlight>
 
<syntaxhighlight>
 
this.smoothingHandler = new SmoothingHandler(smoothingConfig, this.logger, smoothingId);
 
this.smoothingHandler = new SmoothingHandler(smoothingConfig, this.logger, smoothingId);
 
</syntaxhighlight>
 
</syntaxhighlight>
where ''smoothingId'' is used only for logging purpose and ''smoothingConfig'' contains the smoothing configuration (see [[#Smoothing_configuration|below]])
+
where ''smoothingId'' is used only for logging purpose and ''smoothingConfig'' contains the smoothing configuration.
* free the smoothing memory on plugin '''stop''' function
+
 
 +
=== Free memory ===
 +
To avoid issues upon restart of plugins and bridges, the smoothing memory should be freed on plugin '''stop''' function
 
<syntaxhighlight>
 
<syntaxhighlight>
 
this.smoothingHandler.destroy();
 
this.smoothingHandler.destroy();
 
this.smoothingHandler=undefined;
 
this.smoothingHandler=undefined;
 
</syntaxhighlight>
 
</syntaxhighlight>
* verify if the observation satisfy or not the smoothing criteria with the '''smooth''' function of the smoothing handler, for instance:  
+
 
 +
=== Verify smoothing criteria ===
 +
To verify if an observation satisfies or not the smoothing criteria use the '''smooth''' function of the smoothing handler, for instance:  
 
<syntaxhighlight>
 
<syntaxhighlight>
 
if (this.smoothingHandler.smooth(deviceId, observation)){
 
if (this.smoothingHandler.smooth(deviceId, observation)){
Line 59: Line 71:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
If the observation does satisfy any of the smoothing criteria, then an object is returned with every field that satisfied its smoothing criteria
 
+
 
=== Smoothing configuration ===
 
=== Smoothing configuration ===

Revision as of 13:47, 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 :

Import smoothing handler

To import the smoothing handler class include the code belo at the beginning of the nodejs file

const SmoothingHandler = require("../../common/smoothingHandler.js");

Initialize smoothing handler

The smoothing handlers should be initialized on the start function of the plugin with the smoothing configuration to use (see below).

this.smoothingHandler = new SmoothingHandler(smoothingConfig, this.logger, smoothingId);

where smoothingId is used only for logging purpose and smoothingConfig contains the smoothing configuration.

Free memory

To avoid issues upon restart of plugins and bridges, the smoothing memory should be freed on plugin stop function

this.smoothingHandler.destroy();
this.smoothingHandler=undefined;

Verify smoothing criteria

To verify if an observation satisfies or not the smoothing criteria use the smooth function of the smoothing handler, for instance:

if (this.smoothingHandler.smooth(deviceId, observation)){
   this.sendObservation(...);
}

If the observation does satisfy any of the smoothing criteria, then an object is returned with every field that satisfied its smoothing criteria

Smoothing configuration