Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Fix crash on reconnect of Device
Browse files Browse the repository at this point in the history
  • Loading branch information
langovoi committed Jan 13, 2019
1 parent e661fc3 commit aa6792b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
18 changes: 15 additions & 3 deletions Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Device {
this._platform = platform;
this._USN = USN;
this._accessory = accessory;
this._accessoryConfigured = false;

this._client = null;
}
Expand All @@ -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);
});
Expand All @@ -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);
}
});
}
Expand All @@ -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');
}
Expand Down
40 changes: 21 additions & 19 deletions MediaRenderer1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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() {
Expand Down
13 changes: 2 additions & 11 deletions UPnPPlatform.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
10 changes: 1 addition & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-upnp",
"version": "0.1.5",
"version": "0.1.6",
"main": "index.js",
"author": "Mark Langovoi <[email protected]>",
"license": "MIT",
Expand All @@ -19,7 +19,6 @@
},
"dependencies": {
"@langovoi/upnp-device-client": "^1.0.6",
"debug": "^4.1.1",
"node-ssdp": "^4.0.0"
},
"scripts": {
Expand Down

0 comments on commit aa6792b

Please sign in to comment.