@softwaredevelopment/node-red-contrib-bodhi-device-occupancy-sensor
Version:
Occupancy Sensor device for bodhi node red
84 lines • 3.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoteDeviceService = 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 RemoteDeviceService {
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.validateRemoteDevice)(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;
}
this._device.sendCommandToBodhi(data, 'remote');
}
validateEventData(payload) {
if (!payload)
return true;
return payload.id.aid === this._device.assetId || payload.id.guid === this._device.guid;
}
handleDeviceUpdate(payload) {
const outputData = (0, constants_1.handleRemoteBaseDeviceUpdate)(payload.req, payload.val, payload.id);
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.RemoteDeviceService = RemoteDeviceService;
//# sourceMappingURL=remote.device.service.js.map