问题
We are controlling the sonos via a small IOT device. This device will be placed at the homes of our customers. In our home automation system we need to know the playbackstate + volume of the players/groups. At which polling rate can we ask these parameters ?
We cannot use a cloud server to handle the subscription events. I tried to put a https webservice in the IOT device with a self signed certificate but this doesn't work. After I have posted the subscription I get a request in my webservice but the cloud sonos server disconnects immediately. Probably because of the certificate is self signed - Or can there be another reason ?
Is there another way to recieve status events ? During discovery In the players Json object there is a key 'websocketUrl'. The documentation says "The secure WebSocket URL for the device. See Connect for details". But I cannot find more info about this. Can this be used ?
回答1:
At which polling rate can we ask these parameters?
Consider using subscriptions instead of polling. See this answer for more: https://stackoverflow.com/a/60893119/4902948
We cannot use a cloud server to handle the subscription events... Or can there be another reason?
Is there another way to [receive] status events?
You must host a service that satisfies a set of requirements to receive events. See this answer for more: https://stackoverflow.com/a/57189717/4902948
Also see Details on API credentials and events in New features in versions 11.1, 11.2 (S1), & 12.0 (S2).
Can [the 'websocketUrl'] be used?
This is not publicly available for use.
回答2:
It would be a lot easier to use the (undocumented but more useful) UPnP APIs. You could simply Subscribe to the UPnP RenderingControl endpoint, then you will get an event every time the volume changes. To find when the track changes, subscribe to AVTransport endpoint.
You can do this with a local server (this is how every Sonos app works after all) and no special cert requirements either.
UPnP events are described in the UPnP spec.
回答3:
Your question doesn't provide details about the language you're using, but if the device is powerfull enough to run node, you have a good change with my sonos library.
You can just pull the required information from the device as often as you like. Or use the build-in event subscriptions. If you use the events, it will automatically setup an http listener and automatically subscribe to all the events you're listening for.
Events are instant (as in 50ms-100ms after the event took place on the device).
If you request information it is send directly to the device and the device will respond with the answer. Pulling multiple data points per second has never been a problem to me.
Warning as stated above, this uses the undocumented local UPNP api, but since their own app also uses it, I'm guessing that won't change soon.
来源:https://stackoverflow.com/questions/62699587/sonos-control-api-polling-rate-subscription