-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmqtt.js
25 lines (23 loc) · 820 Bytes
/
mqtt.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
const cli = require('./cli');
const mqtt = require('async-mqtt');
const logger = require('./logger');
const args = cli.args;
const mqttOptions = {
username: args.mqttuser,
password: args.mqttpass,
clientId: `nodeRenogy_${Math.random().toString(16).substr(2,8)}`
};
module.exports = {
publish: async function(data, subTopic) {
try {
logger.trace('Connecting to MQTT broker...');
logger.trace(mqttOptions, 'With MQTT options...');
const client = await mqtt.connectAsync(`tcp://${args.mqttbroker}`, mqttOptions)
logger.trace('Publishing data to MQTT...');
await client.publish(`${args.mqtttopic}/${subTopic}`, JSON.stringify(data));
await client.end();
} catch (e){
logger.error(e);
}
}
}