From 617818adc5e8ab94334f8a42f8778507fa3ac1b4 Mon Sep 17 00:00:00 2001 From: Stephan van Rooij Date: Tue, 26 May 2020 12:40:04 +0200 Subject: [PATCH] fix: Group events no longer worked --- examples/manager_events.js | 24 ++++++++++++++++++++++++ src/sonos-device.ts | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 examples/manager_events.js diff --git a/examples/manager_events.js b/examples/manager_events.js new file mode 100644 index 0000000..e52bdb5 --- /dev/null +++ b/examples/manager_events.js @@ -0,0 +1,24 @@ +const SonosManager = require('../lib/').SonosManager + +const manager = new SonosManager(); +manager.InitializeFromDevice(process.env.SONOS_HOST || '192.168.96.56', 1400) + .then(result => { + if(result) { + manager.Devices.forEach(d => { + console.log('Start listening for event from %s', d.Name); + d.Events.on('groupname', name => { + console.log('Device %s has a new group name %s', d.Name, name); + }); + }) + } + }) + +process.on('SIGINT', () => { + console.log('Hold-on cancelling all subscriptions') + manager.Devices.forEach(d => d.CancelEvents()); + manager.CancelSubscription(); + + setTimeout(() => { + process.exit(0) + }, 300) +}) diff --git a/src/sonos-device.ts b/src/sonos-device.ts index 02fc3a8..1630c5a 100644 --- a/src/sonos-device.ts +++ b/src/sonos-device.ts @@ -45,7 +45,7 @@ export default class SonosDevice extends SonosDeviceBase { this.coordinator = groupConfig.coordinator; } if (uuid) { - groupConfig.managerEvents.on(uuid, this.handleGroupUpdate); + groupConfig.managerEvents.on(uuid, this.boundHandleGroupUpdate); } } } @@ -566,7 +566,7 @@ export default class SonosDevice extends SonosDeviceBase { // #endregion // #region Group stuff - private _handleGroupUpdate = this.handleGroupUpdate.bind(this); + private boundHandleGroupUpdate = this.handleGroupUpdate.bind(this); private handleGroupUpdate(data: { coordinator: SonosDevice | undefined; name: string}): void { if (data.coordinator && data.coordinator.uuid !== this.uuid && (!this.coordinator || this.coordinator.uuid !== data.coordinator.uuid)) {