// 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 (used for example/demo purposes) let currentMode = "mode.fanmode.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 (Test mode controller) const deviceName = "Test mode controller"; // Register the device with a unique endpoint ID (you can use something like "endpoint1") const modeControllerDevice = alex2NodeClient.registerDevice( deviceName, "endpoint6", [DisplayCategory.OTHER] ); // Add the 'MODE_CONTROLLER' capability for controlling device modes const modeController = modeControllerDevice.addCapability( AlexaInterfaceType.MODE_CONTROLLER ); modeController.setInstance("mode.fanmode"); modeController.addFriendlyName("Fan Mode", "en-US"); modeController.addSupportedModes([ { value: "mode.fanmode.Normal", modeResources: { friendlyNames: [ { "@type": "text", value: { text: "Normal", locale: "en-US", }, }, ], }, }, { value: "mode.fanmode.Low", modeResources: { friendlyNames: [ { "@type": "text", value: { text: "Low", locale: "en-US", }, }, ], }, }, ]); // Log the device name to verify registration console.log(modeControllerDevice.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). */ modeControllerDevice.on("ReportState", (payload) => { console.log("ReportState received!", payload); const { correlationToken } = payload.header; const status = modeControllerDevice.getStatusMessage(correlationToken); status .addHealthProp("OK") // Device is healthy .addModeControllerProp("mode.fanmode", currentMode); status.send(); // Send the state report back to Alexa }); /** * Handle incoming control directives (e.g., SetMode). * These directives come from Alexa when a user issues a command. */ modeControllerDevice.on("Event", (directive, interfaceType) => { console.log("Event received", { directive, interfaceType }); // Ensure the interfaceType is either PowerController or other valid interfaces if (interfaceType === AlexaInterfaceType.MODE_CONTROLLER) { const name = directive.header.name; const token = directive.header.correlationToken; const instance = directive.header.instance; if (instance == "mode.fanmode") { if (name === "SetMode") { currentMode = directive.payload.mode; } } const status = modeControllerDevice.getStatusMessage(token, true); status .addHealthProp("OK") // Device is healthy .addModeControllerProp("mode.fanmode", currentMode); status.send(); // Send the state report back to Alexa } });