dt-common-device 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TROUBLESHOOTING.md +184 -0
- package/dist/config/config.d.ts +9 -2
- package/dist/config/config.js +97 -14
- package/dist/constants/Event.d.ts +75 -0
- package/dist/constants/Event.js +78 -0
- package/dist/db/db.d.ts +1 -0
- package/dist/db/db.js +18 -2
- package/dist/device/local/entities/AlertBuilder.d.ts +87 -0
- package/dist/device/local/entities/AlertBuilder.example.d.ts +11 -0
- package/dist/device/local/entities/AlertBuilder.example.js +117 -0
- package/dist/device/local/entities/AlertBuilder.js +179 -0
- package/dist/device/local/entities/IssueBuilder.d.ts +109 -0
- package/dist/device/local/entities/IssueBuilder.example.d.ts +16 -0
- package/dist/device/local/entities/IssueBuilder.example.js +196 -0
- package/dist/device/local/entities/IssueBuilder.js +237 -0
- package/dist/device/local/entities/index.d.ts +2 -0
- package/dist/device/local/entities/index.js +7 -0
- package/dist/device/local/interfaces/IDevice.d.ts +10 -9
- package/dist/device/local/interfaces/IDevice.js +7 -0
- package/dist/device/local/models/Alert.model.d.ts +28 -0
- package/dist/device/local/models/Alert.model.js +222 -0
- package/dist/device/local/models/Issue.model.d.ts +28 -0
- package/dist/device/local/models/Issue.model.js +260 -0
- package/dist/device/local/repository/Alert.repository.d.ts +106 -0
- package/dist/device/local/repository/Alert.repository.js +374 -0
- package/dist/device/local/repository/Device.repository.d.ts +10 -2
- package/dist/device/local/repository/Device.repository.js +153 -30
- package/dist/device/local/repository/Hub.repository.d.ts +1 -1
- package/dist/device/local/repository/Hub.repository.js +60 -18
- package/dist/device/local/repository/Issue.repository.d.ts +113 -0
- package/dist/device/local/repository/Issue.repository.js +401 -0
- package/dist/device/local/repository/Schedule.repository.d.ts +1 -1
- package/dist/device/local/repository/Schedule.repository.js +14 -18
- package/dist/device/local/services/Alert.service.d.ts +135 -5
- package/dist/device/local/services/Alert.service.js +471 -7
- package/dist/device/local/services/AlertService.example.d.ts +55 -0
- package/dist/device/local/services/AlertService.example.js +148 -0
- package/dist/device/local/services/Device.service.d.ts +8 -5
- package/dist/device/local/services/Device.service.js +58 -40
- package/dist/device/local/services/Issue.service.d.ts +168 -0
- package/dist/device/local/services/Issue.service.js +642 -0
- package/dist/device/local/services/IssueService.example.d.ts +68 -0
- package/dist/device/local/services/IssueService.example.js +177 -0
- package/dist/device/local/services/index.d.ts +7 -5
- package/dist/device/local/services/index.js +21 -11
- package/dist/events/BaseEventHandler.d.ts +43 -0
- package/dist/events/BaseEventHandler.js +111 -0
- package/dist/events/BaseEventTransformer.d.ts +26 -0
- package/dist/events/BaseEventTransformer.js +72 -0
- package/dist/events/DeviceEventHandler.d.ts +15 -0
- package/dist/events/DeviceEventHandler.js +152 -0
- package/dist/events/DeviceEventTransformerFactory.d.ts +27 -0
- package/dist/events/DeviceEventTransformerFactory.js +116 -0
- package/dist/events/EventHandler.d.ts +11 -0
- package/dist/events/EventHandler.js +106 -0
- package/dist/events/EventHandlerOrchestrator.d.ts +35 -0
- package/dist/events/EventHandlerOrchestrator.js +141 -0
- package/dist/events/EventProcessingService.d.ts +43 -0
- package/dist/events/EventProcessingService.js +243 -0
- package/dist/events/InternalEventSubscription.d.ts +44 -0
- package/dist/events/InternalEventSubscription.js +152 -0
- package/dist/events/index.d.ts +9 -0
- package/dist/events/index.js +21 -0
- package/dist/events/interfaces/DeviceEvent.d.ts +48 -0
- package/dist/events/interfaces/DeviceEvent.js +2 -0
- package/dist/events/interfaces/IEventHandler.d.ts +23 -0
- package/dist/events/interfaces/IEventHandler.js +2 -0
- package/dist/events/interfaces/IEventTransformer.d.ts +7 -0
- package/dist/events/interfaces/IEventTransformer.js +2 -0
- package/dist/events/interfaces/IInternalEvent.d.ts +42 -0
- package/dist/events/interfaces/IInternalEvent.js +2 -0
- package/dist/events/interfaces/index.d.ts +4 -0
- package/dist/events/interfaces/index.js +20 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +9 -2
- package/dist/types/alert.types.d.ts +57 -0
- package/dist/types/alert.types.js +22 -0
- package/dist/types/config.types.d.ts +15 -4
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/issue.types.d.ts +90 -0
- package/dist/types/issue.types.js +40 -0
- package/dist/utils/http-utils.d.ts +13 -0
- package/dist/utils/http-utils.js +117 -0
- package/package.json +2 -1
- package/src/config/config.ts +117 -14
- package/src/{device/local/events/Events.ts → constants/Event.ts} +34 -13
- package/src/db/db.ts +14 -5
- package/src/device/local/entities/AlertBuilder.example.ts +126 -0
- package/src/device/local/entities/AlertBuilder.ts +202 -0
- package/src/device/local/entities/IssueBuilder.example.ts +210 -0
- package/src/device/local/entities/IssueBuilder.ts +263 -0
- package/src/device/local/entities/README.md +173 -0
- package/src/device/local/entities/index.ts +2 -0
- package/src/device/local/interfaces/IDevice.ts +11 -9
- package/src/device/local/models/Alert.model.md +319 -0
- package/src/device/local/models/Alert.model.ts +283 -0
- package/src/device/local/models/Issue.model.md +386 -0
- package/src/device/local/models/Issue.model.ts +350 -0
- package/src/device/local/models/README.md +312 -0
- package/src/device/local/repository/Alert.repository.ts +465 -0
- package/src/device/local/repository/Device.repository.ts +241 -32
- package/src/device/local/repository/Hub.repository.ts +74 -18
- package/src/device/local/repository/Issue.repository.ts +517 -0
- package/src/device/local/repository/Schedule.repository.ts +28 -22
- package/src/device/local/services/Alert.service.ts +617 -5
- package/src/device/local/services/AlertService.example.ts +229 -0
- package/src/device/local/services/Device.service.ts +70 -50
- package/src/device/local/services/Issue.service.ts +872 -0
- package/src/device/local/services/IssueService.example.ts +307 -0
- package/src/device/local/services/index.ts +7 -5
- package/src/events/BaseEventHandler.ts +145 -0
- package/src/events/BaseEventTransformer.ts +97 -0
- package/src/events/DeviceEventHandler.ts +211 -0
- package/src/events/DeviceEventTransformerFactory.ts +77 -0
- package/src/{device/local/events → events}/EventHandler.ts +19 -15
- package/src/events/EventHandlerOrchestrator.ts +119 -0
- package/src/events/EventProcessingService.ts +248 -0
- package/src/events/InternalEventSubscription.ts +219 -0
- package/src/events/index.ts +9 -0
- package/src/events/interfaces/DeviceEvent.ts +56 -0
- package/src/events/interfaces/IEventHandler.ts +28 -0
- package/src/events/interfaces/IEventTransformer.ts +8 -0
- package/src/events/interfaces/IInternalEvent.ts +47 -0
- package/src/events/interfaces/index.ts +4 -0
- package/src/index.ts +9 -2
- package/src/types/alert.types.ts +64 -0
- package/src/types/config.types.ts +17 -4
- package/src/types/index.ts +2 -0
- package/src/types/issue.types.ts +98 -0
- package/src/utils/http-utils.ts +143 -0
- package/src/device/local/events/index.ts +0 -2
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeviceEventHandler = void 0;
|
|
4
|
+
const Event_1 = require("../constants/Event");
|
|
5
|
+
const services_1 = require("../device/local/services");
|
|
6
|
+
const BaseEventHandler_1 = require("./BaseEventHandler");
|
|
7
|
+
class DeviceEventHandler extends BaseEventHandler_1.BaseEventHandler {
|
|
8
|
+
constructor() {
|
|
9
|
+
super([
|
|
10
|
+
Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
|
|
11
|
+
Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE,
|
|
12
|
+
Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED,
|
|
13
|
+
Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL,
|
|
14
|
+
Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW,
|
|
15
|
+
Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED,
|
|
16
|
+
], 100);
|
|
17
|
+
this.localHubService = new services_1.LocalHubService();
|
|
18
|
+
}
|
|
19
|
+
async onEvent(event) {
|
|
20
|
+
this.logger.info("[DT | CDL]:[DeviceEventHandler]: Processing event", {
|
|
21
|
+
eventName: event.eventName,
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
const device = await this.localDeviceService.getDevice(event.deviceId);
|
|
25
|
+
if (!device?.deviceId) {
|
|
26
|
+
throw new Error("[DT | CDL]:[DeviceEventHandler]: deviceId does not exist");
|
|
27
|
+
}
|
|
28
|
+
await this.updateDeviceEventData(device, event);
|
|
29
|
+
// Update relevant tables based on event type
|
|
30
|
+
await this.updateTablesBasedOnEventType(event, device);
|
|
31
|
+
// Save event logs
|
|
32
|
+
await this.dumpToEventOperation(event, device);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error processing event", error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async updateDeviceEventData(deviceData, event) {
|
|
40
|
+
switch (event.eventName) {
|
|
41
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
|
|
42
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
|
|
43
|
+
await this.localDeviceService.setState(deviceData.deviceId, {
|
|
44
|
+
status: {
|
|
45
|
+
online: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
|
|
46
|
+
lastUpdated: new Date().toISOString(),
|
|
47
|
+
error: {
|
|
48
|
+
type: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
|
|
49
|
+
? "offline"
|
|
50
|
+
: "",
|
|
51
|
+
message: event.eventName === Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
|
|
52
|
+
? "Device is offline"
|
|
53
|
+
: "",
|
|
54
|
+
default: {},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
break;
|
|
59
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
|
|
60
|
+
await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async extractHubDetails(device) {
|
|
65
|
+
const hubIds = device?.hubId;
|
|
66
|
+
const hubDetails = [];
|
|
67
|
+
if (hubIds) {
|
|
68
|
+
for (const hubId of hubIds) {
|
|
69
|
+
const hubDetail = await this.localHubService.getHub(hubId);
|
|
70
|
+
hubDetails.push({
|
|
71
|
+
id: hubDetail?.deviceId,
|
|
72
|
+
name: hubDetail?.name,
|
|
73
|
+
type: hubDetail?.deviceType?.type,
|
|
74
|
+
brand: hubDetail?.specifications?.manufacturer,
|
|
75
|
+
model: hubDetail?.specifications?.model,
|
|
76
|
+
status: hubDetail?.status?.online ? "ONLINE" : "OFFLINE",
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return hubDetails;
|
|
81
|
+
}
|
|
82
|
+
constructDeviceEventEntity(eventData, device, hubDetails, options = {}) {
|
|
83
|
+
return {
|
|
84
|
+
event: {
|
|
85
|
+
eventId: options.eventId || eventData?.eventId || "",
|
|
86
|
+
eventName: options.eventName || eventData?.eventName || "",
|
|
87
|
+
status: options.status || eventData?.status || "",
|
|
88
|
+
mode: options.mode || eventData?.data?.mode,
|
|
89
|
+
userName: options.userName || eventData?.userName || "",
|
|
90
|
+
userId: options.userId || eventData?.userId || "",
|
|
91
|
+
userType: options.userType || eventData?.userType || "",
|
|
92
|
+
raw_event: options.rawEvent || eventData?.rawData || {},
|
|
93
|
+
batteryLevel: options.batteryLevel || eventData?.data?.batteryLevel || "",
|
|
94
|
+
reason: options.reason || eventData?.data?.reason || "",
|
|
95
|
+
eventDescription: options.eventDescription || eventData?.event || "",
|
|
96
|
+
},
|
|
97
|
+
device: {
|
|
98
|
+
id: eventData?.deviceId || "",
|
|
99
|
+
name: device?.name || "",
|
|
100
|
+
type: device?.deviceType?.type || "",
|
|
101
|
+
brand: device?.specifications?.manufacturer || "",
|
|
102
|
+
model: device?.specifications?.model || "",
|
|
103
|
+
},
|
|
104
|
+
hub: hubDetails,
|
|
105
|
+
property: {
|
|
106
|
+
id: eventData?.property?.id || "",
|
|
107
|
+
name: eventData?.property?.name || "",
|
|
108
|
+
location: eventData?.property?.apartment || "",
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async dumpToEventOperation(data, device) {
|
|
113
|
+
try {
|
|
114
|
+
const hubDetails = await this.extractHubDetails(device);
|
|
115
|
+
const eventData = this.constructDeviceEventEntity(data, device, hubDetails, {
|
|
116
|
+
status: data.eventName === "lock.unlock" ? "UNLOCKED" : "LOCKED",
|
|
117
|
+
rawEvent: data.rawData,
|
|
118
|
+
});
|
|
119
|
+
this.logger.info("[DT | CDL]:[DeviceEventHandler]: Event data dumped to event operation", { eventData });
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error dumping to event operation", error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async updateTablesBasedOnEventType(eventData, device) {
|
|
126
|
+
switch (eventData.eventName) {
|
|
127
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
|
|
128
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
|
|
129
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED:
|
|
130
|
+
this.updateDeviceStatus(eventData, device);
|
|
131
|
+
break;
|
|
132
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
|
|
133
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL:
|
|
134
|
+
case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW:
|
|
135
|
+
this.updateDeviceBattery(eventData, device);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async updateDeviceStatus(data, device) {
|
|
140
|
+
this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device status", {
|
|
141
|
+
data,
|
|
142
|
+
device,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async updateDeviceBattery(data, device) {
|
|
146
|
+
this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device battery", {
|
|
147
|
+
data,
|
|
148
|
+
device,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.DeviceEventHandler = DeviceEventHandler;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IEventTransformer } from './interfaces/IEventTransformer';
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base factory for creating device event transformers
|
|
4
|
+
* This class can be moved to dt-common-device and extended by specific device type factories
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class DeviceEventTransformerFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Create the appropriate event transformer based on device type
|
|
9
|
+
* This method should be implemented by subclasses to handle specific device types
|
|
10
|
+
*/
|
|
11
|
+
abstract createTransformer(deviceType: string, rawData: any): IEventTransformer | null;
|
|
12
|
+
/**
|
|
13
|
+
* Create a default transformer for unsupported or generic device types
|
|
14
|
+
* This method can be overridden by subclasses to provide device-specific default behavior
|
|
15
|
+
*/
|
|
16
|
+
protected createDefaultTransformer(deviceType: string, rawData: any): IEventTransformer;
|
|
17
|
+
/**
|
|
18
|
+
* Handle unsupported device types
|
|
19
|
+
* This method can be overridden by subclasses to provide custom handling
|
|
20
|
+
*/
|
|
21
|
+
protected handleUnsupportedDeviceType(deviceType: string): IEventTransformer | null;
|
|
22
|
+
/**
|
|
23
|
+
* Handle errors during transformer creation
|
|
24
|
+
* This method can be overridden by subclasses to provide custom error handling
|
|
25
|
+
*/
|
|
26
|
+
protected handleTransformerCreationError(deviceType: string, error: any): IEventTransformer | null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.DeviceEventTransformerFactory = void 0;
|
|
42
|
+
const typedi_1 = require("typedi");
|
|
43
|
+
/**
|
|
44
|
+
* Abstract base factory for creating device event transformers
|
|
45
|
+
* This class can be moved to dt-common-device and extended by specific device type factories
|
|
46
|
+
*/
|
|
47
|
+
let DeviceEventTransformerFactory = (() => {
|
|
48
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
49
|
+
let _classDescriptor;
|
|
50
|
+
let _classExtraInitializers = [];
|
|
51
|
+
let _classThis;
|
|
52
|
+
var DeviceEventTransformerFactory = _classThis = class {
|
|
53
|
+
/**
|
|
54
|
+
* Create a default transformer for unsupported or generic device types
|
|
55
|
+
* This method can be overridden by subclasses to provide device-specific default behavior
|
|
56
|
+
*/
|
|
57
|
+
createDefaultTransformer(deviceType, rawData) {
|
|
58
|
+
return new (class {
|
|
59
|
+
constructor(deviceType, rawData) {
|
|
60
|
+
this.deviceType = deviceType;
|
|
61
|
+
this.rawData = rawData;
|
|
62
|
+
}
|
|
63
|
+
parseData(rawData) {
|
|
64
|
+
// Default parsing - return as-is
|
|
65
|
+
return rawData;
|
|
66
|
+
}
|
|
67
|
+
transform(parsedData) {
|
|
68
|
+
// Default transformation - return as-is
|
|
69
|
+
return parsedData;
|
|
70
|
+
}
|
|
71
|
+
validate(transformedEvent) {
|
|
72
|
+
// Default validation - always return true
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
executeTransformation() {
|
|
76
|
+
try {
|
|
77
|
+
const parsedData = this.parseData(this.rawData);
|
|
78
|
+
const transformedResult = this.transform(parsedData);
|
|
79
|
+
const events = Array.isArray(transformedResult) ? transformedResult : [transformedResult];
|
|
80
|
+
return events.filter((event) => this.validate(event));
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error(`Error in default transformer for device type ${this.deviceType}:`, error);
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})(deviceType, rawData);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handle unsupported device types
|
|
91
|
+
* This method can be overridden by subclasses to provide custom handling
|
|
92
|
+
*/
|
|
93
|
+
handleUnsupportedDeviceType(deviceType) {
|
|
94
|
+
console.warn(`Unsupported device type: ${deviceType}`);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Handle errors during transformer creation
|
|
99
|
+
* This method can be overridden by subclasses to provide custom error handling
|
|
100
|
+
*/
|
|
101
|
+
handleTransformerCreationError(deviceType, error) {
|
|
102
|
+
console.error(`Error creating transformer for device type ${deviceType}:`, error);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
__setFunctionName(_classThis, "DeviceEventTransformerFactory");
|
|
107
|
+
(() => {
|
|
108
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
109
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
110
|
+
DeviceEventTransformerFactory = _classThis = _classDescriptor.value;
|
|
111
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
112
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
113
|
+
})();
|
|
114
|
+
return DeviceEventTransformerFactory = _classThis;
|
|
115
|
+
})();
|
|
116
|
+
exports.DeviceEventTransformerFactory = DeviceEventTransformerFactory;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class EventHandler {
|
|
2
|
+
private readonly source;
|
|
3
|
+
constructor();
|
|
4
|
+
onDeviceCreate(body: any): Promise<void>;
|
|
5
|
+
onDeviceUpdate(deviceId: string, body: any): Promise<void>;
|
|
6
|
+
onDeviceDelete(deviceId: string): Promise<void>;
|
|
7
|
+
onStateChange(deviceId: string, state: any): Promise<void>;
|
|
8
|
+
onStatusChange(deviceId: string, status: any): Promise<void>;
|
|
9
|
+
onBatteryLevelChange(deviceId: string, batteryLevel: number): Promise<void>;
|
|
10
|
+
onDeviceMetaChange(deviceId: string, metaData: Record<string, any>): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventHandler = void 0;
|
|
4
|
+
const dt_pub_sub_1 = require("dt-pub-sub");
|
|
5
|
+
const dt_audit_library_1 = require("dt-audit-library");
|
|
6
|
+
const Event_1 = require("../constants/Event");
|
|
7
|
+
class EventHandler {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.source = "dt-common-device";
|
|
10
|
+
}
|
|
11
|
+
async onDeviceCreate(body) {
|
|
12
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Event_1.DT_EVENT_TYPES.DEVICE.CREATE.SUCCESS, body, this.source);
|
|
13
|
+
const payload = {
|
|
14
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.CREATE.SUCCESS,
|
|
15
|
+
properties: {
|
|
16
|
+
...body,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
20
|
+
}
|
|
21
|
+
async onDeviceUpdate(deviceId, body) {
|
|
22
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Event_1.DT_EVENT_TYPES.DEVICE.UPDATE.SUCCESS, { deviceId, body }, this.source);
|
|
23
|
+
const payload = {
|
|
24
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.UPDATE.SUCCESS,
|
|
25
|
+
properties: {
|
|
26
|
+
...body,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
30
|
+
}
|
|
31
|
+
async onDeviceDelete(deviceId) {
|
|
32
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Event_1.DT_EVENT_TYPES.DEVICE.DELETE.SUCCESS, { deviceId }, this.source);
|
|
33
|
+
const payload = {
|
|
34
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.DELETE.SUCCESS,
|
|
35
|
+
properties: {
|
|
36
|
+
deviceId,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
40
|
+
}
|
|
41
|
+
async onStateChange(deviceId, state) {
|
|
42
|
+
// TODO: For Future Consumption
|
|
43
|
+
/* await eventDispatcher.publishEvent(
|
|
44
|
+
DT_EVENT_TYPES.DEVICE.STATE.SET,
|
|
45
|
+
{ deviceId, state },
|
|
46
|
+
this.source
|
|
47
|
+
); */
|
|
48
|
+
const payload = {
|
|
49
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.STATE.SET,
|
|
50
|
+
properties: {
|
|
51
|
+
deviceId,
|
|
52
|
+
state,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
56
|
+
}
|
|
57
|
+
async onStatusChange(deviceId, status) {
|
|
58
|
+
// TODO: For Future Consumption
|
|
59
|
+
/* await eventDispatcher.publishEvent(
|
|
60
|
+
DT_EVENT_TYPES.DEVICE.STATUS.SET,
|
|
61
|
+
{ deviceId, status },
|
|
62
|
+
this.source
|
|
63
|
+
); */
|
|
64
|
+
const payload = {
|
|
65
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.STATUS.SET,
|
|
66
|
+
properties: {
|
|
67
|
+
deviceId,
|
|
68
|
+
status,
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
72
|
+
}
|
|
73
|
+
async onBatteryLevelChange(deviceId, batteryLevel) {
|
|
74
|
+
// TODO: For Future Consumption
|
|
75
|
+
/* await eventDispatcher.publishEvent(
|
|
76
|
+
DT_EVENT_TYPES.DEVICE.BATTERY.SET,
|
|
77
|
+
{ deviceId, batteryLevel },
|
|
78
|
+
this.source
|
|
79
|
+
); */
|
|
80
|
+
const payload = {
|
|
81
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.SET,
|
|
82
|
+
properties: {
|
|
83
|
+
deviceId,
|
|
84
|
+
batteryLevel,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
88
|
+
}
|
|
89
|
+
async onDeviceMetaChange(deviceId, metaData) {
|
|
90
|
+
// TODO: For Future Consumption
|
|
91
|
+
/* await eventDispatcher.publishEvent(
|
|
92
|
+
DT_EVENT_TYPES.DEVICE.META_DATA.SET,
|
|
93
|
+
{ deviceId, metaData },
|
|
94
|
+
this.source
|
|
95
|
+
); */
|
|
96
|
+
const payload = {
|
|
97
|
+
eventType: Event_1.DT_EVENT_TYPES.DEVICE.META_DATA.SET,
|
|
98
|
+
properties: {
|
|
99
|
+
deviceId,
|
|
100
|
+
metaData,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.EventHandler = EventHandler;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { IEventHandler } from "./interfaces/IEventHandler";
|
|
2
|
+
import { DeviceEvent } from "./interfaces/DeviceEvent";
|
|
3
|
+
export declare class EventHandlerOrchestrator {
|
|
4
|
+
private handlers;
|
|
5
|
+
private readonly logger;
|
|
6
|
+
constructor();
|
|
7
|
+
/**
|
|
8
|
+
* Register a handler with the orchestrator
|
|
9
|
+
*/
|
|
10
|
+
registerHandler(handler: IEventHandler): void;
|
|
11
|
+
/**
|
|
12
|
+
* Register multiple handlers
|
|
13
|
+
*/
|
|
14
|
+
registerHandlers(handlers: IEventHandler[]): void;
|
|
15
|
+
/**
|
|
16
|
+
* Process a single event through the handler hierarchy
|
|
17
|
+
*/
|
|
18
|
+
processEvent(event: DeviceEvent): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Process multiple events
|
|
21
|
+
*/
|
|
22
|
+
processEvents(events: DeviceEvent[]): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Get all registered handlers
|
|
25
|
+
*/
|
|
26
|
+
getHandlers(): IEventHandler[];
|
|
27
|
+
/**
|
|
28
|
+
* Get handlers that can handle a specific event type
|
|
29
|
+
*/
|
|
30
|
+
getHandlersForEventType(eventType: string): IEventHandler[];
|
|
31
|
+
/**
|
|
32
|
+
* Clear all registered handlers
|
|
33
|
+
*/
|
|
34
|
+
clearHandlers(): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.EventHandlerOrchestrator = void 0;
|
|
42
|
+
const typedi_1 = require("typedi");
|
|
43
|
+
const config_1 = require("../config/config");
|
|
44
|
+
let EventHandlerOrchestrator = (() => {
|
|
45
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
46
|
+
let _classDescriptor;
|
|
47
|
+
let _classExtraInitializers = [];
|
|
48
|
+
let _classThis;
|
|
49
|
+
var EventHandlerOrchestrator = _classThis = class {
|
|
50
|
+
constructor() {
|
|
51
|
+
this.handlers = [];
|
|
52
|
+
this.logger = (0, config_1.getConfig)().LOGGER;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Register a handler with the orchestrator
|
|
56
|
+
*/
|
|
57
|
+
registerHandler(handler) {
|
|
58
|
+
this.handlers.push(handler);
|
|
59
|
+
// Sort handlers by priority (lower numbers = higher priority)
|
|
60
|
+
this.handlers.sort((a, b) => a.getPriority() - b.getPriority());
|
|
61
|
+
this.logger.info(`Registered handler: ${handler.constructor.name} with priority ${handler.getPriority()}`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Register multiple handlers
|
|
65
|
+
*/
|
|
66
|
+
registerHandlers(handlers) {
|
|
67
|
+
handlers.forEach((handler) => this.registerHandler(handler));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Process a single event through the handler hierarchy
|
|
71
|
+
*/
|
|
72
|
+
async processEvent(event) {
|
|
73
|
+
try {
|
|
74
|
+
this.logger.info(`Processing event: ${event.eventName} for device: ${event.deviceId}`);
|
|
75
|
+
const applicableHandlers = this.handlers.filter((handler) => handler.canHandle(event.eventName));
|
|
76
|
+
if (applicableHandlers.length === 0) {
|
|
77
|
+
this.logger.warn(`No handlers found for event type: ${event.eventName}`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.logger.info(`Found ${applicableHandlers.length} handlers for event: ${event.eventName}`);
|
|
81
|
+
// Process with each applicable handler in priority order
|
|
82
|
+
for (const handler of applicableHandlers) {
|
|
83
|
+
try {
|
|
84
|
+
await handler.handleEvent(event);
|
|
85
|
+
this.logger.info(`Event processed by: ${handler.constructor.name}`);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
this.logger.error(`Error in handler ${handler.constructor.name}:`, error);
|
|
89
|
+
// Continue with other handlers even if one fails
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
this.logger.error("Error in EventHandlerOrchestrator.processEvent:", error);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Process multiple events
|
|
99
|
+
*/
|
|
100
|
+
async processEvents(events) {
|
|
101
|
+
try {
|
|
102
|
+
this.logger.info(`Processing ${events.length} events`);
|
|
103
|
+
for (const event of events) {
|
|
104
|
+
await this.processEvent(event);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
this.logger.error("Error in EventHandlerOrchestrator.processEvents:", error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get all registered handlers
|
|
113
|
+
*/
|
|
114
|
+
getHandlers() {
|
|
115
|
+
return [...this.handlers];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get handlers that can handle a specific event type
|
|
119
|
+
*/
|
|
120
|
+
getHandlersForEventType(eventType) {
|
|
121
|
+
return this.handlers.filter((handler) => handler.canHandle(eventType));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Clear all registered handlers
|
|
125
|
+
*/
|
|
126
|
+
clearHandlers() {
|
|
127
|
+
this.handlers = [];
|
|
128
|
+
this.logger.info("All handlers cleared");
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
__setFunctionName(_classThis, "EventHandlerOrchestrator");
|
|
132
|
+
(() => {
|
|
133
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
134
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
135
|
+
EventHandlerOrchestrator = _classThis = _classDescriptor.value;
|
|
136
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
137
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
138
|
+
})();
|
|
139
|
+
return EventHandlerOrchestrator = _classThis;
|
|
140
|
+
})();
|
|
141
|
+
exports.EventHandlerOrchestrator = EventHandlerOrchestrator;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DeviceEventHandler } from "./DeviceEventHandler";
|
|
2
|
+
import { DeviceEvent } from "./interfaces/DeviceEvent";
|
|
3
|
+
import { DeviceEventTransformerFactory } from "./DeviceEventTransformerFactory";
|
|
4
|
+
export declare class EventProcessingService {
|
|
5
|
+
private readonly deviceEventTransformerFactory;
|
|
6
|
+
private readonly handlerOrchestrator;
|
|
7
|
+
readonly deviceEventHandler: DeviceEventHandler;
|
|
8
|
+
private readonly logger;
|
|
9
|
+
private deviceHandlers;
|
|
10
|
+
constructor(deviceEventTransformerFactory: DeviceEventTransformerFactory);
|
|
11
|
+
/**
|
|
12
|
+
* Initialize device-specific handlers - to be implemented by subclasses
|
|
13
|
+
*/
|
|
14
|
+
protected initializeDeviceHandlers(): void;
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the handler hierarchy - to be implemented by subclasses
|
|
17
|
+
*/
|
|
18
|
+
protected initializeHandlers(): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get device-specific handler based on connection provider
|
|
21
|
+
*/
|
|
22
|
+
private getDeviceHandler;
|
|
23
|
+
/**
|
|
24
|
+
* Transform device event using EventTransformerFactory
|
|
25
|
+
*/
|
|
26
|
+
transformDeviceEventWithTransformer(connectionProvider: string, rawData: any): DeviceEvent | null;
|
|
27
|
+
/**
|
|
28
|
+
* Transform raw event data to DeviceEvent format using EventTransformer
|
|
29
|
+
*/
|
|
30
|
+
private transformToDeviceEvent;
|
|
31
|
+
/**
|
|
32
|
+
* Process a single event from device.event.controller
|
|
33
|
+
*/
|
|
34
|
+
processEvent(connectionProvider: string, rawData: any): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Process a single event (legacy method for DeviceEvent objects)
|
|
37
|
+
*/
|
|
38
|
+
processEventLegacy(event: DeviceEvent): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Process multiple events
|
|
41
|
+
*/
|
|
42
|
+
processEvents(events: DeviceEvent[]): Promise<void>;
|
|
43
|
+
}
|