i'm actualy working on a module to trigger presence state according to a defined host ping response.
my problem is that i can't manage to get back the presence virtual switchBinary from config.
Here is my code (with all the things i've tryed in comment):
module.json:
Code: Select all
{
"author" : "Gwenhael Le Normand",
"category" : "automation_basic",
"defaults" : {
"description" : "__m_descr__",
"title" : "__m_title__",
"device" : "",
"pingInterval" : 1
},
"dependencies" : [
"Presence",
"Cron",
"BaseModule"
],
"homepage" : "https://github.com/Nander2/Zway-PingPresence/",
"icon" : "icon.png",
"maturity" : "beta",
"moduleName" : "PingPresence",
"options" : {
"fields" : {
"ipToPing" : {
"helper" : "__ip_to_ping_helper__",
"label" : "__ip_to_ping_label__",
"order" : 1,
"type" : "ipv4"
},
"device": {
"label": "__l_dev__",
"datasource": "namespaces",
"field": "optionLabels",
"optionLabels": "namespaces:devices_switchBinary:deviceName"
},
"pingInterval" : {
"label" : "__ping_interval__",
"order" : 2,
"type" : "integer"
}
}
},
"repository" : {
"source" : "https://github.com/Nander2/Zway-PingPresence/",
"type" : "git"
},
"schema" : {
"properties" : {
"device": {
"field": "enum",
"datasource": "namespaces",
"enum": "namespaces:devices_switchBinary:deviceId",
"required": true
},
"ipToPing" : {
"format": "ip-address",
"type" : "string",
"required" : true
},
"pingInterval" : {
"type" : "integer",
"required" : true
}
},
"required" : true,
"type" : "object"
},
"singleton" : true,
"version" : 1.0
}
Code: Select all
/*** Ping Presence Z-Way module *******************************************
Version: 1.0
(c) Gwenhaël Le Normand, 2016
-----------------------------------------------------------------------------
Author: Gwenhaël Le Normand
Description:
Module to set presence switch according to ip ping response (of mobile phone for example).
******************************************************************************/
function PingPresence (id, controller) {
// Call superconstructor first (AutomationModule)
PingPresence.super_.call(this, id, controller);
}
inherits(PingPresence, BaseModule);
_module = PingPresence;
// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------
PingPresence.prototype.init = function (config) {
PingPresence.super_.prototype.init.call(this, config);
var self = this;
// add cron schedule every self.config['pingInterval'] minutes
this.controller.emit("cron.addTask", "pingPresence.poll", {
minute: [0, 59, self.config['pingInterval']],
hour: null,
weekDay: null,
day: null,
month: null
});
self.last3PingResult = [0, 0, 0];
controller.on('pingPresence.poll',function()
{
debugPrint(self.config['device']);
code = system('ping -c 1 ' + self.config['ipToPing'] + ' | grep -c \'1 received\'');
if (code != null)
{
if(code[0] != null)
{
debugPrint('Code is ' + code[0]);
//rotate in the table
self.last3PingResult[2] = self.last3PingResult[1];
self.last3PingResult[1] = self.last3PingResult[0];
//add the last one in the table
self.last3PingResult[0] = code[0];
self.checkPresence();
}
}
});
};
PingPresence.prototype.stop = function () {
this.controller.emit("cron.removeTask", "pingPresence.poll");
PingPresence.super_.prototype.stop.call(this);
};
// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------
PingPresence.prototype.checkPresence = function() {
var self = this;
//if at least one ping is ok presence is set to 'on' otherwise presence is set to 'off'
var Presence = self.last3PingResult[0] + self.last3PingResult[1] + self.last3PingResult[2];
//var vDev = controller.devices.get('Presence_presence_13');
var vDev = controller.devices.get(self.config['device']);
if (vDev)
{
if(Presence != 768) //256 * 3
{
vDev.performCommand('on')
}
else
{
vDev.performCommand('off')
}
}
else
{
debugPrint('PingPresence can\'t find Presence_presence_13 device')
}
};
Another strange thing is that when i go in my module configuration page the previously selected device is never set by default (but the others parameters are).
the index.js work fine when i swich the vDev declaration line in the checkPresence function.
So if somebody can take time to see what's wrong in my code i would be grateful.