dt-common-device 3.1.6 → 4.0.1

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.
Files changed (48) hide show
  1. package/dist/alerts/Alert.service.d.ts +7 -3
  2. package/dist/alerts/Alert.service.js +10 -4
  3. package/dist/device/cloud/interface.d.ts +101 -0
  4. package/dist/device/cloud/interface.js +3 -0
  5. package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
  6. package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -0
  7. package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
  8. package/dist/device/cloud/interfaces/IDevicesService.js +2 -0
  9. package/dist/device/cloud/services/Device.service.d.ts +39 -0
  10. package/dist/device/cloud/services/Device.service.js +9 -0
  11. package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
  12. package/dist/device/cloud/services/DeviceCloudService.js +59 -0
  13. package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
  14. package/dist/device/cloud/services/DeviceHub.service.js +6 -0
  15. package/dist/device/cloud/services/Hub.service.d.ts +25 -0
  16. package/dist/device/cloud/services/Hub.service.js +9 -0
  17. package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
  18. package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
  19. package/dist/device/index.d.ts +4 -0
  20. package/dist/device/index.js +20 -0
  21. package/dist/device/local/events/EventHandler.js +6 -6
  22. package/dist/device/local/events/Events.d.ts +12 -33
  23. package/dist/device/local/events/Events.js +12 -33
  24. package/dist/device/local/interface.d.ts +0 -0
  25. package/dist/device/local/interface.js +1 -0
  26. package/dist/device/local/services/Device.service.d.ts +8 -6
  27. package/dist/device/local/services/Device.service.js +49 -27
  28. package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
  29. package/dist/device/local/services/DeviceHub.service.js +40 -0
  30. package/dist/events/BaseEventHandler.d.ts +2 -1
  31. package/dist/events/BaseEventHandler.js +3 -2
  32. package/dist/events/BaseEventTransformer.d.ts +2 -2
  33. package/dist/events/BaseEventTransformer.js +2 -2
  34. package/dist/events/DeviceEventHandler.d.ts +1 -6
  35. package/dist/events/DeviceEventHandler.js +14 -75
  36. package/dist/events/DeviceEventTransformerFactory.js +2 -2
  37. package/dist/events/EventHandler.d.ts +7 -6
  38. package/dist/events/EventHandler.js +17 -62
  39. package/dist/events/EventProcessingService.d.ts +1 -1
  40. package/dist/events/EventProcessingService.js +5 -5
  41. package/dist/events/interfaces/DeviceEvent.d.ts +8 -4
  42. package/dist/events/interfaces/IEventTransformer.d.ts +2 -2
  43. package/dist/issues/Issue.service.d.ts +5 -1
  44. package/dist/issues/Issue.service.js +7 -1
  45. package/dist/queue/utils/queueUtils.js +1 -1
  46. package/dist/utils/redis.utils.d.ts +21 -0
  47. package/dist/utils/redis.utils.js +35 -0
  48. package/package.json +1 -1
@@ -3,17 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DT_EVENT_TYPES = void 0;
4
4
  exports.DT_EVENT_TYPES = {
5
5
  DEVICE: {
6
- CREATE: {
7
- SUCCESS: "device.create.success",
8
- FAILED: "device.create.failed",
9
- },
10
- UPDATE: {
11
- SUCCESS: "device.update.success",
12
- FAILED: "device.update.failed",
13
- },
14
- DELETE: {
15
- SUCCESS: "device.delete.success",
16
- FAILED: "device.delete.failed",
6
+ DEVICE: {
7
+ CREATED: "device.device.created",
8
+ UPDATED: "device.device.updated",
9
+ DELETED: "device.device.deleted",
17
10
  },
18
11
  STATE: {
19
12
  SET: "device.state.set",
@@ -44,31 +37,17 @@ exports.DT_EVENT_TYPES = {
44
37
  },
45
38
  },
46
39
  CONNECTION: {
47
- CREATE: {
48
- SUCCESS: "connection.create.success",
49
- FAILED: "connection.create.failed",
50
- },
51
- UPDATE: {
52
- SUCCESS: "connection.update.success",
53
- FAILED: "connection.update.failed",
54
- },
55
- DELETE: {
56
- SUCCESS: "connection.delete.success",
57
- FAILED: "connection.delete.failed",
40
+ CONNECTION: {
41
+ CREATED: "connection.connection.created",
42
+ UPDATED: "connection.connection.updated",
43
+ DELETED: "connection.connection.deleted",
58
44
  },
59
45
  },
60
46
  PROPERTY: {
61
- CREATE: {
62
- SUCCESS: "property.create.success",
63
- FAILED: "property.create.failed",
64
- },
65
- UPDATE: {
66
- SUCCESS: "property.update.success",
67
- FAILED: "property.update.failed",
68
- },
69
- DELETE: {
70
- SUCCESS: "property.delete.success",
71
- FAILED: "property.delete.failed",
47
+ PROPERTY: {
48
+ CREATED: "property.property.created",
49
+ UPDATED: "property.property.updated",
50
+ DELETED: "property.property.deleted",
72
51
  },
73
52
  PREFERENCES: {
74
53
  UPDATED: "property.preferences.updated",
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,5 +1,6 @@
1
1
  import { IDevice, IStatus } from "../interfaces";
2
2
  import { Source } from "../../../constants/Service";
3
+ import { AuditProperties } from "../../../audit/AuditProperties";
3
4
  export declare class LocalDeviceService {
4
5
  private readonly eventHandler;
5
6
  private readonly deviceRepository;
@@ -12,21 +13,22 @@ export declare class LocalDeviceService {
12
13
  getDevices(deviceIds: string[], withHubDetails?: boolean): Promise<IDevice[]>;
13
14
  getPropertyDevices(propertyId: string, selectDeviceId?: boolean, type?: string, withHubDetails?: boolean): Promise<IDevice[]>;
14
15
  getPropertyDeviceIds(propertyId: string, selectDeviceId: boolean | undefined, type: string): Promise<any>;
15
- updateDevice(deviceId: string, body: any): Promise<any>;
16
- deleteDevice(deviceId: string): Promise<any>;
16
+ updateDevice(deviceId: string, body: any, auditBody: AuditProperties): Promise<any>;
17
+ deleteDevice(deviceId: string, auditBody: AuditProperties): Promise<any>;
17
18
  getState(deviceId: string): Promise<any>;
18
- setState(deviceId: string, newState: any): Promise<void>;
19
+ setState(deviceId: string, newState: any, auditProperties: AuditProperties): Promise<void>;
19
20
  getStatus(deviceId: string): Promise<Record<string, any>>;
20
- setStatus(deviceId: string, newStatus: IStatus, source: Source, reason?: string): Promise<void>;
21
+ setStatus(deviceId: string, newStatus: IStatus, source: Source, auditBody: AuditProperties, reason?: string): Promise<void>;
21
22
  private handleOfflineStatus;
22
23
  private handleOnlineStatus;
23
24
  private getDeviceBaseline;
24
25
  getBatteryLevel(deviceId: string): Promise<Record<string, any>>;
25
- setBatteryLevel(deviceId: string, batteryLevel: number, source: Source): Promise<void>;
26
+ setBatteryLevel(deviceId: string, batteryLevel: number, source: Source, auditBody: AuditProperties): Promise<void>;
26
27
  private shouldUpdateBatteryLevel;
27
28
  private getPropertyBatteryThreshold;
29
+ private checkForDeviceMalfunctions;
28
30
  getMetaData(deviceId: string): Promise<any>;
29
- setMetaData(deviceId: string, metaData: Record<string, any>): Promise<any>;
31
+ setMetaData(deviceId: string, metaData: Record<string, any>, auditBody: AuditProperties): Promise<any>;
30
32
  getDevicesByZone(zoneId: string): Promise<import("../interfaces").IDtDevice[]>;
31
33
  getDevicesByAccessGroup(accessGroupId: string): Promise<import("../interfaces").IDtDevice[]>;
32
34
  querySelect(query: any, fields: string[]): Promise<any>;
@@ -93,19 +93,19 @@ let LocalDeviceService = (() => {
93
93
  }
94
94
  return await this.deviceRepository.getPropertyDeviceIds(propertyId, selectDeviceId, type);
95
95
  }
96
- async updateDevice(deviceId, body) {
96
+ async updateDevice(deviceId, body, auditBody) {
97
97
  if (!deviceId) {
98
98
  throw new Error("Device ID is required");
99
99
  }
100
100
  await this.deviceRepository.updateDevice(deviceId, body);
101
- return await this.eventHandler.onDeviceUpdate(deviceId, body);
101
+ return await this.eventHandler.onDeviceUpdate(deviceId, body, auditBody);
102
102
  }
103
- async deleteDevice(deviceId) {
103
+ async deleteDevice(deviceId, auditBody) {
104
104
  if (!deviceId) {
105
105
  throw new Error("Device ID is required");
106
106
  }
107
107
  await this.deviceRepository.deleteDevice(deviceId);
108
- return await this.eventHandler.onDeviceDelete(deviceId);
108
+ return await this.eventHandler.onDeviceDelete(deviceId, auditBody);
109
109
  }
110
110
  async getState(deviceId) {
111
111
  if (!deviceId) {
@@ -113,7 +113,7 @@ let LocalDeviceService = (() => {
113
113
  }
114
114
  return await this.deviceRepository.getState(deviceId);
115
115
  }
116
- async setState(deviceId, newState) {
116
+ async setState(deviceId, newState, auditProperties) {
117
117
  if (!deviceId || !newState) {
118
118
  throw new Error("Device ID and new state are required");
119
119
  }
@@ -122,7 +122,7 @@ let LocalDeviceService = (() => {
122
122
  const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
123
123
  if (changedKeys.length > 0) {
124
124
  await this.deviceRepository.setState(deviceId, newState);
125
- return await this.eventHandler.onStateChange(deviceId, newState);
125
+ return await this.eventHandler.onStateChange(deviceId, newState, auditProperties);
126
126
  }
127
127
  }
128
128
  async getStatus(deviceId) {
@@ -131,7 +131,7 @@ let LocalDeviceService = (() => {
131
131
  }
132
132
  return await this.deviceRepository.getStatus(deviceId);
133
133
  }
134
- async setStatus(deviceId, newStatus, source, reason) {
134
+ async setStatus(deviceId, newStatus, source, auditBody, reason) {
135
135
  if (!deviceId || !newStatus) {
136
136
  throw new Error("Device ID and new status are required");
137
137
  }
@@ -143,19 +143,19 @@ let LocalDeviceService = (() => {
143
143
  const isNewStatusOffline = newStatus.liveStatus === "OFFLINE";
144
144
  if (isNewStatusOffline) {
145
145
  // New Status = OFFLINE
146
- await this.handleOfflineStatus(deviceId, device, oldStatus, newStatus, source, reason, currentTime);
146
+ await this.handleOfflineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime);
147
147
  }
148
148
  else if (isNewStatusOnline) {
149
149
  // New Status = ONLINE
150
- await this.handleOnlineStatus(deviceId, device, oldStatus, newStatus, source, reason, currentTime);
150
+ await this.handleOnlineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime);
151
151
  }
152
152
  else {
153
153
  // For any other status, just update normally
154
154
  await this.deviceRepository.setStatus(deviceId, newStatus);
155
- await this.eventHandler.onStatusChange(deviceId, newStatus);
155
+ await this.eventHandler.onStatusChange(deviceId, newStatus, auditBody);
156
156
  }
157
157
  }
158
- async handleOfflineStatus(deviceId, device, oldStatus, newStatus, source, reason, currentTime) {
158
+ async handleOfflineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime) {
159
159
  const isExistingStatusOnline = oldStatus?.online === true;
160
160
  const isExistingStatusOffline = oldStatus?.online === false;
161
161
  if (isExistingStatusOnline) {
@@ -168,15 +168,15 @@ let LocalDeviceService = (() => {
168
168
  message: reason || "Device went offline",
169
169
  default: {},
170
170
  };
171
- await this.deviceRepository.setStatus(deviceId, newStatus);
172
- await this.eventHandler.onStatusChange(deviceId, newStatus);
171
+ await this.deviceRepository.setStatus(device.deviceId, newStatus);
172
+ await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
173
173
  }
174
174
  else if (isExistingStatusOffline) {
175
175
  // Existing status is Offline
176
176
  const timeLapsed = oldStatus?.lastUpdated
177
177
  ? Date.now() - new Date(oldStatus.lastUpdated).getTime()
178
178
  : 0;
179
- const baselineTime = await this.getDeviceBaseline(deviceId);
179
+ const baselineTime = await this.getDeviceBaseline(device.deviceId);
180
180
  if (timeLapsed > baselineTime) {
181
181
  // When the time lapsed is higher than the baseline time
182
182
  newStatus.online = false;
@@ -187,16 +187,16 @@ let LocalDeviceService = (() => {
187
187
  message: reason || "Device has been offline for longer than baseline",
188
188
  default: {},
189
189
  };
190
- await this.deviceRepository.setStatus(deviceId, newStatus);
191
- await this.eventHandler.onStatusChange(deviceId, newStatus);
192
- // Raise alert
190
+ await this.deviceRepository.setStatus(device.deviceId, newStatus);
191
+ await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
192
+ // Raise alert (OPERATIONAL only)
193
193
  await this.alertService.raiseDeviceOfflineAlert(device, source, reason);
194
- // Raise issue when the device goes offline if longer than the baseline
194
+ // Raise issue when the device goes offline if longer than the baseline (OPERATIONAL only)
195
195
  await this.issueService.createDeviceOfflineIssue(device, source, reason);
196
196
  }
197
197
  }
198
198
  }
199
- async handleOnlineStatus(deviceId, device, oldStatus, newStatus, source, reason, currentTime) {
199
+ async handleOnlineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime) {
200
200
  const isExistingStatusOnline = oldStatus?.online === true;
201
201
  const isExistingStatusOffline = oldStatus?.online === false;
202
202
  if (isExistingStatusOnline) {
@@ -211,8 +211,8 @@ let LocalDeviceService = (() => {
211
211
  newStatus.liveStatus = "ONLINE";
212
212
  newStatus.lastUpdated = currentTime;
213
213
  newStatus.error = {}; // Clear the error
214
- await this.deviceRepository.setStatus(deviceId, newStatus);
215
- await this.eventHandler.onStatusChange(deviceId, newStatus);
214
+ await this.deviceRepository.setStatus(device.deviceId, newStatus);
215
+ await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
216
216
  //TODO: ALERT NEEDED?
217
217
  // Raise alert
218
218
  await this.alertService.raiseDeviceOnlineAlert(device, source, reason);
@@ -224,8 +224,8 @@ let LocalDeviceService = (() => {
224
224
  newStatus.liveStatus = "ONLINE";
225
225
  newStatus.lastUpdated = currentTime;
226
226
  newStatus.error = undefined; // Clear the error
227
- await this.deviceRepository.setStatus(deviceId, newStatus);
228
- await this.eventHandler.onStatusChange(deviceId, newStatus);
227
+ await this.deviceRepository.setStatus(device.deviceId, newStatus);
228
+ await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
229
229
  //TODO: ALERT NEEDED?
230
230
  // Raise alert
231
231
  await this.alertService.raiseDeviceOnlineAlert(device, source, reason);
@@ -244,7 +244,7 @@ let LocalDeviceService = (() => {
244
244
  }
245
245
  return await this.deviceRepository.getBatteryLevel(deviceId);
246
246
  }
247
- async setBatteryLevel(deviceId, batteryLevel, source) {
247
+ async setBatteryLevel(deviceId, batteryLevel, source, auditBody) {
248
248
  if (!deviceId || batteryLevel === undefined || batteryLevel === null) {
249
249
  throw new Error("Device ID and battery level are required");
250
250
  }
@@ -260,7 +260,7 @@ let LocalDeviceService = (() => {
260
260
  if (shouldUpdate) {
261
261
  // Save the battery level in the device
262
262
  await this.deviceRepository.setBatteryLevel(deviceId, batteryLevel);
263
- await this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel);
263
+ await this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel, auditBody);
264
264
  // Get property threshold
265
265
  const propertyThreshold = await this.getPropertyBatteryThreshold(device.propertyId);
266
266
  // Check if battery level is below threshold
@@ -290,17 +290,39 @@ let LocalDeviceService = (() => {
290
290
  // In a real implementation, this would fetch from property configuration or settings
291
291
  return 20; // 20% default threshold
292
292
  }
293
+ async checkForDeviceMalfunctions(device, source, reason) {
294
+ // TODO: Implement device malfunction detection logic
295
+ // This should check for:
296
+ // - Lock jammed
297
+ // - Device not accepting codes
298
+ // - Other malfunction indicators
299
+ // For now, we'll check if the reason indicates a malfunction
300
+ const malfunctionIndicators = [
301
+ "jammed",
302
+ "not accepting codes",
303
+ "malfunction",
304
+ "error",
305
+ "failure",
306
+ ];
307
+ const hasMalfunction = malfunctionIndicators.some((indicator) => reason?.toLowerCase().includes(indicator));
308
+ if (hasMalfunction) {
309
+ // Raise alert for device malfunction (READINESS + OPERATIONAL)
310
+ await this.alertService.raiseDeviceIssueAlert(device, "Device Malfunction Detected", source, reason);
311
+ // Raise issue for device malfunction (READINESS + OPERATIONAL)
312
+ await this.issueService.createDeviceMalfunctionIssue(device, "Device Malfunction", source, reason);
313
+ }
314
+ }
293
315
  async getMetaData(deviceId) {
294
316
  if (!deviceId) {
295
317
  throw new Error("Device ID is required");
296
318
  }
297
319
  return await this.deviceRepository.getMetaData(deviceId);
298
320
  }
299
- async setMetaData(deviceId, metaData) {
321
+ async setMetaData(deviceId, metaData, auditBody) {
300
322
  if (!deviceId || !metaData) {
301
323
  throw new Error("Device ID and meta data are required");
302
324
  }
303
- await this.eventHandler.onDeviceMetaChange(deviceId, metaData);
325
+ await this.eventHandler.onDeviceMetaChange(deviceId, metaData, auditBody);
304
326
  return await this.deviceRepository.setMetaData(deviceId, metaData);
305
327
  }
306
328
  async getDevicesByZone(zoneId) {
@@ -0,0 +1,11 @@
1
+ import { IHubCreateParams } from "../interfaces";
2
+ export declare class DeviceHubService {
3
+ private readonly baseUrl;
4
+ constructor();
5
+ addHub(body: IHubCreateParams): Promise<any>;
6
+ getHubs(hubIds: string[]): Promise<any>;
7
+ getHub(hubId: string): Promise<any>;
8
+ updateHub(hubId: string, body: any): Promise<any>;
9
+ deleteHub(hubId: string): Promise<any>;
10
+ deleteAllHubs(hubIds: string[]): Promise<any>;
11
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DeviceHubService = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const config_1 = require("../../../config/config");
9
+ class DeviceHubService {
10
+ constructor() {
11
+ const { DEVICE_SERVICE } = (0, config_1.getConfig)();
12
+ if (!DEVICE_SERVICE) {
13
+ throw new Error("DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE.");
14
+ }
15
+ this.baseUrl = DEVICE_SERVICE;
16
+ }
17
+ async addHub(body) {
18
+ return await axios_1.default.post(`${this.baseUrl}/devices/hubs`, body);
19
+ }
20
+ //get hubs takes an array of hub ids as query params
21
+ async getHubs(hubIds) {
22
+ const query = hubIds && hubIds.length ? `?ids=${hubIds.join(",")}` : "";
23
+ return await axios_1.default.get(`${this.baseUrl}/devices/hubs${query}`);
24
+ }
25
+ //get hub takes a hub id in params
26
+ async getHub(hubId) {
27
+ return await axios_1.default.get(`${this.baseUrl}/devices/hubs/${hubId}`);
28
+ }
29
+ async updateHub(hubId, body) {
30
+ return await axios_1.default.put(`${this.baseUrl}/devices/hubs/${hubId}`, body);
31
+ }
32
+ async deleteHub(hubId) {
33
+ return await axios_1.default.delete(`${this.baseUrl}/devices/hubs/${hubId}`);
34
+ }
35
+ async deleteAllHubs(hubIds) {
36
+ const query = hubIds.length ? `?ids=${hubIds.join(",")}` : "";
37
+ return await axios_1.default.delete(`${this.baseUrl}/devices/hubs${query}`);
38
+ }
39
+ }
40
+ exports.DeviceHubService = DeviceHubService;
@@ -2,6 +2,7 @@ import { DeviceEvent } from "./interfaces/DeviceEvent";
2
2
  import { IEventHandler } from "./interfaces/IEventHandler";
3
3
  import { ILogger } from "../config/config.types";
4
4
  import { LocalDeviceService } from "../device/local/services/Device.service";
5
+ import { AuditProperties } from "../audit/AuditProperties";
5
6
  export declare abstract class BaseEventHandler implements IEventHandler {
6
7
  protected readonly supportedEventTypes: string[];
7
8
  protected readonly priority: number;
@@ -19,7 +20,7 @@ export declare abstract class BaseEventHandler implements IEventHandler {
19
20
  * Handle multiple events - default implementation processes them sequentially
20
21
  */
21
22
  handleEvents(events: DeviceEvent[]): Promise<void>;
22
- onStateChange(deviceId: string, state: Record<string, any>, event: DeviceEvent): Promise<void>;
23
+ onStateChange(deviceId: string, state: Record<string, any>, event: DeviceEvent, auditProperties: AuditProperties): Promise<void>;
23
24
  /**
24
25
  * Check if this handler can process the given event type
25
26
  */
@@ -65,9 +65,10 @@ class BaseEventHandler {
65
65
  this.logger.error(`${this.constructor.name}: handleEvents Error`, error);
66
66
  }
67
67
  }
68
- async onStateChange(deviceId, state, event) {
68
+ // TODO: Check AuditBody for this function
69
+ async onStateChange(deviceId, state, event, auditProperties) {
69
70
  try {
70
- await this.localDeviceService.setState(deviceId, state);
71
+ await this.localDeviceService.setState(deviceId, state, auditProperties);
71
72
  await this.afterEvent(event);
72
73
  }
73
74
  catch (error) {
@@ -10,7 +10,7 @@ export declare abstract class BaseEventTransformer implements IEventTransformer
10
10
  /**
11
11
  * Transform parsed data into DeviceEvent format - to be implemented by device-specific transformers
12
12
  */
13
- abstract transform(parsedData: any): DeviceEvent | DeviceEvent[];
13
+ abstract transform(parsedData: any): Promise<DeviceEvent | DeviceEvent[]>;
14
14
  /**
15
15
  * Validate transformed event - default implementation, can be overridden
16
16
  */
@@ -22,5 +22,5 @@ export declare abstract class BaseEventTransformer implements IEventTransformer
22
22
  /**
23
23
  * Execute the full transformation pipeline
24
24
  */
25
- executeTransformation(): DeviceEvent[];
25
+ executeTransformation(): Promise<DeviceEvent[]>;
26
26
  }
@@ -46,13 +46,13 @@ class BaseEventTransformer {
46
46
  /**
47
47
  * Execute the full transformation pipeline
48
48
  */
49
- executeTransformation() {
49
+ async executeTransformation() {
50
50
  try {
51
51
  this.logger.info(`Starting transformation for device type: ${this.deviceType}`);
52
52
  // Step 1: Parse the raw data
53
53
  const parsedData = this.parseData(this.rawData);
54
54
  // Step 2: Transform to DeviceEvent format
55
- const transformedResult = this.transform(parsedData);
55
+ const transformedResult = await this.transform(parsedData);
56
56
  // Step 3: Convert to array format
57
57
  // TODO: Check if any service sends multiple events
58
58
  const events = Array.isArray(transformedResult)
@@ -1,15 +1,10 @@
1
1
  import { IDevice } from "../device/local/interfaces";
2
2
  import { BaseEventHandler } from "./BaseEventHandler";
3
- import { DeviceEvent, DeviceEventEntity, EventConstructionOptions } from "./interfaces/DeviceEvent";
3
+ import { DeviceEvent } from "./interfaces/DeviceEvent";
4
4
  export declare class DeviceEventHandler extends BaseEventHandler {
5
5
  private readonly localHubService;
6
6
  constructor();
7
7
  onEvent(event: DeviceEvent): Promise<void>;
8
8
  updateDeviceEventData(deviceData: IDevice, event: DeviceEvent): Promise<void>;
9
9
  extractHubDetails(device: IDevice): Promise<any[]>;
10
- constructDeviceEventEntity(eventData: any, device: IDevice, hubDetails: any[], options?: EventConstructionOptions): DeviceEventEntity;
11
- dumpToEventOperation(data: any, device: IDevice): Promise<void>;
12
- updateTablesBasedOnEventType(eventData: any, device: IDevice): Promise<void>;
13
- updateDeviceStatus(data: any, device: IDevice): Promise<void>;
14
- updateDeviceBattery(data: any, device: IDevice): Promise<void>;
15
10
  }
@@ -92,6 +92,8 @@ let DeviceEventHandler = (() => {
92
92
  Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL,
93
93
  Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW,
94
94
  Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED,
95
+ Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.UPDATED,
96
+ Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.UNKNOWN,
95
97
  ], 100);
96
98
  // Use dependency injection instead of creating new instance
97
99
  this.localHubService = typedi_1.default.get(services_1.LocalHubService);
@@ -106,13 +108,9 @@ let DeviceEventHandler = (() => {
106
108
  throw new Error("[DT | CDL]:[DeviceEventHandler]: deviceId does not exist");
107
109
  }
108
110
  await this.updateDeviceEventData(device, event);
109
- // Update relevant tables based on event type
110
- await this.updateTablesBasedOnEventType(event, device);
111
- // Save event logs
112
- await this.dumpToEventOperation(event, device);
113
111
  }
114
112
  catch (error) {
115
- this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error processing event", error);
113
+ this.logger.error(`[DT | CDL]:[DeviceEventHandler]: Error processing event: ${event.eventName} ERROR: ${error}`);
116
114
  throw error;
117
115
  }
118
116
  }
@@ -134,10 +132,20 @@ let DeviceEventHandler = (() => {
134
132
  default: {},
135
133
  },
136
134
  },
135
+ }, {
136
+ deviceId: deviceData.deviceId,
137
+ deviceName: deviceData.name,
138
+ propertyId: deviceData.propertyId,
139
+ resource: "device",
137
140
  });
138
141
  break;
139
142
  case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
140
- await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0, constants_1.Source.CLOUD_EVENT);
143
+ await this.localDeviceService.setBatteryLevel(deviceData.deviceId, event?.data?.batteryLevel ?? 0, constants_1.Source.CLOUD_EVENT, {
144
+ deviceId: deviceData.deviceId,
145
+ deviceName: deviceData.name,
146
+ propertyId: deviceData.propertyId,
147
+ resource: "device",
148
+ });
141
149
  break;
142
150
  }
143
151
  }
@@ -159,75 +167,6 @@ let DeviceEventHandler = (() => {
159
167
  }
160
168
  return hubDetails;
161
169
  }
162
- constructDeviceEventEntity(eventData, device, hubDetails, options = {}) {
163
- return {
164
- event: {
165
- eventId: options.eventId || eventData?.eventId || "",
166
- eventName: options.eventName || eventData?.eventName || "",
167
- status: options.status || eventData?.status || "",
168
- mode: options.mode || eventData?.data?.mode,
169
- userName: options.userName || eventData?.userName || "",
170
- userId: options.userId || eventData?.userId || "",
171
- userType: options.userType || eventData?.userType || "",
172
- raw_event: options.rawEvent || eventData?.rawData || {},
173
- batteryLevel: options.batteryLevel || eventData?.data?.batteryLevel || "",
174
- reason: options.reason || eventData?.data?.reason || "",
175
- eventDescription: options.eventDescription || eventData?.event || "",
176
- },
177
- device: {
178
- id: eventData?.deviceId || "",
179
- name: device?.name || "",
180
- type: device?.deviceType?.type || "",
181
- brand: device?.specifications?.manufacturer || "",
182
- model: device?.specifications?.model || "",
183
- },
184
- hub: hubDetails,
185
- property: {
186
- id: eventData?.property?.id || "",
187
- name: eventData?.property?.name || "",
188
- location: eventData?.property?.apartment || "",
189
- },
190
- };
191
- }
192
- async dumpToEventOperation(data, device) {
193
- try {
194
- const hubDetails = await this.extractHubDetails(device);
195
- const eventData = this.constructDeviceEventEntity(data, device, hubDetails, {
196
- status: data.eventName === "lock.unlock" ? "UNLOCKED" : "LOCKED",
197
- rawEvent: data.rawData,
198
- });
199
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Event data dumped to event operation", { eventData });
200
- }
201
- catch (error) {
202
- this.logger.error("[DT | CDL]:[DeviceEventHandler]: Error dumping to event operation", error);
203
- }
204
- }
205
- async updateTablesBasedOnEventType(eventData, device) {
206
- switch (eventData.eventName) {
207
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.ONLINE:
208
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.OFFLINE:
209
- case Event_1.DT_EVENT_TYPES.DEVICE.STATUS.CHANGED:
210
- this.updateDeviceStatus(eventData, device);
211
- break;
212
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CHANGED:
213
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.CRITICAL:
214
- case Event_1.DT_EVENT_TYPES.DEVICE.BATTERY.LOW:
215
- this.updateDeviceBattery(eventData, device);
216
- break;
217
- }
218
- }
219
- async updateDeviceStatus(data, device) {
220
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device status", {
221
- data,
222
- device,
223
- });
224
- }
225
- async updateDeviceBattery(data, device) {
226
- this.logger.info("[DT | CDL]:[DeviceEventHandler]: Updating device battery", {
227
- data,
228
- device,
229
- });
230
- }
231
170
  };
232
171
  __setFunctionName(_classThis, "DeviceEventHandler");
233
172
  (() => {
@@ -72,10 +72,10 @@ let DeviceEventTransformerFactory = (() => {
72
72
  // Default validation - always return true
73
73
  return true;
74
74
  }
75
- executeTransformation() {
75
+ async executeTransformation() {
76
76
  try {
77
77
  const parsedData = this.parseData(this.rawData);
78
- const transformedResult = this.transform(parsedData);
78
+ const transformedResult = await this.transform(parsedData);
79
79
  const events = Array.isArray(transformedResult) ? transformedResult : [transformedResult];
80
80
  return events.filter((event) => this.validate(event));
81
81
  }
@@ -1,11 +1,12 @@
1
+ import { AuditProperties } from "../audit/AuditProperties";
1
2
  export declare class EventHandler {
2
3
  private readonly source;
3
4
  constructor();
4
5
  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>;
6
+ onDeviceUpdate(deviceId: string, body: any, auditBody: AuditProperties): Promise<void>;
7
+ onDeviceDelete(deviceId: string, auditBody: AuditProperties): Promise<void>;
8
+ onStateChange(deviceId: string, state: any, auditProperties: AuditProperties): Promise<void>;
9
+ onStatusChange(deviceId: string, status: any, auditProperties: AuditProperties): Promise<void>;
10
+ onBatteryLevelChange(deviceId: string, batteryLevel: number, auditProperties: AuditProperties): Promise<void>;
11
+ onDeviceMetaChange(deviceId: string, metaData: Record<string, any>, auditProperties: AuditProperties): Promise<void>;
11
12
  }