@softwaredevelopment/node-red-contrib-bodhi-device-occupancy-sensor
Version:
Occupancy Sensor device for bodhi node red
118 lines • 4.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BodhiBaseDevice = void 0;
const utils_1 = require("./utils");
class BodhiBaseDevice {
constructor(deviceId, logger, events) {
this._deviceId = deviceId;
this._systemReady = false;
this._logger = logger;
this._events = events;
this._logger.debug(`Got the id object for device creation = ${JSON.stringify(deviceId)}`);
if (utils_1.fileStorageManager.ifKeyExists(this._deviceId.guid)) {
const oldDeviceData = this.retrieveDeviceDataFromStorage();
const updatedDevicedata = Object.assign(Object.assign({}, oldDeviceData), this._deviceId);
this._deviceId = updatedDevicedata;
utils_1.fileStorageManager.sendStringToStorage(this._deviceId.guid, JSON.stringify(updatedDevicedata));
}
else {
utils_1.fileStorageManager.sendStringToStorage(this._deviceId.guid, JSON.stringify(this._deviceId));
}
this._logger.debug(`Updated the id object from existing or created new device = ${JSON.stringify(this._deviceId)}`);
}
validateDeviceForAssetCreation() {
this._logger.debug(`ID object for device = ${JSON.stringify(this._deviceId)}`);
if (this._deviceId.aid) {
this._logger.debug(`No need to validate the device info as asset already created in bodhi.`);
return true;
}
this._logger.debug(`Validating the request for asset creation.`);
if (!this._deviceId.dn || this._deviceId.dn === '') {
this._logger.error(`Device Name is not provided in config. Please add name in config for creating the device asset in bodhi.`);
return false;
}
if (!this._deviceId.dl || this._deviceId.dl === '') {
this._logger.error(`Device Location is not provided in config. Please add location in config for creating the device asset in bodhi.`);
return false;
}
return true;
}
sendCommandToBodhi(deviceInfo, deviceCategory) {
const infoObjectToSend = Object.assign(Object.assign({}, deviceInfo), { id: this.generateDeviceIdObject() });
if (this.systemReady) {
if (this.validateDeviceForAssetCreation())
this._events.emitEvent('device_data', infoObjectToSend, this._logger, deviceCategory);
}
else {
this._logger.warn(`System/connector is not ready for recieving the device data yet. Kindly add the connector in dashboard to get ready`);
}
}
generateDeviceIdObject() {
if (this.assetId && this.assetId !== '')
return { aid: this.assetId };
return this._deviceId;
}
get guid() {
return this._deviceId.guid;
}
set assetId(value) {
this._deviceId.aid = value;
this.updateValueInStorage('aid', value);
}
get assetId() {
return this._deviceId.aid;
}
get systemReady() {
return this._systemReady;
}
set systemReady(value) {
this._systemReady = value;
}
get connectorId() {
return this._deviceId.cid;
}
set connectorId(value) {
this._deviceId.cid = value;
this.updateValueInStorage('cid', value);
}
get deviceName() {
return this._deviceId.dn;
}
set deviceName(value) {
this._deviceId.dn = value;
this.updateValueInStorage('dn', value);
}
get deviceLocation() {
return this._deviceId.dl;
}
set deviceLocation(value) {
this._deviceId.dl = value;
this.updateValueInStorage('dl', value);
}
get deviceType() {
return this._deviceId.dt;
}
set deviceType(value) {
this._deviceId.dt = value;
this.updateValueInStorage('dt', value);
}
get deviceClass() {
return this._deviceId.dc;
}
set deviceClass(value) {
this._deviceId.dc = value;
this.updateValueInStorage('dc', value);
}
updateValueInStorage(key, value) {
const storedDeviceData = this.retrieveDeviceDataFromStorage();
storedDeviceData[key] = value;
utils_1.fileStorageManager.sendStringToStorage(this._deviceId.guid, JSON.stringify(storedDeviceData));
this._logger.debug(`Value updated in storage: ${key} - ${value}`);
}
retrieveDeviceDataFromStorage() {
const storedData = utils_1.fileStorageManager.retrieveStringFromStorage(this._deviceId.guid);
return storedData ? JSON.parse(storedData) : this._deviceId;
}
}
exports.BodhiBaseDevice = BodhiBaseDevice;
//# sourceMappingURL=bodhi-device.js.map