-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (66 loc) · 2.39 KB
/
main.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
const mqtt = require("async-mqtt")
const yaml = require("node-yaml")
const inquirer = require("inquirer")
const chalk = require('chalk');
// Load configuration
var config = yaml.readSync("config.yaml")
console.log("Connecting to MQTT...")
// Try to connect to mqtt
let connection = mqtt.connect(config.mqtt.url, config.mqtt.options)
// Wait until we connect
connection.on("connect", onConnect)
// Complain if we can't
connection.on("error", function(e) {
console.error(e)
})
async function onConnect() {
try {
// Keep track of the last choice to reduce navigation
var lastChoice = 0
// Work inside of a function since we'll want to do this repeatedly
async function askThenSend() {
// Check what message the user wants to send
let answer = await inquirer.prompt({
type: "list",
name: "message",
message: "Which message would you like to send?",
default: lastChoice,
choices: function() {
// Make a list of function choices
return config.messages.map(function(message, index) {
return {
name: message.name,
value: index
}
})
}
})
// Update the last choice
lastChoice = answer.message
// Fetch the message to send
let messageToSend = config.messages[answer.message]
// Publish the message on MQTT
var data = messageToSend.data
// If this is an array or object JSONify
if (data instanceof Array || data instanceof Object) {
data = JSON.stringify(data)
}
// Log
console.log()
console.log(chalk.blue("Topic:") + " " + messageToSend.topic)
console.log(chalk.blue("Message:"), data)
// Publish
await connection.publish(messageToSend.topic, data)
console.log()
console.log("Message published 💬")
console.log()
}
// Stay running and asking for messages to send
while (true) {
await askThenSend()
}
} catch (e) {
console.error("Whoops,", e)
process.exit()
}
}