diff --git a/db/patch-added-mqtt-monitor.sql b/db/patch-added-mqtt-monitor.sql index 744829b67..2da67240e 100644 --- a/db/patch-added-mqtt-monitor.sql +++ b/db/patch-added-mqtt-monitor.sql @@ -7,4 +7,13 @@ ALTER TABLE monitor ALTER TABLE monitor ADD mqtt_success_message VARCHAR(255); +ALTER TABLE monitor + ADD mqtt_port NUMBER(10); + +ALTER TABLE monitor + ADD mqtt_username VARCHAR(255); + +ALTER TABLE monitor + ADD mqtt_password VARCHAR(255); + COMMIT; diff --git a/server/model/monitor.js b/server/model/monitor.js index 445ce04b1..be0f98f7b 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -321,7 +321,7 @@ class Monitor extends BeanModel { } } else if (this.type === "mqtt") { try { - bean.msg = await mqttAsync(this.url, this.topic, this.successMessage); + bean.msg = await mqttAsync(this.url, this.mqttPort, this.mqttUsername, this.mqttPassword, this.mqttTopic, this.mqttSuccessMessage); bean.status = UP; } catch (error) { bean.status = DOWN; diff --git a/server/util-server.js b/server/util-server.js index 757d91034..2c515cc35 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -90,21 +90,33 @@ exports.pingAsync = function (hostname, ipv6 = false) { }); }; -exports.mqttAsync = function (hostname, topic, okMessage) { +exports.mqttAsync = function (hostname, port = undefined, username = undefined, password = undefined, topic, okMessage) { return new Promise((resolve, reject) => { try { - let client = mqtt.connect(hostname); + console.log({ + hostname, + port, + username, + password + }); + let client = mqtt.connect(hostname, { + port, + username, + password + }); client.on("connect", () => { + console.log(`Connected to ${hostname}:${port}, ${username}, ${password}`); client.subscribe(topic); }); client.on("message", (messageTopic, message) => { - console.log(messageTopic); - if (messageTopic == topic && message.toString() !== okMessage) { - client.end(); - reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`)); - } else { - client.end(); - resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`); + if (messageTopic == topic) { + if (message.toString() === okMessage) { + client.end(); + resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`); + } else { + client.end(); + reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`)); + } } }); } catch (error) { diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 7d982072e..f34a945fb 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -126,9 +126,24 @@ +
+ + +
+ +
+ + +
+ +
+ + +
+
- +
{{ $t("topicExplanation") }}
@@ -136,7 +151,7 @@
- +
{{ $t("successMessageExplanation") }}