Skip to content

Commit 03cdd3c

Browse files
committed
Converting irsend to return promises
* Using bluebird for promises library (need Promisfy method) * Breaking change, revving version to 1.0.0 * Tell Travis to test on node 4 and 5 * Updated readme/changelog/package.json * Init flow is much cleaner
1 parent 5456d79 commit 03cdd3c

8 files changed

Lines changed: 191 additions & 117 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
3-
- "0.10"
4-
- "0.8"
3+
- "5.5"
4+
- "4.2"

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66

77
## Unreleased
88

9+
## [1.0.0] - 2016-02-05
10+
11+
* Switching irsend from callbacks to promises. Breaking change.
912
* Removing `Makefile` and just using `package.json` for running tests.
1013

1114
## [0.0.4] - 2015-12-30

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ answer any ambiguities. There are additional options that are not shown here.
6262

6363
// Sending commands
6464
lirc_node = require('lirc_node');
65-
lirc_node.init();
6665

67-
// To see all of the remotes and commands that LIRC knows about:
68-
console.log(lirc_node.remotes);
66+
// After lirc_node has initialized, see all of the remotes and commands that LIRC knows about:
67+
lirc_node.init().then(function(remotes) {
68+
console.log(remotes);
69+
70+
// or
71+
// console.log(lirc_node.remotes)
72+
})
6973

7074
/*
71-
Let's pretend that the output of lirc_node.remotes looks like this:
75+
Output of lirc_node.remotes will look like this:
7276

7377
{
7478
"tv": ["Power", "VolumeUp", "VolumeDown"],
@@ -77,31 +81,37 @@ answer any ambiguities. There are additional options that are not shown here.
7781
*/
7882

7983
// Tell the TV to turn on
80-
lirc_node.irsend.send_once("tv", "power", function() {
84+
lirc_node.irsend.send_once("tv", "Power").then(function() {
8185
console.log("Sent TV power command!");
8286
});
8387

8488
// Tell the Xbox360 to turn on
85-
lirc_node.irsend.send_once("xbox360", "power", function() {
89+
lirc_node.irsend.send_once("xbox360", "Power").then(function() {
8690
console.log("Sent Xbox360 power command!");
8791
});
8892

93+
// Turn the volume up for 2 seconds
94+
lirc_node.irsend.send_start("tv", "VolumeUp");
95+
setTimeout(function() {
96+
lirc_node.irsend.send_stop("tv", "VolumeUp");
97+
}, 2000);
8998

90-
// Listening for commands
99+
// Listen for all IR commands
91100
var listenerId = lirc_node.addListener(function(data) {
92101
console.log("Received IR keypress '" + data.key + "'' from remote '" + data.remote +"'");
93102
});
94103

104+
// Listen for a specific IR command
95105
lirc_node.addListener('KEY_UP', 'remote1', function(data) {
106+
// `data` also has `code` and `repeat` properties from output of `irw`
107+
96108
console.log("Received IR keypress 'KEY_UP' from remote 'remote1'");
97-
// data also has `code` and `repeat` properties from the output of `irw`
98-
// The final argument after this callback is a throttle allowing you to
109+
110+
// The final argument after this callback is a throttle allowing you to
99111
// specify to only execute this callback once every x milliseconds.
100112
}, 400);
101113

102114

103-
104-
105115
## Development
106116

107117
Would you like to contribute to and improve this module? Fantastic. To contribute

lib/irsend.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var exec = require('child_process').exec;
1+
var Promise = require('bluebird');
2+
var exec = Promise.promisify(require('child_process').exec, { multiArgs: true });
23

3-
exports = module.exports = IRSend;
4-
5-
function IRSend () {
4+
// TODO: execOverride is used for testing. I don't like it.
5+
function IRSend (execOverride) {
66
this.command = 'irsend';
7+
this.exec = execOverride || exec;
78
};
89

910
// In some cases the default lirc socket does not work
@@ -16,34 +17,34 @@ IRSend.prototype.setSocket = function(socket) {
1617
}
1718
};
1819

19-
IRSend.prototype.list = function(remote, code, callback) {
20+
IRSend.prototype.list = function(remote, code) {
2021
var command = this._list(remote, code);
21-
return exec(command, callback);
22+
return this.exec(command);
2223
};
2324

24-
IRSend.prototype.send_once = function(remote, code, callback) {
25+
IRSend.prototype.send_once = function(remote, code) {
2526
var command = this._send_once(remote, code);
26-
return exec(command, callback);
27+
return this.exec(command);
2728
};
2829

29-
IRSend.prototype.send_start = function(remote, code, callback) {
30+
IRSend.prototype.send_start = function(remote, code) {
3031
var command = this._send_start(remote, code);
31-
return exec(command, callback);
32+
return this.exec(command);
3233
};
3334

34-
IRSend.prototype.send_stop = function(remote, code, callback) {
35+
IRSend.prototype.send_stop = function(remote, code) {
3536
var command = this._send_stop(remote, code);
36-
return exec(command, callback);
37+
return this.exec(command);
3738
};
3839

39-
IRSend.prototype.set_transmitters = function(transmitters, callback) {
40+
IRSend.prototype.set_transmitters = function(transmitters) {
4041
var command = this._set_transmitters(transmitters);
41-
return exec(command, callback);
42+
return this.exec(command);
4243
};
4344

44-
IRSend.prototype.simulate = function(code, callback) {
45+
IRSend.prototype.simulate = function(code) {
4546
var command = this._simulate(code);
46-
return exec(command, callback);
47+
return this.exec(command);
4748
};
4849

4950
// Internal methods
@@ -103,3 +104,5 @@ IRSend.prototype._set_transmitters = function(transmitters) {
103104
IRSend.prototype._simulate = function(code) {
104105
return this.command + ' SIMULATE "' + code + '"';
105106
};
107+
108+
exports = module.exports = IRSend;

lib/lirc_node.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
exports.version = '0.0.2';
1+
var Promise = require('bluebird');
2+
3+
// Setup irsend
24
exports.IRSend = require('./irsend');
35
exports.irsend = new exports.IRSend();
46
exports.remotes = {};
57

8+
// Setup irreceive
69
exports.IRReceive = require('./irreceive');
710
var irreceive = new exports.IRReceive();
11+
12+
// Setup event listeners for irreceive
813
exports.addListener = irreceive.addListener.bind(irreceive);
914
exports.on = exports.addListener;
1015
exports.removeListener = irreceive.removeListener.bind(irreceive);
@@ -15,45 +20,64 @@ exports.setSocket = function(socket) {
1520
exports.irsend.setSocket(socket);
1621
}
1722

18-
exports.init = function(callback) {
19-
exports.irsend.list('', '', irsendCallback);
23+
exports.init = function() {
24+
return exports.irsend.list('', '')
25+
.then(exports._populateRemotes)
26+
.then(exports._populateCommands)
27+
.then(function(remotes) {
28+
return exports.remotes = remotes;
29+
});
30+
};
2031

21-
function irsendCallback(error, stdout, stderr) {
22-
exports._populateRemotes(error, stdout, stderr);
23-
exports._populateCommands();
24-
if (callback) callback();
25-
}
32+
// Parse the list of remotes that irsend knows about
33+
exports._populateRemotes = function (irsendResult) {
34+
var remotes = {};
35+
36+
irsendResult[1].split('\n').forEach(function (remote) {
37+
var remoteName = remote.match(/\s(.*)$/);
38+
if (remoteName) remotes[remoteName[1]] = [];
39+
});
2640

27-
return true;
41+
return remotes;
2842
};
2943

30-
// Private
31-
exports._populateRemotes = function(error, stdout, stderr) {
32-
var remotes = stderr.split('\n');
44+
// Given object whose keys represent remotes, get commands for each remote
45+
// Returns promise that will resolve when all irsend invocations complete
46+
exports._populateCommands = function (remotesObject) {
47+
var commandPromises = [];
48+
var remoteNames = Object.keys(remotesObject);
3349

34-
exports.remotes = {};
50+
remoteNames.forEach(function (remote) {
51+
commandPromises.push(exports.irsend.list(remote, '')
52+
.then(function (irsendResult) {
53+
return { [remote]: exports._parseCommands(irsendResult) }
54+
})
55+
);
56+
})
3557

36-
remotes.forEach(function(element, index, array) {
37-
var remoteName = element.match(/\s(.*)$/);
38-
if (remoteName) exports.remotes[remoteName[1]] = [];
39-
});
58+
return Promise.all(commandPromises)
59+
.then(exports._joinRemoteCommands);
4060
};
4161

42-
exports._populateCommands = function() {
43-
for (var remote in exports.remotes) {
44-
(function(remote) {
45-
exports.irsend.list(remote, '', function(error, stdout, stderr) {
46-
exports._populateRemoteCommands(remote, error, stdout, stderr);
47-
});
48-
})(remote);
49-
}
62+
// Merges an array of remoteCommands into a single object
63+
exports._joinRemoteCommands = function(remoteCommands) {
64+
var remotes = {};
65+
66+
remoteCommands.forEach(function(remote) {
67+
remotes = Object.assign(remotes, remote);
68+
});
69+
70+
return remotes;
5071
};
5172

52-
exports._populateRemoteCommands = function(remote, error, stdout, stderr) {
53-
var commands = stderr.split('\n');
73+
// Parses the results of irsend for a specifif remote
74+
exports._parseCommands = function (irsendResult) {
75+
var commands = [];
5476

55-
commands.forEach(function(element, index, array) {
56-
var commandName = element.match(/\s.*\s(.*)$/);
57-
if (commandName && commandName[1]) exports.remotes[remote].push(commandName[1]);
77+
irsendResult[1].split('\n').forEach(function (command) {
78+
var commandName = command.match(/\s.*\s(.*)$/);
79+
if (commandName && commandName[1]) commands.push(commandName[1]);
5880
});
81+
82+
return commands;
5983
};

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lirc_node",
3-
"version": "0.0.4",
3+
"version": "1.0.0",
44
"description": "Control LIRC from Node",
55
"main": "lib/lirc_node.js",
66
"scripts": {
@@ -13,12 +13,15 @@
1313
"keywords": [
1414
"lirc"
1515
],
16-
"dependencies": {},
16+
"dependencies": {
17+
"bluebird": "3.2.1"
18+
},
1719
"devDependencies": {
1820
"mocha": "2.3.3",
1921
"q": "1.4.1",
2022
"should": "7.1.0",
21-
"sinon": "1.17.1"
23+
"sinon": "1.17.1",
24+
"sinon-as-promised": "4.0.0"
2225
},
2326
"author": "Alex Bain <alex@alexba.in>",
2427
"contributors": [

0 commit comments

Comments
 (0)