UNPKG

@softwaredevelopment/node-red-contrib-bodhi-device-occupancy-sensor

Version:

Occupancy Sensor device for bodhi node red

68 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const events_manager_1 = require("../lib/events.manager"); const constants_1 = require("../lib/constants"); const local_device_service_1 = require("../local.device.service"); const utils_1 = require("../lib/utils"); const types_1 = require("../lib/types"); module.exports = (RED) => { function bodhiDevice(config) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore RED.nodes.createNode(this, config); this.status(constants_1.CONNECTION_STATUS.Disconnected); this.name = config.name; this.location = config.location; this.connector = config.connector; this.logLevel = config.logLevel; this.ignoreValidation = config.ignoreValidation; const logger = new utils_1.CustomLogger(this.name, this.logLevel); logger.level = this.logLevel; logger.debug('Initializing device service'); // eslint-disable-next-line dot-notation const redSettingsFilePath = RED.settings['settingsFile']; utils_1.fileStorageManager.storagePath = redSettingsFilePath; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const connectorNode = RED.nodes.getNode(config.connector); if (!connectorNode) { this.status(constants_1.CONNECTION_STATUS.Invalid); return; } this.status(constants_1.CONNECTION_STATUS.Disconnected); // Access the connector node and its configuration const events = new events_manager_1.EventManager(); events.bodhiEvents = connectorNode.bodhiEvents; events.emitterType = types_1.EmitterType.DEVICE; const bodhiDevice = new local_device_service_1.LocalDeviceService({ nodeId: config.id, name: config.name, location: config.location, }, { ignoreValidation: config.ignoreValidation, logger, redNodeContext: this, }, events); logger.debug('Local Device Service created'); this.on('input', (msg, send, done) => { logger.debug(`Input received at node with message = ${JSON.stringify(msg)}`); bodhiDevice.handleInput(msg.payload); if (done) { done(); } }); connectorNode.bodhiEvents.on(String(types_1.EventNames.DATA_FOR_DEVICE), (eventData) => { const data = JSON.parse(eventData); const evetPayload = data.payload; logger.debug(`Validating event for device=> ${JSON.stringify(evetPayload === null || evetPayload === void 0 ? void 0 : evetPayload.id)} and result is=> ${bodhiDevice.validateEventData(evetPayload)}`); if (bodhiDevice.validateEventData(evetPayload)) { if (data.deviceCategory.toLowerCase() === 'local' || data.deviceCategory === 'all') bodhiDevice.handleEvent(data); } }); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore RED.nodes.registerType(types_1.RedNodeNames.OCCUPANCY_SENSOR, bodhiDevice); }; //# sourceMappingURL=local.device.node.js.map