-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (80 loc) · 2.86 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
let awsIot = require('aws-iot-device-sdk');
let fs = require('fs');
let sp = require('./signal-processor.js');
let tools = require('./tools');
let winston = require('winston');
winston.configure({
level: 'debug',
transports: [
new (winston.transports.Console)({
timestamp: function () {
return new Date().toISOString();
},
formatter: function (options) {
// - Return string will be passed to logger.
// - Optionally, use options.colorize(options.level, <string>) to
// colorize output based on the log level.
return options.timestamp() + ' ' +
winston.config.colorize(options.level, options.level.toUpperCase()) + ' ' +
(options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '');
}
})
]
})
let awsIotConfg = JSON.parse(fs.readFileSync('./config/aws-iot.json', 'UTF-8'));
winston.info('starting loxprox \\o/');
let signalProcessor = new sp.SignalProcessor('./config/messages.json', winston);
signalProcessor.init();
let device = awsIot.device({
keyPath: awsIotConfg.keyPath,
certPath: awsIotConfg.certPath,
caPath: awsIotConfg.caPath,
clientId: awsIotConfg.clientId,
region: awsIotConfg.region,
maximumReconnectTimeMs: 300000 // 300s = 5min
});
device.on('connect', function () {
winston.info('connected');
device.subscribe(awsIotConfg.signalTopic);
});
device.on('reconnect', function () {
winston.info('reconnected');
});
device.on('close', function () {
winston.info('connection closed');
});
device.on('offline', function () {
winston.info('offline');
});
device.on('error', function () {
winston.error('cannot connect');
});
device.on('message', function (topic, payload) {
winston.debug(`got message on topic '${topic}':`, payload.toString());
try {
if (topic == awsIotConfg.signalTopic) {
let performedAnything = false;
let data = JSON.parse(payload.toString());
if (data.signal) {
signalProcessor.process(data.signal);
performedAnything = true;
}
if (data.command) {
if (data.command == 'marco') {
device.publish(awsIotConfg.signalTopic, JSON.stringify({
command: 'polo',
localNetwork: tools.getIpAddresses(),
version: 1
}));
}
performedAnything = true;
}
if (!performedAnything) {
winston.warn('message contains no signal and no command');
}
}
} catch (err) {
winston.error('Error while working on message:', err);
}
});