-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (122 loc) · 3.26 KB
/
Copy pathserver.js
File metadata and controls
145 lines (122 loc) · 3.26 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
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
import { SerialPort } from 'serialport';
import { ReadlineParser } from '@serialport/parser-readline';
import mqtt from 'mqtt';
import { createLogger, format, transports } from 'winston';
import config from './config.json' with { "type": "json" };
const { combine, timestamp, printf } = format;
const port = new SerialPort(config.serial);
const parser = port.pipe(new ReadlineParser({
delimiter: '\r\n',
}));
const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});
const logger = createLogger({
format: combine(
timestamp(),
myFormat
),
transports: [new transports.Console()]
});
port.on('open', function() {
logger.info('Serial port open');
});
const mqttClient = mqtt.connect(config.mqtt.options);
mqttClient.on('connect', function() {
logger.info('MQTT connected');
port.open();
});
mqttClient.on('message', function(topic, message) {
if (topic.indexOf(config.mqtt.topics.subscribePrefix) !== 0) {
logger.warn('Unknown topic: ' + topic);
return;
}
logger.info('MQTT receive: ' + topic + ' ' + message.toString());
const cmd = topic.substr(config.mqtt.topics.subscribePrefix.length);
let msg = null;
if (['send', 'setreceive'].indexOf(cmd) >= 0) {
try {
msg = JSON.parse(message);
}
catch (e) {
logger.error(e.message);
return;
}
}
switch (cmd) {
case 'send':
let optionalParams = '';
// noinspection JSObjectNullOrUndefined
if (msg.pulseLength) {
optionalParams = ' ' + msg.pulseLength;
}
if (msg.numRepeats) {
if (optionalParams.length === 0) {
optionalParams += ' 0';
}
optionalParams += ' ' + msg.numRepeats;
}
serialSend('SEND ' + msg.protocol + ' ' + msg.numBits + ' ' + msg.value + optionalParams);
break;
case 'setreceive':
// noinspection JSObjectNullOrUndefined
serialSend('SETRECEIVE ' + (msg.state ? '1' : '0'));
break;
case 'ping':
serialSend('PING');
break;
default:
logger.error('Unknown MQTT command: ' + cmd);
break;
}
});
mqttClient.subscribe(['send', 'setreceive', 'ping'].map(function(topic) {
return config.mqtt.topics.subscribePrefix + topic;
}));
function mqttSend(cmd, data) {
const topic = config.mqtt.topics.publishPrefix + cmd;
logger.info('MQTT send: ' + topic + ' ' + data);
mqttClient.publish(topic, data.toString());
}
function serialSend(data) {
logger.info('Serial send: ' + data);
port.write(data + '\n');
}
parser.on('data', data => {
logger.info('Serial receive: ' + data);
const msgParts = data.split(' ');
const cmd = msgParts[0].toLowerCase();
const args = msgParts.slice(1);
let mqttArgs = {
ts: Date.now(),
};
switch (cmd) {
case 'init':
case 'ready':
case 'sent':
case 'pong':
// no other details added
break;
case 'setreceive':
mqttArgs.state = args[0] === 'ON';
break;
case 'receive':
mqttArgs.protocol = parseInt(args[0], 10);
mqttArgs.numBits = parseInt(args[1], 10);
mqttArgs.value = parseInt(args[2], 10);
mqttArgs.pulseLength = parseInt(args[3], 10);
break;
case 'error':
mqttArgs = null;
logger.error('Arduino error: ' + args);
break;
default:
mqttArgs = null;
logger.error('Unknown command: ' + cmd + ' ' + args);
break;
}
if (mqttArgs) {
mqttArgs = JSON.stringify(mqttArgs);
mqttSend(cmd, mqttArgs);
}
});