This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (53 loc) · 1.64 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
import * as config from "./config.js";
import * as mqtt from "mqtt";
import setFaker from "./fakers/faker.js";
import * as mqttUtils from "./mqtt/utils.js";
import * as mqttLogger from "./mqtt/logger.js";
const topics = mqttUtils.parseTopicsDirectory();
topics.forEach(setFaker);
console.dir(topics, { depth: null });
const client = mqtt.connect(mqttUtils.formatConnectionOpts(config.mqtt));
console.log(client.options);
client.on("connect", () => {
mqttLogger.connect();
topics.forEach((topic) => {
if (!topic.definition.faker.callback) {
// ! HACK
return;
}
console.log(topic.definition.faker);
publishOnInterval(
client,
topic.topic,
topic.definition.faker,
topic.opts,
topic.definition.frequencyEvent
);
});
});
client.on("message", function (topic, message) {
console.log(message.toString());
// ...
});
// 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);
/**
*
* @param {mqtt.MqttClient} client
* @param {string} topic
* @param {obj} msg
* @param {obj|null} opts
* @param {int} interval
* @returns
*/
function publishOnInterval(client, topic, msg, opts, interval) {
client.publish(topic, msg.callback(msg.opts).toString(), opts);
return setInterval(() => {
client.publish(topic, msg.callback(msg.opts).toString(), opts);
}, interval);
}