diff --git a/dist/Alex2Node.js b/dist/Alex2Node.js index 7e63d2e..7ef4af6 100644 --- a/dist/Alex2Node.js +++ b/dist/Alex2Node.js @@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const mqtt_1 = __importDefault(require("mqtt")); const Device_1 = __importDefault(require("./Device")); -class Alex2MQTT { +const events_1 = require("events"); +class Alex2MQTT extends events_1.EventEmitter { constructor(username, password, rootTopic, debugLogging) { + super(); // Initialize EventEmitter this.username = username; this.password = password; this.rootTopic = rootTopic; @@ -30,7 +32,8 @@ class Alex2MQTT { console.log("[Alex2Node.ts] Subscribed to topics"); } if (err) { - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); } }); }); @@ -48,7 +51,8 @@ class Alex2MQTT { if (deviceArray.length > 0) { this.client.publish(topic + "_r", JSON.stringify(deviceArray), (err) => { if (err) { - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); } else if (this.debugLogging) { console.log(`[Alex2Node.ts] Discovery payloads published to ${topic + "_r"}`); @@ -94,7 +98,8 @@ class Alex2MQTT { if (this.debugLogging) { console.error("[Alex2Node.ts] Connection failed:", err); } - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); }); } registerDevice(name, endpointId) { diff --git a/package.json b/package.json index 92dc81d..7a7ea67 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alex2node", - "version": "1.0.0", + "version": "1.0.1", "description": "A Node.js library for creating Alexa-compatible devices using MQTT, based on Alex2MQTT.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/Alex2Node.ts b/src/Alex2Node.ts index bf18271..d0682ca 100644 --- a/src/Alex2Node.ts +++ b/src/Alex2Node.ts @@ -1,7 +1,8 @@ import mqtt, { MqttClient } from "mqtt"; import Device from "./Device"; +import { EventEmitter } from "events"; -class Alex2MQTT { +class Alex2MQTT extends EventEmitter { private client: MqttClient | null = null; private devices: Device[] = []; private MqttHost = "mqtt://Alex2MQTT.stormysdream.club"; @@ -11,7 +12,10 @@ class Alex2MQTT { private password: string, private rootTopic: string, private debugLogging: boolean - ) {} + ) { + super(); // Initialize EventEmitter + } + connect(): void { const options = { @@ -31,7 +35,8 @@ class Alex2MQTT { console.log("[Alex2Node.ts] Subscribed to topics"); } if (err) { - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); } }); }); @@ -56,7 +61,8 @@ class Alex2MQTT { JSON.stringify(deviceArray), (err) => { if (err) { - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); } else if (this.debugLogging) { console.log( `[Alex2Node.ts] Discovery payloads published to ${ @@ -109,7 +115,8 @@ class Alex2MQTT { if (this.debugLogging) { console.error("[Alex2Node.ts] Connection failed:", err); } - throw err; + console.error("[Alex2Node.ts] MQTT connection error:", err); + this.emit("error", err); }); }