-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgateway.js
executable file
·55 lines (44 loc) · 1.55 KB
/
gateway.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
#!/usr/bin/env node
var program = require('commander');
program
.option('--discovery', 'Discover new devices')
.parse(process.argv);
var config = require('/etc/yarf/config.js')
var mqtt = require('mqtt')
var mqtt_client = mqtt.connect("mqtt://" + config.mqtt.host)
var isSubset=require('is-subset');
var devices = require('/etc/yarf/devices.json');
const controller = require('rfcontroljs')
mqtt_client.on('connect', function() {console.log('Connected to MQTT')} )
function check_rf(data){
var result = null
for (var i in data) {
for (var j in devices) {
if (isSubset(data[i], devices[j].ident)){
result = {"name": devices[j].name,
"type": devices[j].type,
"values": data[i].values}
result = JSON.stringify(result)
}
}
}
return(result)
}
function decode_RF(line){
result = controller.prepareCompressedPulses(line)
decoded = controller.decodePulses(result.pulseLengths, result.pulses)
if (decoded.length > 0){
measurement = check_rf(decoded);
if (measurement != null) {
mqtt_client.publish(config.mqtt.topic, measurement)
}
if (program.discovery) {
console.log(JSON.stringify(decoded, null, 4))
}
}
}
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort(config.serialport, { baudRate: 9600});
const parser = port.pipe(new Readline({ delimiter: '\n' }));
parser.on('data', decode_RF);