UNPKG

node-red-contrib-home-assistant-websocket

Version:
60 lines (59 loc) 2.76 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _OutputController_instances, _OutputController_preOnClose; Object.defineProperty(exports, "__esModule", { value: true }); const node_1 = require("../../helpers/node"); const Events_1 = require("../events/Events"); class OutputController { constructor({ homeAssistant, jsonataService, nodeRedContextService, node, status, typedInputService, }) { var _a, _b, _c; _OutputController_instances.add(this); this.homeAssistant = homeAssistant; this.contextService = nodeRedContextService; this.jsonataService = jsonataService; this.node = node; this.status = status; this.typedInputService = typedInputService; node.on(Events_1.NodeEvent.Close, __classPrivateFieldGet(this, _OutputController_instances, "m", _OutputController_preOnClose).bind(this)); const name = (_c = (_b = (_a = this.node) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : 'undefined'; node.debug(`instantiated node, name: ${name}`); } debugToClient(message) { (0, node_1.debugToClient)(this.node, message); } async setCustomOutputs(properties = [], message, extras) { for (const item of properties) { const value = await this.typedInputService.getValue(item.value, item.valueType, { message, ...extras, }); try { this.contextService.set(value, item.propertyType, item.property, message); } catch (e) { this.node.warn(`Custom Ouput Error (${item.propertyType}:${item.property}): ${e}`); } } } } _OutputController_instances = new WeakSet(), _OutputController_preOnClose = function _OutputController_preOnClose(removed, done) { var _a; this.node.debug(`closing node. Reason: ${removed ? 'node deleted' : 'node re-deployed'}`); try { (_a = this.onClose) === null || _a === void 0 ? void 0 : _a.call(this, removed); done(); } catch (e) { if (e instanceof Error) { done(e); } else { done(new Error(e)); } } }; exports.default = OutputController;