Add momentary example
This commit is contained in:
parent
3bcd767127
commit
c7f0c37aa1
1 changed files with 109 additions and 0 deletions
109
examples/ExamplePowerControllerWithMomentary.js
Normal file
109
examples/ExamplePowerControllerWithMomentary.js
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||||
|
const {
|
||||||
|
Alex2MQTT,
|
||||||
|
AlexaInterfaceType,
|
||||||
|
PowerController,
|
||||||
|
DisplayCategory
|
||||||
|
} = require("../dist/index.js");
|
||||||
|
|
||||||
|
require("dotenv").config(); // Load environment variables from .env file
|
||||||
|
|
||||||
|
// Simulated in-memory device state
|
||||||
|
let outputState = PowerController.OFF;
|
||||||
|
const momentaryModeInstance = "mode.signal.beep";
|
||||||
|
const defaultMomentaryMode = "mode.signal.beep.Normal";
|
||||||
|
|
||||||
|
// Load MQTT connection credentials and root topic from environment variables
|
||||||
|
const username = process.env.MQTT_USERNAME;
|
||||||
|
const password = process.env.MQTT_PASSWORD;
|
||||||
|
const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
||||||
|
|
||||||
|
// Create and initialize a new Alexa-to-MQTT client
|
||||||
|
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||||
|
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||||
|
|
||||||
|
// Define the device name and register it
|
||||||
|
const deviceName = "Kitchen Light";
|
||||||
|
const kitchenLight = alex2NodeClient.registerDevice(
|
||||||
|
deviceName,
|
||||||
|
"endpoint7",
|
||||||
|
[DisplayCategory.LIGHT]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add PowerController capability
|
||||||
|
kitchenLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||||
|
|
||||||
|
// Add ModeController for momentary "beep" functionality
|
||||||
|
const momentaryController = kitchenLight.addCapability(AlexaInterfaceType.MODE_CONTROLLER);
|
||||||
|
momentaryController.setInstance(momentaryModeInstance);
|
||||||
|
momentaryController.addFriendlyName("Signal", "en-US");
|
||||||
|
momentaryController.addSupportedModes([
|
||||||
|
{
|
||||||
|
value: "mode.signal.beep.Normal",
|
||||||
|
modeResources: {
|
||||||
|
friendlyNames: [{
|
||||||
|
"@type": "text",
|
||||||
|
value: { text: "Normal", locale: "en-US" }
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "mode.signal.beep.Beep",
|
||||||
|
modeResources: {
|
||||||
|
friendlyNames: [{
|
||||||
|
"@type": "text",
|
||||||
|
value: { text: "Beep", locale: "en-US" }
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Log the device name to verify registration
|
||||||
|
console.log(kitchenLight.getName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle Alexa's ReportState directive
|
||||||
|
*/
|
||||||
|
kitchenLight.on("ReportState", (payload) => {
|
||||||
|
console.log("ReportState received!", payload);
|
||||||
|
const { correlationToken } = payload.header;
|
||||||
|
const status = kitchenLight.getStatusMessage(correlationToken);
|
||||||
|
status
|
||||||
|
.addHealthProp("OK")
|
||||||
|
.addPowerControllerProp(outputState)
|
||||||
|
.addModeControllerProp(momentaryModeInstance, defaultMomentaryMode);
|
||||||
|
status.send();
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle incoming directives (PowerController and ModeController)
|
||||||
|
*/
|
||||||
|
kitchenLight.on("Event", (directive, interfaceType) => {
|
||||||
|
console.log("Event received", { directive, interfaceType });
|
||||||
|
const { name, correlationToken, instance } = directive.header;
|
||||||
|
|
||||||
|
const status = kitchenLight.getStatusMessage(correlationToken, true).addHealthProp("OK");
|
||||||
|
|
||||||
|
// PowerController logic
|
||||||
|
if (interfaceType === AlexaInterfaceType.POWER_CONTROLLER) {
|
||||||
|
if (name === "TurnOn") {
|
||||||
|
outputState = PowerController.ON;
|
||||||
|
console.log("Turning ON the Kitchen Light");
|
||||||
|
} else if (name === "TurnOff") {
|
||||||
|
outputState = PowerController.OFF;
|
||||||
|
console.log("Turning OFF the Kitchen Light");
|
||||||
|
}
|
||||||
|
status.addPowerControllerProp(outputState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Momentary ModeController logic
|
||||||
|
else if (interfaceType === AlexaInterfaceType.MODE_CONTROLLER && instance === momentaryModeInstance) {
|
||||||
|
if (name === "SetMode") {
|
||||||
|
const modeValue = directive.payload.mode;
|
||||||
|
console.log(`[Momentary Trigger] Received mode: ${modeValue}`);
|
||||||
|
}
|
||||||
|
status.addModeControllerProp(momentaryModeInstance, defaultMomentaryMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
status.send();
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue