-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_publisher.js
92 lines (79 loc) · 2.44 KB
/
test_publisher.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// This file creates an MQTT client and connects to the HiveMQ Cloud Broker
// Example of publishing to a topic
// helper function for formatting date in simulated meter data
function printOffsetTime(s) {
// create date object
let date_ob = new Date();
// offset the date by s
date_ob.setSeconds(date_ob.getSeconds() + s);
// get values
let year = date_ob.getFullYear();
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
let day = ("0" + date_ob.getDate()).slice(-2);
let hour = date_ob.getHours();
let minutes = date_ob.getMinutes();
let seconds = ('0' + date_ob.getSeconds()).slice(-2);
// return formatted string
return `${hour}:${minutes}:${seconds} ${month}/${day}/${year}`;
}
//let jsonData = require('./test_data.json');
const mqtt = require('mqtt');
// set options for emqx broker
/*
const options = {
// Clean session
clean: true,
connectTimeout: 4000,
// Auth
clientId: 'emqx_test',
username: 'emqx_test',
password: 'emqx_test',
}
*/
var client = mqtt.connect('mqtt://broker.hivemq.com');
// on connect event begin publishing messages every 15 seconds
/*
client.on('connect', function() {
var x = 0;
setInterval(function() {
client.publish("cameronhowley888",
"Num published to personal channel: "+ x.toString());
console.log(x);
x++;
}, 1500);
});
*/
// simulate meter sending data every 15 secs:
client.on("connect", function() {
setInterval(function() {
// generate random usage readings
var usage_readings = [];
for (let i = 0; i < 10; i++) {
usage_readings.push(Math.floor(Math.random()*100));
}
//format data
var lines = [];
for (let i = 0; i < 10; i++) {
lines.push(usage_readings[i] + ", " + printOffsetTime(i));
}
// store data in json
let data = {
"MeterNum": "1",
"Data": [
lines[0],
lines[1],
lines[2],
lines[3],
lines[4],
lines[5],
lines[6],
lines[7],
lines[8],
lines[9],
]
}
// publish data
client.publish('cameronhowley888', JSON.stringify(data));
console.log("Successfully published data at: " + printOffsetTime(0));
}, 10000)
})