UNPKG

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

Version:

Occupancy Sensor device for bodhi node red

90 lines 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalDeviceService = void 0; const bodhi_device_1 = require("./lib/bodhi-device"); const types_1 = require("./lib/types"); const constants_1 = require("./lib/constants"); const helper_service_1 = require("./utils/helper.service"); class LocalDeviceService { constructor(registerOptions, options, events) { this._device = new bodhi_device_1.BodhiBaseDevice({ guid: registerOptions.nodeId, cid: registerOptions.connectorId, dl: registerOptions.location, dn: registerOptions.name, dt: String(types_1.DeviceType.OccupancySensor), dc: '0', }, options.logger, events); this._logger = options.logger; this._nodeContext = options.redNodeContext; this._ignoreValidation = options.ignoreValidation; this._logger.debug(`Got the device registration props => ${JSON.stringify(registerOptions)}`); } get nodeContext() { return this._nodeContext; } handleDeviceCreation(assetId) { this._logger.debug(`Device created in bodhi and retreived asset ID successfully`); this._device.assetId = assetId; } handleInput(data) { this._logger.debug(`Updating the device and sending updated value to bodhi. Selected value is: ${JSON.stringify(data)}`); const unknownKeys = (0, helper_service_1.validateLocalDevice)(data); if (this._ignoreValidation === false && unknownKeys.length > 0) { this._logger.error(`Invalid input received, following unknown keys are passed in input: "${unknownKeys.join(', ')}". Please consider using ignore validation toggle if you want to pass unknown keys`); this.nodeContext.status(constants_1.CONNECTION_STATUS.InvalidInput); setTimeout(() => { this.nodeContext.status(constants_1.CONNECTION_STATUS.Connected); }, 5000); return; } const dataToSend = Object.assign(Object.assign({}, data), { offlineFb: data.offlineFb || false }); this._device.sendCommandToBodhi(dataToSend, 'local'); } validateEventData(payload) { if (!payload) return true; return payload.id.aid === this._device.assetId || payload.id.guid === this._device.guid; } handleDeviceUpdate(payload) { var _a; const outputData = { message: 'Device got an update from bodhi', asset_id: (_a = payload.id) === null || _a === void 0 ? void 0 : _a.aid, data: { [payload.req]: payload.val }, }; this.nodeContext.send({ payload: outputData }); this._logger.debug(`Bodhi device updated ${JSON.stringify(outputData)}`); } handleEvent(data) { const { topic } = data; const payload = data.payload; this._logger.info(`Event received on topic '${topic}' with data = ${JSON.stringify(payload)}`); switch (topic) { case 'system_ready': this._device.systemReady = true; this.nodeContext.status(constants_1.CONNECTION_STATUS.Connected); break; case 'device_created_in_bodhi': this.handleDeviceCreation(payload.id.aid); break; case 'partial_connected': this.nodeContext.status(constants_1.CONNECTION_STATUS.PartialConnected); break; case 'fully_connected': this.nodeContext.status(constants_1.CONNECTION_STATUS.Connected); break; case 'disconnected': this.nodeContext.status(constants_1.CONNECTION_STATUS.Disconnected); break; case 'bodhi_device_updated': this.handleDeviceUpdate(payload); break; default: this._logger.warn(`Unhandled event received on topic '${topic}' with data = ${JSON.stringify(payload)}`); break; } } } exports.LocalDeviceService = LocalDeviceService; //# sourceMappingURL=local.device.service.js.map