Remove try-catch and fix username/password/port not working for mqtt

This commit is contained in:
Louis Lam 2022-04-17 01:06:47 +08:00
parent 566133e350
commit e34420368b
2 changed files with 11 additions and 19 deletions

View File

@ -87,6 +87,7 @@ class Monitor extends BeanModel {
notificationIDList, notificationIDList,
tags: tags, tags: tags,
mqttUsername: this.mqttUsername, mqttUsername: this.mqttUsername,
mqttPassword: this.mqttPassword,
mqttTopic: this.mqttTopic, mqttTopic: this.mqttTopic,
mqttSuccessMessage: this.mqttSuccessMessage mqttSuccessMessage: this.mqttSuccessMessage
}; };
@ -400,20 +401,13 @@ class Monitor extends BeanModel {
throw new Error("Server not found on Steam"); throw new Error("Server not found on Steam");
} }
} else if (this.type === "mqtt") { } else if (this.type === "mqtt") {
try { bean.msg = await mqttAsync(this.hostname, this.mqttTopic, this.mqttSuccessMessage, {
bean.msg = await mqttAsync(this.hostname, this.mqttTopic, this.mqttSuccessMessage, { port: this.port,
mqttPort: this.port, username: this.mqttUsername,
mqttUsername: this.mqttUsername, password: this.mqttPassword,
mqttPassword: this.mqttPassword, interval: this.interval,
interval: this.interval, });
}); bean.status = UP;
bean.status = UP;
} catch (error) {
if (error.message !== "Timeout") {
bean.status = DOWN;
bean.msg = error.message;
}
}
} else { } else {
bean.msg = "Unknown Monitor Type"; bean.msg = "Unknown Monitor Type";
bean.status = PENDING; bean.status = PENDING;

View File

@ -90,9 +90,6 @@ exports.pingAsync = function (hostname, ipv6 = false) {
}; };
exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
log.debug("mqtt", `Topic: ${topic}`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { port, username, password, interval = 20 } = options; const { port, username, password, interval = 20 } = options;
@ -104,7 +101,7 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
const timeoutID = setTimeout(() => { const timeoutID = setTimeout(() => {
log.debug("mqtt", "MQTT timeout triggered"); log.debug("mqtt", "MQTT timeout triggered");
client.end(); client.end();
reject("Timeout"); reject(new Error("Timeout"));
}, interval * 1000 * 0.8); }, interval * 1000 * 0.8);
log.debug("mqtt", "MQTT connecting"); log.debug("mqtt", "MQTT connecting");
@ -116,9 +113,10 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
}); });
client.on("connect", () => { client.on("connect", () => {
log.debug("mqtt", "MQTT subscribe topic"); log.debug("mqtt", "MQTT connected");
try { try {
log.debug("mqtt", "MQTT subscribe topic");
client.subscribe(topic); client.subscribe(topic);
} catch (e) { } catch (e) {
client.end(); client.end();