-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (27 loc) · 1.02 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
import * as config from "./config.js";
import * as fs from "fs";
import * as mqtt from "mqtt";
import * as mqttUtils from "./mqtt/utils.js";
import * as mqttLogger from "./mqtt/logger.js";
const subTopics = JSON.parse(fs.readFileSync("./sub_topics.json"));
const client = mqtt.connect(mqttUtils.formatConnectionOpts(config.mqtt));
console.log(client.options);
client.on("connect", () => {
mqttLogger.connect();
client.subscribe(subTopics, (err, grant) => {
if (err) mqttLogger.subscriptionError(err);
mqttLogger.subscribed(grant);
});
});
client.on("message", function (topic, message) {
// message is Buffer
console.log(message.toString());
// * most of logic is here when subscribing ...
});
// client.on("packetsend", mqttLogger.packetsend);
// client.on("packetreceive", mqttLogger.packetreceive);
client.on("reconnect", mqttLogger.reconnect);
client.on("close", mqttLogger.close);
client.on("offline", mqttLogger.offline);
client.on("end", mqttLogger.end);
client.on("error", mqttLogger.error);