Development : can't manage to get deviceId from config

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
Nander2
Posts: 8
Joined: 08 Nov 2016 12:37

Development : can't manage to get deviceId from config

Post by Nander2 »

Hi everyone,

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
}
And index.js

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')
        }
};
In the PingPresence.prototype.checkPresence function i can't manage to get the selected device. i also try to get the device direclty in the pingPresence.poll callback and pass it to checkPresence as parameter but it doesn't work too.

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.
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Re: Development : can't manage to get deviceId from config

Post by gsaw »

Could you commit your version? Then i whould checkout it from git an try by me.
Nander2
Posts: 8
Joined: 08 Nov 2016 12:37

Re: Development : can't manage to get deviceId from config

Post by Nander2 »

it's done, you can checkout it.

thank
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Re: Development : can't manage to get deviceId from config

Post by gsaw »

Seems to be working.

Code: Select all

[2016-11-08 20:34:39.664] [I] [core] Loading module PingPresence from userModules/PingPresence
[2016-11-08 20:34:39.667] [I] [core] Executing script: /*** Ping Presence Z-Way module ******************************************* ...
[2016-11-08 20:34:39.673] [I] [core] Instantiating module 26 from class PingPresence
[2016-11-08 20:34:39.676] [I] [core] --- Starting module Ping Presence state
[2016-11-08 20:34:39.692] [I] [core] PingPresence: Update interval = 1
[2016-11-08 20:34:40.881] [I] [core] Loading module PingPresence_ from userModules/PingPresence_
[2016-11-08 20:34:40.885] [I] [core] Executing script: /*** Ping Presence Z-Way module ******************************************* ...
[2016-11-08 20:34:42.954] [I] [core] PingPresence: check device: DummyDevice_24
[2016-11-08 20:34:52.993] [I] [core] PingPresence: Code is 256
[2016-11-08 20:34:52.998] [I] [core] PingPresence: Update device [object Object]
[2016-11-08 20:35:00.996] [I] [core] PingPresence: check device: DummyDevice_24
[2016-11-08 20:35:11.026] [I] [core] PingPresence: Code is 256
[2016-11-08 20:35:11.028] [I] [core] PingPresence: Update device [object Object]
[2016-11-08 20:35:51.780] [I] [core] --- Stopping module Ping Presence state
[2016-11-08 20:35:51.789] [I] [core] --- Starting module Ping Presence state
[2016-11-08 20:35:51.807] [I] [core] PingPresence: Update interval = 1
[2016-11-08 20:36:00.275] [I] [core] PingPresence: check device: DummyDevice_24
[2016-11-08 20:36:00.325] [I] [core] PingPresence: Code is 0
[2016-11-08 20:36:00.328] [I] [core] PingPresence: Update device [object Object]
[2016-11-08 20:36:00.367] [I] [core] PingPresence: check device: DummyDevice_24

Changed Code is below.

main change is "self.config.device" instead of "self.config['device']" and i commented out first var vDev in last function.

One thing is remains. The ping tool blocks for seconds the whole z-way-server if remote host is non reachable. May be you can add a timeout for ping, but it would block anyway the server. May be there is a possibility to start ping in a separate thread. I don't know.

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;

	self.debug =function(msg) {
		debugPrint("PingPresence: " + msg);
	};	
		
	// add cron schedule every self.config.pingInterval minutes
	self.debug("Update interval = " + self.config.pingInterval);
	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() 
	{
		self.debug("check device: " + self.config.device);
                code = system('ping -c 1 ' + self.config.ipToPing + ' | grep -c \'1 received\'');
		if (code != null)
		{
			if(code[0] != null)
			{
				self.debug('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;
    
	//var vDev = controller.devices.get('Presence_presence_13');
	
	//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(self.config.device);
    	self.debug("Update device " + vDev );
	//debugPrint('[0] ' + self.last3PingResult[0]);
	//debugPrint('[1] ' + self.last3PingResult[1]);
	//debugPrint('[2] ' + self.last3PingResult[2]);
	if (vDev)
	{
		if(Presence != 768) //256 * 3
		{	
			vDev.performCommand('on')
		}
		else
		{
			vDev.performCommand('off')
		}
	}
	else
	{
		self.debug("PingPresence can\'t find " + self.config.device + " device")
	}
};

Nander2
Posts: 8
Joined: 08 Nov 2016 12:37

Re: Development : can't manage to get deviceId from config

Post by Nander2 »

Ok, thank a lot. it's working for me too now.

But there is just a thing i still doesn't understand:

Why do i have to use :

Code: Select all

code = system('ping -c 1 ' + self.config['ipToPing'] + ' | grep -c \'1 received\'');
to get ip parameter and to use :

Code: Select all

 var vDev = controller.devices.get(this.config.device);
to get the device?

do i have to use :

Code: Select all

code = system('ping -c 1 ' + self.config.ipToPing + ' | grep -c \'1 received\'');
?
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Re: Development : can't manage to get deviceId from config

Post by gsaw »

I think both variants are ok. your problem seemed to be related to twice vDev decloration. I replaced acess config parameter to names just in case. One place i forgot to change.
Nander2
Posts: 8
Joined: 08 Nov 2016 12:37

Re: Development : can't manage to get deviceId from config

Post by Nander2 »

Ok great. I will take a look at the possibility to use a non blocking (or timeouted) call to ping system call and submit this module in the app store.

Thank a lot
Post Reply