fix: mqtt reconnect state #153

This commit is contained in:
leukipp
2025-12-07 15:59:23 +01:00
parent 3b150fc540
commit 73edbaedb7
+16 -6
View File
@@ -53,18 +53,17 @@ const init = async () => {
const options = user === null || password === null ? {} : { username: user, password: password }; const options = user === null || password === null ? {} : { username: user, password: password };
options.will = { topic: `${INTEGRATION.root}/kiosk/state`, payload: "Terminated", qos: 1, retain: true }; options.will = { topic: `${INTEGRATION.root}/kiosk/state`, payload: "Terminated", qos: 1, retain: true };
options.rejectUnauthorized = !("ignore_certificate_errors" in ARGS); options.rejectUnauthorized = !("ignore_certificate_errors" in ARGS);
options.reconnectPeriod = 10 * 1000;
// Client connect // Client connecting
console.info("MQTT Connecting:", `${user}:${masked}@${url.toString()}`); const connection = `${user}:${masked}@${url.toString()}`;
console.info("MQTT Connecting:", connection);
INTEGRATION.client = mqtt.connect(url.toString(), options); INTEGRATION.client = mqtt.connect(url.toString(), options);
INTEGRATION.client.setMaxListeners(20); INTEGRATION.client.setMaxListeners(20);
// Client connected // Client connected
INTEGRATION.client INTEGRATION.client
.once("connect", () => { .once("connect", () => {
console.info(`MQTT Connected: ${url.toString()}`);
process.stdout.write("\n");
// Init client controls // Init client controls
initApp(); initApp();
initShutdown(); initShutdown();
@@ -120,8 +119,19 @@ const init = async () => {
EVENTS.on("updateScreenshot", updateScreenshot); EVENTS.on("updateScreenshot", updateScreenshot);
EVENTS.on("consoleLog", updateErrors); EVENTS.on("consoleLog", updateErrors);
}) })
.on("connect", () => {
console.info(`MQTT Connected: ${connection}`);
process.stdout.write("\n");
updateKiosk();
})
.on("offline", () => {
console.warn(`MQTT Disconnected: ${connection}`);
})
.on("reconnect", () => {
console.info(`MQTT Reconnecting: ${connection}`);
})
.on("error", (error) => { .on("error", (error) => {
console.error("MQTT", error.message); console.error("MQTT Error:", error.message);
}); });
// Update time sensors periodically (30s) // Update time sensors periodically (30s)