diff --git a/dist/Alex2Node.js b/dist/Alex2Node.js index 58758af..f32cff7 100644 --- a/dist/Alex2Node.js +++ b/dist/Alex2Node.js @@ -111,11 +111,11 @@ class Alex2MQTT extends events_1.EventEmitter { console.log(`[Alex2Node.ts] Creating new device with endpoint: ${endpointId}`); } const normalizedCategory = displayCategory === null - ? null + ? [DisplayCategory_1.DisplayCategory.LIGHT] : Array.isArray(displayCategory) ? displayCategory - : [displayCategory]; - const device = new Device_1.default(this.client, this.rootTopic, name, endpointId, normalizedCategory || [DisplayCategory_1.DisplayCategory.LIGHT]); + : [displayCategory || DisplayCategory_1.DisplayCategory.LIGHT]; + const device = new Device_1.default(this.client, this.rootTopic, name, endpointId, normalizedCategory); this.devices.push(device); return device; } diff --git a/dist/Device.js b/dist/Device.js index 393310b..e2abf6b 100644 --- a/dist/Device.js +++ b/dist/Device.js @@ -69,10 +69,12 @@ class Device extends events_1.EventEmitter { return this.softwareVersion; } addCapability(type) { - const existing = this.capabilities.find((iface) => iface.getType() === type); - if (existing) { - return existing; - } + // const existing = this.capabilities.find( + // (iface) => iface.getType() === type + // ); + // if (existing) { + // return existing; + // } const newCapability = new AlexaInterface_1.AlexaInterface(type); this.capabilities.push(newCapability); // console.log( diff --git a/examples/ExampleToggleController.js b/examples/ExampleToggleController.js new file mode 100644 index 0000000..0d6deca --- /dev/null +++ b/examples/ExampleToggleController.js @@ -0,0 +1,77 @@ +const { + Alex2MQTT, + AlexaInterfaceType, + PowerController, + } = require("../dist/index.js"); + + require("dotenv").config(); + + const username = process.env.MQTT_USERNAME; + const password = process.env.MQTT_PASSWORD; + const rootTopic = process.env.MQTT_ROOT_TOPIC; + + const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, true); + alex2NodeClient.connect(); + + const deviceName = "Custom Device"; + const customToggle = alex2NodeClient.registerDevice(deviceName, "endpoint8"); + + // Define 8 toggle instances and friendly names + const switchCount = 8; + const instanceNames = Array.from({ length: switchCount }, (_, i) => `nodejs.toggle.switch${i + 1}`); + const friendlyNames = Array.from({ length: switchCount }, (_, i) => `Switch ${i + 1}`); + + // Add toggle capabilities and store toggle objects + const toggles = []; + for (let i = 0; i < switchCount; i++) { + const toggle = customToggle.addCapability(AlexaInterfaceType.TOGGLE_CONTROLLER); + toggle.setInstance(instanceNames[i]); + toggle.addFriendlyName(friendlyNames[i], "en-US"); + toggles.push(toggle); + } + + // Output states for all switches + const toggleStates = Array(switchCount).fill(PowerController.OFF); + + console.log(customToggle.getName()); + + customToggle.on("ReportState", (payload) => { + console.log("ReportState received!", payload); + const { correlationToken } = payload.header; + const status = customToggle.getStatusMessage(correlationToken); + + status.addHealthProp("OK"); + + // Report all toggle states + for (let i = 0; i < switchCount; i++) { + status.addToggleControllerProp(toggleStates[i], instanceNames[i]); + } + + status.send(); + }); + + customToggle.on("Event", (directive, interfaceType) => { + console.log("Event received", { directive, interfaceType }); + + if (interfaceType === AlexaInterfaceType.TOGGLE_CONTROLLER) { + const { name, correlationToken, instance } = directive.header; + + const index = instanceNames.indexOf(instance); + if (index !== -1) { + if (name === "TurnOn") { + toggleStates[index] = PowerController.ON; + console.log(`Turning ON ${friendlyNames[index]}`); + } else if (name === "TurnOff") { + toggleStates[index] = PowerController.OFF; + console.log(`Turning OFF ${friendlyNames[index]}`); + } + + const status = customToggle.getStatusMessage(correlationToken, true); + status + .addHealthProp("OK") + .addToggleControllerProp(toggleStates[index], instance) + .send(); + } + } + }); + \ No newline at end of file diff --git a/package.json b/package.json index 873d0b5..dacc0fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alex2node", - "version": "1.3.0", + "version": "1.4.0", "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 4fd3793..dc3b821 100644 --- a/src/Alex2Node.ts +++ b/src/Alex2Node.ts @@ -135,17 +135,17 @@ class Alex2MQTT extends EventEmitter { } const normalizedCategory = displayCategory === null - ? null + ? [DisplayCategory.LIGHT] : Array.isArray(displayCategory) ? displayCategory - : [displayCategory]; + : [displayCategory||DisplayCategory.LIGHT]; const device = new Device( this.client!, this.rootTopic, name, endpointId, - normalizedCategory|| [DisplayCategory.LIGHT] + normalizedCategory ); this.devices.push(device); return device; diff --git a/src/Device.ts b/src/Device.ts index 74cb6e5..8a1871d 100644 --- a/src/Device.ts +++ b/src/Device.ts @@ -101,12 +101,12 @@ class Device extends EventEmitter { } addCapability(type: AlexaInterfaceType): AlexaInterface { - const existing = this.capabilities.find( - (iface) => iface.getType() === type - ); - if (existing) { - return existing; - } + // const existing = this.capabilities.find( + // (iface) => iface.getType() === type + // ); + // if (existing) { + // return existing; + // } const newCapability = new AlexaInterface(type); this.capabilities.push(newCapability);