fix example with errors
This commit is contained in:
parent
4af45ff726
commit
689a357df0
1 changed files with 57 additions and 45 deletions
|
|
@ -1,77 +1,89 @@
|
||||||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
const {
|
||||||
const {
|
Alex2MQTT,
|
||||||
Alex2MQTT,
|
AlexaInterfaceType,
|
||||||
AlexaInterfaceType,
|
PowerController,
|
||||||
PowerController
|
AlexaErrorType,
|
||||||
} = require("../dist/index.js");
|
} = require("../dist/index.js");
|
||||||
|
|
||||||
require("dotenv").config(); // Load environment variables from .env file
|
require("dotenv").config();
|
||||||
|
|
||||||
// Simulated in-memory device state (used for example/demo purposes)
|
let outputState = PowerController.OFF;
|
||||||
let outputState = PowerController.OFF; // Initialize the power state to OFF
|
|
||||||
|
|
||||||
// Load MQTT connection credentials and root topic from environment variables
|
|
||||||
const username = process.env.MQTT_USERNAME;
|
const username = process.env.MQTT_USERNAME;
|
||||||
const password = process.env.MQTT_PASSWORD;
|
const password = process.env.MQTT_PASSWORD;
|
||||||
const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
||||||
|
|
||||||
// Create and initialize a new Alexa-to-MQTT client
|
|
||||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
alex2NodeClient.connect();
|
||||||
|
|
||||||
// Define the device name (bedroom light)
|
const errorLight = alex2NodeClient.registerDevice("bad light", "endpoint4");
|
||||||
const deviceName = "Bedroom Light";
|
errorLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||||
|
|
||||||
// Register the device with a unique endpoint ID (you can use something like "endpoint1")
|
console.log(errorLight.getName());
|
||||||
const bedroomLight = alex2NodeClient.registerDevice(deviceName, "endpoint2");
|
|
||||||
|
|
||||||
// Add the PowerController capability (for turning on/off the device)
|
// Error cycling setup
|
||||||
bedroomLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
const errorCycle = [
|
||||||
|
AlexaErrorType.ENDPOINT_LOW_POWER,
|
||||||
// Log the device name to verify registration
|
AlexaErrorType.VALUE_OUT_OF_RANGE,
|
||||||
console.log(bedroomLight.getName());
|
AlexaErrorType.HARDWARE_MALFUNCTION,
|
||||||
|
AlexaErrorType.BRIDGE_UNREACHABLE,
|
||||||
|
AlexaErrorType.FIRMWARE_OUT_OF_DATE,
|
||||||
|
AlexaErrorType.NOT_CALIBRATED,
|
||||||
|
AlexaErrorType.RATE_LIMIT_EXCEEDED,
|
||||||
|
];
|
||||||
|
let currentErrorIndex = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle Alexa's ReportState directive.
|
* Always return ENDPOINT_BUSY error on state queries.
|
||||||
* This occurs when Alexa queries the current state of the device (e.g., during routines or device status checks).
|
|
||||||
*/
|
*/
|
||||||
bedroomLight.on("ReportState", (payload) => {
|
errorLight.on("ReportState", (payload) => {
|
||||||
console.log("ReportState received!", payload);
|
console.log("ReportState received!", payload);
|
||||||
|
|
||||||
const { correlationToken } = payload.header;
|
const { correlationToken } = payload.header;
|
||||||
|
const errorResponse = errorLight.getErrorMessage(correlationToken);
|
||||||
|
|
||||||
const status = bedroomLight.getStatusMessage(correlationToken);
|
errorResponse.setErrorMessage(
|
||||||
|
AlexaErrorType.HARDWARE_MALFUNCTION,
|
||||||
status
|
"The device is currently busy and cannot report its state."
|
||||||
.addHealthProp("OK") // Device is healthy
|
);
|
||||||
.addPowerControllerProp(outputState); // Report the current power state (ON/OFF)
|
console.log("Sending error: HARDWARE_MALFUNCTION");
|
||||||
|
errorResponse.send();
|
||||||
status.send(); // Send the state report back to Alexa
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle incoming control directives (e.g., TurnOn, TurnOff).
|
* Handle control directives with 90% error chance, cycling through fixed errors.
|
||||||
* These directives come from Alexa when a user issues a command.
|
|
||||||
*/
|
*/
|
||||||
bedroomLight.on("Event", (directive, interfaceType) => {
|
errorLight.on("Event", (directive, interfaceType) => {
|
||||||
console.log("Event received", { directive, interfaceType });
|
|
||||||
|
|
||||||
// Ensure the interfaceType is either PowerController or other valid interfaces
|
|
||||||
if (interfaceType === AlexaInterfaceType.POWER_CONTROLLER) {
|
if (interfaceType === AlexaInterfaceType.POWER_CONTROLLER) {
|
||||||
const name = directive.header.name;
|
const name = directive.header.name;
|
||||||
const token = directive.header.correlationToken;
|
const token = directive.header.correlationToken;
|
||||||
|
|
||||||
// Update the internal state based on the command (TurnOn / TurnOff)
|
const shouldError = Math.random() < 0.9;
|
||||||
if (name === "TurnOn") {
|
|
||||||
outputState = PowerController.ON;
|
if (shouldError) {
|
||||||
console.log("Turning ON the Bedroom Light");
|
const errorType = errorCycle[currentErrorIndex];
|
||||||
} else if (name === "TurnOff") {
|
currentErrorIndex = (currentErrorIndex + 1) % errorCycle.length;
|
||||||
outputState = PowerController.OFF;
|
|
||||||
console.log("Turning OFF the Bedroom Light");
|
const errorResponse = errorLight.getErrorMessage(token);
|
||||||
|
errorResponse.setErrorMessage(
|
||||||
|
errorType,
|
||||||
|
`Failed to process ${name} due to ${errorType.replace(/_/g, " ").toLowerCase()}.`
|
||||||
|
);
|
||||||
|
console.log(`Sending error: ${errorType}`);
|
||||||
|
errorResponse.send();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond to the directive with the updated device status
|
// Success path
|
||||||
const status = bedroomLight.getStatusMessage(token, true);
|
if (name === "TurnOn") {
|
||||||
|
outputState = PowerController.ON;
|
||||||
|
console.log("Turning ON the Light");
|
||||||
|
} else if (name === "TurnOff") {
|
||||||
|
outputState = PowerController.OFF;
|
||||||
|
console.log("Turning OFF the Light");
|
||||||
|
}
|
||||||
|
|
||||||
|
const status = errorLight.getStatusMessage(token, true);
|
||||||
status
|
status
|
||||||
.addHealthProp("OK")
|
.addHealthProp("OK")
|
||||||
.addPowerControllerProp(outputState)
|
.addPowerControllerProp(outputState)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue