20 lines
882 B
TypeScript
Raw Normal View History

2025-07-21 22:28:31 +02:00
import mqtt, { MqttClient } from "mqtt";
export function connectHub(opts: { hubIp: string; username: string; password: string; }): Promise<MqttClient> {
const url = `mqtt://${opts.hubIp || "futurehome-smarthub.local"}`;
2025-07-21 23:04:44 +02:00
return makeClient(url, 1884, opts.username, opts.password);
2025-07-21 22:28:31 +02:00
}
2025-07-21 23:04:44 +02:00
export function connectHA(opts: { mqttHost: string; mqttPort: number; mqttUsername: string; mqttPassword: string; }): Promise<MqttClient> {
const url = `mqtt://${opts.mqttHost}`;
return makeClient(url, opts.mqttPort, opts.mqttUsername, opts.mqttPassword);
2025-07-21 22:28:31 +02:00
}
2025-07-21 23:04:44 +02:00
function makeClient(url: string, port: number, username: string, password: string): Promise<MqttClient> {
2025-07-21 22:28:31 +02:00
return new Promise((resolve, reject) => {
2025-07-21 23:04:44 +02:00
const client = mqtt.connect(url, { port, username, password, protocolVersion: 4 });
2025-07-21 22:28:31 +02:00
client.once("connect", () => resolve(client));
client.once("error", reject);
});
}