From aa6792b6cf00674ab49b916e5da7a6f6f9f905a7 Mon Sep 17 00:00:00 2001 From: Mark Langovoi Date: Sun, 13 Jan 2019 20:22:42 +0300 Subject: [PATCH] Fix crash on reconnect of Device --- Device.js | 18 +++++++++++++++--- MediaRenderer1.js | 40 +++++++++++++++++++++------------------- UPnPPlatform.js | 13 ++----------- package-lock.json | 10 +--------- package.json | 3 +-- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Device.js b/Device.js index 799bc84..0a543e4 100644 --- a/Device.js +++ b/Device.js @@ -5,6 +5,7 @@ class Device { this._platform = platform; this._USN = USN; this._accessory = accessory; + this._accessoryConfigured = false; this._client = null; } @@ -27,7 +28,9 @@ class Device { this._client = null; } - this._client = new Client(location); + this._client = new Client(location, { + customLogger: this._platform.log.debug.bind(this._platform.log) + }); this._client.on('error', (err) => { this._platform.log.error(err); }); @@ -40,11 +43,16 @@ class Device { let accessoryCreated = this._accessory === null; - const accessory = this._createAccessory(description); + if(!this._accessoryConfigured) { + this._createAccessory(description); + this._accessoryConfigured = true; + } else { + this._updateAccessory(description); + } if (accessoryCreated) { this._platform.addDevice(this); - this._platform.addAccessory(accessory); + this._platform.addAccessory(this.accessory); } }); } @@ -65,6 +73,10 @@ class Device { throw new Error('_createAccessory must be implemented'); } + _updateAccessory() { + throw new Error('_updateAccessory must be implemented'); + } + stop() { throw new Error('stop must be implemented'); } diff --git a/MediaRenderer1.js b/MediaRenderer1.js index 4c1fe51..e1a1024 100644 --- a/MediaRenderer1.js +++ b/MediaRenderer1.js @@ -20,23 +20,7 @@ class MediaRenderer1 extends Device { this._accessory = accessory; } - const informationService = this.accessory.getService(homebridge.hap.Service.AccessoryInformation); - - if (description.friendlyName) { - informationService.getCharacteristic(homebridge.hap.Characteristic.Manufacturer).updateValue(description.friendlyName); - } - - if (description.manufacturer) { - informationService.getCharacteristic(homebridge.hap.Characteristic.Manufacturer).updateValue(description.manufacturer); - } - - if (description.modelName) { - informationService.getCharacteristic(homebridge.hap.Characteristic.Model).updateValue(description.modelName); - } - - if (description.serialNumber) { - informationService.getCharacteristic(homebridge.hap.Characteristic.SerialNumber).updateValue(description.serialNumber); - } + this._updateAccessory(description); let switchService = this.accessory.getService(homebridge.hap.Service.Lightbulb); @@ -61,10 +45,28 @@ class MediaRenderer1 extends Device { switchService.getCharacteristic(homebridge.hap.Characteristic.Brightness) .on('get', this._getVolume.bind(this)) .on('set', this._setVolume.bind(this)); + } - this._client.subscribe('RenderingControl', this._handleEvent); + _updateAccessory(description) { + const informationService = this.accessory.getService(homebridge.hap.Service.AccessoryInformation); + + if (description.friendlyName) { + informationService.getCharacteristic(homebridge.hap.Characteristic.Manufacturer).updateValue(description.friendlyName); + } + + if (description.manufacturer) { + informationService.getCharacteristic(homebridge.hap.Characteristic.Manufacturer).updateValue(description.manufacturer); + } - return accessory + if (description.modelName) { + informationService.getCharacteristic(homebridge.hap.Characteristic.Model).updateValue(description.modelName); + } + + if (description.serialNumber) { + informationService.getCharacteristic(homebridge.hap.Characteristic.SerialNumber).updateValue(description.serialNumber); + } + + this._client.subscribe('RenderingControl', this._handleEvent); } onAlive() { diff --git a/UPnPPlatform.js b/UPnPPlatform.js index 5d2a193..5e9b65f 100644 --- a/UPnPPlatform.js +++ b/UPnPPlatform.js @@ -1,26 +1,17 @@ const Client = require('node-ssdp').Client; const Server = require('node-ssdp').Server; -const debug = require('debug'); - -const clientDebug = debug('node-ssdp-client'); -const serverDebug = debug('node-ssdp-server'); -debug.enable('node-ssdp-client'); -debug.enable('node-ssdp-server'); class UPnPPlatform { constructor(log, config, api) { this.log = log; - clientDebug.log = log.debug.bind(log); - serverDebug.log = log.debug.bind(log); - this._client = new Client({ ...config.ssdpClient, - customLogger: clientDebug + customLogger: this.log.debug.bind(this.log) }); this._server = new Server({ ...config.ssdpServer, - customLogger: serverDebug + customLogger: this.log.debug.bind(this.log) }); this._devices = []; this._STToDevice = { diff --git a/package-lock.json b/package-lock.json index cbfb39e..38edd8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-upnp", - "version": "0.1.1", + "version": "0.1.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -64,14 +64,6 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, - "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "requires": { - "ms": "^2.1.1" - } - }, "elementtree": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/elementtree/-/elementtree-0.1.7.tgz", diff --git a/package.json b/package.json index 204b8c5..3d80b69 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-upnp", - "version": "0.1.5", + "version": "0.1.6", "main": "index.js", "author": "Mark Langovoi ", "license": "MIT", @@ -19,7 +19,6 @@ }, "dependencies": { "@langovoi/upnp-device-client": "^1.0.6", - "debug": "^4.1.1", "node-ssdp": "^4.0.0" }, "scripts": {