Skip to content

Commit

Permalink
Fix publish MQTT topic
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Dec 13, 2024
1 parent fd82308 commit 1398cfb
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/coordinator/helpers/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { type Config } from '../../helpers/config.js';
import { type ActiveInverterControlLimit } from './inverterController.js';

export class Publish {
private mqttClient: mqtt.MqttClient | undefined;
private mqtt: { client: mqtt.MqttClient; topic: string } | undefined;

constructor({ config }: { config: Config }) {
if (config.publish?.mqtt) {
this.mqttClient = mqtt.connect(config.publish.mqtt.host, {
username: config.publish.mqtt.username,
password: config.publish.mqtt.password,
});
this.mqtt = {
client: mqtt.connect(config.publish.mqtt.host, {
username: config.publish.mqtt.username,
password: config.publish.mqtt.password,
}),
topic: config.publish.mqtt.topic,
};
}
}

Expand All @@ -19,11 +22,8 @@ export class Publish {
}: {
limit: ActiveInverterControlLimit;
}) {
if (this.mqttClient) {
this.mqttClient.publish(
'inverterControlLimit',
JSON.stringify(limit),
);
if (this.mqtt) {
this.mqtt.client.publish(this.mqtt.topic, JSON.stringify(limit));
}
}
}

0 comments on commit 1398cfb

Please sign in to comment.