dt-common-device 1.2.10 → 1.2.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.
@@ -9,9 +9,9 @@ class EventHandler {
9
9
  this.source = "dt-common-device";
10
10
  }
11
11
  async onDeviceCreate(body) {
12
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_CREATED, body, this.source);
12
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.CREATED, body, this.source);
13
13
  const payload = {
14
- eventType: Events_1.DeviceEvents.DEVICE_CREATED,
14
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.CREATED,
15
15
  properties: {
16
16
  ...body,
17
17
  },
@@ -19,9 +19,9 @@ class EventHandler {
19
19
  await (0, dt_audit_library_1.publishAudit)(payload);
20
20
  }
21
21
  async onDeviceUpdate(deviceId, body) {
22
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_UPDATED, { deviceId, body }, this.source);
22
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED, { deviceId, body }, this.source);
23
23
  const payload = {
24
- eventType: Events_1.DeviceEvents.DEVICE_UPDATED,
24
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED,
25
25
  properties: {
26
26
  ...body,
27
27
  },
@@ -29,9 +29,9 @@ class EventHandler {
29
29
  await (0, dt_audit_library_1.publishAudit)(payload);
30
30
  }
31
31
  async onDeviceDelete(deviceId) {
32
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_DELETED, { deviceId }, this.source);
32
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.DELETED, { deviceId }, this.source);
33
33
  const payload = {
34
- eventType: Events_1.DeviceEvents.DEVICE_DELETED,
34
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.DELETED,
35
35
  properties: {
36
36
  deviceId,
37
37
  },
@@ -39,9 +39,9 @@ class EventHandler {
39
39
  await (0, dt_audit_library_1.publishAudit)(payload);
40
40
  }
41
41
  async onStateChange(deviceId, state) {
42
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_STATE_SET, { deviceId, state }, this.source);
42
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.STATE.SET, { deviceId, state }, this.source);
43
43
  const payload = {
44
- eventType: Events_1.DeviceEvents.DEVICE_STATE_SET,
44
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.STATE.SET,
45
45
  properties: {
46
46
  deviceId,
47
47
  state,
@@ -50,9 +50,9 @@ class EventHandler {
50
50
  await (0, dt_audit_library_1.publishAudit)(payload);
51
51
  }
52
52
  async onStatusChange(deviceId, status) {
53
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_STATUS_SET, { deviceId, status }, this.source);
53
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.STATUS.SET, { deviceId, status }, this.source);
54
54
  const payload = {
55
- eventType: Events_1.DeviceEvents.DEVICE_STATUS_SET,
55
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.STATUS.SET,
56
56
  properties: {
57
57
  deviceId,
58
58
  status,
@@ -61,9 +61,9 @@ class EventHandler {
61
61
  await (0, dt_audit_library_1.publishAudit)(payload);
62
62
  }
63
63
  async onBatteryLevelChange(deviceId, batteryLevel) {
64
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_BATTERY_SET, { deviceId, batteryLevel }, this.source);
64
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.BATTERY.SET, { deviceId, batteryLevel }, this.source);
65
65
  const payload = {
66
- eventType: Events_1.DeviceEvents.DEVICE_BATTERY_SET,
66
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.BATTERY.SET,
67
67
  properties: {
68
68
  deviceId,
69
69
  batteryLevel,
@@ -72,9 +72,9 @@ class EventHandler {
72
72
  await (0, dt_audit_library_1.publishAudit)(payload);
73
73
  }
74
74
  async onDeviceMetaChange(deviceId, metaData) {
75
- await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_META_DATA_SET, { deviceId, metaData }, this.source);
75
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.META_DATA.SET, { deviceId, metaData }, this.source);
76
76
  const payload = {
77
- eventType: Events_1.DeviceEvents.DEVICE_META_DATA_SET,
77
+ eventType: Events_1.DT_EVENT_TYPES.DEVICE.META_DATA.SET,
78
78
  properties: {
79
79
  deviceId,
80
80
  metaData,
@@ -1,21 +1,54 @@
1
- export declare const DeviceEvents: {
2
- DEVICE_CREATED: string;
3
- DEVICE_UPDATED: string;
4
- DEVICE_DELETED: string;
5
- DEVICE_STATE_SET: string;
6
- DEVICE_STATUS_SET: string;
7
- DEVICE_BATTERY_SET: string;
8
- DEVICE_META_DATA_SET: string;
9
- };
10
- export declare const ConnectionEvents: {
11
- CONNECTION_UPDATED: string;
12
- CONNECTION_DELETED: string;
13
- CONNECTION_CREATED: string;
14
- };
15
- export declare const PropertyEvents: {
16
- PROPERTY_UPDATED: string;
17
- PROPERTY_DELETED: string;
18
- PROPERTY_CREATED: string;
19
- PROPERTY_PREFERENCES_UPDATED: string;
20
- PROPERTY_PREFERENCES_CREATED: string;
1
+ export declare const DT_EVENT_TYPES: {
2
+ DEVICE: {
3
+ DEVICE: {
4
+ CREATED: string;
5
+ UPDATED: string;
6
+ DELETED: string;
7
+ };
8
+ STATE: {
9
+ SET: string;
10
+ UNKNOWN: string;
11
+ UPDATED: string;
12
+ CHANGED: string;
13
+ };
14
+ STATUS: {
15
+ SET: string;
16
+ ONLINE: string;
17
+ OFFLINE: string;
18
+ UNKNOWN: string;
19
+ UPDATED: string;
20
+ CHANGED: string;
21
+ };
22
+ BATTERY: {
23
+ SET: string;
24
+ UNKNOWN: string;
25
+ LOW: string;
26
+ CRITICAL: string;
27
+ UPDATED: string;
28
+ CHANGED: string;
29
+ };
30
+ META_DATA: {
31
+ SET: string;
32
+ UPDATED: string;
33
+ CHANGED: string;
34
+ };
35
+ };
36
+ CONNECTION: {
37
+ CONNECTION: {
38
+ CREATED: string;
39
+ UPDATED: string;
40
+ DELETED: string;
41
+ };
42
+ };
43
+ PROPERTY: {
44
+ PROPERTY: {
45
+ CREATED: string;
46
+ UPDATED: string;
47
+ DELETED: string;
48
+ };
49
+ PREFERENCES: {
50
+ UPDATED: string;
51
+ CREATED: string;
52
+ };
53
+ };
21
54
  };
@@ -1,24 +1,57 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PropertyEvents = exports.ConnectionEvents = exports.DeviceEvents = void 0;
4
- exports.DeviceEvents = {
5
- DEVICE_CREATED: "device.created",
6
- DEVICE_UPDATED: "device.updated",
7
- DEVICE_DELETED: "device.deleted",
8
- DEVICE_STATE_SET: "device.state.set",
9
- DEVICE_STATUS_SET: "device.status.set",
10
- DEVICE_BATTERY_SET: "device.battery.set",
11
- DEVICE_META_DATA_SET: "device.metaData.set",
12
- };
13
- exports.ConnectionEvents = {
14
- CONNECTION_UPDATED: "connection.updated",
15
- CONNECTION_DELETED: "connection.deleted",
16
- CONNECTION_CREATED: "connection.created",
17
- };
18
- exports.PropertyEvents = {
19
- PROPERTY_UPDATED: "property.updated",
20
- PROPERTY_DELETED: "property.deleted",
21
- PROPERTY_CREATED: "property.created",
22
- PROPERTY_PREFERENCES_UPDATED: "property.preferences.updated",
23
- PROPERTY_PREFERENCES_CREATED: "property.preferences.created",
3
+ exports.DT_EVENT_TYPES = void 0;
4
+ exports.DT_EVENT_TYPES = {
5
+ DEVICE: {
6
+ DEVICE: {
7
+ CREATED: "device.device.created",
8
+ UPDATED: "device.device.updated",
9
+ DELETED: "device.device.deleted",
10
+ },
11
+ STATE: {
12
+ SET: "device.state.set",
13
+ UNKNOWN: "device.state.unknown",
14
+ UPDATED: "device.state.updated",
15
+ CHANGED: "device.state.changed",
16
+ },
17
+ STATUS: {
18
+ SET: "device.status.set",
19
+ ONLINE: "device.status.online",
20
+ OFFLINE: "device.status.offline",
21
+ UNKNOWN: "device.status.unknown",
22
+ UPDATED: "device.status.updated",
23
+ CHANGED: "device.status.changed",
24
+ },
25
+ BATTERY: {
26
+ SET: "device.battery.set",
27
+ UNKNOWN: "device.battery.unknown",
28
+ LOW: "device.battery.low",
29
+ CRITICAL: "device.battery.critical",
30
+ UPDATED: "device.battery.updated",
31
+ CHANGED: "device.battery.changed",
32
+ },
33
+ META_DATA: {
34
+ SET: "device.metaData.set",
35
+ UPDATED: "device.metaData.updated",
36
+ CHANGED: "device.metaData.changed",
37
+ },
38
+ },
39
+ CONNECTION: {
40
+ CONNECTION: {
41
+ CREATED: "connection.connection.created",
42
+ UPDATED: "connection.connection.updated",
43
+ DELETED: "connection.connection.deleted",
44
+ },
45
+ },
46
+ PROPERTY: {
47
+ PROPERTY: {
48
+ CREATED: "property.property.created",
49
+ UPDATED: "property.property.updated",
50
+ DELETED: "property.property.deleted",
51
+ },
52
+ PREFERENCES: {
53
+ UPDATED: "property.preferences.updated",
54
+ CREATED: "property.preferences.created",
55
+ },
56
+ },
24
57
  };
@@ -68,7 +68,7 @@ class LocalDeviceService {
68
68
  throw new Error("Device ID and new state are required");
69
69
  }
70
70
  // If old state and new state are different
71
- const oldState = (await this.getState(deviceId))?.data?.state || {};
71
+ const oldState = (await this.getState(deviceId))?.data?.state ?? {};
72
72
  const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
73
73
  if (changedKeys.length > 0) {
74
74
  return await this.eventHandler.onStateChange(deviceId, newState);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -1,19 +1,21 @@
1
1
  import { eventDispatcher } from "dt-pub-sub";
2
2
  import { publishAudit } from "dt-audit-library";
3
- import { DeviceEvents } from "./Events";
3
+ import { DT_EVENT_TYPES } from "./Events";
4
4
 
5
5
  export class EventHandler {
6
- private readonly source = "dt-common-device";
7
- constructor() {}
6
+ private readonly source: string;
7
+ constructor() {
8
+ this.source = "dt-common-device";
9
+ }
8
10
 
9
11
  async onDeviceCreate(body: any) {
10
12
  await eventDispatcher.publishEvent(
11
- DeviceEvents.DEVICE_CREATED,
13
+ DT_EVENT_TYPES.DEVICE.DEVICE.CREATED,
12
14
  body,
13
15
  this.source
14
16
  );
15
17
  const payload = {
16
- eventType: DeviceEvents.DEVICE_CREATED,
18
+ eventType: DT_EVENT_TYPES.DEVICE.DEVICE.CREATED,
17
19
  properties: {
18
20
  ...body,
19
21
  },
@@ -23,12 +25,12 @@ export class EventHandler {
23
25
 
24
26
  async onDeviceUpdate(deviceId: string, body: any) {
25
27
  await eventDispatcher.publishEvent(
26
- DeviceEvents.DEVICE_UPDATED,
28
+ DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED,
27
29
  { deviceId, body },
28
30
  this.source
29
31
  );
30
32
  const payload = {
31
- eventType: DeviceEvents.DEVICE_UPDATED,
33
+ eventType: DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED,
32
34
  properties: {
33
35
  ...body,
34
36
  },
@@ -38,12 +40,12 @@ export class EventHandler {
38
40
 
39
41
  async onDeviceDelete(deviceId: string) {
40
42
  await eventDispatcher.publishEvent(
41
- DeviceEvents.DEVICE_DELETED,
43
+ DT_EVENT_TYPES.DEVICE.DEVICE.DELETED,
42
44
  { deviceId },
43
45
  this.source
44
46
  );
45
47
  const payload = {
46
- eventType: DeviceEvents.DEVICE_DELETED,
48
+ eventType: DT_EVENT_TYPES.DEVICE.DEVICE.DELETED,
47
49
  properties: {
48
50
  deviceId,
49
51
  },
@@ -53,12 +55,12 @@ export class EventHandler {
53
55
 
54
56
  async onStateChange(deviceId: string, state: any) {
55
57
  await eventDispatcher.publishEvent(
56
- DeviceEvents.DEVICE_STATE_SET,
58
+ DT_EVENT_TYPES.DEVICE.STATE.SET,
57
59
  { deviceId, state },
58
60
  this.source
59
61
  );
60
62
  const payload = {
61
- eventType: DeviceEvents.DEVICE_STATE_SET,
63
+ eventType: DT_EVENT_TYPES.DEVICE.STATE.SET,
62
64
  properties: {
63
65
  deviceId,
64
66
  state,
@@ -69,12 +71,12 @@ export class EventHandler {
69
71
 
70
72
  async onStatusChange(deviceId: string, status: any) {
71
73
  await eventDispatcher.publishEvent(
72
- DeviceEvents.DEVICE_STATUS_SET,
74
+ DT_EVENT_TYPES.DEVICE.STATUS.SET,
73
75
  { deviceId, status },
74
76
  this.source
75
77
  );
76
78
  const payload = {
77
- eventType: DeviceEvents.DEVICE_STATUS_SET,
79
+ eventType: DT_EVENT_TYPES.DEVICE.STATUS.SET,
78
80
  properties: {
79
81
  deviceId,
80
82
  status,
@@ -85,12 +87,12 @@ export class EventHandler {
85
87
 
86
88
  async onBatteryLevelChange(deviceId: string, batteryLevel: number) {
87
89
  await eventDispatcher.publishEvent(
88
- DeviceEvents.DEVICE_BATTERY_SET,
90
+ DT_EVENT_TYPES.DEVICE.BATTERY.SET,
89
91
  { deviceId, batteryLevel },
90
92
  this.source
91
93
  );
92
94
  const payload = {
93
- eventType: DeviceEvents.DEVICE_BATTERY_SET,
95
+ eventType: DT_EVENT_TYPES.DEVICE.BATTERY.SET,
94
96
  properties: {
95
97
  deviceId,
96
98
  batteryLevel,
@@ -101,12 +103,12 @@ export class EventHandler {
101
103
 
102
104
  async onDeviceMetaChange(deviceId: string, metaData: Record<string, any>) {
103
105
  await eventDispatcher.publishEvent(
104
- DeviceEvents.DEVICE_META_DATA_SET,
106
+ DT_EVENT_TYPES.DEVICE.META_DATA.SET,
105
107
  { deviceId, metaData },
106
108
  this.source
107
109
  );
108
110
  const payload = {
109
- eventType: DeviceEvents.DEVICE_META_DATA_SET,
111
+ eventType: DT_EVENT_TYPES.DEVICE.META_DATA.SET,
110
112
  properties: {
111
113
  deviceId,
112
114
  metaData,
@@ -1,23 +1,54 @@
1
- export const DeviceEvents = {
2
- DEVICE_CREATED: "device.created",
3
- DEVICE_UPDATED: "device.updated",
4
- DEVICE_DELETED: "device.deleted",
5
- DEVICE_STATE_SET: "device.state.set",
6
- DEVICE_STATUS_SET: "device.status.set",
7
- DEVICE_BATTERY_SET: "device.battery.set",
8
- DEVICE_META_DATA_SET: "device.metaData.set",
9
- };
10
-
11
- export const ConnectionEvents = {
12
- CONNECTION_UPDATED: "connection.updated",
13
- CONNECTION_DELETED: "connection.deleted",
14
- CONNECTION_CREATED: "connection.created",
15
- };
16
-
17
- export const PropertyEvents = {
18
- PROPERTY_UPDATED: "property.updated",
19
- PROPERTY_DELETED: "property.deleted",
20
- PROPERTY_CREATED: "property.created",
21
- PROPERTY_PREFERENCES_UPDATED: "property.preferences.updated",
22
- PROPERTY_PREFERENCES_CREATED: "property.preferences.created",
23
- };
1
+ export const DT_EVENT_TYPES = {
2
+ DEVICE: {
3
+ DEVICE: {
4
+ CREATED: "device.device.created",
5
+ UPDATED: "device.device.updated",
6
+ DELETED: "device.device.deleted",
7
+ },
8
+ STATE: {
9
+ SET: "device.state.set",
10
+ UNKNOWN: "device.state.unknown",
11
+ UPDATED: "device.state.updated",
12
+ CHANGED: "device.state.changed",
13
+ },
14
+ STATUS: {
15
+ SET: "device.status.set",
16
+ ONLINE: "device.status.online",
17
+ OFFLINE: "device.status.offline",
18
+ UNKNOWN: "device.status.unknown",
19
+ UPDATED: "device.status.updated",
20
+ CHANGED: "device.status.changed",
21
+ },
22
+ BATTERY: {
23
+ SET: "device.battery.set",
24
+ UNKNOWN: "device.battery.unknown",
25
+ LOW: "device.battery.low",
26
+ CRITICAL: "device.battery.critical",
27
+ UPDATED: "device.battery.updated",
28
+ CHANGED: "device.battery.changed",
29
+ },
30
+ META_DATA: {
31
+ SET: "device.metaData.set",
32
+ UPDATED: "device.metaData.updated",
33
+ CHANGED: "device.metaData.changed",
34
+ },
35
+ },
36
+ CONNECTION: {
37
+ CONNECTION: {
38
+ CREATED: "connection.connection.created",
39
+ UPDATED: "connection.connection.updated",
40
+ DELETED: "connection.connection.deleted",
41
+ },
42
+ },
43
+ PROPERTY: {
44
+ PROPERTY: {
45
+ CREATED: "property.property.created",
46
+ UPDATED: "property.property.updated",
47
+ DELETED: "property.property.deleted",
48
+ },
49
+ PREFERENCES: {
50
+ UPDATED: "property.preferences.updated",
51
+ CREATED: "property.preferences.created",
52
+ },
53
+ },
54
+ };
@@ -100,7 +100,7 @@ export class LocalDeviceService {
100
100
  throw new Error("Device ID and new state are required");
101
101
  }
102
102
  // If old state and new state are different
103
- const oldState = (await this.getState(deviceId))?.data?.state || {};
103
+ const oldState = (await this.getState(deviceId))?.data?.state ?? {};
104
104
  const changedKeys = Object.keys(newState).filter(
105
105
  (key) => !isEqual(oldState[key], newState[key])
106
106
  );