add full support and examples for toggle controller
This commit is contained in:
parent
c7f0c37aa1
commit
f09d03636d
6 changed files with 96 additions and 17 deletions
6
dist/Alex2Node.js
vendored
6
dist/Alex2Node.js
vendored
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
10
dist/Device.js
vendored
10
dist/Device.js
vendored
|
|
@ -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(
|
||||
|
|
|
|||
77
examples/ExampleToggleController.js
Normal file
77
examples/ExampleToggleController.js
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue