Skip to content

Instantly share code, notes, and snippets.

@achingbrain
Last active August 29, 2015 14:25
Show Gist options
  • Save achingbrain/8108055a9cfd8512dd90 to your computer and use it in GitHub Desktop.
Save achingbrain/8108055a9cfd8512dd90 to your computer and use it in GitHub Desktop.
node-hue-api and LOCATION headers
var hue = require("node-hue-api"),
timeout = 2000; // 2 seconds
var displayBridges = function(bridge) {
console.log("Hue Bridges Found: " + JSON.stringify(bridge));
};
hue.upnpSearch(timeout).then(displayBridges).done();
var http = require('http')
var dgram = require('dgram')
// start two servers - one on port 80, one on port 8080
http.createServer(function (request, response) {
console.info('Got request for %s on port 80', request.url)
response.writeHead(200, {'Content-Type': 'text/xml'});
response.end(description('WRONG PORT!'))
}).listen(80, '127.0.0.1')
http.createServer(function (request, response) {
console.info('Got request for %s on port 8080', request.url)
response.writeHead(200, {'Content-Type': 'text/xml'});
response.end(description('Right port!'))
}).listen(8080, '127.0.0.1')
// listen for SSDP messages
var socket = dgram.createSocket('udp4')
socket.bind(1900)
socket.on('listening', function () {
socket.addMembership('239.255.255.250')
console.info('Socket listening')
})
// respond to ssdp search requests
socket.on('message', function (message, remote) {
message = message.toString('utf8')
if (message.indexOf('ssdp:discover') !== -1 &&
message.indexOf('urn:schemas-upnp-org:device:Basic:1') !== -1) {
console.info('Responding to bridge search request')
var response = 'HTTP/1.1 200 OK\r\n' +
'ST: urn:schemas-upnp-org:device:Basic:1\r\n' +
'USN: something-unique\r\n' +
'LOCATION: http://localhost:8080/foo.xml' // <--- client should make request for /foo.xml to port 8080
socket.send(new Buffer(response), 0, response.length, remote.port, remote.address, function () {
console.info('Responded to bridge search request')
})
}
})
socket.on('error', console.error)
function description(serial) {
return '<root xmlns="urn:schemas-upnp-org:device-1-0">' +
'<specVersion>' +
'<major>1</major>' +
'<minor>0</minor>' +
'</specVersion>' +
'<URLBase>http://localhost:8081</URLBase>' +
'<device>' +
'<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>' +
'<friendlyName>I am a light controller</friendlyName>' +
'<manufacturer>Royal Philips Electronics</manufacturer>' +
'<manufacturerURL>http://www.philips.com</manufacturerURL>' +
'<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>' +
'<modelName>Philips hue bridge 2012</modelName>' +
'<modelNumber>23409823049823</modelNumber>' +
'<modelURL>http://www.meethue.com</modelURL>' +
'<serialNumber>' + serial + '</serialNumber>' +
'<UDN>uuid:2f402f80-da50-12321-9b23-2131298129</UDN>' +
'<presentationURL>index.html</presentationURL>' +
'</device>' +
'</root>'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment