initial release of Alex2Node

This commit is contained in:
David 2025-04-20 21:19:57 +00:00
parent 0804bd7397
commit 4eb2a84d72
20 changed files with 2047 additions and 3 deletions

32
dist/ActionMapping.js vendored Normal file
View file

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionMapping = exports.AlexaActions = void 0;
var AlexaActions;
(function (AlexaActions) {
AlexaActions["Open"] = "Alexa.Actions.Open";
AlexaActions["Close"] = "Alexa.Actions.Close";
AlexaActions["Raise"] = "Alexa.Actions.Raise";
AlexaActions["Lower"] = "Alexa.Actions.Lower";
AlexaActions["SetEcoOn"] = "Alexa.Actions.SetEcoOn";
AlexaActions["SetEcoOff"] = "Alexa.Actions.SetEcoOff";
})(AlexaActions || (exports.AlexaActions = AlexaActions = {}));
class ActionMapping {
constructor(actions, directiveName, directivePayload) {
this.type = "ActionsToDirective";
this.actions = actions;
this.directive = {
name: directiveName,
};
if (directivePayload) {
this.directive.payload = directivePayload;
}
}
toJSON() {
return {
"@type": this.type,
actions: this.actions,
directive: this.directive,
};
}
}
exports.ActionMapping = ActionMapping;

112
dist/Alex2Node.js vendored Normal file
View file

@ -0,0 +1,112 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mqtt_1 = __importDefault(require("mqtt"));
const Device_1 = __importDefault(require("./Device"));
class Alex2MQTT {
constructor(username, password, rootTopic, debugLogging) {
this.username = username;
this.password = password;
this.rootTopic = rootTopic;
this.debugLogging = debugLogging;
this.client = null;
this.devices = [];
this.MqttHost = "mqtt://Alex2MQTT.stormysdream.club";
}
connect() {
const options = {
username: this.username,
password: this.password,
};
this.client = mqtt_1.default.connect(this.MqttHost, options);
this.client.on("connect", () => {
if (this.debugLogging) {
console.log("[Alex2Node.ts] Connected to MQTT broker");
}
this.client.subscribe(this.rootTopic + "/#", (err) => {
if (this.debugLogging && !err) {
console.log("[Alex2Node.ts] Subscribed to topics");
}
if (err) {
throw err;
}
});
});
this.client.on("message", (topic, message) => {
if (this.debugLogging) {
console.log(`[Alex2Node.ts] MQTT Message Received`);
console.log(` Topic: ${topic}`);
console.log(` Payload: ${message.toString()}`);
}
if (topic == `${this.rootTopic}/discover`) {
if (this.debugLogging) {
console.log("[Alex2Node.ts] Discovery request received, getting device json...");
}
const deviceArray = this.devices.map((device) => device.getJSON());
if (deviceArray.length > 0) {
this.client.publish(topic + "_r", JSON.stringify(deviceArray), (err) => {
if (err) {
throw err;
}
else if (this.debugLogging) {
console.log(`[Alex2Node.ts] Discovery payloads published to ${topic + "_r"}`);
console.log(deviceArray);
}
});
}
}
else if (topic.split("/").length == 3) {
const [root, endpointId, directiveType] = topic.split("/");
if (directiveType != "alexaDirective") {
return;
}
const device = this.devices.find((d) => d.endpointId === endpointId);
if (!device) {
if (this.debugLogging) {
console.warn(`[Alex2Node.ts] No device found for endpointId: ${endpointId}`);
}
return;
}
let payload;
try {
payload = JSON.parse(message.toString());
}
catch (e) {
console.error(`[Alex2Node.ts] Failed to parse payload JSON`, e);
return;
}
if (payload.header &&
payload.header.namespace === "Alexa" &&
payload.header.name === "ReportState") {
if (this.debugLogging) {
console.log(`[Alex2Node.ts] ReportState directive for device ${endpointId}`);
}
device.emit("ReportState", payload);
}
else {
device.emit("Event", payload, payload.header.namespace);
}
}
});
this.client.on("error", (err) => {
if (this.debugLogging) {
console.error("[Alex2Node.ts] Connection failed:", err);
}
throw err;
});
}
registerDevice(name, endpointId) {
if (!this.client) {
throw new Error("Must call connect before creating devices");
}
if (this.debugLogging) {
console.log(`[Alex2Node.ts] Creating new device with endpoint: ${endpointId}`);
}
const device = new Device_1.default(this.client, this.rootTopic, name, endpointId);
this.devices.push(device);
return device;
}
}
exports.default = Alex2MQTT;

291
dist/AlexaInterface.js vendored Normal file
View file

@ -0,0 +1,291 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlexaInterface = exports.AlexaInterfaceType = void 0;
var AlexaInterfaceType;
(function (AlexaInterfaceType) {
AlexaInterfaceType["APPLICATION_STATE_REPORTER"] = "Alexa.ApplicationStateReporter";
AlexaInterfaceType["AUDIO_PLAY_QUEUE"] = "Alexa.Audio.PlayQueue";
AlexaInterfaceType["AUTHORIZATION_CONTROLLER"] = "Alexa.AuthorizationController";
AlexaInterfaceType["AUTOMATION_MANAGEMENT"] = "Alexa.AutomationManagement";
AlexaInterfaceType["AUTOMOTIVE_VEHICLE_DATA"] = "Alexa.Automotive.VehicleData";
AlexaInterfaceType["BRIGHTNESS_CONTROLLER"] = "Alexa.BrightnessController";
AlexaInterfaceType["CAMERA_LIVE_VIEW_CONTROLLER"] = "Alexa.Camera.LiveViewController";
AlexaInterfaceType["CAMERA_STREAM_CONTROLLER"] = "Alexa.CameraStreamController";
AlexaInterfaceType["CHANNEL_CONTROLLER"] = "Alexa.ChannelController";
AlexaInterfaceType["COLOR_CONTROLLER"] = "Alexa.ColorController";
AlexaInterfaceType["COLOR_TEMPERATURE_CONTROLLER"] = "Alexa.ColorTemperatureController";
AlexaInterfaceType["COMMISSIONABLE"] = "Alexa.Commissionable";
AlexaInterfaceType["CONSENT_MANAGEMENT_CONSENT_REQUIRED_REPORTER"] = "Alexa.ConsentManagement.ConsentRequiredReporter";
AlexaInterfaceType["CONTACT_SENSOR"] = "Alexa.ContactSensor";
AlexaInterfaceType["COOKING"] = "Alexa.Cooking";
AlexaInterfaceType["COOKING_FOOD_TEMPERATURE_CONTROLLER"] = "Alexa.Cooking.FoodTemperatureController";
AlexaInterfaceType["COOKING_FOOD_TEMPERATURE_SENSOR"] = "Alexa.Cooking.FoodTemperatureSensor";
AlexaInterfaceType["COOKING_PRESET_CONTROLLER"] = "Alexa.Cooking.PresetController";
AlexaInterfaceType["COOKING_TEMPERATURE_CONTROLLER"] = "Alexa.Cooking.TemperatureController";
AlexaInterfaceType["COOKING_TEMPERATURE_SENSOR"] = "Alexa.Cooking.TemperatureSensor";
AlexaInterfaceType["COOKING_TIME_CONTROLLER"] = "Alexa.Cooking.TimeController";
AlexaInterfaceType["DATA_CONTROLLER"] = "Alexa.DataController";
AlexaInterfaceType["DEVICE_USAGE_ESTIMATION"] = "Alexa.DeviceUsage.Estimation";
AlexaInterfaceType["DEVICE_USAGE_METER"] = "Alexa.DeviceUsage.Meter";
AlexaInterfaceType["DOORBELL_EVENT_SOURCE"] = "Alexa.DoorbellEventSource";
AlexaInterfaceType["ENDPOINT_HEALTH"] = "Alexa.EndpointHealth";
AlexaInterfaceType["EQUALIZER_CONTROLLER"] = "Alexa.EqualizerController";
AlexaInterfaceType["INPUT_CONTROLLER"] = "Alexa.InputController";
AlexaInterfaceType["INVENTORY_LEVEL_SENSOR"] = "Alexa.InventoryLevelSensor";
AlexaInterfaceType["INVENTORY_LEVEL_USAGE_SENSOR"] = "Alexa.InventoryLevelUsageSensor";
AlexaInterfaceType["INVENTORY_USAGE_SENSOR"] = "Alexa.InventoryUsageSensor";
AlexaInterfaceType["KEYPAD_CONTROLLER"] = "Alexa.KeypadController";
AlexaInterfaceType["LAUNCHER"] = "Alexa.Launcher";
AlexaInterfaceType["LOCK_CONTROLLER"] = "Alexa.LockController";
AlexaInterfaceType["MEDIA_PLAYBACK"] = "Alexa.Media.Playback";
AlexaInterfaceType["MEDIA_PLAY_QUEUE"] = "Alexa.Media.PlayQueue";
AlexaInterfaceType["MEDIA_SEARCH"] = "Alexa.Media.Search";
AlexaInterfaceType["MODE_CONTROLLER"] = "Alexa.ModeController";
AlexaInterfaceType["MOTION_SENSOR"] = "Alexa.MotionSensor";
AlexaInterfaceType["PERCENTAGE_CONTROLLER"] = "Alexa.PercentageController";
AlexaInterfaceType["PLAYBACK_CONTROLLER"] = "Alexa.PlaybackController";
AlexaInterfaceType["PLAYBACK_STATE_REPORTER"] = "Alexa.PlaybackStateReporter";
AlexaInterfaceType["POWER_CONTROLLER"] = "Alexa.PowerController";
AlexaInterfaceType["POWER_LEVEL_CONTROLLER"] = "Alexa.PowerLevelController";
AlexaInterfaceType["PROACTIVE_NOTIFICATION_SOURCE"] = "Alexa.ProactiveNotificationSource";
AlexaInterfaceType["RANGE_CONTROLLER"] = "Alexa.RangeController";
AlexaInterfaceType["RECORD_CONTROLLER"] = "Alexa.RecordController";
AlexaInterfaceType["REMOTE_VIDEO_PLAYER"] = "Alexa.RemoteVideoPlayer";
AlexaInterfaceType["RTC_SESSION_CONTROLLER"] = "Alexa.RTCSessionController";
AlexaInterfaceType["SCENE_CONTROLLER"] = "Alexa.SceneController";
AlexaInterfaceType["SECURITY_PANEL_CONTROLLER"] = "Alexa.SecurityPanelController";
AlexaInterfaceType["SECURITY_PANEL_CONTROLLER_ALERT"] = "Alexa.SecurityPanelController.Alert";
AlexaInterfaceType["SEEK_CONTROLLER"] = "Alexa.SeekController";
AlexaInterfaceType["SIMPLE_EVENT_SOURCE"] = "Alexa.SimpleEventSource";
AlexaInterfaceType["SMART_VISION_OBJECT_DETECTION_SENSOR"] = "Alexa.SmartVision.ObjectDetectionSensor";
AlexaInterfaceType["SMART_VISION_SNAPSHOT_PROVIDER"] = "Alexa.SmartVision.SnapshotProvider";
AlexaInterfaceType["SPEAKER"] = "Alexa.Speaker";
AlexaInterfaceType["STEP_SPEAKER"] = "Alexa.StepSpeaker";
AlexaInterfaceType["TEMPERATURE_SENSOR"] = "Alexa.TemperatureSensor";
AlexaInterfaceType["THERMOSTAT_CONTROLLER"] = "Alexa.ThermostatController";
AlexaInterfaceType["THERMOSTAT_CONTROLLER_CONFIGURATION"] = "Alexa.ThermostatController.Configuration";
AlexaInterfaceType["THERMOSTAT_CONTROLLER_HVAC_COMPONENTS"] = "Alexa.ThermostatController.HVAC.Components";
AlexaInterfaceType["THERMOSTAT_CONTROLLER_SCHEDULE"] = "Alexa.ThermostatController.Schedule";
AlexaInterfaceType["TIME_HOLD_CONTROLLER"] = "Alexa.TimeHoldController";
AlexaInterfaceType["TOGGLE_CONTROLLER"] = "Alexa.ToggleController";
AlexaInterfaceType["UI_CONTROLLER"] = "Alexa.UIController";
AlexaInterfaceType["USER_PREFERENCE"] = "Alexa.UserPreference";
AlexaInterfaceType["VIDEO_RECORDER"] = "Alexa.VideoRecorder";
AlexaInterfaceType["WAKE_ON_LAN_CONTROLLER"] = "Alexa.WakeOnLANController";
AlexaInterfaceType["UNKNOWN"] = "UNKNOWN";
})(AlexaInterfaceType || (exports.AlexaInterfaceType = AlexaInterfaceType = {}));
class AlexaInterface {
constructor(type, retrievable = true, proactivelyReported = false, instance = "") {
this.type = type;
this.retrievable = retrievable;
this.proactivelyReported = proactivelyReported;
this.instance = instance;
this.friendlyNames = [];
this.actionMappings = [];
}
addActionMapping(mapping) {
this.actionMappings.push(mapping);
}
addFriendlyName(name, locale) {
this.friendlyNames.push({ text: name, locale });
}
setInstance(name) {
this.instance = name;
}
getType() {
return this.type;
}
getTypeString() {
return this.type;
}
getVersion() {
switch (this.type) {
case AlexaInterfaceType.APPLICATION_STATE_REPORTER:
case AlexaInterfaceType.AUDIO_PLAY_QUEUE:
case AlexaInterfaceType.AUTHORIZATION_CONTROLLER:
case AlexaInterfaceType.AUTOMATION_MANAGEMENT:
case AlexaInterfaceType.AUTOMOTIVE_VEHICLE_DATA:
case AlexaInterfaceType.COMMISSIONABLE:
case AlexaInterfaceType.CONSENT_MANAGEMENT_CONSENT_REQUIRED_REPORTER:
case AlexaInterfaceType.COOKING:
case AlexaInterfaceType.COOKING_FOOD_TEMPERATURE_CONTROLLER:
case AlexaInterfaceType.COOKING_FOOD_TEMPERATURE_SENSOR:
case AlexaInterfaceType.COOKING_PRESET_CONTROLLER:
case AlexaInterfaceType.COOKING_TEMPERATURE_CONTROLLER:
case AlexaInterfaceType.COOKING_TEMPERATURE_SENSOR:
case AlexaInterfaceType.COOKING_TIME_CONTROLLER:
case AlexaInterfaceType.DATA_CONTROLLER:
case AlexaInterfaceType.DEVICE_USAGE_ESTIMATION:
case AlexaInterfaceType.DEVICE_USAGE_METER:
case AlexaInterfaceType.EQUALIZER_CONTROLLER:
case AlexaInterfaceType.INVENTORY_LEVEL_SENSOR:
case AlexaInterfaceType.INVENTORY_LEVEL_USAGE_SENSOR:
case AlexaInterfaceType.INVENTORY_USAGE_SENSOR:
case AlexaInterfaceType.KEYPAD_CONTROLLER:
case AlexaInterfaceType.MEDIA_PLAYBACK:
case AlexaInterfaceType.MEDIA_PLAY_QUEUE:
case AlexaInterfaceType.MEDIA_SEARCH:
case AlexaInterfaceType.PLAYBACK_STATE_REPORTER:
case AlexaInterfaceType.PROACTIVE_NOTIFICATION_SOURCE:
case AlexaInterfaceType.REMOTE_VIDEO_PLAYER:
case AlexaInterfaceType.RTC_SESSION_CONTROLLER:
case AlexaInterfaceType.SECURITY_PANEL_CONTROLLER:
case AlexaInterfaceType.SECURITY_PANEL_CONTROLLER_ALERT:
case AlexaInterfaceType.SIMPLE_EVENT_SOURCE:
case AlexaInterfaceType.SMART_VISION_OBJECT_DETECTION_SENSOR:
case AlexaInterfaceType.SMART_VISION_SNAPSHOT_PROVIDER:
case AlexaInterfaceType.SPEAKER:
case AlexaInterfaceType.STEP_SPEAKER:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_CONFIGURATION:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_HVAC_COMPONENTS:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_SCHEDULE:
case AlexaInterfaceType.UI_CONTROLLER:
case AlexaInterfaceType.USER_PREFERENCE:
case AlexaInterfaceType.VIDEO_RECORDER:
case AlexaInterfaceType.WAKE_ON_LAN_CONTROLLER:
return "1";
case AlexaInterfaceType.BRIGHTNESS_CONTROLLER:
case AlexaInterfaceType.CAMERA_STREAM_CONTROLLER:
case AlexaInterfaceType.CHANNEL_CONTROLLER:
case AlexaInterfaceType.COLOR_CONTROLLER:
case AlexaInterfaceType.COLOR_TEMPERATURE_CONTROLLER:
case AlexaInterfaceType.CONTACT_SENSOR:
case AlexaInterfaceType.DOORBELL_EVENT_SOURCE:
case AlexaInterfaceType.INPUT_CONTROLLER:
case AlexaInterfaceType.LOCK_CONTROLLER:
case AlexaInterfaceType.MODE_CONTROLLER:
case AlexaInterfaceType.MOTION_SENSOR:
case AlexaInterfaceType.PERCENTAGE_CONTROLLER:
case AlexaInterfaceType.PLAYBACK_CONTROLLER:
case AlexaInterfaceType.POWER_CONTROLLER:
case AlexaInterfaceType.POWER_LEVEL_CONTROLLER:
case AlexaInterfaceType.RANGE_CONTROLLER:
case AlexaInterfaceType.RECORD_CONTROLLER:
case AlexaInterfaceType.SCENE_CONTROLLER:
case AlexaInterfaceType.SEEK_CONTROLLER:
case AlexaInterfaceType.TEMPERATURE_SENSOR:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER:
case AlexaInterfaceType.TOGGLE_CONTROLLER:
return "3";
case AlexaInterfaceType.LAUNCHER:
return "1.1";
case AlexaInterfaceType.ENDPOINT_HEALTH:
return "3.3";
default:
return "UNKNOWN";
}
}
getProps() {
switch (this.type) {
case AlexaInterfaceType.AUTOMATION_MANAGEMENT:
return ["automationStatuses"];
case AlexaInterfaceType.BRIGHTNESS_CONTROLLER:
return ["brightness"];
case AlexaInterfaceType.CHANNEL_CONTROLLER:
return ["channel"];
case AlexaInterfaceType.COLOR_CONTROLLER:
return ["color"];
case AlexaInterfaceType.COLOR_TEMPERATURE_CONTROLLER:
return ["colorTemperatureInKelvin"];
case AlexaInterfaceType.CONTACT_SENSOR:
return ["detectionState"];
case AlexaInterfaceType.POWER_CONTROLLER:
return ["powerState"];
case AlexaInterfaceType.ENDPOINT_HEALTH:
return ["connectivity"];
case AlexaInterfaceType.TOGGLE_CONTROLLER:
return ["toggleState"];
case AlexaInterfaceType.TEMPERATURE_SENSOR:
return ["temperature"];
case AlexaInterfaceType.APPLICATION_STATE_REPORTER:
case AlexaInterfaceType.AUDIO_PLAY_QUEUE:
case AlexaInterfaceType.AUTHORIZATION_CONTROLLER:
case AlexaInterfaceType.AUTOMOTIVE_VEHICLE_DATA:
case AlexaInterfaceType.CAMERA_LIVE_VIEW_CONTROLLER:
case AlexaInterfaceType.CAMERA_STREAM_CONTROLLER:
case AlexaInterfaceType.COMMISSIONABLE:
case AlexaInterfaceType.CONSENT_MANAGEMENT_CONSENT_REQUIRED_REPORTER:
case AlexaInterfaceType.COOKING:
case AlexaInterfaceType.DATA_CONTROLLER:
case AlexaInterfaceType.DEVICE_USAGE_ESTIMATION:
case AlexaInterfaceType.DEVICE_USAGE_METER:
case AlexaInterfaceType.DOORBELL_EVENT_SOURCE:
case AlexaInterfaceType.EQUALIZER_CONTROLLER:
case AlexaInterfaceType.INPUT_CONTROLLER:
case AlexaInterfaceType.INVENTORY_LEVEL_SENSOR:
case AlexaInterfaceType.INVENTORY_LEVEL_USAGE_SENSOR:
case AlexaInterfaceType.INVENTORY_USAGE_SENSOR:
case AlexaInterfaceType.KEYPAD_CONTROLLER:
case AlexaInterfaceType.LAUNCHER:
case AlexaInterfaceType.LOCK_CONTROLLER:
case AlexaInterfaceType.MEDIA_PLAYBACK:
case AlexaInterfaceType.MEDIA_SEARCH:
case AlexaInterfaceType.MODE_CONTROLLER:
case AlexaInterfaceType.MOTION_SENSOR:
case AlexaInterfaceType.PERCENTAGE_CONTROLLER:
case AlexaInterfaceType.PLAYBACK_CONTROLLER:
case AlexaInterfaceType.PLAYBACK_STATE_REPORTER:
case AlexaInterfaceType.PROACTIVE_NOTIFICATION_SOURCE:
case AlexaInterfaceType.RANGE_CONTROLLER:
case AlexaInterfaceType.RECORD_CONTROLLER:
case AlexaInterfaceType.REMOTE_VIDEO_PLAYER:
case AlexaInterfaceType.RTC_SESSION_CONTROLLER:
case AlexaInterfaceType.SCENE_CONTROLLER:
case AlexaInterfaceType.SECURITY_PANEL_CONTROLLER:
case AlexaInterfaceType.SEEK_CONTROLLER:
case AlexaInterfaceType.SIMPLE_EVENT_SOURCE:
case AlexaInterfaceType.SMART_VISION_OBJECT_DETECTION_SENSOR:
case AlexaInterfaceType.SMART_VISION_SNAPSHOT_PROVIDER:
case AlexaInterfaceType.SPEAKER:
case AlexaInterfaceType.STEP_SPEAKER:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_CONFIGURATION:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_HVAC_COMPONENTS:
case AlexaInterfaceType.THERMOSTAT_CONTROLLER_SCHEDULE:
case AlexaInterfaceType.TIME_HOLD_CONTROLLER:
case AlexaInterfaceType.UI_CONTROLLER:
case AlexaInterfaceType.USER_PREFERENCE:
case AlexaInterfaceType.VIDEO_RECORDER:
case AlexaInterfaceType.WAKE_ON_LAN_CONTROLLER:
console.error("UNSUPORTED INTERFACE TYPE");
return [];
default:
return [];
}
}
getJSON() {
const doc = {
interface: this.getTypeString(),
version: this.getVersion(),
type: "AlexaInterface",
properties: {
retrievable: this.retrievable,
proactivelyReported: this.proactivelyReported,
supported: [{ name: this.getProps()[0] }],
},
};
if (this.instance) {
doc.instance = this.instance;
}
if (this.friendlyNames.length > 0) {
const capabilityResources = doc["capabilityResources"] || {};
const friendlyNamesArray = capabilityResources["friendlyNames"] || [];
this.friendlyNames.forEach((fn) => {
const friendlyNameObj = { "@type": "text" };
friendlyNameObj["value"] = {
text: fn.text,
locale: fn.locale,
};
friendlyNamesArray.push(friendlyNameObj);
});
doc["capabilityResources"] = capabilityResources;
}
if (this.actionMappings.length > 0) {
doc.semantics = {
actionMappings: this.actionMappings.map((am) => am.toJSON()),
};
}
return doc;
}
}
exports.AlexaInterface = AlexaInterface;

105
dist/AlexaStatusMessage.js vendored Normal file
View file

@ -0,0 +1,105 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlexaStatusMessage = exports.TemperatureSensorScale = exports.PowerController = exports.EndpointHealth = void 0;
const uuid_1 = require("uuid");
const AlexaInterface_1 = require("./AlexaInterface");
var EndpointHealth;
(function (EndpointHealth) {
EndpointHealth["OK"] = "OK";
EndpointHealth["UNREACHABLE"] = "UNREACHABLE";
})(EndpointHealth || (exports.EndpointHealth = EndpointHealth = {}));
var PowerController;
(function (PowerController) {
PowerController["ON"] = "ON";
PowerController["OFF"] = "OFF";
})(PowerController || (exports.PowerController = PowerController = {}));
var TemperatureSensorScale;
(function (TemperatureSensorScale) {
TemperatureSensorScale["CELSIUS"] = "CELSIUS";
TemperatureSensorScale["FAHRENHEIT"] = "FAHRENHEIT";
})(TemperatureSensorScale || (exports.TemperatureSensorScale = TemperatureSensorScale = {}));
class AlexaStatusMessage {
constructor(correlationToken, rootTopic, endpointId, mqttClient, isResponse = false) {
this.context = { properties: [] };
this.rootTopic = rootTopic;
this.endpointId = endpointId;
this.mqttClient = mqttClient;
this.event = {
header: {
namespace: "Alexa",
name: isResponse ? "Response" : "StateReport",
payloadVersion: "3",
messageId: this.generateMessageId(),
correlationToken,
},
endpoint: {
endpointId,
},
payload: {},
};
}
generateMessageId() {
return (0, uuid_1.v4)();
}
getTimestamp() {
return new Date().toISOString();
}
addProperty(namespace, name, value, uncertaintyInMilliseconds = 0, instance) {
const prop = {
namespace,
name,
value,
timeOfSample: this.getTimestamp(),
uncertaintyInMilliseconds,
};
if (instance)
prop.instance = instance;
this.context.properties.push(prop);
return this;
}
addHealthProp(health, uncertaintyInMs = 0) {
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.ENDPOINT_HEALTH, "connectivity", { value: health }, uncertaintyInMs);
}
addPowerControllerProp(power, uncertaintyInMs = 0) {
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.POWER_CONTROLLER, "powerState", power, uncertaintyInMs);
}
addTemperatureSensorProp(scale, value, uncertaintyInMs = 0) {
const tempValue = {
value: scale === TemperatureSensorScale.FAHRENHEIT
? (value - 32) * (5 / 9)
: value,
scale: "CELSIUS",
};
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.TEMPERATURE_SENSOR, "temperature", tempValue, uncertaintyInMs);
}
addBrightnessControllerProp(brightness, uncertaintyInMs = 0) {
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.BRIGHTNESS_CONTROLLER, "brightness", brightness, uncertaintyInMs);
}
addColorTemperatureControllerProp(colorTemp, uncertaintyInMs = 0) {
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.COLOR_TEMPERATURE_CONTROLLER, "colorTemperatureInKelvin", colorTemp, uncertaintyInMs);
}
addToggleControllerProp(state, instance, uncertaintyInMs = 0) {
return this.addProperty(AlexaInterface_1.AlexaInterfaceType.TOGGLE_CONTROLLER, "toggleState", state, uncertaintyInMs, instance);
}
addContextProp(prop) {
this.context.properties.push(prop);
return this;
}
send() {
const payload = {
context: this.context,
event: this.event,
};
const topic = `${this.rootTopic}/${this.endpointId}/alexaResponce`; //Yes this should be response but it is incorrect in both Alex2MQTT and Alex2ESP so for consistency is is wrong here too
const payloadStr = JSON.stringify(payload);
this.mqttClient.publish(topic, payloadStr, (err) => {
if (err) {
console.error("[AlexaStatusMessage] Failed to publish status message:", err);
}
else {
// console.log(`[AlexaStatusMessage] Sent message to ${topic}`);
}
});
}
}
exports.AlexaStatusMessage = AlexaStatusMessage;

98
dist/Device.js vendored Normal file
View file

@ -0,0 +1,98 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AlexaInterface_1 = require("./AlexaInterface");
const DisplayCategory_1 = require("./DisplayCategory");
const events_1 = require("events");
const AlexaStatusMessage_1 = require("./AlexaStatusMessage");
class Device extends events_1.EventEmitter {
constructor(mqttClient, rootTopic, name, endpointId, displayCategory = DisplayCategory_1.DisplayCategory.LIGHT, description = "Alexa to Node.js bridge", manufacturerName = "Alex2Node", manufacturer = "Alex2Node", model = "Alex2Node_v1.0.0") {
super();
this.mqttClient = mqttClient;
this.rootTopic = rootTopic;
this.name = name;
this.endpointId = endpointId;
this.displayCategory = displayCategory;
this.description = description;
this.manufacturerName = manufacturerName;
this.manufacturer = manufacturer;
this.model = model;
this.softwareVersion = "1.0.0";
this.capabilities = [];
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
getEndpointId() {
return this.endpointId;
}
setDisplayCategory(category) {
this.displayCategory = category;
}
getDisplayCategory() {
return this.displayCategory;
}
setDescription(description) {
this.description = description;
}
getDescription() {
return this.description;
}
getStatusMessage(correlationToken, isResponse = false) {
return new AlexaStatusMessage_1.AlexaStatusMessage(correlationToken, this.rootTopic, this.endpointId, this.mqttClient, isResponse);
}
setManufacturerName(name) {
this.manufacturerName = name;
}
getManufacturerName() {
return this.manufacturerName;
}
setManufacturer(manufacturer) {
this.manufacturer = manufacturer;
}
getManufacturer() {
return this.manufacturer;
}
setModel(model) {
this.model = model;
}
getModel() {
return this.model;
}
getSoftwareVersion() {
return this.softwareVersion;
}
addCapability(type) {
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(
// `Created new interface with type: ${newCapability.getTypeString()}`
// );
return newCapability;
}
getJSON() {
return {
endpointId: this.endpointId,
friendlyName: this.name,
description: this.description,
manufacturerName: this.manufacturerName,
displayCategories: [this.displayCategory],
additionalAttributes: {
manufacturer: this.manufacturer,
model: this.model,
serialNumber: "Alex2Node",
firmwareVersion: "1.0.0",
softwareVersion: this.softwareVersion,
customIdentifier: "Alex2Node",
},
capabilities: this.capabilities.map((cap) => cap.getJSON()),
};
}
}
exports.default = Device;

62
dist/DisplayCategory.js vendored Normal file
View file

@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DisplayCategory = void 0;
var DisplayCategory;
(function (DisplayCategory) {
DisplayCategory["ACTIVITY_TRIGGER"] = "ACTIVITY_TRIGGER";
DisplayCategory["AIR_CONDITIONER"] = "AIR_CONDITIONER";
DisplayCategory["AIR_FRESHENER"] = "AIR_FRESHENER";
DisplayCategory["AIR_PURIFIER"] = "AIR_PURIFIER";
DisplayCategory["AIR_QUALITY_MONITOR"] = "AIR_QUALITY_MONITOR";
DisplayCategory["ALEXA_VOICE_ENABLED"] = "ALEXA_VOICE_ENABLED";
DisplayCategory["AUTO_ACCESSORY"] = "AUTO_ACCESSORY";
DisplayCategory["BLUETOOTH_SPEAKER"] = "BLUETOOTH_SPEAKER";
DisplayCategory["CAMERA"] = "CAMERA";
DisplayCategory["CHRISTMAS_TREE"] = "CHRISTMAS_TREE";
DisplayCategory["COFFEE_MAKER"] = "COFFEE_MAKER";
DisplayCategory["COMPUTER"] = "COMPUTER";
DisplayCategory["CONTACT_SENSOR"] = "CONTACT_SENSOR";
DisplayCategory["DISHWASHER"] = "DISHWASHER";
DisplayCategory["DOOR"] = "DOOR";
DisplayCategory["DOORBELL"] = "DOORBELL";
DisplayCategory["DRYER"] = "DRYER";
DisplayCategory["EXTERIOR_BLIND"] = "EXTERIOR_BLIND";
DisplayCategory["FAN"] = "FAN";
DisplayCategory["GAME_CONSOLE"] = "GAME_CONSOLE";
DisplayCategory["GARAGE_DOOR"] = "GARAGE_DOOR";
DisplayCategory["HEADPHONES"] = "HEADPHONES";
DisplayCategory["HUB"] = "HUB";
DisplayCategory["INTERIOR_BLIND"] = "INTERIOR_BLIND";
DisplayCategory["LAPTOP"] = "LAPTOP";
DisplayCategory["LIGHT"] = "LIGHT";
DisplayCategory["MICROWAVE"] = "MICROWAVE";
DisplayCategory["MOBILE_PHONE"] = "MOBILE_PHONE";
DisplayCategory["MOTION_SENSOR"] = "MOTION_SENSOR";
DisplayCategory["MUSIC_SYSTEM"] = "MUSIC_SYSTEM";
DisplayCategory["NETWORK_HARDWARE"] = "NETWORK_HARDWARE";
DisplayCategory["OTHER"] = "OTHER";
DisplayCategory["OVEN"] = "OVEN";
DisplayCategory["PHONE"] = "PHONE";
DisplayCategory["PRINTER"] = "PRINTER";
DisplayCategory["REMOTE"] = "REMOTE";
DisplayCategory["ROUTER"] = "ROUTER";
DisplayCategory["SCENE_TRIGGER"] = "SCENE_TRIGGER";
DisplayCategory["SCREEN"] = "SCREEN";
DisplayCategory["SECURITY_PANEL"] = "SECURITY_PANEL";
DisplayCategory["SECURITY_SYSTEM"] = "SECURITY_SYSTEM";
DisplayCategory["SLOW_COOKER"] = "SLOW_COOKER";
DisplayCategory["SMARTLOCK"] = "SMARTLOCK";
DisplayCategory["SMARTPLUG"] = "SMARTPLUG";
DisplayCategory["SPEAKER"] = "SPEAKER";
DisplayCategory["STREAMING_DEVICE"] = "STREAMING_DEVICE";
DisplayCategory["SWITCH"] = "SWITCH";
DisplayCategory["TABLET"] = "TABLET";
DisplayCategory["TEMPERATURE_SENSOR"] = "TEMPERATURE_SENSOR";
DisplayCategory["THERMOSTAT"] = "THERMOSTAT";
DisplayCategory["TV"] = "TV";
DisplayCategory["VACUUM_CLEANER"] = "VACUUM_CLEANER";
DisplayCategory["VEHICLE"] = "VEHICLE";
DisplayCategory["WASHER"] = "WASHER";
DisplayCategory["WATER_HEATER"] = "WATER_HEATER";
DisplayCategory["WEARABLE"] = "WEARABLE";
})(DisplayCategory || (exports.DisplayCategory = DisplayCategory = {}));

15
dist/index.js vendored Normal file
View file

@ -0,0 +1,15 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PowerController = exports.AlexaActions = exports.ActionMapping = exports.AlexaInterfaceType = exports.Alex2MQTT = void 0;
var Alex2Node_1 = require("./Alex2Node");
Object.defineProperty(exports, "Alex2MQTT", { enumerable: true, get: function () { return __importDefault(Alex2Node_1).default; } });
var AlexaInterface_1 = require("./AlexaInterface");
Object.defineProperty(exports, "AlexaInterfaceType", { enumerable: true, get: function () { return AlexaInterface_1.AlexaInterfaceType; } });
var ActionMapping_1 = require("./ActionMapping");
Object.defineProperty(exports, "ActionMapping", { enumerable: true, get: function () { return ActionMapping_1.ActionMapping; } });
Object.defineProperty(exports, "AlexaActions", { enumerable: true, get: function () { return ActionMapping_1.AlexaActions; } });
var AlexaStatusMessage_1 = require("./AlexaStatusMessage");
Object.defineProperty(exports, "PowerController", { enumerable: true, get: function () { return AlexaStatusMessage_1.PowerController; } });