dt-common-device 3.0.9 → 3.0.11
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/dist/config/config.d.ts +1 -2
- package/dist/config/config.js +4 -5
- package/dist/device/cloud/interface.d.ts +101 -0
- package/dist/device/cloud/interface.js +3 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -0
- package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
- package/dist/device/cloud/services/Device.service.d.ts +39 -0
- package/dist/device/cloud/services/Device.service.js +9 -0
- package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
- package/dist/device/cloud/services/DeviceCloudService.js +59 -0
- package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
- package/dist/device/cloud/services/DeviceHub.service.js +6 -0
- package/dist/device/cloud/services/Hub.service.d.ts +25 -0
- package/dist/device/cloud/services/Hub.service.js +9 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
- package/dist/device/index.d.ts +4 -0
- package/dist/device/index.js +20 -0
- package/dist/device/local/events/EventHandler.js +6 -6
- package/dist/device/local/events/Events.d.ts +12 -33
- package/dist/device/local/events/Events.js +12 -33
- package/dist/device/local/interface.d.ts +0 -0
- package/dist/device/local/interface.js +1 -0
- package/dist/device/local/interfaces/IDevice.d.ts +1 -0
- package/dist/device/local/repository/Schedule.repository.d.ts +0 -1
- package/dist/device/local/repository/Schedule.repository.js +6 -6
- package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
- package/dist/device/local/services/DeviceHub.service.js +40 -0
- package/dist/queue/entities/HybridHttpQueue.d.ts +4 -14
- package/dist/queue/entities/HybridHttpQueue.js +31 -119
- package/dist/queue/interfaces/IHybridHttpQueue.d.ts +2 -12
- package/dist/queue/interfaces/IJobResult.d.ts +1 -8
- package/dist/queue/interfaces/index.d.ts +0 -1
- package/dist/queue/interfaces/index.js +0 -1
- package/dist/queue/services/QueueService.d.ts +2 -12
- package/dist/queue/types/queue.types.d.ts +10 -29
- package/dist/queue/utils/jobUtils.d.ts +0 -3
- package/dist/queue/utils/jobUtils.js +0 -48
- package/dist/queue/utils/queueUtils.d.ts +7 -0
- package/dist/queue/utils/queueUtils.js +113 -4
- package/package.json +6 -1
- package/.eslintrc.js +0 -44
- package/dist/audit/AuditProperties.d.ts +0 -16
- package/dist/audit/AuditUtils.d.ts +0 -2
- package/dist/audit/AuditUtils.js +0 -36
- package/src/alerts/Alert.model.ts +0 -289
- package/src/alerts/Alert.repository.ts +0 -487
- package/src/alerts/Alert.service.ts +0 -711
- package/src/alerts/AlertBuilder.example.ts +0 -126
- package/src/alerts/AlertBuilder.ts +0 -208
- package/src/alerts/AlertService.example.ts +0 -232
- package/src/alerts/alert.types.ts +0 -64
- package/src/alerts/index.ts +0 -3
- package/src/audit/AuditProperties.ts +0 -16
- package/src/audit/AuditUtils.ts +0 -38
- package/src/config/config.ts +0 -202
- package/src/config/config.types.ts +0 -21
- package/src/connection/Connection.repository.ts +0 -52
- package/src/connection/Connection.service.ts +0 -39
- package/src/connection/IConnection.ts +0 -27
- package/src/connection/index.ts +0 -3
- package/src/constants/ConnectionProviders.ts +0 -11
- package/src/constants/Event.ts +0 -89
- package/src/constants/Service.ts +0 -17
- package/src/constants/index.ts +0 -3
- package/src/db/db.ts +0 -24
- package/src/db/index.ts +0 -2
- package/src/db/redis.ts +0 -20
- package/src/device/cloud/entities/CloudDevice.ts +0 -40
- package/src/device/cloud/entities/CloudDeviceService.ts +0 -8
- package/src/device/cloud/entities/DeviceFactory.ts +0 -27
- package/src/device/cloud/entities/index.ts +0 -3
- package/src/device/cloud/interfaces/ICloudDevice.ts +0 -14
- package/src/device/cloud/interfaces/ICloudDeviceService.ts +0 -6
- package/src/device/cloud/interfaces/IDeviceFactory.ts +0 -5
- package/src/device/cloud/interfaces/IRawDataTransformer.ts +0 -5
- package/src/device/cloud/interfaces/IRawDevice.ts +0 -19
- package/src/device/cloud/interfaces/index.ts +0 -5
- package/src/device/local/interfaces/IDevice.ts +0 -61
- package/src/device/local/interfaces/IDtDevice.ts +0 -16
- package/src/device/local/interfaces/ISchedule.ts +0 -40
- package/src/device/local/interfaces/index.ts +0 -3
- package/src/device/local/repository/Device.repository.ts +0 -368
- package/src/device/local/repository/Hub.repository.ts +0 -107
- package/src/device/local/repository/Schedule.repository.ts +0 -72
- package/src/device/local/services/Device.service.ts +0 -436
- package/src/device/local/services/Hub.service.ts +0 -57
- package/src/device/local/services/Schedule.service.ts +0 -26
- package/src/device/local/services/index.ts +0 -3
- package/src/docs/Alert.model.md +0 -319
- package/src/docs/Alerts&IssuesModel.md +0 -312
- package/src/docs/Issue.model.md +0 -386
- package/src/docs/SECURITY.md +0 -67
- package/src/docs/TROUBLESHOOTING.md +0 -184
- package/src/events/BaseEventHandler.ts +0 -145
- package/src/events/BaseEventTransformer.ts +0 -97
- package/src/events/DeviceEventHandler.ts +0 -213
- package/src/events/DeviceEventTransformerFactory.ts +0 -77
- package/src/events/EventHandler.ts +0 -124
- package/src/events/EventHandlerOrchestrator.ts +0 -119
- package/src/events/EventProcessingService.ts +0 -248
- package/src/events/InternalEventSubscription.ts +0 -194
- package/src/events/index.ts +0 -9
- package/src/events/interfaces/DeviceEvent.ts +0 -56
- package/src/events/interfaces/IEventHandler.ts +0 -28
- package/src/events/interfaces/IEventTransformer.ts +0 -8
- package/src/events/interfaces/IInternalEvent.ts +0 -33
- package/src/events/interfaces/index.ts +0 -4
- package/src/index.ts +0 -43
- package/src/issues/Issue.model.ts +0 -350
- package/src/issues/Issue.repository.ts +0 -517
- package/src/issues/Issue.service.ts +0 -932
- package/src/issues/IssueBuilder.example.ts +0 -210
- package/src/issues/IssueBuilder.ts +0 -263
- package/src/issues/IssueService.example.ts +0 -310
- package/src/issues/index.ts +0 -2
- package/src/issues/issue.types.ts +0 -98
- package/src/property/IProperty.ts +0 -30
- package/src/property/Property.repository.ts +0 -53
- package/src/property/Property.service.ts +0 -38
- package/src/property/index.ts +0 -2
- package/src/queue/entities/HybridHttpQueue.ts +0 -274
- package/src/queue/entities/index.ts +0 -1
- package/src/queue/index.ts +0 -6
- package/src/queue/interfaces/IHttpRequestJob.ts +0 -10
- package/src/queue/interfaces/IHybridHttpQueue.ts +0 -25
- package/src/queue/interfaces/IJobResult.ts +0 -15
- package/src/queue/interfaces/IRateLimitConfig.ts +0 -5
- package/src/queue/interfaces/index.ts +0 -4
- package/src/queue/services/QueueService.ts +0 -40
- package/src/queue/services/index.ts +0 -1
- package/src/queue/types/http.types.ts +0 -23
- package/src/queue/types/index.ts +0 -2
- package/src/queue/types/queue.types.ts +0 -21
- package/src/queue/utils/index.ts +0 -3
- package/src/queue/utils/jobUtils.ts +0 -79
- package/src/queue/utils/queueUtils.ts +0 -84
- package/src/queue/utils/rateLimit.utils.ts +0 -131
- package/src/utils/http.utils.ts +0 -143
- package/src/utils/index.ts +0 -2
- package/src/utils/redis.utils.ts +0 -74
- package/tsconfig.json +0 -20
- /package/dist/{audit/AuditProperties.js → device/cloud/interfaces/IDevicesService.js} +0 -0
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { DeviceEvent } from "./interfaces/DeviceEvent";
|
|
2
|
-
import { IEventHandler } from "./interfaces/IEventHandler";
|
|
3
|
-
import { getConfig } from "../config/config";
|
|
4
|
-
import { ILogger } from "../config/config.types";
|
|
5
|
-
import { LocalDeviceService } from "../device/local/services/Device.service";
|
|
6
|
-
|
|
7
|
-
export abstract class BaseEventHandler implements IEventHandler {
|
|
8
|
-
protected readonly supportedEventTypes: string[];
|
|
9
|
-
protected readonly priority: number;
|
|
10
|
-
protected readonly logger: ILogger;
|
|
11
|
-
protected readonly localDeviceService: LocalDeviceService;
|
|
12
|
-
constructor(supportedEventTypes: string[], priority: number = 100) {
|
|
13
|
-
this.supportedEventTypes = supportedEventTypes;
|
|
14
|
-
this.priority = priority;
|
|
15
|
-
this.logger = getConfig().LOGGER;
|
|
16
|
-
this.localDeviceService = new LocalDeviceService();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Handle a single event - to be implemented by specific handlers
|
|
21
|
-
*/
|
|
22
|
-
async handleEvent(event: DeviceEvent): Promise<void> {
|
|
23
|
-
try {
|
|
24
|
-
await this.beforeEvent(event);
|
|
25
|
-
await this.onEvent(event);
|
|
26
|
-
} catch (error) {
|
|
27
|
-
this.logger.error(
|
|
28
|
-
`${this.constructor.name}: Error during event processing`,
|
|
29
|
-
{
|
|
30
|
-
eventName: event.eventName,
|
|
31
|
-
deviceId: event.deviceId,
|
|
32
|
-
error,
|
|
33
|
-
}
|
|
34
|
-
);
|
|
35
|
-
throw error;
|
|
36
|
-
} finally {
|
|
37
|
-
await this.afterEvent(event);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async beforeEvent(event: DeviceEvent): Promise<void> {
|
|
42
|
-
this.logger.info(
|
|
43
|
-
"[DT | CDL]:[BaseEventHandler]: Before event - Handle Before Event"
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
abstract onEvent(event: DeviceEvent): Promise<void>;
|
|
48
|
-
|
|
49
|
-
async afterEvent(event: DeviceEvent): Promise<void> {
|
|
50
|
-
this.logger.info(
|
|
51
|
-
"[DT | CDL]:[BaseEventHandler]: After event - Event processing completed",
|
|
52
|
-
{
|
|
53
|
-
eventName: event.eventName,
|
|
54
|
-
deviceId: event.deviceId,
|
|
55
|
-
handler: this.constructor.name,
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
//TODO: Implement AUDIT Here!
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Handle multiple events - default implementation processes them sequentially
|
|
63
|
-
*/
|
|
64
|
-
async handleEvents(events: DeviceEvent[]): Promise<void> {
|
|
65
|
-
try {
|
|
66
|
-
this.logger.info(
|
|
67
|
-
`Processing ${events.length} events with ${this.constructor.name}`
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
for (const event of events) {
|
|
71
|
-
if (this.canHandle(event.eventName || "unknown")) {
|
|
72
|
-
await this.handleEvent(event);
|
|
73
|
-
} else {
|
|
74
|
-
this.logger.warn(
|
|
75
|
-
`Event type ${event.eventName} not supported by ${this.constructor.name}`
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
} catch (error) {
|
|
80
|
-
this.logger.error(`${this.constructor.name}: handleEvents Error`, error);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public async onStateChange(
|
|
85
|
-
deviceId: string,
|
|
86
|
-
state: Record<string, any>,
|
|
87
|
-
event: DeviceEvent
|
|
88
|
-
): Promise<void> {
|
|
89
|
-
try {
|
|
90
|
-
await this.localDeviceService.setState(deviceId, state);
|
|
91
|
-
await this.afterEvent(event);
|
|
92
|
-
} catch (error: any) {
|
|
93
|
-
this.logger.error(
|
|
94
|
-
`[DT | CDL]:[BaseEventHandler]: Error during state change: ${
|
|
95
|
-
error.message || error.toString()
|
|
96
|
-
}`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Check if this handler can process the given event type
|
|
103
|
-
*/
|
|
104
|
-
canHandle(eventType: string): boolean {
|
|
105
|
-
return this.supportedEventTypes.includes(eventType);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Get the priority of this handler
|
|
110
|
-
*/
|
|
111
|
-
getPriority(): number {
|
|
112
|
-
return this.priority;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get the event types this handler can process
|
|
117
|
-
*/
|
|
118
|
-
getSupportedEventTypes(): string[] {
|
|
119
|
-
return this.supportedEventTypes;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Common validation method
|
|
124
|
-
*/
|
|
125
|
-
protected validateEvent(event: DeviceEvent): boolean {
|
|
126
|
-
if (!event) {
|
|
127
|
-
this.logger.error("Event is null or undefined");
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (!event.eventName) {
|
|
132
|
-
this.logger.error("Event name is missing");
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Common error handling method
|
|
141
|
-
*/
|
|
142
|
-
protected async handleError(error: any, context: string): Promise<void> {
|
|
143
|
-
this.logger.error(`${this.constructor.name}: ${context} Error`, error);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { getConfig } from "../config/config";
|
|
2
|
-
import { ILogger } from "../config/config.types";
|
|
3
|
-
import { DeviceEvent } from "./interfaces/DeviceEvent";
|
|
4
|
-
import { IEventTransformer } from "./interfaces/IEventTransformer";
|
|
5
|
-
|
|
6
|
-
export abstract class BaseEventTransformer implements IEventTransformer {
|
|
7
|
-
protected readonly deviceType: string;
|
|
8
|
-
protected rawData: any;
|
|
9
|
-
protected readonly logger: ILogger;
|
|
10
|
-
constructor(deviceType: string, rawData: any) {
|
|
11
|
-
this.deviceType = deviceType;
|
|
12
|
-
this.rawData = rawData;
|
|
13
|
-
this.logger = getConfig().LOGGER;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Parse raw event data - to be implemented by device-specific transformers
|
|
17
|
-
parseData(rawData: any): any {
|
|
18
|
-
try {
|
|
19
|
-
this.logger.info(`${this.deviceType}EventTransformer: Parsed event data`);
|
|
20
|
-
return rawData;
|
|
21
|
-
} catch (error) {
|
|
22
|
-
this.logger.error(
|
|
23
|
-
`${this.deviceType}EventTransformer: Error parsing data`,
|
|
24
|
-
error
|
|
25
|
-
);
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Transform parsed data into DeviceEvent format - to be implemented by device-specific transformers
|
|
32
|
-
*/
|
|
33
|
-
abstract transform(parsedData: any): DeviceEvent | DeviceEvent[];
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Validate transformed event - default implementation, can be overridden
|
|
37
|
-
*/
|
|
38
|
-
validate(transformedEvent: DeviceEvent): boolean {
|
|
39
|
-
try {
|
|
40
|
-
// Basic validation - ensure required fields exist
|
|
41
|
-
if (!transformedEvent) {
|
|
42
|
-
this.logger.error("Transformed event is null or undefined");
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Add more validation logic as needed
|
|
47
|
-
return true;
|
|
48
|
-
} catch (error) {
|
|
49
|
-
this.logger.error("BaseEventTransformer: validate Error", error);
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Get the device type this transformer handles
|
|
56
|
-
*/
|
|
57
|
-
getDeviceType(): string {
|
|
58
|
-
return this.deviceType;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Execute the full transformation pipeline
|
|
63
|
-
*/
|
|
64
|
-
executeTransformation(): DeviceEvent[] {
|
|
65
|
-
try {
|
|
66
|
-
this.logger.info(
|
|
67
|
-
`Starting transformation for device type: ${this.deviceType}`
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
// Step 1: Parse the raw data
|
|
71
|
-
const parsedData = this.parseData(this.rawData);
|
|
72
|
-
|
|
73
|
-
// Step 2: Transform to DeviceEvent format
|
|
74
|
-
const transformedResult = this.transform(parsedData);
|
|
75
|
-
|
|
76
|
-
// Step 3: Convert to array format
|
|
77
|
-
// TODO: Check if any service sends multiple events
|
|
78
|
-
const events = Array.isArray(transformedResult)
|
|
79
|
-
? transformedResult
|
|
80
|
-
: [transformedResult];
|
|
81
|
-
|
|
82
|
-
// Step 4: Validate each event
|
|
83
|
-
const validEvents = events.filter((event) => this.validate(event));
|
|
84
|
-
|
|
85
|
-
this.logger.info(
|
|
86
|
-
`Transformation completed. ${validEvents.length} valid events created.`
|
|
87
|
-
);
|
|
88
|
-
return validEvents;
|
|
89
|
-
} catch (error) {
|
|
90
|
-
this.logger.error(
|
|
91
|
-
"BaseEventTransformer: executeTransformation Error",
|
|
92
|
-
error
|
|
93
|
-
);
|
|
94
|
-
return [];
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import { Source } from "../constants";
|
|
2
|
-
import { DT_EVENT_TYPES } from "../constants/Event";
|
|
3
|
-
import { IDevice } from "../device/local/interfaces";
|
|
4
|
-
import { LocalHubService } from "../device/local/services";
|
|
5
|
-
import { BaseEventHandler } from "./BaseEventHandler";
|
|
6
|
-
import {
|
|
7
|
-
DeviceEvent,
|
|
8
|
-
DeviceEventEntity,
|
|
9
|
-
EventConstructionOptions,
|
|
10
|
-
} from "./interfaces/DeviceEvent";
|
|
11
|
-
|
|
12
|
-
export class DeviceEventHandler extends BaseEventHandler {
|
|
13
|
-
private readonly localHubService: LocalHubService;
|
|
14
|
-
constructor() {
|
|
15
|
-
super(
|
|
16
|
-
[
|
|
17
|
-
DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
|
|
18
|
-
DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE,
|
|
19
|
-
DT_EVENT_TYPES.DEVICE.STATUS.CHANGED,
|
|
20
|
-
DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL,
|
|
21
|
-
DT_EVENT_TYPES.DEVICE.BATTERY.LOW,
|
|
22
|
-
DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED,
|
|
23
|
-
],
|
|
24
|
-
100
|
|
25
|
-
);
|
|
26
|
-
this.localHubService = new LocalHubService();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async onEvent(event: DeviceEvent): Promise<void> {
|
|
30
|
-
this.logger.info("[DT | CDL]:[DeviceEventHandler]: Processing event", {
|
|
31
|
-
eventName: event.eventName,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
const device: IDevice = await this.localDeviceService.getDevice(
|
|
36
|
-
event.deviceId
|
|
37
|
-
);
|
|
38
|
-
if (!device?.deviceId) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
"[DT | CDL]:[DeviceEventHandler]: deviceId does not exist"
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
await this.updateDeviceEventData(device, event);
|
|
44
|
-
// Update relevant tables based on event type
|
|
45
|
-
await this.updateTablesBasedOnEventType(event, device);
|
|
46
|
-
|
|
47
|
-
// Save event logs
|
|
48
|
-
await this.dumpToEventOperation(event, device);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
this.logger.error(
|
|
51
|
-
"[DT | CDL]:[DeviceEventHandler]: Error processing event",
|
|
52
|
-
error
|
|
53
|
-
);
|
|
54
|
-
throw error;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async updateDeviceEventData(
|
|
59
|
-
deviceData: IDevice,
|
|
60
|
-
event: DeviceEvent
|
|
61
|
-
): Promise<void> {
|
|
62
|
-
switch (event.eventName) {
|
|
63
|
-
case DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
|
|
64
|
-
case DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
|
|
65
|
-
await this.localDeviceService.setState(deviceData.deviceId, {
|
|
66
|
-
status: {
|
|
67
|
-
online: event.eventName === DT_EVENT_TYPES.DEVICE.STATUS.ONLINE,
|
|
68
|
-
lastUpdated: new Date().toISOString(),
|
|
69
|
-
error: {
|
|
70
|
-
type:
|
|
71
|
-
event.eventName === DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
|
|
72
|
-
? "offline"
|
|
73
|
-
: "",
|
|
74
|
-
message:
|
|
75
|
-
event.eventName === DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE
|
|
76
|
-
? "Device is offline"
|
|
77
|
-
: "",
|
|
78
|
-
default: {},
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
break;
|
|
83
|
-
case DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
|
|
84
|
-
await this.localDeviceService.setBatteryLevel(
|
|
85
|
-
deviceData.deviceId,
|
|
86
|
-
event?.data?.batteryLevel ?? 0,
|
|
87
|
-
Source.CLOUD_EVENT
|
|
88
|
-
);
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async extractHubDetails(device: IDevice): Promise<any[]> {
|
|
94
|
-
const hubIds = device?.hubId;
|
|
95
|
-
const hubDetails: any[] = [];
|
|
96
|
-
|
|
97
|
-
if (hubIds) {
|
|
98
|
-
for (const hubId of hubIds) {
|
|
99
|
-
const hubDetail: IDevice = await this.localHubService.getHub(hubId);
|
|
100
|
-
hubDetails.push({
|
|
101
|
-
id: hubDetail?.deviceId,
|
|
102
|
-
name: hubDetail?.name,
|
|
103
|
-
type: hubDetail?.deviceType?.type,
|
|
104
|
-
brand: hubDetail?.specifications?.manufacturer,
|
|
105
|
-
model: hubDetail?.specifications?.model,
|
|
106
|
-
status: hubDetail?.status?.online ? "ONLINE" : "OFFLINE",
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return hubDetails;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
constructDeviceEventEntity(
|
|
115
|
-
eventData: any,
|
|
116
|
-
device: IDevice,
|
|
117
|
-
hubDetails: any[],
|
|
118
|
-
options: EventConstructionOptions = {}
|
|
119
|
-
): DeviceEventEntity {
|
|
120
|
-
return {
|
|
121
|
-
event: {
|
|
122
|
-
eventId: options.eventId || eventData?.eventId || "",
|
|
123
|
-
eventName: options.eventName || eventData?.eventName || "",
|
|
124
|
-
status: options.status || eventData?.status || "",
|
|
125
|
-
mode: options.mode || eventData?.data?.mode,
|
|
126
|
-
userName: options.userName || eventData?.userName || "",
|
|
127
|
-
userId: options.userId || eventData?.userId || "",
|
|
128
|
-
userType: options.userType || eventData?.userType || "",
|
|
129
|
-
raw_event: options.rawEvent || eventData?.rawData || {},
|
|
130
|
-
batteryLevel:
|
|
131
|
-
options.batteryLevel || eventData?.data?.batteryLevel || "",
|
|
132
|
-
reason: options.reason || eventData?.data?.reason || "",
|
|
133
|
-
eventDescription: options.eventDescription || eventData?.event || "",
|
|
134
|
-
},
|
|
135
|
-
device: {
|
|
136
|
-
id: eventData?.deviceId || "",
|
|
137
|
-
name: device?.name || "",
|
|
138
|
-
type: device?.deviceType?.type || "",
|
|
139
|
-
brand: device?.specifications?.manufacturer || "",
|
|
140
|
-
model: device?.specifications?.model || "",
|
|
141
|
-
},
|
|
142
|
-
hub: hubDetails,
|
|
143
|
-
property: {
|
|
144
|
-
id: eventData?.property?.id || "",
|
|
145
|
-
name: eventData?.property?.name || "",
|
|
146
|
-
location: eventData?.property?.apartment || "",
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async dumpToEventOperation(data: any, device: IDevice): Promise<void> {
|
|
152
|
-
try {
|
|
153
|
-
const hubDetails = await this.extractHubDetails(device);
|
|
154
|
-
|
|
155
|
-
const eventData: DeviceEventEntity = this.constructDeviceEventEntity(
|
|
156
|
-
data,
|
|
157
|
-
device,
|
|
158
|
-
hubDetails,
|
|
159
|
-
{
|
|
160
|
-
status: data.eventName === "lock.unlock" ? "UNLOCKED" : "LOCKED",
|
|
161
|
-
rawEvent: data.rawData,
|
|
162
|
-
}
|
|
163
|
-
);
|
|
164
|
-
this.logger.info(
|
|
165
|
-
"[DT | CDL]:[DeviceEventHandler]: Event data dumped to event operation",
|
|
166
|
-
{ eventData }
|
|
167
|
-
);
|
|
168
|
-
} catch (error) {
|
|
169
|
-
this.logger.error(
|
|
170
|
-
"[DT | CDL]:[DeviceEventHandler]: Error dumping to event operation",
|
|
171
|
-
error
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
async updateTablesBasedOnEventType(
|
|
177
|
-
eventData: any,
|
|
178
|
-
device: IDevice
|
|
179
|
-
): Promise<void> {
|
|
180
|
-
switch (eventData.eventName) {
|
|
181
|
-
case DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
|
|
182
|
-
case DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
|
|
183
|
-
case DT_EVENT_TYPES.DEVICE.STATUS.CHANGED:
|
|
184
|
-
this.updateDeviceStatus(eventData, device);
|
|
185
|
-
break;
|
|
186
|
-
case DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
|
|
187
|
-
case DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL:
|
|
188
|
-
case DT_EVENT_TYPES.DEVICE.BATTERY.LOW:
|
|
189
|
-
this.updateDeviceBattery(eventData, device);
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async updateDeviceStatus(data: any, device: IDevice) {
|
|
195
|
-
this.logger.info(
|
|
196
|
-
"[DT | CDL]:[DeviceEventHandler]: Updating device status",
|
|
197
|
-
{
|
|
198
|
-
data,
|
|
199
|
-
device,
|
|
200
|
-
}
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
async updateDeviceBattery(data: any, device: IDevice) {
|
|
205
|
-
this.logger.info(
|
|
206
|
-
"[DT | CDL]:[DeviceEventHandler]: Updating device battery",
|
|
207
|
-
{
|
|
208
|
-
data,
|
|
209
|
-
device,
|
|
210
|
-
}
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Service } from 'typedi';
|
|
2
|
-
import { IEventTransformer } from './interfaces/IEventTransformer';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Abstract base factory for creating device event transformers
|
|
6
|
-
* This class can be moved to dt-common-device and extended by specific device type factories
|
|
7
|
-
*/
|
|
8
|
-
@Service()
|
|
9
|
-
export abstract class DeviceEventTransformerFactory {
|
|
10
|
-
/**
|
|
11
|
-
* Create the appropriate event transformer based on device type
|
|
12
|
-
* This method should be implemented by subclasses to handle specific device types
|
|
13
|
-
*/
|
|
14
|
-
public abstract createTransformer(deviceType: string, rawData: any): IEventTransformer | null;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Create a default transformer for unsupported or generic device types
|
|
18
|
-
* This method can be overridden by subclasses to provide device-specific default behavior
|
|
19
|
-
*/
|
|
20
|
-
protected createDefaultTransformer(deviceType: string, rawData: any): IEventTransformer {
|
|
21
|
-
return new (class implements IEventTransformer {
|
|
22
|
-
private readonly deviceType: string;
|
|
23
|
-
private readonly rawData: any;
|
|
24
|
-
|
|
25
|
-
constructor(deviceType: string, rawData: any) {
|
|
26
|
-
this.deviceType = deviceType;
|
|
27
|
-
this.rawData = rawData;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
parseData(rawData: any): any {
|
|
31
|
-
// Default parsing - return as-is
|
|
32
|
-
return rawData;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
transform(parsedData: any): any {
|
|
36
|
-
// Default transformation - return as-is
|
|
37
|
-
return parsedData;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
validate(transformedEvent: any): boolean {
|
|
41
|
-
// Default validation - always return true
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
executeTransformation(): any[] {
|
|
46
|
-
try {
|
|
47
|
-
const parsedData = this.parseData(this.rawData);
|
|
48
|
-
const transformedResult = this.transform(parsedData);
|
|
49
|
-
const events = Array.isArray(transformedResult) ? transformedResult : [transformedResult];
|
|
50
|
-
return events.filter((event) => this.validate(event));
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error(`Error in default transformer for device type ${this.deviceType}:`, error);
|
|
53
|
-
return [];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
})(deviceType, rawData);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Handle unsupported device types
|
|
62
|
-
* This method can be overridden by subclasses to provide custom handling
|
|
63
|
-
*/
|
|
64
|
-
protected handleUnsupportedDeviceType(deviceType: string): IEventTransformer | null {
|
|
65
|
-
console.warn(`Unsupported device type: ${deviceType}`);
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Handle errors during transformer creation
|
|
71
|
-
* This method can be overridden by subclasses to provide custom error handling
|
|
72
|
-
*/
|
|
73
|
-
protected handleTransformerCreationError(deviceType: string, error: any): IEventTransformer | null {
|
|
74
|
-
console.error(`Error creating transformer for device type ${deviceType}:`, error);
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { eventDispatcher } from "dt-pub-sub";
|
|
2
|
-
import { publishAudit } from "dt-audit-library";
|
|
3
|
-
import { DT_EVENT_TYPES } from "../constants/Event";
|
|
4
|
-
|
|
5
|
-
export class EventHandler {
|
|
6
|
-
private readonly source: string;
|
|
7
|
-
constructor() {
|
|
8
|
-
this.source = "dt-common-device";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async onDeviceCreate(body: any) {
|
|
12
|
-
// TODO: For Future Consumption
|
|
13
|
-
// await eventDispatcher.publishEvent(
|
|
14
|
-
// DT_EVENT_TYPES.DEVICE.CREATE.SUCCESS,
|
|
15
|
-
// body,
|
|
16
|
-
// this.source
|
|
17
|
-
// );
|
|
18
|
-
const payload = {
|
|
19
|
-
eventType: DT_EVENT_TYPES.DEVICE.CREATE.SUCCESS,
|
|
20
|
-
properties: {
|
|
21
|
-
...body,
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
await publishAudit(payload);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async onDeviceUpdate(deviceId: string, body: any) {
|
|
28
|
-
await eventDispatcher.publishEvent(
|
|
29
|
-
DT_EVENT_TYPES.DEVICE.UPDATE.SUCCESS,
|
|
30
|
-
{ deviceId, body },
|
|
31
|
-
this.source
|
|
32
|
-
);
|
|
33
|
-
const payload = {
|
|
34
|
-
eventType: DT_EVENT_TYPES.DEVICE.UPDATE.SUCCESS,
|
|
35
|
-
properties: {
|
|
36
|
-
...body,
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
await publishAudit(payload);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async onDeviceDelete(deviceId: string) {
|
|
43
|
-
await eventDispatcher.publishEvent(
|
|
44
|
-
DT_EVENT_TYPES.DEVICE.DELETE.SUCCESS,
|
|
45
|
-
{ deviceId },
|
|
46
|
-
this.source
|
|
47
|
-
);
|
|
48
|
-
const payload = {
|
|
49
|
-
eventType: DT_EVENT_TYPES.DEVICE.DELETE.SUCCESS,
|
|
50
|
-
properties: {
|
|
51
|
-
deviceId,
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
await publishAudit(payload);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async onStateChange(deviceId: string, state: any) {
|
|
58
|
-
// TODO: For Future Consumption
|
|
59
|
-
/* await eventDispatcher.publishEvent(
|
|
60
|
-
DT_EVENT_TYPES.DEVICE.STATE.SET,
|
|
61
|
-
{ deviceId, state },
|
|
62
|
-
this.source
|
|
63
|
-
); */
|
|
64
|
-
const payload = {
|
|
65
|
-
eventType: DT_EVENT_TYPES.DEVICE.STATE.SET,
|
|
66
|
-
properties: {
|
|
67
|
-
deviceId,
|
|
68
|
-
state,
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
await publishAudit(payload);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async onStatusChange(deviceId: string, status: any) {
|
|
75
|
-
// TODO: For Future Consumption
|
|
76
|
-
/* await eventDispatcher.publishEvent(
|
|
77
|
-
DT_EVENT_TYPES.DEVICE.STATUS.SET,
|
|
78
|
-
{ deviceId, status },
|
|
79
|
-
this.source
|
|
80
|
-
); */
|
|
81
|
-
const payload = {
|
|
82
|
-
eventType: DT_EVENT_TYPES.DEVICE.STATUS.SET,
|
|
83
|
-
properties: {
|
|
84
|
-
deviceId,
|
|
85
|
-
status,
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
await publishAudit(payload);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async onBatteryLevelChange(deviceId: string, batteryLevel: number) {
|
|
92
|
-
// TODO: For Future Consumption
|
|
93
|
-
/* await eventDispatcher.publishEvent(
|
|
94
|
-
DT_EVENT_TYPES.DEVICE.BATTERY.SET,
|
|
95
|
-
{ deviceId, batteryLevel },
|
|
96
|
-
this.source
|
|
97
|
-
); */
|
|
98
|
-
const payload = {
|
|
99
|
-
eventType: DT_EVENT_TYPES.DEVICE.BATTERY.SET,
|
|
100
|
-
properties: {
|
|
101
|
-
deviceId,
|
|
102
|
-
batteryLevel,
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
await publishAudit(payload);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async onDeviceMetaChange(deviceId: string, metaData: Record<string, any>) {
|
|
109
|
-
// TODO: For Future Consumption
|
|
110
|
-
/* await eventDispatcher.publishEvent(
|
|
111
|
-
DT_EVENT_TYPES.DEVICE.META_DATA.SET,
|
|
112
|
-
{ deviceId, metaData },
|
|
113
|
-
this.source
|
|
114
|
-
); */
|
|
115
|
-
const payload = {
|
|
116
|
-
eventType: DT_EVENT_TYPES.DEVICE.META_DATA.SET,
|
|
117
|
-
properties: {
|
|
118
|
-
deviceId,
|
|
119
|
-
metaData,
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
|
-
await publishAudit(payload);
|
|
123
|
-
}
|
|
124
|
-
}
|