Add support for sending Error Responses
This commit is contained in:
parent
61b032c889
commit
4af45ff726
9 changed files with 396 additions and 19 deletions
80
examples/ExamplePowerControllerWithErrors.js
Normal file
80
examples/ExamplePowerControllerWithErrors.js
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController
|
||||
} = require("../dist/index.js");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
// Simulated in-memory device state (used for example/demo purposes)
|
||||
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 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 (bedroom light)
|
||||
const deviceName = "Bedroom Light";
|
||||
|
||||
// Register the device with a unique endpoint ID (you can use something like "endpoint1")
|
||||
const bedroomLight = alex2NodeClient.registerDevice(deviceName, "endpoint2");
|
||||
|
||||
// Add the PowerController capability (for turning on/off the device)
|
||||
bedroomLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
|
||||
// Log the device name to verify registration
|
||||
console.log(bedroomLight.getName());
|
||||
|
||||
/**
|
||||
* Handle Alexa's ReportState directive.
|
||||
* This occurs when Alexa queries the current state of the device (e.g., during routines or device status checks).
|
||||
*/
|
||||
bedroomLight.on("ReportState", (payload) => {
|
||||
console.log("ReportState received!", payload);
|
||||
|
||||
const { correlationToken } = payload.header;
|
||||
|
||||
const status = bedroomLight.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy
|
||||
.addPowerControllerProp(outputState); // Report the current power state (ON/OFF)
|
||||
|
||||
status.send(); // Send the state report back to Alexa
|
||||
});
|
||||
|
||||
/**
|
||||
* Handle incoming control directives (e.g., TurnOn, TurnOff).
|
||||
* These directives come from Alexa when a user issues a command.
|
||||
*/
|
||||
bedroomLight.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) {
|
||||
const name = directive.header.name;
|
||||
const token = directive.header.correlationToken;
|
||||
|
||||
// Update the internal state based on the command (TurnOn / TurnOff)
|
||||
if (name === "TurnOn") {
|
||||
outputState = PowerController.ON;
|
||||
console.log("Turning ON the Bedroom Light");
|
||||
} else if (name === "TurnOff") {
|
||||
outputState = PowerController.OFF;
|
||||
console.log("Turning OFF the Bedroom Light");
|
||||
}
|
||||
|
||||
// Respond to the directive with the updated device status
|
||||
const status = bedroomLight.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addPowerControllerProp(outputState)
|
||||
.send();
|
||||
}
|
||||
});
|
||||
|
|
@ -45,11 +45,11 @@ console.log(thermostat.getName());
|
|||
*/
|
||||
|
||||
const deviceData = {
|
||||
lowerSetpoint: 20,
|
||||
upperSetpoint: 20,
|
||||
targetSetpoint: 20,
|
||||
current: 20,
|
||||
thermostatMode: ThermostatMode.AUTO,
|
||||
lowerSetpoint: 50,
|
||||
upperSetpoint: 70,
|
||||
targetSetpoint: 60,
|
||||
current: 69,
|
||||
thermostatMode: ThermostatMode.HEAT,
|
||||
};
|
||||
thermostat.on("ReportState", (payload) => {
|
||||
// console.log("ReportState received!", payload);
|
||||
|
|
@ -101,17 +101,17 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
if (name == "SetTargetTemperature") {
|
||||
console.log({ name: directive.payload });
|
||||
if (directive.payload.upperSetpoint) {
|
||||
deviceData.upperSetpoint = praseTempValue(
|
||||
deviceData.upperSetpoint = parseTempValue(
|
||||
directive.payload.upperSetpoint
|
||||
);
|
||||
}
|
||||
if (directive.payload.lowerSetpoint) {
|
||||
deviceData.lowerSetpoint = praseTempValue(
|
||||
deviceData.lowerSetpoint = parseTempValue(
|
||||
directive.payload.lowerSetpoint
|
||||
);
|
||||
}
|
||||
if (directive.payload.targetSetpoint) {
|
||||
deviceData.targetSetpoint = praseTempValue(
|
||||
deviceData.targetSetpoint = parseTempValue(
|
||||
directive.payload.targetSetpoint
|
||||
);
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
status
|
||||
.addHealthProp("OK") // Device is healthy and reachable
|
||||
.addTemperatureSensorProp(
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.current
|
||||
) // Report current temperature
|
||||
.addThermostatModeProp(deviceData.thermostatMode); // Report current thermostat mode
|
||||
|
|
@ -136,18 +136,18 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
status = status
|
||||
.addThermostatControllerProp(
|
||||
"lowerSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.lowerSetpoint
|
||||
) // Lower bound of temperature range
|
||||
.addThermostatControllerProp(
|
||||
"upperSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.upperSetpoint
|
||||
); // Upper bound of temperature range
|
||||
} else {
|
||||
status = status.addThermostatControllerProp(
|
||||
"targetSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.targetSetpoint
|
||||
); // Lower bound of temperature range
|
||||
}
|
||||
|
|
@ -155,6 +155,13 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
}
|
||||
});
|
||||
|
||||
function praseTempValue(data) {
|
||||
return data.value;
|
||||
function parseTempValue(data) {
|
||||
console.log(data);
|
||||
|
||||
if (data.scale === "FAHRENHEIT") {
|
||||
return data.value;
|
||||
}
|
||||
|
||||
const fahrenheit = (data.value * 9) / 5 + 32;
|
||||
return fahrenheit;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue