dt-common-device 1.0.18 → 1.0.20
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/device/cloud/entities/DeviceFactory.js +9 -1
- package/dist/device/local/interfaces/IConnection.d.ts +1 -1
- package/dist/device/local/services/Device.service.d.ts +3 -3
- package/dist/device/local/services/Device.service.js +23 -13
- package/package.json +5 -2
- package/src/device/cloud/entities/DeviceFactory.ts +13 -1
- package/src/device/local/interfaces/IConnection.ts +2 -2
- package/src/device/local/services/Device.service.ts +34 -16
|
@@ -7,7 +7,15 @@ class DeviceFactory {
|
|
|
7
7
|
this.localDeviceService = new Device_service_1.LocalDeviceService();
|
|
8
8
|
}
|
|
9
9
|
async getDevice(deviceId) {
|
|
10
|
-
|
|
10
|
+
try {
|
|
11
|
+
return await this.localDeviceService.getDevice(deviceId);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
// Log the error for debugging purposes
|
|
15
|
+
console.error(`DeviceFactory: Failed to get device ${deviceId}:`, error);
|
|
16
|
+
// Re-throw the error with additional context
|
|
17
|
+
throw new Error(`DeviceFactory: Unable to retrieve device ${deviceId}. ${error instanceof Error ? error.message : "Unknown error occurred"}`);
|
|
18
|
+
}
|
|
11
19
|
}
|
|
12
20
|
}
|
|
13
21
|
exports.DeviceFactory = DeviceFactory;
|
|
@@ -13,11 +13,11 @@ export declare class LocalDeviceService {
|
|
|
13
13
|
updateDevice(deviceId: string, body: any): Promise<any>;
|
|
14
14
|
deleteDevice(deviceId: string): Promise<any>;
|
|
15
15
|
getState(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
16
|
-
setState(deviceId: string,
|
|
16
|
+
setState(deviceId: string, newState: any): Promise<void>;
|
|
17
17
|
getStatus(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
18
|
-
setStatus(deviceId: string,
|
|
18
|
+
setStatus(deviceId: string, newStatus: any): Promise<void>;
|
|
19
19
|
getBatteryLevel(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
20
|
-
setBatteryLevel(deviceId: string,
|
|
20
|
+
setBatteryLevel(deviceId: string, newBatteryLevel: any): Promise<void>;
|
|
21
21
|
getMetaData(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
22
22
|
setMetaData(deviceId: string, metaData: Record<string, any>): Promise<void>;
|
|
23
23
|
getDeviceByZone(zoneId: string): Promise<any>;
|
|
@@ -38,7 +38,14 @@ class LocalDeviceService {
|
|
|
38
38
|
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
39
39
|
}
|
|
40
40
|
async getDevice(deviceId, withHubDetails = false) {
|
|
41
|
-
|
|
41
|
+
try {
|
|
42
|
+
const response = await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}?withHubDetails=${withHubDetails} `);
|
|
43
|
+
return response.data;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.log(error);
|
|
47
|
+
throw new Error(`Failed to get device: ${error.message}`);
|
|
48
|
+
}
|
|
42
49
|
}
|
|
43
50
|
async getDevices(deviceIds, withHubDetails = false) {
|
|
44
51
|
return await axios_1.default.get(`${this.baseUrl}/devices`, {
|
|
@@ -74,26 +81,28 @@ class LocalDeviceService {
|
|
|
74
81
|
async getState(deviceId) {
|
|
75
82
|
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
76
83
|
}
|
|
77
|
-
async setState(deviceId,
|
|
84
|
+
async setState(deviceId, newState) {
|
|
78
85
|
// If old state and new state are different
|
|
79
|
-
const oldState = await this.getState(deviceId);
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
const oldState = (await this.getState(deviceId))?.data?.state || {};
|
|
87
|
+
const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
|
|
88
|
+
if (changedKeys.length > 0) {
|
|
89
|
+
return await this.eventHandler.onStateChange(deviceId, newState);
|
|
82
90
|
}
|
|
83
91
|
}
|
|
84
92
|
async getStatus(deviceId) {
|
|
85
93
|
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
86
94
|
}
|
|
87
|
-
async setStatus(deviceId,
|
|
95
|
+
async setStatus(deviceId, newStatus) {
|
|
88
96
|
// If old status and new status are different
|
|
89
97
|
const oldStatus = await this.getStatus(deviceId);
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
const changedKeys = Object.keys(newStatus).filter((key) => !(0, lodash_1.isEqual)(oldStatus?.data?.[key], newStatus?.[key]));
|
|
99
|
+
if (changedKeys.length > 0) {
|
|
100
|
+
return await this.eventHandler.onStatusChange(deviceId, newStatus);
|
|
92
101
|
}
|
|
93
102
|
// If both old and new status are offline, check the time difference between the last offline status and current time
|
|
94
103
|
if (oldStatus?.data?.online === false &&
|
|
95
104
|
oldStatus?.data?.lastUpdated &&
|
|
96
|
-
|
|
105
|
+
newStatus?.online === false) {
|
|
97
106
|
const lastOfflineTime = new Date(oldStatus?.data?.lastUpdated).getTime();
|
|
98
107
|
const currentTime = Date.now();
|
|
99
108
|
const timeDifference = currentTime - lastOfflineTime;
|
|
@@ -106,14 +115,15 @@ class LocalDeviceService {
|
|
|
106
115
|
async getBatteryLevel(deviceId) {
|
|
107
116
|
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
108
117
|
}
|
|
109
|
-
async setBatteryLevel(deviceId,
|
|
118
|
+
async setBatteryLevel(deviceId, newBatteryLevel) {
|
|
110
119
|
// If old battery level and new battery level are different
|
|
111
120
|
const oldBatteryLevel = await this.getBatteryLevel(deviceId);
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
const changedKeys = Object.keys(newBatteryLevel).filter((key) => !(0, lodash_1.isEqual)(oldBatteryLevel?.data?.[key], newBatteryLevel?.[key]));
|
|
122
|
+
if (changedKeys.length > 0) {
|
|
123
|
+
return await this.eventHandler.onBatteryLevelChange(deviceId, newBatteryLevel);
|
|
114
124
|
}
|
|
115
125
|
// If current battery level is less than 20% and lsat updated time is more than 8 hours, raise an energy alert
|
|
116
|
-
if (
|
|
126
|
+
if (newBatteryLevel?.value < 20 &&
|
|
117
127
|
oldBatteryLevel?.data?.value < 20 &&
|
|
118
128
|
new Date(oldBatteryLevel?.data?.lastUpdated).getTime() <
|
|
119
129
|
Date.now() - 1000 * 60 * 60 * 8) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-common-device",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.20",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "tsc",
|
|
7
|
+
"build": "tsc ",
|
|
8
|
+
"patch": "npm version patch && npm run build",
|
|
9
|
+
"minor": "npm version minor && npm run build",
|
|
10
|
+
"major": "npm version major && npm run build",
|
|
8
11
|
"security:audit": "npm audit --audit-level=moderate",
|
|
9
12
|
"security:fix": "npm audit fix",
|
|
10
13
|
"security:check": "npm audit && npm outdated",
|
|
@@ -9,6 +9,18 @@ export class DeviceFactory {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
async getDevice(deviceId: string): Promise<IDevice> {
|
|
12
|
-
|
|
12
|
+
try {
|
|
13
|
+
return await this.localDeviceService.getDevice(deviceId);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
// Log the error for debugging purposes
|
|
16
|
+
console.error(`DeviceFactory: Failed to get device ${deviceId}:`, error);
|
|
17
|
+
|
|
18
|
+
// Re-throw the error with additional context
|
|
19
|
+
throw new Error(
|
|
20
|
+
`DeviceFactory: Unable to retrieve device ${deviceId}. ${
|
|
21
|
+
error instanceof Error ? error.message : "Unknown error occurred"
|
|
22
|
+
}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
@@ -10,7 +10,7 @@ import { publishAudit } from "dt-audit-library";
|
|
|
10
10
|
import { EventHandler } from "../handler/EventHandler";
|
|
11
11
|
import { isEqual } from "lodash";
|
|
12
12
|
import { AlertService } from "./Alert.service";
|
|
13
|
-
import {
|
|
13
|
+
import { getPostgresClient } from "../../../db";
|
|
14
14
|
|
|
15
15
|
export class LocalDeviceService {
|
|
16
16
|
private readonly baseUrl: string;
|
|
@@ -50,9 +50,15 @@ export class LocalDeviceService {
|
|
|
50
50
|
deviceId: string,
|
|
51
51
|
withHubDetails: boolean = false
|
|
52
52
|
): Promise<IDevice> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
try {
|
|
54
|
+
const response = await axios.get(
|
|
55
|
+
`${this.baseUrl}/devices/${deviceId}?withHubDetails=${withHubDetails} `
|
|
56
|
+
);
|
|
57
|
+
return response.data;
|
|
58
|
+
} catch (error: any) {
|
|
59
|
+
console.log(error);
|
|
60
|
+
throw new Error(`Failed to get device: ${error.message}`);
|
|
61
|
+
}
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
async getDevices(
|
|
@@ -108,11 +114,14 @@ export class LocalDeviceService {
|
|
|
108
114
|
return await axios.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
109
115
|
}
|
|
110
116
|
|
|
111
|
-
async setState(deviceId: string,
|
|
117
|
+
async setState(deviceId: string, newState: any) {
|
|
112
118
|
// If old state and new state are different
|
|
113
|
-
const oldState = await this.getState(deviceId);
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
const oldState = (await this.getState(deviceId))?.data?.state || {};
|
|
120
|
+
const changedKeys = Object.keys(newState).filter(
|
|
121
|
+
(key) => !isEqual(oldState[key], newState[key])
|
|
122
|
+
);
|
|
123
|
+
if (changedKeys.length > 0) {
|
|
124
|
+
return await this.eventHandler.onStateChange(deviceId, newState);
|
|
116
125
|
}
|
|
117
126
|
}
|
|
118
127
|
|
|
@@ -120,17 +129,20 @@ export class LocalDeviceService {
|
|
|
120
129
|
return await axios.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
121
130
|
}
|
|
122
131
|
|
|
123
|
-
async setStatus(deviceId: string,
|
|
132
|
+
async setStatus(deviceId: string, newStatus: any) {
|
|
124
133
|
// If old status and new status are different
|
|
125
134
|
const oldStatus = await this.getStatus(deviceId);
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
const changedKeys = Object.keys(newStatus).filter(
|
|
136
|
+
(key) => !isEqual(oldStatus?.data?.[key], newStatus?.[key])
|
|
137
|
+
);
|
|
138
|
+
if (changedKeys.length > 0) {
|
|
139
|
+
return await this.eventHandler.onStatusChange(deviceId, newStatus);
|
|
128
140
|
}
|
|
129
141
|
// If both old and new status are offline, check the time difference between the last offline status and current time
|
|
130
142
|
if (
|
|
131
143
|
oldStatus?.data?.online === false &&
|
|
132
144
|
oldStatus?.data?.lastUpdated &&
|
|
133
|
-
|
|
145
|
+
newStatus?.online === false
|
|
134
146
|
) {
|
|
135
147
|
const lastOfflineTime = new Date(oldStatus?.data?.lastUpdated).getTime();
|
|
136
148
|
const currentTime = Date.now();
|
|
@@ -145,15 +157,21 @@ export class LocalDeviceService {
|
|
|
145
157
|
async getBatteryLevel(deviceId: string) {
|
|
146
158
|
return await axios.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
147
159
|
}
|
|
148
|
-
async setBatteryLevel(deviceId: string,
|
|
160
|
+
async setBatteryLevel(deviceId: string, newBatteryLevel: any) {
|
|
149
161
|
// If old battery level and new battery level are different
|
|
150
162
|
const oldBatteryLevel = await this.getBatteryLevel(deviceId);
|
|
151
|
-
|
|
152
|
-
|
|
163
|
+
const changedKeys = Object.keys(newBatteryLevel).filter(
|
|
164
|
+
(key) => !isEqual(oldBatteryLevel?.data?.[key], newBatteryLevel?.[key])
|
|
165
|
+
);
|
|
166
|
+
if (changedKeys.length > 0) {
|
|
167
|
+
return await this.eventHandler.onBatteryLevelChange(
|
|
168
|
+
deviceId,
|
|
169
|
+
newBatteryLevel
|
|
170
|
+
);
|
|
153
171
|
}
|
|
154
172
|
// If current battery level is less than 20% and lsat updated time is more than 8 hours, raise an energy alert
|
|
155
173
|
if (
|
|
156
|
-
|
|
174
|
+
newBatteryLevel?.value < 20 &&
|
|
157
175
|
oldBatteryLevel?.data?.value < 20 &&
|
|
158
176
|
new Date(oldBatteryLevel?.data?.lastUpdated).getTime() <
|
|
159
177
|
Date.now() - 1000 * 60 * 60 * 8
|