As part of my on going playing with some Belkins WEMO devices I started to have a look at the UPNP Event support.
The UPNP standard as well as including discover and endpoints for control has an event system, this allows devices to publish their status changes to interested parties. The event system uses some extensions to HTTP.
SUBSCRIBE
To subscribe to events from a given device you send a request similar to the following to the eventSubURL given as defined in the setup.xml which is linked to in the UPNP discovery response.
SUBSCRIBE /upnp/event/bridge1 HTTP/1.1 CALLBACK: <http://192.168.1.1:3000/> NT: upnp:event TIMEOUT: Second-600 Host: 192.168.1.2:49154
CALLBACK -> is the URL to be notified
TIMEOUT -> how long to send notifications
Gets the following response:
HTTP/1.1 200 OK DATE: Sun, 11 Jan 2015 18:27:05 GMT SERVER: Unspecified, UPnP/1.0, Unspecified CONTENT-LENGTH: 0 X-User-Agent: redsonic SID: uuid:7206f5ac-1dd2-11b2-80f3-e76de858414e TIMEOUT: Second-600
SID -> the Subscription ID
The SID is used to identify which notification come from this subscription, it can also be used to renew the subscription before the timeout expires by sending a SUBSCRIBE message like this:
SUBSCRIBE /upnp/event/bridge1 HTTP/1.1 SID: uuid:7206f5ac-1dd2-11b2-80f3-e76de858414e TIMEOUT: Second-600 Host: 192.168.1.2:49154
NOTIFY
Incoming event notifications get delivered every time the state of the device changes and look like this for the WeMo Socket:
NOTIFY / HTTP/1.1 HOST: 192.168.1.1:3000 CONTENT-TYPE: text/xml; charset="utf-8" CONTENT-LENGTH: 212 NT: upnp:event NTS: upnp:propchange SID: uuid:7206f5ac-1dd2-11b2-80f3-e76de858414e SEQ: 0 <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0"> <e:property> <BinaryState>0</BinaryState> </e:property> </e:propertyset>
The events from the light bulbs is a bit more complex, how parse them is demonstrated in the code.
UNSUBSCRIBE
And when your done you can unsubscribe with the following:
UNSUBSCRIBE / HTTP/1.1 Host: 192.168.1.2:49154 SID: uuid:7206f5ac-1dd2-11b2-80f3-e76de858414e
WeMo
Having worked all these bits out I put the following node js app together to test it all out with the WeMo devices I have:
Next step is to put all this together to build a new WeMo node module and then a improved Node-RED node.