forked from grafana/mqtt-datasource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_broker.js
46 lines (39 loc) · 1.2 KB
/
test_broker.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
const aedes = require('aedes')();
const server = require('net').createServer(aedes.handle);
const PORT = 1883;
const publishers = {};
const toMillis = {
millisecond: ms => ms,
second: sec => sec * 1000,
minute: min => min * 60 * 1000,
hour: hour => hour * 60 * 60 * 1000,
};
const createPublisher = ({ topic, qos }) => {
let i = 0;
const [duration, value] = topic.split('/');
const fn = toMillis[duration];
if (!fn || !value || value < 1) {
console.log(`unknown interval:`, topic);
return;
}
const interval = fn(value);
if (!publishers[topic]) {
console.log(`publishing to topic:`, topic);
publishers[topic] = setInterval(() => {
aedes.publish({
topic,
cmd: 'publish',
qos,
retain: false,
payload: Math.random().toString(),
});
}, interval);
}
};
server.listen(PORT, () => {
console.log('server started and listening on port ', PORT);
aedes.on('subscribe', subscriptions => subscriptions.forEach(createPublisher));
aedes.on('connectionError', console.error);
aedes.on('clientDisconnect', client => console.log(`disconnect: ${client.id}`));
aedes.on('client', client => console.log(`connect: ${client.id}`));
});