Created
May 11, 2014 19:31
-
-
Save stephen/43a657bedac488cc44da to your computer and use it in GitHub Desktop.
node-sonos eventing framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sonos = require('../index').Sonos; | |
var Listener = require('../lib/events/listener'); | |
var x = new Listener(new Sonos(process.env.SONOS_HOST || '192.168.2.11')); | |
x.listen('/ZoneGroupTopology/Event', function(err, sid) { | |
console.log(sid); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var http = require('http'); | |
var portastic = require('portastic'); | |
var Listener = function(device) { | |
this.device = device; | |
}; | |
Listener.prototype._startInternalServer = function(callback) { | |
portastic.find({ | |
min: 3400, | |
max: 3500, | |
retrieve: 1 | |
}, function(err, port) { | |
if (err) { | |
callback(err); | |
} else { | |
this.server = http.createServer(function(req, res) { | |
var buffer = ''; | |
console.log('omg someone said hi'); | |
console.log(req.method); | |
console.log(req.url); | |
console.log(req.headers); | |
req.on('data', function(d) { | |
buffer += d; | |
}); | |
req.on('end', function() { | |
console.log(buffer); | |
}); | |
res.end('helo i am server'); | |
}).listen(port, function() { | |
callback(port); | |
}); | |
} | |
}); | |
}; | |
Listener.prototype.listen = function(serviceEndpoint, callback) { | |
var opt = { | |
url: 'http://' + this.device.host + ':' + this.device.port + serviceEndpoint, | |
method: 'SUBSCRIBE', | |
headers: { | |
callback: '<http://172.17.104.77:3401/notify>', | |
NT: 'upnp:event', | |
Timeout: 'Second-3600' | |
} | |
}; | |
var cb = function(err, response) { | |
if (err) { | |
callback(err); | |
} else { | |
callback(null, response.headers.sid ) | |
} | |
}; | |
if (!this.server) { | |
this._startInternalServer(function() { | |
request(opt, cb); | |
}); | |
} else { | |
request(opt, cb); | |
} | |
}; | |
module.exports = Listener; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment