"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"); const uuid_1 = require("uuid"); const AlexaErrorResponse_1 = require("./AlexaErrorResponse"); class Device extends events_1.EventEmitter { constructor(mqttClient, rootTopic, name, endpointId, displayCategory, 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 = Array.isArray(category) ? category : [category]; } getDisplayCategory() { return this.displayCategory || [DisplayCategory_1.DisplayCategory.LIGHT]; } setDescription(description) { this.description = description; } getDescription() { return this.description; } getErrorMessage(correlationToken) { return new AlexaErrorResponse_1.AlexaErrorResponse(correlationToken, this.rootTopic, this.endpointId, this.mqttClient); } getStatusMessage(correlationToken, isResponse = false, isDeferred = false) { return new AlexaStatusMessage_1.AlexaStatusMessage(correlationToken, this.rootTopic, this.endpointId, this.mqttClient, isResponse, isDeferred); } /** * A proactive Alexa.ChangeReport (1.5.1): add the changed properties (the default target), optionally * .unchanged() then the others, and .send() - it goes to /changeReport, which Alex2MQTT forwards to the Alexa * event gateway with the user's token. Alexa only accepts it for capabilities registered with proactivelyReported. */ getChangeReport(cause = "PHYSICAL_INTERACTION") { return new AlexaStatusMessage_1.AlexaStatusMessage("", this.rootTopic, this.endpointId, this.mqttClient, false, false, cause); } /** * Alexa.SceneController: answer an Activate / Deactivate directive with ActivationStarted / DeactivationStarted * (1.5.1). Resolves with the topic published to. */ sendSceneResponse(correlationToken, activated, cause = "VOICE_INTERACTION", sendAsync = false) { const payload = { context: {}, event: { header: { namespace: "Alexa.SceneController", name: activated ? "ActivationStarted" : "DeactivationStarted", messageId: (0, uuid_1.v4)(), correlationToken, payloadVersion: "3" }, endpoint: { endpointId: this.endpointId }, payload: { cause: { type: cause }, timestamp: new Date().toISOString() }, }, }; const topic = `${this.rootTopic}/${this.endpointId}/${sendAsync ? "deferredResponse" : "alexaResponce"}`; return new Promise((resolve, reject) => { this.mqttClient.publish(topic, JSON.stringify(payload), (err) => (err ? reject(err) : resolve(topic))); }); } getCapabilities() { return this.capabilities.slice(); } 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; } /** Add a capability. 1.5.1: options.retrievable / proactivelyReported / instance (a change report needs proactivelyReported). */ addCapability(type, options = {}) { var _a, _b, _c; const newCapability = new AlexaInterface_1.AlexaInterface(type, (_a = options.retrievable) !== null && _a !== void 0 ? _a : true, (_b = options.proactivelyReported) !== null && _b !== void 0 ? _b : false, (_c = options.instance) !== null && _c !== void 0 ? _c : ""); 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.getDisplayCategory(), 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;