-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.74 KB
/
app.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
#!/usr/bin/env node
var mqtt = require('mqtt');
// const util = require('util');
const ipc = require('node-ipc');
ipc.config.id = 'nanoStreamMQTT';
ipc.config.retry = 1500;
ipc.config.logger = () => {}; // Make ipc logger a no-op
const args = {};
// Collect all args passed in
process.argv.slice(2).forEach((arg) => {
const [key, value] = arg.split('=');
args[key] = value;
});
const HOST = args.host || 'mqtt://127.0.0.1';
const TOPIC = args.topic || 'nanostream';
const options = {};
if (args.username) options.username = args.username;
if (args.password) options.password = args.password;
const connection = mqtt.connect(HOST, options);
connection.on('error', function(e) {
console.error('Error from mqtt: ', e);
});
const publishCallback = (failed, error) => {
if (failed) {
console.error(`Error encountered when publishing to topic ${TOPIC}`, error);
}
};
// Wait for connection to become established
connection.on('connect', function () {
console.info('Connected to MQTT server');
console.info(`Will publish to topic "${TOPIC}"`);
// Connect to the block data streaming socket
ipc.connectTo(
'nanoStream', () => {
ipc.of.nanoStream.on('error', (err) => {
if (err.errno == 'ECONNREFUSED') {
console.error('Error trying to connect to nano-stream-x, nano-stream-x is not running');
} else {
console.error('Error trying to connect to nano-stream-x', err);
}
});
ipc.of.nanoStream.on('connect', () => console.info('Connected to nano-stream-x'));
ipc.of.nanoStream.on(
'payload', // topic
function(data){
connection.publish(TOPIC, data, publishCallback);
console.debug(`Sending data to topic ${TOPIC}`);
}
);
}
);
});