add internal event emitter for error handling
This commit is contained in:
parent
13f0b1811c
commit
7f051dfdcb
3 changed files with 22 additions and 10 deletions
13
dist/Alex2Node.js
vendored
13
dist/Alex2Node.js
vendored
|
|
@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const mqtt_1 = __importDefault(require("mqtt"));
|
const mqtt_1 = __importDefault(require("mqtt"));
|
||||||
const Device_1 = __importDefault(require("./Device"));
|
const Device_1 = __importDefault(require("./Device"));
|
||||||
class Alex2MQTT {
|
const events_1 = require("events");
|
||||||
|
class Alex2MQTT extends events_1.EventEmitter {
|
||||||
constructor(username, password, rootTopic, debugLogging) {
|
constructor(username, password, rootTopic, debugLogging) {
|
||||||
|
super(); // Initialize EventEmitter
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.rootTopic = rootTopic;
|
this.rootTopic = rootTopic;
|
||||||
|
|
@ -30,7 +32,8 @@ class Alex2MQTT {
|
||||||
console.log("[Alex2Node.ts] Subscribed to topics");
|
console.log("[Alex2Node.ts] Subscribed to topics");
|
||||||
}
|
}
|
||||||
if (err) {
|
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) {
|
if (deviceArray.length > 0) {
|
||||||
this.client.publish(topic + "_r", JSON.stringify(deviceArray), (err) => {
|
this.client.publish(topic + "_r", JSON.stringify(deviceArray), (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
console.error("[Alex2Node.ts] MQTT connection error:", err);
|
||||||
|
this.emit("error", err);
|
||||||
}
|
}
|
||||||
else if (this.debugLogging) {
|
else if (this.debugLogging) {
|
||||||
console.log(`[Alex2Node.ts] Discovery payloads published to ${topic + "_r"}`);
|
console.log(`[Alex2Node.ts] Discovery payloads published to ${topic + "_r"}`);
|
||||||
|
|
@ -94,7 +98,8 @@ class Alex2MQTT {
|
||||||
if (this.debugLogging) {
|
if (this.debugLogging) {
|
||||||
console.error("[Alex2Node.ts] Connection failed:", err);
|
console.error("[Alex2Node.ts] Connection failed:", err);
|
||||||
}
|
}
|
||||||
throw err;
|
console.error("[Alex2Node.ts] MQTT connection error:", err);
|
||||||
|
this.emit("error", err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
registerDevice(name, endpointId) {
|
registerDevice(name, endpointId) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "alex2node",
|
"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.",
|
"description": "A Node.js library for creating Alexa-compatible devices using MQTT, based on Alex2MQTT.",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import mqtt, { MqttClient } from "mqtt";
|
import mqtt, { MqttClient } from "mqtt";
|
||||||
import Device from "./Device";
|
import Device from "./Device";
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
|
||||||
class Alex2MQTT {
|
class Alex2MQTT extends EventEmitter {
|
||||||
private client: MqttClient | null = null;
|
private client: MqttClient | null = null;
|
||||||
private devices: Device[] = [];
|
private devices: Device[] = [];
|
||||||
private MqttHost = "mqtt://Alex2MQTT.stormysdream.club";
|
private MqttHost = "mqtt://Alex2MQTT.stormysdream.club";
|
||||||
|
|
@ -11,7 +12,10 @@ class Alex2MQTT {
|
||||||
private password: string,
|
private password: string,
|
||||||
private rootTopic: string,
|
private rootTopic: string,
|
||||||
private debugLogging: boolean
|
private debugLogging: boolean
|
||||||
) {}
|
) {
|
||||||
|
super(); // Initialize EventEmitter
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
connect(): void {
|
connect(): void {
|
||||||
const options = {
|
const options = {
|
||||||
|
|
@ -31,7 +35,8 @@ class Alex2MQTT {
|
||||||
console.log("[Alex2Node.ts] Subscribed to topics");
|
console.log("[Alex2Node.ts] Subscribed to topics");
|
||||||
}
|
}
|
||||||
if (err) {
|
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),
|
JSON.stringify(deviceArray),
|
||||||
(err) => {
|
(err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
console.error("[Alex2Node.ts] MQTT connection error:", err);
|
||||||
|
this.emit("error", err);
|
||||||
} else if (this.debugLogging) {
|
} else if (this.debugLogging) {
|
||||||
console.log(
|
console.log(
|
||||||
`[Alex2Node.ts] Discovery payloads published to ${
|
`[Alex2Node.ts] Discovery payloads published to ${
|
||||||
|
|
@ -109,7 +115,8 @@ class Alex2MQTT {
|
||||||
if (this.debugLogging) {
|
if (this.debugLogging) {
|
||||||
console.error("[Alex2Node.ts] Connection failed:", err);
|
console.error("[Alex2Node.ts] Connection failed:", err);
|
||||||
}
|
}
|
||||||
throw err;
|
console.error("[Alex2Node.ts] MQTT connection error:", err);
|
||||||
|
this.emit("error", err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue