hap-node-client
Advanced tools
Comparing version 0.1.10 to 0.1.11
'use strict'; | ||
var request = require('requestretry'); | ||
// var request = require('./lib/hapRequest.js'); | ||
var hapRequest = require('./lib/hapRequest.js'); | ||
@@ -18,3 +17,3 @@ var EventEmitter = require('events').EventEmitter; | ||
var filter = false; | ||
var pin; | ||
var pins = {}; | ||
@@ -48,3 +47,3 @@ module.exports = { | ||
this.reqTimeout = options.reqTimeout || 7000; | ||
pin = options.pin || '031-45-154'; | ||
this.RegisterPin('default', options.pin || '031-45-154'); | ||
filter = options.filter || false; | ||
@@ -64,3 +63,3 @@ if (this.debug) { | ||
} | ||
this.eventRegistry = []; | ||
this.eventRegistry = {}; | ||
_discovery.call(this); | ||
@@ -200,3 +199,32 @@ this._eventBus = new EventEmitter(); | ||
function _findPinByKey(key) { | ||
if (!key) { | ||
return pins['default']; | ||
} | ||
key = key.toLowerCase(); | ||
return pins[key] || pins['default']; | ||
} | ||
/** | ||
* HAPNodeJSClient.prototype.RegisterPin - Register pin numbers () | ||
* | ||
* @class | ||
* @param {type} key Unique identifier of homebridge instance (ip:port or deviceID) | ||
* @param {type} pin Homebridge PIN | ||
* @return {type} bool updated | ||
*/ | ||
HAPNodeJSClient.prototype.RegisterPin = function(key, pin) { | ||
if (!key || (key in pins && pins[key] === pin)) { | ||
return false; | ||
} | ||
key = key.toLowerCase(); | ||
pins[key] = pin; | ||
debug('Registered/updated PIN for `%s`: %s', key, pin); | ||
return true; | ||
}; | ||
/** | ||
* HAPNodeJSClient.prototype.HAPaccessories - Returns an array of all homebridge instances, and the accessories for each. | ||
@@ -250,6 +278,8 @@ * | ||
HAPNodeJSClient.prototype.HAPcontrol = function(ipAddress, port, body, callback) { | ||
var host = ipAddress + ':' + port; | ||
var pin = _findPinByKey(host); | ||
request({ | ||
eventBus: this._eventBus, | ||
method: 'PUT', | ||
url: 'http://' + ipAddress + ':' + port + '/characteristics', | ||
url: 'http://' + host + '/characteristics', | ||
timeout: this.reqTimeout, | ||
@@ -337,2 +367,3 @@ maxAttempts: 5, // (default) try 5 times | ||
function clearTimer(err, rsp) { | ||
var events = []; | ||
if (err) { | ||
@@ -345,3 +376,2 @@ debug('HAPevent event reregister failed, retry in 60', server); | ||
debug('clearTimer', server, this.eventRegistry[server.deviceID]); | ||
var events = []; | ||
this.eventRegistry[server.deviceID].forEach(function(device) { | ||
@@ -364,3 +394,2 @@ events.push({ | ||
debug('HAPevent event reregister succeeded', server); | ||
var events = []; | ||
this.eventRegistry[server.deviceID].forEach(function(device) { | ||
@@ -401,2 +430,5 @@ events.push({ | ||
} else { | ||
var host = instance.host + ':' + instance.port; | ||
var pin = _findPinByKey(deviceID); | ||
hapRequest({ | ||
@@ -434,4 +466,4 @@ eventBus: this._eventBus, | ||
var rsp; | ||
this.RegisterPin(host, pin); | ||
// var key = ipAddress + ':' + port; | ||
if (!this.eventRegistry[deviceID]) { | ||
@@ -473,6 +505,8 @@ this.eventRegistry[deviceID] = []; | ||
HAPNodeJSClient.prototype.HAPevent = function(ipAddress, port, body, callback) { | ||
var host = ipAddress + ':' + port; | ||
var pin = _findPinByKey(host); | ||
hapRequest({ | ||
eventBus: this._eventBus, | ||
method: 'PUT', | ||
url: 'http://' + ipAddress + ':' + port + '/characteristics', | ||
url: 'http://' + host + '/characteristics', | ||
timeout: this.reqTimeout, | ||
@@ -563,6 +597,9 @@ maxAttempts: 5, // (default) try 5 times | ||
HAPNodeJSClient.prototype.HAPresource = function(ipAddress, port, body, callback) { | ||
var host = ipAddress + ':' + port; | ||
var pin = _findPinByKey(host); | ||
request({ | ||
eventBus: this._eventBus, | ||
method: 'POST', | ||
url: 'http://' + ipAddress + ':' + port + '/resource', | ||
url: 'http://' + host + '/resource', | ||
timeout: this.reqTimeout, | ||
@@ -642,2 +679,5 @@ maxAttempts: 5, // (default) try 5 times | ||
HAPNodeJSClient.prototype.HAPstatus = function(ipAddress, port, body, callback) { | ||
var host = ipAddress + ':' + port; | ||
var pin = _findPinByKey(host); | ||
// debug('HAPstatus', pin); | ||
@@ -647,3 +687,3 @@ request({ | ||
method: 'GET', | ||
url: 'http://' + ipAddress + ':' + port + '/characteristics' + body, | ||
url: 'http://' + host + '/characteristics' + body, | ||
timeout: this.reqTimeout, | ||
@@ -691,2 +731,5 @@ maxAttempts: 5, // (default) try 5 times | ||
if ((filter && filter === instance.host + ':' + instance.port) || !filter) { | ||
var host = instance.host + ':' + instance.port; | ||
var pin = _findPinByKey(host); | ||
request({ | ||
@@ -693,0 +736,0 @@ eventBus: this._eventBus, |
{ | ||
"name": "hap-node-client", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"description": "Client for Hap-NodeJS", | ||
@@ -5,0 +5,0 @@ "main": "HAPNodeJSClient.js", |
71805
1739