Skip to content

Commit

Permalink
Add mqtt service readiness logging
Browse files Browse the repository at this point in the history
  • Loading branch information
amberstarlight committed Apr 2, 2024
1 parent 716deb1 commit fc468ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: check-added-large-files

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
rev: v1.5.5
hooks:
- id: insert-license
files: '\.(js.?|ts.?)$'
Expand Down
10 changes: 9 additions & 1 deletion backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const mqttPort: number = process.env.MQTT_ENDPOINT_PORT
const mqttOptions: IClientOptions = {
reconnectPeriod: 1000,
port: mqttPort,
clientId: `zigbee-backend-${Math.random().toString(16).substring(2, 8)}`,
clientId: `starlight-backend-${Math.random().toString(16).substring(2, 8)}`,
connectTimeout: 10000,
};

Expand Down Expand Up @@ -63,4 +63,12 @@ app.use(

app.listen(port, () => {
logger(logLevel, "Express", `Server listening on ${port}.`);

if (!mqttService.ready) {
logger(
logLevel,
"MQTT",
"Can't establish a connection to MQTT endpoint. Maybe the server is offline?",
);
}
});
5 changes: 2 additions & 3 deletions backend/zigbee2mqttService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export class Zigbee2MqttService {
this.#baseTopic = baseTopic;
this.#client = mqtt.connect(endpoint, options);

this.#mqttClientConnected = new Promise((resolve) => {
this.#mqttClientConnected = new Promise((resolve, reject) => {
this.#client.once("error", (error) => reject({ success: false, error }));
this.#client.once("connect", () => {
this.#client.on("message", (topic, payload) =>
this.#handleMessage(topic, payload),
Expand All @@ -191,8 +192,6 @@ export class Zigbee2MqttService {
this.#ready = true;
resolve(SUCCESS);
});

this.#client.once("error", (error) => resolve({ success: false, error }));
});
}

Expand Down

0 comments on commit fc468ca

Please sign in to comment.