-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_only.js
81 lines (68 loc) · 2.51 KB
/
publish_only.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
// --------------------------------- check and load deps -----------------------------------------------//
let config, mqtt, sensor;
try {
config = require('./config');
} catch (ex) {
console.log('Config not found. Remember to copy config.js.example and set variables. Exiting.');
process.exit(1);
}
try {
mqtt = require('async-mqtt');
} catch (ex) {
console.log('mqtt package not found. Exiting.');
process.exit(1);
}
// ---------------------------------------- main script --------------------------------------------------//
// exit after configured timeout
setTimeout((function() {
console.log('Timeout reached, exiting');
process.exit(1);
}), config.other_options.timeout);
let client = mqtt.connect(config.mqtt_data.host, { port: config.mqtt_data.port, username: config.mqtt_data.username, password: config.mqtt_data.password});
main().then(
() => {
console.log('Exiting');
process.exit(0);
}).catch(
(message) => {
console.log(message);
process.exit(0);
}
);
// ---------------------------------------- async functions --------------------------------------------------//
function main() {
return new Promise((resolve, reject) => {
client.on('connect',
async () => {
let temperature = parseFloat(process.argv[2]);
let humidity = parseFloat(process.argv[3]);
let battery = parseFloat(process.argv[4]);
if (isNaN(temperature)) {
reject('Temperature is not a number');
return;
}
if (isNaN(humidity)) {
reject('Humidity is not a number');
return;
}
if (isNaN(battery)) {
reject('Battery is not a number');
return;
}
await publishData(null, temperature, humidity, battery).then(() => resolve());
}
);
});
}
async function publishData(error, temperature, humidity, battery) {
try {
if (temperature != 0 && humidity != 0 && battery != 0) {
await client.publish(config.mqtt_data.topic, JSON.stringify({temperature: temperature, humidity: humidity, battery: battery}));
await client.end();
console.log('Correctly published temperature: '+ temperature + ' and humidity: ' + humidity + ' and battery: ' + battery);
}
} catch (e) {
console.log(e.stack);
process.exit(1);
}
}