@softwaredevelopment/node-red-contrib-bodhi-device-occupancy-sensor
Version:
Occupancy Sensor device for bodhi node red
69 lines • 3.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../lib/constants");
const remote_device_service_1 = require("../remote.device.service");
const utils_1 = require("../lib/utils");
const types_1 = require("../lib/types");
const events_manager_1 = require("../lib/events.manager");
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 { bodhiEvents } = connectorNode;
const events = new events_manager_1.EventManager();
events.bodhiEvents = bodhiEvents;
events.emitterType = types_1.EmitterType.DEVICE;
const bodhiDevice = new remote_device_service_1.RemoteDeviceService({
nodeId: config.id,
name: config.name,
location: config.location,
}, {
ignoreValidation: config.ignoreValidation,
logger,
redNodeContext: this,
}, events);
logger.debug('Remote 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();
}
});
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() === 'remote' || data.deviceCategory === 'all')
bodhiDevice.handleEvent(data);
}
});
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
RED.nodes.registerType(`Remote ${types_1.RedNodeNames.OCCUPANCY_SENSOR}`, bodhiDevice);
};
//# sourceMappingURL=remote.device.node.js.map
;