hap-node-client
Advanced tools
Comparing version 0.0.14 to 0.0.15
"use strict"; | ||
var request = require('./lib/hapRequest.js'); | ||
var request = require('requestretry'); | ||
// var request = require('./lib/hapRequest.js'); | ||
var hapRequest = require('./lib/hapRequest.js'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -16,23 +18,11 @@ var inherits = require('util').inherits; | ||
/* | ||
HAPaccessories | ||
HAPcontrol | ||
HAPstatus | ||
External events | ||
Ready - Emitted after every discovery cycle | ||
hapEvent - Emitted when an HAP EVENT message is revieved | ||
*/ | ||
/** | ||
* HAPNodeJSClient - description | ||
* HAPNodeJSClient - Client for Homebridge and HAP-NodeJS in insecure mode. | ||
* | ||
* Events | ||
* | ||
* @class | ||
* @param {type} options description | ||
* @return {type} description | ||
* @example | ||
* | ||
*/ | ||
@@ -63,2 +53,16 @@ | ||
debug('Event', event); | ||
/** | ||
* HomeKit Accessory Characteristic event pass thru | ||
* | ||
* @event HAPNodeJSClient#hapEvent | ||
* @Type {object} | ||
* @property {string} host - IP Address of homebridge instance generating event | ||
* @property {number} port - Port of homebridge instance generating event | ||
* @property {number} aid - Accessory ID of accessory generating event | ||
* @property {number} iid - Instance ID of accessory characteristic generating event | ||
* @property {???} status - Updated characteristic value | ||
* @example Sample Message | ||
* | ||
* { host: '192.168.1.4', port: 51826, aid: 16, iid: 11, status: false } | ||
*/ | ||
this.emit('hapEvent', event); | ||
@@ -116,3 +120,3 @@ this.emit(event.host + event.port + event.aid + event.iid, event); | ||
/** | ||
* HAPNodeJSClient.prototype.HAPaccessories - description | ||
* HAPNodeJSClient.prototype.HAPaccessories - Returns an array of all homebridge instances, and the accessories for each. | ||
* | ||
@@ -132,9 +136,8 @@ * @param {type} callback description | ||
/** | ||
* HAPNodeJSClient.prototype.HAPcontrol - description | ||
* HAPNodeJSClient.prototype.HAPcontrol - Send a characteristic PUT Message to a particular homebridge instance | ||
* | ||
* @param {type} ipAddress description | ||
* @param {type} port description | ||
* @param {type} body description | ||
* @param {type} callback description | ||
* @return {type} description | ||
* @param {type} ipAddress IP Address of homebridge instance | ||
* @param {type} port Port of homebridge instance | ||
* @param {type} body An array of HomeKit characteristic updates, [{ \"aid\": 2, \"iid\": 9, \"value\": 0}] | ||
* @param {type} callback Callback to execute upon completion of characteristic setting, function(err, response) | ||
*/ | ||
@@ -186,11 +189,62 @@ | ||
/** | ||
* HAPNodeJSClient.prototype.HAPresource - description | ||
* HAPNodeJSClient.prototype.HAPevent - Send a characteristic PUT Message to a particular homebridge instance, this maintains a socket connection for use in returning Events | ||
* | ||
* @param {type} ipAddress description | ||
* @param {type} port description | ||
* @param {type} body description | ||
* @param {type} callback description | ||
* @return {type} description | ||
* @param {type} ipAddress IP Address of homebridge instance | ||
* @param {type} port Port of homebridge instance | ||
* @param {type} body An array of HomeKit characteristic updates, [{ \"aid\": 2, \"iid\": 9, \"value\": 0}] | ||
* @param {type} callback Callback to execute upon completion of characteristic setting, function(err, response) | ||
*/ | ||
HAPNodeJSClient.prototype.HAPevent = function(ipAddress, port, body, callback) { | ||
hapRequest({ | ||
eventBus: this._eventBus, | ||
method: 'PUT', | ||
url: 'http://' + ipAddress + ':' + port + '/characteristics', | ||
timeout: 7000, | ||
maxAttempts: 1, // (default) try 5 times | ||
headers: { | ||
"Content-Type": "Application/json", | ||
"authorization": this.pin, | ||
"connection": "keep-alive" | ||
}, | ||
body: body | ||
}, function(err, response) { | ||
// Response s/b 200 OK | ||
if (err) { | ||
debug("Homebridge event reg failed %s:%s", ipAddress, port, body, err); | ||
callback(err); | ||
} else if (response.statusCode !== 207) { | ||
if (response.statusCode === 401) { | ||
debug("Homebridge auth failed, invalid PIN %s %s:%s", this.pin, ipAddress, port, body, err, response.body); | ||
callback(new Error("Homebridge auth failed, invalid PIN " + this.pin)); | ||
} else { | ||
debug("Homebridge event reg failed %s:%s Status: %s ", ipAddress, port, response.statusCode, body, err, response.body); | ||
callback(new Error("Homebridge event reg failed")); | ||
} | ||
} else { | ||
var rsp; | ||
try { | ||
rsp = response.body; | ||
} catch (ex) { | ||
debug("Homebridge Response Failed %s:%s", ipAddress, port, response.statusCode, response.statusMessage); | ||
debug("Homebridge Response Failed %s:%s", ipAddress, port, response.body, ex); | ||
callback(new Error(ex)); | ||
} | ||
callback(null, rsp); | ||
} | ||
}); | ||
}; | ||
/** | ||
* HAPNodeJSClient.prototype.HAPresource - Send a characteristic PUT Message to a particular homebridge instance using resource interface, ie camera | ||
* | ||
* @param {type} ipAddress IP Address of homebridge instance | ||
* @param {type} port Port of homebridge instance | ||
* @param {type} body An array of HomeKit characteristic updates, [{ \"aid\": 2, \"iid\": 9, \"value\": 0}] | ||
* @param {type} callback Callback to execute upon completion of characteristic setting, function(err, response) | ||
*/ | ||
HAPNodeJSClient.prototype.HAPresource = function(ipAddress, port, body, callback) { | ||
@@ -239,9 +293,8 @@ request({ | ||
/** | ||
* HAPNodeJSClient.prototype.HAPstatus - description | ||
* HAPNodeJSClient.prototype.HAPstatus - Get current status for characteristics | ||
* | ||
* @param {type} ipAddress description | ||
* @param {type} port description | ||
* @param {type} ipAddress IP Address of homebridge instance | ||
* @param {type} port Port of homebridge instance | ||
* @param {type} body description | ||
* @param {type} callback description | ||
* @return {type} description | ||
* @param {type} callback Callback to execute upon completion of characteristic getting, function(err, response) | ||
*/ | ||
@@ -278,3 +331,3 @@ | ||
try { | ||
rsp = response.body; | ||
rsp = JSON.parse(response.body); | ||
} catch (ex) { | ||
@@ -286,2 +339,3 @@ debug("Homebridge Response Failed %s:%s", ipAddress, port, response.statusCode, response.statusMessage); | ||
} | ||
// debug("HAPStatus callback", rsp); | ||
callback(null, rsp); | ||
@@ -326,8 +380,8 @@ } | ||
} else { | ||
// debug("RESPONSE", response, body); | ||
if (response.body && Object.keys(response.body.accessories).length > 0) { | ||
var message = JSON.parse(response.body); | ||
if (message && Object.keys(message.accessories).length > 0) { | ||
callback(null, { | ||
"ipAddress": ipAddress, | ||
"instance": instance, | ||
"accessories": response.body | ||
"accessories": message | ||
}); | ||
@@ -334,0 +388,0 @@ } else { |
@@ -102,4 +102,5 @@ // Monkey patch before you require http for the first time. | ||
var url = URL.parse(response.url); | ||
if (response.body.characteristics) { | ||
response.body.characteristics.forEach(function(key, element) { | ||
var message = JSON.parse(response.body); | ||
if (message.characteristics) { | ||
message.characteristics.forEach(function(key, element) { | ||
// debug("char", key, element); | ||
@@ -116,5 +117,4 @@ var event = { | ||
} else { | ||
debug("Incomplete EVENT", url, response.body); | ||
debug("Incomplete EVENT", url, message); | ||
} | ||
// request.eventBus.emit('hapEvent', res, response.body); | ||
} | ||
@@ -121,0 +121,0 @@ } |
@@ -133,5 +133,5 @@ // Borrowed and heaviliy modifed from https://github.com/miguelmota/http-message-parser | ||
if (result.headers['Content-Length']) { | ||
result.body = JSON.parse(body); | ||
result.body = body; | ||
} else { | ||
result.body = JSON.parse(body.split('\n')[1]); | ||
result.body = body.split('\n')[1]; | ||
} | ||
@@ -138,0 +138,0 @@ } catch (err) { |
{ | ||
"name": "hap-node-client", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Client for Hap-NodeJS", | ||
@@ -28,3 +28,4 @@ "main": "HAPNodeJSClient.js", | ||
"buffer": "^4.9.1", | ||
"better-queue": ">=3.8.10" | ||
"better-queue": ">=3.8.10", | ||
"requestretry": "^=1.13.0" | ||
}, | ||
@@ -31,0 +32,0 @@ "repository": { |
32508
841
8
+ Addedrequestretry@^=1.13.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedrequestretry@1.13.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedwhen@3.7.8(transitive)