hap-node-client
Advanced tools
Comparing version 0.1.30-beta.0 to 0.2.0-beta.1
@@ -37,3 +37,3 @@ // | ||
// homebridge.HAPresourceByDeviceID("7E:94:75:31:A2:DD", JSON.stringify({ "resource-type": "image", "image-width": 1920, "image-height": 1080 }), function (err, status) { console.log('err', err, 'status', status); }); | ||
homebridge.HAPresourceByDeviceID("7E:94:75:31:A2:DD", JSON.stringify({ "resource-type": "image", "image-width": 1920, "image-height": 1080 }), function (err, status) { console.log('err', err, 'status', status); }); | ||
@@ -40,0 +40,0 @@ }); |
'use strict'; | ||
const axios = require('axios'); | ||
const axios = require('axios').default; | ||
var hapRequest = require('./lib/hapRequest.js'); | ||
var EventEmitter = require('events').EventEmitter; | ||
const axiosRetry = require('axios-retry'); | ||
var inherits = require('util').inherits; | ||
@@ -12,2 +13,4 @@ var debug = require('debug')('hapNodeJSClient'); | ||
axiosRetry(axios, { retries: 3 }); | ||
var discovered = []; | ||
@@ -62,2 +65,3 @@ var mdnsCache = {}; | ||
} | ||
this.eventRegistry = {}; | ||
@@ -169,9 +173,11 @@ _discovery.call(this); | ||
url = 'http://[' + ipAddress + ']:' + result.port; | ||
break; | ||
} else { | ||
debug('Invalid address found', result.name, result.addresses); | ||
break; | ||
} | ||
} | ||
// debug('result', result); | ||
if (url) { | ||
mdnsCache[result.txt.id] = { | ||
name: result.name, | ||
host: ipAddress, | ||
@@ -181,7 +187,4 @@ port: result.port, | ||
deviceID: result.txt.id, | ||
txt: result.txt, | ||
name: result.name | ||
txt: result.txt | ||
}; | ||
// debug('HAP Device address %s -> ', result.name, mdnsCache[result.txt.id]); | ||
// debug('discovery', discovery); | ||
if (discovery) { | ||
@@ -296,4 +299,2 @@ discovery.call(this, mdnsCache[result.txt.id], function () { }); | ||
HAPNodeJSClient.prototype.HAPcontrol = function (ipAddress, port, body, callback, instance) { | ||
var pin = _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port); | ||
axios({ | ||
@@ -304,6 +305,5 @@ eventBus: this._eventBus, | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port), | ||
'connection': 'keep-alive' | ||
@@ -443,5 +443,3 @@ }, | ||
} else { | ||
var pin = _findPinByKey(deviceID); | ||
// debug("HAPeventByDeviceID", instance); | ||
debug('HAPeventByDeviceID:', instance.url + '/characteristics', body); | ||
hapRequest({ | ||
@@ -453,6 +451,5 @@ eventBus: this._eventBus, | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(deviceID), | ||
'connection': 'keep-alive' | ||
@@ -517,4 +514,2 @@ }, | ||
HAPNodeJSClient.prototype.HAPevent = function (ipAddress, port, body, callback, instance) { | ||
var pin = _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port); | ||
// debug('HAPevent:', 'http://' + ipAddress + ':' + port + '/characteristics'); | ||
hapRequest({ | ||
@@ -525,6 +520,5 @@ eventBus: this._eventBus, | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port), | ||
'connection': 'keep-alive' | ||
@@ -541,4 +535,4 @@ }, | ||
if (response.statusCode === 401 || response.statusCode === 470) { | ||
debug('Homebridge auth failed, invalid PIN %s %s:%s', pin, ipAddress, port, body, err, response.body); | ||
callback(new Error('Homebridge auth failed, invalid PIN ' + pin)); | ||
debug('Homebridge auth failed, invalid PIN %s %s:%s', _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port), ipAddress, port, body, err, response.body); | ||
callback(new Error('Homebridge auth failed, invalid PIN ' + _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port))); | ||
} else { | ||
@@ -611,3 +605,2 @@ debug('Homebridge event reg failed %s:%s Status: %s ', ipAddress, port, response.statusCode, body, err, response.body); | ||
HAPNodeJSClient.prototype.HAPresource = function (ipAddress, port, body, callback, instance) { | ||
var pin = _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port); | ||
@@ -619,7 +612,6 @@ axios({ | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
encoding: null, | ||
responseType: 'arraybuffer', | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port), | ||
'connection': 'keep-alive' | ||
@@ -633,3 +625,3 @@ }, | ||
}).then(function (response) { | ||
debug('HAPcontrol-then', response.status, response.statusText, response.headers, response.config); | ||
// debug('HAPcontrol-then', response.status, response.statusText, response.headers, response.config); | ||
switch (response.status) { | ||
@@ -691,3 +683,2 @@ case 200: | ||
HAPNodeJSClient.prototype.HAPstatus = function (ipAddress, port, body, callback, instance) { | ||
var pin = _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port); | ||
@@ -700,6 +691,5 @@ // debug('HAPstatus', pin); | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(instance ? instance.deviceID : ipAddress + ':' + port), | ||
'connection': 'keep-alive' | ||
@@ -740,3 +730,2 @@ }, | ||
var host = instance.host + ':' + instance.port; | ||
var pin = _findPinByKey(host); | ||
@@ -748,11 +737,10 @@ axios({ | ||
timeout: this.reqTimeout, | ||
maxAttempts: 5, // (default) try 5 times | ||
retryDelay: 5000, // (default) wait for 5s before trying again | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
'authorization': pin, | ||
'authorization': _findPinByKey(instance.deviceID ? instance.deviceID : instance.host + ':' + instance.port), | ||
'connection': 'keep-alive' | ||
}, | ||
validateStatus: function (status) { | ||
return status < 500; // Resolve only if the status code is less than 500 | ||
return true; // Resolve only if the status code is less than 500 | ||
} | ||
@@ -773,3 +761,3 @@ }).then(function (response) { | ||
} | ||
if (message && Object.keys(message.accessories).length > 0) { | ||
if (message && Object.keys(message.accessories)) { // && await _checkInstanceConnection(instance) | ||
debug('Homebridge instance discovered %s @ %s with %s accessories', instance.name, instance.url, Object.keys(message.accessories).length); | ||
@@ -809,1 +797,23 @@ discovered.push({ | ||
} | ||
/** | ||
* This checks the instance pin matches | ||
*/ | ||
async function _checkInstanceConnection(instance) { | ||
try { | ||
await axios.put(`http://${instance.host}:${instance.port}/characteristics`, | ||
{ | ||
characteristics: [{ aid: -1, iid: -1 }], | ||
}, | ||
{ | ||
headers: { | ||
Authorization: _findPinByKey(instance ? instance.deviceID : instance.host + ':' + instance.port), | ||
}, | ||
} | ||
); | ||
return true; | ||
} catch (e) { | ||
throw 'Incorrect PIN \'' + _findPinByKey(instance ? instance.deviceID : instance.host + ':' + instance.port) + '\''; | ||
return false; | ||
} | ||
} |
var uuid = require('./uuid.js'); | ||
var descriptions = require('./hap-types.js').Characteristics; | ||
function normalizeUUID(input) { | ||
function normalizeUUID(message) { | ||
// Thanks Oznu | ||
input.accessories.forEach((accessory) => { | ||
/** Ensure UUIDs are long form */ | ||
for (const service of accessory.services) { | ||
service.type = uuid.toLongFormUUID(service.type); | ||
for (const characteristic of service.characteristics) { | ||
characteristic.type = uuid.toLongFormUUID(characteristic.type); | ||
if (descriptions[characteristic.type]) { | ||
characteristic.description = descriptions[characteristic.type]; | ||
try { | ||
let input = JSON.parse(JSON.stringify(message).replace(/\uFFFD/g, '')); | ||
input.accessories.forEach((accessory) => { | ||
/** Ensure UUIDs are long form */ | ||
for (const service of accessory.services) { | ||
service.type = uuid.toLongFormUUID(service.type); | ||
for (const characteristic of service.characteristics) { | ||
characteristic.type = uuid.toLongFormUUID(characteristic.type); | ||
if (descriptions[characteristic.type]) { | ||
characteristic.description = descriptions[characteristic.type]; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
return input; | ||
}); | ||
return input; | ||
} catch (err) { | ||
console.log('normalizeUUID', err.message); | ||
return message; | ||
} | ||
} | ||
exports.normalizeUUID = normalizeUUID; |
{ | ||
"name": "hap-node-client", | ||
"version": "0.1.30-beta.0", | ||
"version": "0.2.0-beta.1", | ||
"description": "Client for Hap-NodeJS", | ||
@@ -11,3 +11,3 @@ "main": "HAPNodeJSClient.js", | ||
"build": "cd scripts; node gen-hap-types.js", | ||
"watch": "nodemon" | ||
"test": "nodemon" | ||
}, | ||
@@ -21,2 +21,3 @@ "devDependencies": { | ||
"axios": "^1.2.1", | ||
"axios-retry": "^3.3.1", | ||
"better-queue": ">=3.8.12", | ||
@@ -23,0 +24,0 @@ "bonjour-hap": "^3.6.4", |
@@ -6,5 +6,5 @@ # Hap-Node-Client | ||
<!--ts--> | ||
* [Hap-Node-Client](#hap-node-client) | ||
* [TODO List](#todo-list) | ||
* [API documentation](#api-documentation) | ||
- [Hap-Node-Client](#hap-node-client) | ||
- [TODO List](#todo-list) | ||
- [API documentation](#api-documentation) | ||
@@ -17,3 +17,3 @@ <!-- Added by: sgracey, at: --> | ||
* [ ] - Add routine to reregister events once a socket is reconnected. | ||
* [x] - Add routine to reregister events once a socket is reconnected. | ||
@@ -20,0 +20,0 @@ # API documentation |
73383
1726
9
+ Addedaxios-retry@^3.3.1
+ Added@babel/runtime@7.26.9(transitive)
+ Addedaxios-retry@3.9.1(transitive)
+ Addedis-retry-allowed@2.2.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)