UNPKG

dt-common-device

Version:

A secure and robust device management library for IoT applications

185 lines (184 loc) 9.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : ""; return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceEventHandler = void 0; const IAuditProperties_1 = require("../audit/IAuditProperties"); const constants_1 = require("../constants"); const Event_1 = require("../constants/Event"); const services_1 = require("../entities/device/local/services"); const BaseEventHandler_1 = require("./BaseEventHandler"); const typedi_1 = __importStar(require("typedi")); let DeviceEventHandler = (() => { let _classDecorators = [(0, typedi_1.Service)()]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; let _classSuper = BaseEventHandler_1.BaseEventHandler; var DeviceEventHandler = _classThis = class extends _classSuper { constructor() { super([ Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE, Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE, Event_1.DT_EVENT_TYPES.DEVICE.STATUS.UNKNOWN, Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.REPLACED, Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LEVEL.UNKNOWN, Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LEVEL.LOW, Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LEVEL.CRITICAL, Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LEVEL.UPDATED, ], 100); // Use dependency injection instead of creating new instance this.localHubService = typedi_1.default.get(services_1.LocalHubService); } async onEvent(event) { this.logger.info("[DT | CDL]:[DeviceEventHandler]: Processing event", { eventName: event.eventName, }); try { const device = await this.localDeviceService.getDevice(event.deviceId); if (!device?.deviceId) { throw new Error("[DT | CDL]:[DeviceEventHandler]: deviceId does not exist"); } await this.updateDeviceEventData(device, event); } catch (error) { this.logger.error(`[DT | CDL]:[DeviceEventHandler]: Error processing event: ${event.eventName} ERROR: ${error}`); throw error; } } async updateDeviceEventData(deviceData, event) { switch (event.eventName) { case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE: case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE: await this.localDeviceService.setState(deviceData.deviceId, { status: { online: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE, lastUpdated: new Date().toISOString(), error: { type: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE ? "offline" : "", message: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE ? "Device is offline" : "", default: {}, }, }, }, { deviceId: deviceData.deviceId, deviceName: deviceData.name, propertyId: deviceData.propertyId, resource: IAuditProperties_1.Resource.DEVICE, source: constants_1.Source.CLOUD_EVENT, }); break; case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LEVEL.UPDATED: await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0, constants_1.Source.CLOUD_EVENT, { deviceId: deviceData.deviceId, deviceName: deviceData.name, propertyId: deviceData.propertyId, resource: IAuditProperties_1.Resource.DEVICE, source: constants_1.Source.CLOUD_EVENT, }); break; } } async extractHubDetails(device) { const hubIds = device?.hubId; const hubDetails = []; if (hubIds) { for (const hubId of hubIds) { const hubDetail = await this.localHubService.getHub(hubId); hubDetails.push({ id: hubDetail?.deviceId, name: hubDetail?.name, type: hubDetail?.deviceType?.type, brand: hubDetail?.specifications?.manufacturer, model: hubDetail?.specifications?.model, status: hubDetail?.status?.online ? "ONLINE" : "OFFLINE", }); } } return hubDetails; } }; __setFunctionName(_classThis, "DeviceEventHandler"); (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); DeviceEventHandler = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); __runInitializers(_classThis, _classExtraInitializers); })(); return DeviceEventHandler = _classThis; })(); exports.DeviceEventHandler = DeviceEventHandler;