bencevans / node-sonos

🔈 Sonos Media Player Interface/Client

Home Page:https://www.npmjs.com/package/sonos

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get an event when a sonos device is not available anymore

tboudri opened this issue · comments

The packages has a discovery functie that notifies me when a Sonos device becomes available. But there is no event available that notifies me when a device is not available anymore

The Discovery Search could be extended with a mechanism that, when a device does not respond within a few searches it is assumed to be turned off

You can do

import { Listener } from 'sonos';

Listener.on('ZonesChanged', (zones) => {})

Also see:
https://github.com/bencevans/node-sonos/blob/master/examples/zoneTopologySubscription.js

Thank you for your super quick response.
ZonesChanged is not useful for this. This event is not fired when a device is not available anymore.
This event gets fired with useful information on app startup. When devices are added, the event gets fired with the zones param = undefined. But no event when a device is not available anymore

Weird. Just tested this, and I can confirm ... no event comes through immediately when a zone player gets removed.
The weird thing is ... it also doesn't come through on the Android native app. When that updates, I can actually see a ZonesChanged event come through. It takes a considerable while though.

To check this, we would need to create something that would actively be sending a message to each speaker to see if it's still online.

I have a quick&dirty solution. The DeviceDiscovery polls constantly for new Devices. I have modified this so that at each polling interval it checks if one or more 'known' devices are not reacting anymore. If a device does not react within 5 polling iterations it is considered 'dead'. Now my app can react on this but node-sonos still thinks the device is available. Thats why i sometimes get a timeout error when calling getAllGroups on a device that is still available but one or more other devices are not discoverable anymore

Why not just do polling every 1 minute for the volume or something basic. Like status page, preferably something small

I'm closing this issue for now, but if someone feels like creating a PR for this feature you can always re-open.

Instead of polling for volume etc you can just send an html request
http://192.168.178.37:1400/info
with a time out of milliseconds (adjusted to your network).

Although this post was already closed i decided to post my solution. As i mentioned, there is a DeviceDiscovery class that polls via uPnp for devices. I have modified this class so that when a device comes online it is registered in a list inside the DeviceDiscovery. Now every times DeviceDiscovery polls for devices it will also check if devices in that list still respond. If a device does not respond for some time an event is fired. This is very efficient because polling is already done. There is no need for extra polling. And it can react very quickly when devices are not online anymore

Thanks for that information. Very smart solution.

I will check whether that fits in my program. Right now I only do device discovery once after a system shutdown. During run time I just throw the actions to the (static) IP address and act on the error if unreachable. That's good for system load but unreachable speakers can cause problems.

The device Discovery isn't meant to be used all the time. Since it uses since it uses udp broadcast it may cause noise on your network.

I think starting an interval and polling to a /info endpoint is the way to go.