-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·116 lines (95 loc) · 2.85 KB
/
index.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
#!/usr/bin/env node
const Mqtt = require('mqtt');
const Lirc = require('lirc-client');
const log = require('yalm');
const pkg = require('./package.json');
const config = require('./config.js');
log.setLevel(config.v);
log.info(pkg.name + ' ' + pkg.version + ' starting');
let lircConnected;
log.info('lirc trying to connect on ' + config.lircHost + ':' + config.lircPort);
const lirc = new Lirc({
host: config.lircHost,
port: config.lircPort
});
if (typeof config.topic !== 'string') {
config.topic = '';
}
if (config.topic !== '' && !config.topic.match(/\/$/)) {
config.topic += '/';
}
let mqttConnected;
log.info('mqtt trying to connect', config.url);
const mqtt = Mqtt.connect(config.url, {will: {topic: config.name + '/connected', payload: '0'}});
mqtt.on('connect', () => {
mqttConnected = true;
log.info('mqtt connected ' + config.url);
mqtt.publish(config.name + '/connected', lircConnected ? '2' : '1');
log.info('mqtt subscribe', config.name + '/set/#');
mqtt.subscribe(config.name + '/set/+/+');
});
mqtt.on('close', () => {
if (mqttConnected) {
mqttConnected = false;
log.info('mqtt closed ' + config.url);
}
});
mqtt.on('error', err => {
log.error('mqtt', err);
});
lirc.on('connect', () => {
log.info('lirc connected');
lircConnected = true;
mqtt.publish(config.name + '/connected', '2');
});
lirc.on('disconnect', () => {
if (lircConnected) {
log.info('lirc connection closed');
lircConnected = false;
mqtt.publish(config.name + '/connected', '1');
}
});
lirc.on('error', err => {
log.error('lirc', err);
});
lirc.on('receive', (remote, command, repeats) => {
log.debug('receive', remote, command, repeats);
const topic = config.n + '/status/' + remote + '/' + command;
let payload;
if (config.json) {
payload = JSON.stringify({
val: parseInt(repeats, 10)
});
} else {
payload = String(parseInt(repeats, 10));
}
log.debug('mqtt >', topic, payload);
mqtt.publish(topic, payload);
});
mqtt.on('message', (topic, payload) => {
payload = payload.toString();
log.debug('mqtt <', topic, payload);
if (!lircConnected) {
log.error('lirc disconnected. can\'t send command.');
return;
}
const [, , remote, key] = topic.split('/');
let repeats = 0;
let cmd = 'SEND_ONCE';
if (payload.toUpperCase() === 'START') {
cmd = 'SEND_START';
} else if (payload.toUpperCase() === 'STOP') {
cmd = 'SEND_STOP';
} else if (payload) {
repeats = parseInt(payload, 10) || 0;
}
if (repeats) {
lirc.cmd(cmd, remote, key, repeats, () => {
log.debug('lirc >', cmd, remote, key, repeats);
});
} else {
lirc.cmd(cmd, remote, key, () => {
log.debug('lirc >', cmd, remote, key);
});
}
});