-
Notifications
You must be signed in to change notification settings - Fork 2
/
lirc.js
126 lines (110 loc) · 4.12 KB
/
lirc.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Created by aborovsky on 27.08.2015.
*/
var util = require('util');
var lircManager = require('./lib/lircmanager').LircManager;
module.exports = function(RED) {
/**
* ====== Lirc-controller ================
* Holds configuration for lircjs,
* initializes new lircjs connections
* =======================================
*/
function LIRCControllerNode(config) {
var node = this;
RED.nodes.createNode(this, config);
node.name = config.name;
node.devices = [];
node.lirc = new lircManager();
this._updateStatusDevices = function(){
for (var key in node.devices){
var dev = node.devices[key];
if (dev.output == node.lirc.activeOutput && node.lirc.isReady){
dev.status({
fill: "green",
shape: "dot",
text: "active"
});
} else if (node.lirc.isReady) {
dev.status({
fill: "yellow",
shape: "dot",
text: "inactive"
});
} else {
dev.status({
fill: "red",
shape: "dot",
text: "not ready"
});
}
}
};
node.lirc.on('changeOutput', node._updateStatusDevices);
node.lirc.on('ready', node._updateStatusDevices);
node.lirc.on('notReady', node._updateStatusDevices);
this.registerDevice = function(deviceNode){
node.devices.push(deviceNode);
};
this.send = function(device, cmd, output, cb){
RED.comms.publish("debug", {name: node.name, msg: 'sending to lirc: irsend SEND_ONCE ' + device + ' ' + cmd});
//node.log('sending to lirc: ' + device + '/' + cmd);
node.lirc.send(device, cmd, output, cb);
};
this.on("close", function() {
node.log('disconnecting from lirc');
node.lirc.end && node.lirc.end();
});
}
RED.nodes.registerType("lirc-controller", LIRCControllerNode);
/**
* ====== Lirc-out =======================
* Sends outgoing Global Cache device from
* messages received via node-red flows
* =======================================
*/
function LIRCOut(config) {
var node = this;
RED.nodes.createNode(this, config);
node.name = config.name;
node.ctrl = RED.nodes.getNode(config.controller);
node.device = config.device;
node.output = config.output;
node.ctrl && node.ctrl.registerDevice(node);
this.on("input", function(msg) {
RED.comms.publish("debug", {name: node.name, msg: 'lircout.onInput msg[' + util.inspect(msg) + ']'});
//node.log('lircout.onInput msg[' + util.inspect(msg) + ']');
if (!(msg && msg.hasOwnProperty('payload'))) return;
var payload = msg.payload;
if (typeof(msg.payload) === "object") {
payload = msg.payload;
} else if (typeof(msg.payload) === "string") {
try {
payload = JSON.parse(msg.payload);
} catch (e) {
payload = msg.payload.toString();
}
}
if (payload == null || payload.trim().length == 0) {
node.warn(node.name + ': lircout.onInput: illegal msg.payload!');
return;
}
node.ctrl.send(node.device, payload, node.output, function(err) {
if (err) {
node.error(node.name + ': send error: ' + err);
}
if (typeof(msg.cb) === 'function')
msg.cb(err);
});
});
this.on("close", function() {
node.log('lircOut [' + node.name +'] close');
});
node.status({
fill: "red",
shape: "dot",
text: "not ready"
});
}
RED.nodes.registerType("lirc-out", LIRCOut);
};