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

From AMTech WikiDocs
Jump to: navigation, search
(Verify smoothing criteria)
(Implementing smoothing)
Line 35: Line 35:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Implementing smoothing ==
+
== Smoothing observations ==
 +
In order to allow the plugin to reduce  the number of unnecessary observations, we propose a ''SmoothingHandler'' class. This class implements the smoothing process described [[here]] according to some given configurations.
 +
  The main advantages of using this class would be:
 +
* New smoothing criteria could be included in the future according to the users needs.
 +
* Smoothing process can be done in a device-wise manner allowing to handle several devices with the same plugin instance (e.g. with beacon scanners)
 +
* It is possible to use several independent smoothing processes at the same time according to different models (for instance one model for ibeacons and one for eddystone ones)
 +
* Possibility to know which fields satisfied their configured smoothing criteria
  
 +
=== Using smoothing in the plugins ===
 
In order to include the smoothing tool we propose, the nodejs code should :
 
In order to include the smoothing tool we propose, the nodejs code should :
 
*[[#Import_smoothing_handler|Import the code of the smoothing handler]]
 
*[[#Import_smoothing_handler|Import the code of the smoothing handler]]
Line 43: Line 50:
 
*[[#Verify_smoothing_criteria| Verify each observation by the smoothing handler before send it]]
 
*[[#Verify_smoothing_criteria| Verify each observation by the smoothing handler before send it]]
  
=== Import smoothing handler ===
+
==== Import smoothing handler ====
 
To import the smoothing handler class include the code belo at the beginning of the ''nodejs'' file
 
To import the smoothing handler class include the code belo at the beginning of the ''nodejs'' file
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 49: Line 56:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Initialize smoothing handler ===
+
==== 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]]).  
 
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>
Line 56: Line 63:
 
where ''smoothingId'' is used only for logging purpose and ''smoothingConfig'' contains the smoothing configuration.
 
where ''smoothingId'' is used only for logging purpose and ''smoothingConfig'' contains the smoothing configuration.
  
=== Free memory ===
+
==== Free memory ====
 
To avoid issues upon restart of plugins and bridges, the smoothing memory should be freed on plugin '''stop''' function
 
To avoid issues upon restart of plugins and bridges, the smoothing memory should be freed on plugin '''stop''' function
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 63: Line 70:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Verify smoothing criteria ===  
+
==== Verify smoothing criteria ====  
 
To verify if an observation satisfies or not the smoothing criteria use the '''smooth''' function of the smoothing handler, for instance:  
 
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, observationInfo)){
 
   this.sendObservation(...);
 
   this.sendObservation(...);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If the observation does satisfy any of the smoothing criteria, then an object is returned with every field that satisfied its corresponding smoothing criteria. If no field hold its conditions then the function returns '''undefined'''
+
It is possible also to know which fields have satisfied the smoothing criteria by including a third parameter holding an array:
 +
<syntaxhighlight>
 +
  var changedFields = [];
 +
  var smoothingSatisfied=this.smoothingHandler.smooth(deviceId, observationInfo, changedFields));
 +
</syntaxhighlight>
 +
 
  
 
=== Smoothing configuration ===
 
=== Smoothing configuration ===

Revision as of 14:31, 20 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;

Smoothing observations

In order to allow the plugin to reduce the number of unnecessary observations, we propose a SmoothingHandler class. This class implements the smoothing process described here according to some given configurations.

 The main advantages of using this class would be:
  • New smoothing criteria could be included in the future according to the users needs.
  • Smoothing process can be done in a device-wise manner allowing to handle several devices with the same plugin instance (e.g. with beacon scanners)
  • It is possible to use several independent smoothing processes at the same time according to different models (for instance one model for ibeacons and one for eddystone ones)
  • Possibility to know which fields satisfied their configured smoothing criteria

Using smoothing in the plugins

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, observationInfo)){
   this.sendObservation(...);
}

It is possible also to know which fields have satisfied the smoothing criteria by including a third parameter holding an array:

  var changedFields = [];
  var smoothingSatisfied=this.smoothingHandler.smooth(deviceId, observationInfo, changedFields));


Smoothing configuration