Skip to content

Commit

Permalink
log output tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaskalov committed Oct 29, 2024
1 parent dc1747f commit 2f69658
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/mqttClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class MQTTClient {
const handlers = this.handlers.filter(h => this.matchTopic(h, topic));
const prioHandlers = this.prioHandlers.filter(h => this.matchTopic(h, topic));
const handlersCount = handlers.length + prioHandlers.length;
this.log.debug('MQTT: Message on %s, handler(s): %d (%d prio)', topic, handlersCount, prioHandlers.length);
this.log.debug('MQTT: Message on topic: %s, handler(s): %d (%d prio)', topic, handlersCount, prioHandlers.length);
for (const prioHandler of prioHandlers) {
if (prioHandler.callback(message.toString(), topic) === true) {
this.log.debug('MQTT: Message consumed by prioHandler %s', prioHandler.id);
Expand Down Expand Up @@ -94,7 +94,7 @@ export class MQTTClient {
return { handlersCount, prioHandlersCount };
}

subscribeTopic(topic: string, callback: TopicCallback, priority = false, messageDump = false): string | undefined {
subscribeTopic(topic: string, callback: TopicCallback, priority = false): string | undefined {
if (this.client) {
const id = this.uniqueID();
const handler: TopicHandler = { id, topic, callback };
Expand All @@ -104,15 +104,23 @@ export class MQTTClient {
this.handlers.push(handler);
}
const {prioHandlersCount, handlersCount} = this.handersCount(topic);
this.log.debug('MQTT: Subscribed: %s :- %s%s - %d (%d prio) handler(s)',
id,
topic,
priority ? ' (priority)' : '',
handlersCount,
prioHandlersCount,
);
if (handlersCount === 1) {
this.log.debug('MQTT: Subscribed topic: %s, %s: %s, handler(s): %d (%d prio)',
topic,
priority ? 'prioHandler' : 'Handler',
id,
handlersCount,
prioHandlersCount,
);
this.client.subscribe(topic);
} else {
this.log.debug('MQTT: Added %s: %s, on: %s, handler(s): %d (%d prio)',
priority ? 'prioHandler' : 'Handler',
id,
topic,
handlersCount,
prioHandlersCount,
);
}
return id;
}
Expand All @@ -135,14 +143,22 @@ export class MQTTClient {
const {prioHandlersCount, handlersCount} = this.handersCount(handler.topic);
if (handlersCount === 0) {
this.client.unsubscribe(handler.topic);
this.log.debug('MQTT: Unubscribed topic: %s, %s: %s, handler(s): %d (%d prio)',
handler.topic,
priority ? 'prioHandler' : 'Handler',
handler.id,
handlersCount,
prioHandlersCount,
);
} else {
this.log.debug('MQTT: Removed %s: %s on %s, handler(s): %d (%d prio)',
priority ? 'prioHandler' : 'Handler',
handler.id,
handler.topic,
handlersCount,
prioHandlersCount,
);
}
this.log.debug('MQTT: Unsubscribed %s :- %s%s - %d (%d prio) handler(s)',
handler.id,
handler.topic,
priority ? ' (priority)' : '',
handlersCount,
prioHandlersCount,
);
} else {
this.log.warn('MQTT: Cannot unsubscribe %s - not found', id);
}
Expand Down

0 comments on commit 2f69658

Please sign in to comment.