-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (195 loc) · 5.65 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const debug = require('debug')('iogear');
const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const mqtt = require('mqtt');
const argv = yargs(hideBin(process.argv))
.options({
'broker': {
alias: 'b',
describe: 'URL of your MQTT broker',
demandOption: true,
nargs: 1,
requiresArg: true,
type: 'string',
},
'ports': {
alias: 'p',
describe: 'Number of ports on the HDMI switch',
default: 8,
nargs: 1,
requiresArg: true,
type: 'number',
},
'serialport': {
alias: 's',
describe: 'The serial port connected to the HDMI switch',
default: '/dev/ttyUSB0',
nargs: 1,
requiresArg: true,
type: 'string',
},
'name': {
alias: 'n',
describe: 'The name of the HDMI switch',
nargs: 1,
requiresArg: true,
type: 'string',
},
'unique_id': {
alias: 'u',
describe: 'A unique identifier for this switch',
demandOption: true,
nargs: 1,
requiresArg: true,
type: 'string',
},
})
.argv;
const hdmi_name = argv.name || 'IOGEAR HDMI ' + argv.ports + '-port switch';
const hdmi_unique_id = argv.unique_id;
const hdmi_ports = argv.ports;
console.log('-------\nConfig\n-------');
console.log('Switch ports: ' + argv.ports);
console.log('TTY: ' + argv.serialport);
console.log('MQTT Broker: ' + argv.broker);
console.log('Device name: ' + hdmi_name);
console.log('Unique ID: ' + hdmi_unique_id);
console.log('\n-------------\nStarting up!\n-------------\n');
const availability_topic = 'homeassistant/switch/' + hdmi_unique_id + '/availability';
var hdmi_firmware = undefined;
var hdmi_active_port = undefined;
var registered = false;
var serial_conn = {};
var mqtt_conn = {};
function serial_write(command) {
debug('Sending command: ' + command);
serial_conn.port.write(command + '\r');
}
function mqtt_write(topic, msg) {
debug(
'Sending message to MQTT topic: ' +
topic + '\n' + ' -> ' + msg
);
mqtt_conn.client.publish(topic, msg, { retain: true });
}
function mqtt_topic(part, i) {
var p = i || '+';
return [
'homeassistant/switch',
hdmi_unique_id,
p,
part,
].join('/');
}
function port_config(i) {
var config = {
name: argv.name + ' - Input ' + i,
availability_topic: availability_topic,
command_topic: mqtt_topic('set', i),
state_topic: mqtt_topic('state', i),
payload_off: 'off',
payload_on: 'on',
state_off: 'off',
state_on: 'on',
icon: 'mdi:set-top-box',
unique_id: hdmi_unique_id + '_input_' + i,
device: {
identifiers: [hdmi_unique_id],
manufacturer: 'IOGEAR',
model: hdmi_ports + ' port HDMI switch',
name: hdmi_name,
sw_version: hdmi_firmware,
},
};
return JSON.stringify(config);
}
function bail(err, prefix) {
if (err !== null) {
var message = err.message;
if (prefix) message = prefix + message;
console.log(message);
process.exit(1);
}
}
function reregister() {
console.log('Registering with homeassistant');
for (var i = 1; i <= hdmi_ports; i++) {
mqtt_write(mqtt_topic('config', i), port_config(i));
}
mqtt_write(availability_topic, 'online');
registered = true;
}
function updateports() {
debug('updating ports with homeassistant');
for (var i = 1; i <= hdmi_ports; i++) {
var state = (hdmi_active_port === i ? 'on' : 'off');
mqtt_write(mqtt_topic('state', i), state);
}
mqtt_write(availability_topic, 'online');
}
serial_conn.port = new SerialPort(argv.serialport, { baudRate: 19200 });
serial_conn.port.on('error', function(err) {
return bail(err, 'Serial port: ');
});
serial_conn.parser = serial_conn.port.pipe(new Readline({ delimiter: '\r' }));
serial_conn.parser.on('error', function(err) {
return bail(err, 'Serial port: ');
});
mqtt_conn.client = mqtt.connect(argv.broker, {
will: {
topic: availability_topic,
payload: 'offline',
retain: true,
}
});
var mqtt_errs = ['error', 'close', 'offline'];
for (const e of mqtt_errs) {
mqtt_conn.client.on(e, function(err) {
return bail(err, 'MQTT: ');
});
}
mqtt_conn.client.on('connect', function() {
console.log('MQTT connected!');
mqtt_conn.client.subscribe(mqtt_topic('set'));
mqtt_conn.client.subscribe('homeassistant/status');
});
mqtt_conn.client.on('message', function(topic, message) {
debug('Got message: ', topic + ' - ' + message);
if (topic.indexOf('/set') != -1 && message == 'on') {
var match = topic.match(/homeassistant\/switch\/[^/]+\/(?<input>\d)\/set/)
var input = parseInt(match.groups.input);
serial_write('sw i0' + input);
serial_write('read');
} else if (topic == 'homeassistant/status') {
if (message == 'offline') {
console.log('homeassistant is offline, but MQTT broker is alive');
} else if (message == 'online') {
console.log('homeassistant is back online');
reregister();
updateports();
}
}
});
serial_conn.parser.on('data', function(line) {
var shouldUpdate = false;
if (line.indexOf('Input: port') != -1) {
var match = line.match(/Input: port\s+(?<port>\d+)/);
var input = parseInt(match.groups.port);
shouldUpdate = (hdmi_active_port !== input);
hdmi_active_port = input;
} else if (line.indexOf('F/W: V') != -1) {
var match = line.match(/F\/W: (?<version>.+)$/);
var fw = match.groups.version;
hdmi_firmware = fw;
}
if (shouldUpdate) {
if (registered === false) reregister();
console.log('Got new active port: ' + hdmi_active_port);
updateports();
}
});
setInterval(function() {
serial_write('read');
}, 3000);