Skip to content

Commit

Permalink
Pass back info about the requester in on/off/state events
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
dhleong committed Jan 24, 2018
1 parent 6bb3912 commit 3616b71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,34 @@ stereo.on('listening', function() {
console.log("Stereo listening on", this.port);
});

tv.on('state', function(binaryState) {
tv.on('state', function(binaryState, self, sender) {
console.log("TV set to=", binaryState);
tv.close(); // stop advertising the device
});

// also, 'on' and 'off' events corresponding to binary state
stereo.on('on', function() {
stereo.on('on', function(self, sender) {
console.log("Stereo turned on");
});

stereo.on('off', function() {
stereo.on('off', function(self, sender) {
console.log("Stereo turned off");
});
```

If you need information about who requested the event, it is provided as a
"Sender object" that looks something like this:

```javascript
{
address: '::ffff:192.168.1.23',
port: 12345
}
```

See [Socket.remoteAddress](https://nodejs.org/api/net.html#net_socket_remoteaddress)
for more information about these values.

### Binary

Installing with `-g` provides the `wemore-toggle` executable:
Expand Down
18 changes: 11 additions & 7 deletions lib/emulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,21 @@ EmulatedDevice.prototype._endpoints = {
res.end();

} else if (body.SetBinaryState) {

var senderInfo = {
address: req.socket.remoteAddress
, port: req.socket.remotePort
};

self.binaryState =
parseInt(body.SetBinaryState.BinaryState);
if (self.binaryState) {
self.emit('on', self);
self.emit('on', self, senderInfo);
} else {
self.emit('off', self);
self.emit('off', self, senderInfo);
}

self.emit('state', self.binaryState, self);
self.emit('state', self.binaryState, self, senderInfo);
res.writeHead(200);
res.write(XML({
's:Envelope': {
Expand Down Expand Up @@ -311,15 +317,13 @@ module.exports = function Emulate(opts) {

if (!SERVER.__started) {
SERVER.__started = true;
var serverStarter;
serverStarter = function() {
device.removeListener('listening', serverStarter);
var serverStarter = function() {
SERVER.start();
process.on('exit', function() {
SERVER.stop();
});
};
device.on('listening', serverStarter);
device.once('listening', serverStarter);
}
return device;
};

0 comments on commit 3616b71

Please sign in to comment.