-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlirc_node.js
More file actions
83 lines (66 loc) · 2.34 KB
/
lirc_node.js
File metadata and controls
83 lines (66 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var Promise = require('bluebird');
// Setup irsend
exports.IRSend = require('./irsend');
exports.irsend = new exports.IRSend();
exports.remotes = {};
// Setup irreceive
exports.IRReceive = require('./irreceive');
var irreceive = new exports.IRReceive();
// Setup event listeners for irreceive
exports.addListener = irreceive.addListener.bind(irreceive);
exports.on = exports.addListener;
exports.removeListener = irreceive.removeListener.bind(irreceive);
// In some cases the default lirc socket does not work
// More info at http://wiki.openelec.tv/index.php?title=Guide_to_Lirc_IR_Blasting
exports.setSocket = function(socket) {
exports.irsend.setSocket(socket);
}
exports.init = function() {
return exports.irsend.list('', '')
.then(exports._populateRemotes)
.then(exports._populateCommands)
.then(function(remotes) {
return exports.remotes = remotes;
});
};
// Parse the list of remotes that irsend knows about
exports._populateRemotes = function (irsendResult) {
var remotes = {};
irsendResult[1].split('\n').forEach(function (remote) {
var remoteName = remote.match(/\s(.*)$/);
if (remoteName) remotes[remoteName[1]] = [];
});
return remotes;
};
// Given object whose keys represent remotes, get commands for each remote
// Returns promise that will resolve when all irsend invocations complete
exports._populateCommands = function (remotesObject) {
var commandPromises = [];
var remoteNames = Object.keys(remotesObject);
remoteNames.forEach(function (remote) {
commandPromises.push(exports.irsend.list(remote, '')
.then(function (irsendResult) {
return { [remote]: exports._parseCommands(irsendResult) }
})
);
})
return Promise.all(commandPromises)
.then(exports._joinRemoteCommands);
};
// Merges an array of remoteCommands into a single object
exports._joinRemoteCommands = function(remoteCommands) {
var remotes = {};
remoteCommands.forEach(function(remote) {
remotes = Object.assign(remotes, remote);
});
return remotes;
};
// Parses the results of irsend for a specifif remote
exports._parseCommands = function (irsendResult) {
var commands = [];
irsendResult[1].split('\n').forEach(function (command) {
var commandName = command.match(/\s.*\s(.*)$/);
if (commandName && commandName[1]) commands.push(commandName[1]);
});
return commands;
};