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
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { IDevice } from "../interfaces/IDevice";
|
|
1
|
+
import { IDevice, INewStatus } from "../interfaces/IDevice";
|
|
3
2
|
import { getConfig } from "../../../config/config";
|
|
4
3
|
import { getPostgresClient } from "../../../db";
|
|
5
4
|
import { Service } from "typedi";
|
|
6
5
|
import { IDtDevice } from "../interfaces/IDtDevice";
|
|
6
|
+
import { getDeviceServiceAxiosInstance } from "../../../utils/http-utils";
|
|
7
7
|
|
|
8
8
|
@Service()
|
|
9
9
|
export class DeviceRepository {
|
|
10
|
-
private readonly baseUrl: string;
|
|
11
10
|
private readonly postgres;
|
|
11
|
+
private readonly axiosInstance;
|
|
12
|
+
|
|
12
13
|
constructor() {
|
|
13
|
-
const { DEVICE_SERVICE } = getConfig();
|
|
14
|
-
if (!DEVICE_SERVICE) {
|
|
15
|
-
throw new Error(
|
|
16
|
-
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
this.baseUrl = DEVICE_SERVICE;
|
|
20
14
|
this.postgres = getPostgresClient();
|
|
15
|
+
this.axiosInstance = getDeviceServiceAxiosInstance();
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
async getDevice(
|
|
@@ -25,13 +20,15 @@ export class DeviceRepository {
|
|
|
25
20
|
withHubDetails: boolean = false
|
|
26
21
|
): Promise<IDevice> {
|
|
27
22
|
try {
|
|
28
|
-
const response = await
|
|
29
|
-
|
|
23
|
+
const response = await this.axiosInstance.get(
|
|
24
|
+
`/devices/${deviceId}?withHubDetails=${withHubDetails} `
|
|
30
25
|
);
|
|
31
26
|
return response.data;
|
|
32
27
|
} catch (error: any) {
|
|
33
|
-
|
|
34
|
-
throw new Error(
|
|
28
|
+
getConfig().LOGGER.error(`Failed to get device ${deviceId}:`, error);
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Failed to get device: ${error.message || "Unknown error"}`
|
|
31
|
+
);
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
34
|
|
|
@@ -39,10 +36,17 @@ export class DeviceRepository {
|
|
|
39
36
|
deviceIds: string[],
|
|
40
37
|
withHubDetails: boolean = false
|
|
41
38
|
): Promise<IDevice[]> {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
try {
|
|
40
|
+
const response = await this.axiosInstance.get(`/devices`, {
|
|
41
|
+
params: { deviceIds, withHubDetails },
|
|
42
|
+
});
|
|
43
|
+
return response.data;
|
|
44
|
+
} catch (error: any) {
|
|
45
|
+
getConfig().LOGGER.error("Failed to get devices:", error);
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Failed to get devices: ${error.message || "Unknown error"}`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
async getPropertyDevices(
|
|
@@ -51,10 +55,20 @@ export class DeviceRepository {
|
|
|
51
55
|
type?: string,
|
|
52
56
|
withHubDetails: boolean = false
|
|
53
57
|
): Promise<IDevice[]> {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
try {
|
|
59
|
+
const response = await this.axiosInstance.get(`/devices`, {
|
|
60
|
+
params: { propertyId, selectDeviceId, type, withHubDetails },
|
|
61
|
+
});
|
|
62
|
+
return response.data;
|
|
63
|
+
} catch (error: any) {
|
|
64
|
+
getConfig().LOGGER.error(
|
|
65
|
+
`Failed to get property devices for ${propertyId}:`,
|
|
66
|
+
error
|
|
67
|
+
);
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Failed to get property devices: ${error.message || "Unknown error"}`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
58
72
|
}
|
|
59
73
|
|
|
60
74
|
async getPropertyDeviceIds(
|
|
@@ -62,26 +76,160 @@ export class DeviceRepository {
|
|
|
62
76
|
selectDeviceId: boolean = false,
|
|
63
77
|
manufacturer: string
|
|
64
78
|
) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
79
|
+
try {
|
|
80
|
+
const response = await this.axiosInstance.get(`/devices`, {
|
|
81
|
+
params: { propertyId, selectDeviceId, manufacturer },
|
|
82
|
+
});
|
|
83
|
+
return response.data;
|
|
84
|
+
} catch (error: any) {
|
|
85
|
+
getConfig().LOGGER.error(
|
|
86
|
+
`Failed to get property device IDs for ${propertyId}:`,
|
|
87
|
+
error
|
|
88
|
+
);
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Failed to get property device IDs: ${error.message || "Unknown error"}`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
69
93
|
}
|
|
70
94
|
|
|
71
95
|
async getState(deviceId: string): Promise<any> {
|
|
72
|
-
|
|
96
|
+
try {
|
|
97
|
+
const response = await this.axiosInstance.get(
|
|
98
|
+
`/devices/${deviceId}/state`
|
|
99
|
+
);
|
|
100
|
+
return response.data;
|
|
101
|
+
} catch (error: any) {
|
|
102
|
+
getConfig().LOGGER.error(
|
|
103
|
+
`Failed to get device state for ${deviceId}:`,
|
|
104
|
+
error
|
|
105
|
+
);
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Failed to get device state: ${error.message || "Unknown error"}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async setState(deviceId: string, state: any): Promise<any> {
|
|
113
|
+
try {
|
|
114
|
+
const response = await this.axiosInstance.put(
|
|
115
|
+
`/devices/${deviceId}/state`,
|
|
116
|
+
state
|
|
117
|
+
);
|
|
118
|
+
return response.data;
|
|
119
|
+
} catch (error: any) {
|
|
120
|
+
getConfig().LOGGER.error(
|
|
121
|
+
`Failed to set device state for ${deviceId}:`,
|
|
122
|
+
error
|
|
123
|
+
);
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Failed to set device state: ${error.message || "Unknown error"}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
73
128
|
}
|
|
74
129
|
|
|
75
130
|
async getStatus(deviceId: string): Promise<Record<string, any>> {
|
|
76
|
-
|
|
131
|
+
try {
|
|
132
|
+
const response = await this.axiosInstance.get(
|
|
133
|
+
`/devices/${deviceId}/status`
|
|
134
|
+
);
|
|
135
|
+
return response.data;
|
|
136
|
+
} catch (error: any) {
|
|
137
|
+
getConfig().LOGGER.error(
|
|
138
|
+
`Failed to get device status for ${deviceId}:`,
|
|
139
|
+
error
|
|
140
|
+
);
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Failed to get device status: ${error.message || "Unknown error"}`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async setStatus(deviceId: string, newStatus: INewStatus) {
|
|
148
|
+
try {
|
|
149
|
+
const response = await this.axiosInstance.put(
|
|
150
|
+
`/devices/${deviceId}/status`,
|
|
151
|
+
newStatus
|
|
152
|
+
);
|
|
153
|
+
return response.data;
|
|
154
|
+
} catch (error: any) {
|
|
155
|
+
getConfig().LOGGER.error(
|
|
156
|
+
`Failed to set device status for ${deviceId}:`,
|
|
157
|
+
error
|
|
158
|
+
);
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Failed to set device status: ${error.message || "Unknown error"}`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
77
163
|
}
|
|
78
164
|
|
|
79
165
|
async getBatteryLevel(deviceId: string): Promise<Record<string, any>> {
|
|
80
|
-
|
|
166
|
+
try {
|
|
167
|
+
const response = await this.axiosInstance.get(
|
|
168
|
+
`/devices/${deviceId}/battery-level`
|
|
169
|
+
);
|
|
170
|
+
return response.data;
|
|
171
|
+
} catch (error: any) {
|
|
172
|
+
getConfig().LOGGER.error(
|
|
173
|
+
`Failed to get battery level for ${deviceId}:`,
|
|
174
|
+
error
|
|
175
|
+
);
|
|
176
|
+
throw new Error(
|
|
177
|
+
`Failed to get battery level: ${error.message || "Unknown error"}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async setBatteryLevel(deviceId: string, newBatteryLevel: any) {
|
|
183
|
+
try {
|
|
184
|
+
const response = await this.axiosInstance.put(
|
|
185
|
+
`/devices/${deviceId}/battery-level`,
|
|
186
|
+
{ newBatteryLevel }
|
|
187
|
+
);
|
|
188
|
+
return response.data;
|
|
189
|
+
} catch (error: any) {
|
|
190
|
+
getConfig().LOGGER.error(
|
|
191
|
+
`Failed to set battery level for ${deviceId}:`,
|
|
192
|
+
error
|
|
193
|
+
);
|
|
194
|
+
throw new Error(
|
|
195
|
+
`Failed to set battery level: ${error.message || "Unknown error"}`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
81
198
|
}
|
|
82
199
|
|
|
83
200
|
async getMetaData(deviceId: string): Promise<any> {
|
|
84
|
-
|
|
201
|
+
try {
|
|
202
|
+
const response = await this.axiosInstance.get(
|
|
203
|
+
`/devices/${deviceId}/metaData`
|
|
204
|
+
);
|
|
205
|
+
return response.data;
|
|
206
|
+
} catch (error: any) {
|
|
207
|
+
getConfig().LOGGER.error(
|
|
208
|
+
`Failed to get metadata for ${deviceId}:`,
|
|
209
|
+
error
|
|
210
|
+
);
|
|
211
|
+
throw new Error(
|
|
212
|
+
`Failed to get metadata: ${error.message || "Unknown error"}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async setMetaData(deviceId: string, metaData: Record<string, any>) {
|
|
218
|
+
try {
|
|
219
|
+
const response = await this.axiosInstance.put(
|
|
220
|
+
`/devices/${deviceId}/metaData`,
|
|
221
|
+
metaData
|
|
222
|
+
);
|
|
223
|
+
return response.data;
|
|
224
|
+
} catch (error: any) {
|
|
225
|
+
getConfig().LOGGER.error(
|
|
226
|
+
`Failed to set metadata for ${deviceId}:`,
|
|
227
|
+
error
|
|
228
|
+
);
|
|
229
|
+
throw new Error(
|
|
230
|
+
`Failed to set metadata: ${error.message || "Unknown error"}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
85
233
|
}
|
|
86
234
|
|
|
87
235
|
async getDevicesByAccessGroup(accessGroupId: string): Promise<IDtDevice[]> {
|
|
@@ -98,7 +246,10 @@ export class DeviceRepository {
|
|
|
98
246
|
}
|
|
99
247
|
return [];
|
|
100
248
|
} catch (error) {
|
|
101
|
-
|
|
249
|
+
getConfig().LOGGER.error(
|
|
250
|
+
`Failed to get devices by access group ${accessGroupId}:`,
|
|
251
|
+
error
|
|
252
|
+
);
|
|
102
253
|
throw new Error("Failed to get devices by access group");
|
|
103
254
|
}
|
|
104
255
|
}
|
|
@@ -114,8 +265,66 @@ export class DeviceRepository {
|
|
|
114
265
|
}
|
|
115
266
|
return [];
|
|
116
267
|
} catch (error) {
|
|
117
|
-
|
|
268
|
+
getConfig().LOGGER.error(
|
|
269
|
+
`Failed to get devices by zone ${zoneId}:`,
|
|
270
|
+
error
|
|
271
|
+
);
|
|
118
272
|
throw new Error("Failed to get device by zone");
|
|
119
273
|
}
|
|
120
274
|
}
|
|
275
|
+
|
|
276
|
+
async querySelect(query: any, fields: string[]) {
|
|
277
|
+
try {
|
|
278
|
+
const response = await this.axiosInstance.post(`/devices/select`, {
|
|
279
|
+
query,
|
|
280
|
+
fields,
|
|
281
|
+
});
|
|
282
|
+
return response.data;
|
|
283
|
+
} catch (error: any) {
|
|
284
|
+
getConfig().LOGGER.error("Failed to query select devices:", error);
|
|
285
|
+
throw new Error(
|
|
286
|
+
`Failed to query select devices: ${error.message || "Unknown error"}`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
//remove - use existing query function
|
|
292
|
+
async queryCount(query: any): Promise<number> {
|
|
293
|
+
try {
|
|
294
|
+
const response = await this.axiosInstance.post(`/devices/count`, query);
|
|
295
|
+
return response.data;
|
|
296
|
+
} catch (error: any) {
|
|
297
|
+
getConfig().LOGGER.error("Failed to query count devices:", error);
|
|
298
|
+
throw new Error(
|
|
299
|
+
`Failed to query count devices: ${error.message || "Unknown error"}`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async deleteDevices(query: any) {
|
|
305
|
+
try {
|
|
306
|
+
const response = await this.axiosInstance.post(
|
|
307
|
+
`/devices/deleteMany`,
|
|
308
|
+
query
|
|
309
|
+
);
|
|
310
|
+
return response.data;
|
|
311
|
+
} catch (error: any) {
|
|
312
|
+
getConfig().LOGGER.error("Failed to delete devices:", error);
|
|
313
|
+
throw new Error(
|
|
314
|
+
`Failed to delete devices: ${error.message || "Unknown error"}`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async queryDevices(query: any) {
|
|
320
|
+
try {
|
|
321
|
+
const response = await this.axiosInstance.post(`/devices/query`, query);
|
|
322
|
+
return response.data;
|
|
323
|
+
} catch (error: any) {
|
|
324
|
+
getConfig().LOGGER.error("Failed to query devices:", error);
|
|
325
|
+
throw new Error(
|
|
326
|
+
`Failed to query devices: ${error.message || "Unknown error"}`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
121
330
|
}
|
|
@@ -1,51 +1,107 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
import { getConfig } from "../../../config/config";
|
|
3
2
|
import { getPostgresClient } from "../../../db";
|
|
4
3
|
import { Service } from "typedi";
|
|
5
4
|
import { IDevice } from "../interfaces";
|
|
5
|
+
import { getDeviceServiceAxiosInstance } from "../../../utils/http-utils";
|
|
6
6
|
|
|
7
7
|
@Service()
|
|
8
8
|
export class HubRepository {
|
|
9
|
-
private readonly baseUrl: string;
|
|
10
9
|
private readonly postgres;
|
|
10
|
+
private readonly axiosInstance;
|
|
11
|
+
|
|
11
12
|
constructor() {
|
|
12
|
-
const { DEVICE_SERVICE } = getConfig();
|
|
13
|
-
if (!DEVICE_SERVICE) {
|
|
14
|
-
throw new Error(
|
|
15
|
-
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
this.baseUrl = DEVICE_SERVICE;
|
|
19
13
|
this.postgres = getPostgresClient();
|
|
14
|
+
this.axiosInstance = getDeviceServiceAxiosInstance();
|
|
20
15
|
}
|
|
21
16
|
|
|
22
17
|
async addHub(body: Partial<IDevice>): Promise<IDevice> {
|
|
23
|
-
|
|
18
|
+
try {
|
|
19
|
+
const response = await this.axiosInstance.post(`/devices/hubs`, body);
|
|
20
|
+
return response.data;
|
|
21
|
+
} catch (error: any) {
|
|
22
|
+
getConfig().LOGGER.error("Failed to add hub:", error);
|
|
23
|
+
throw new Error(`Failed to add hub: ${error.message || "Unknown error"}`);
|
|
24
|
+
}
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
async getHubs(hubIds: string[]): Promise<IDevice[]> {
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
try {
|
|
29
|
+
const query = hubIds && hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
30
|
+
const response = await this.axiosInstance.get(`/devices/hubs${query}`);
|
|
31
|
+
return response.data;
|
|
32
|
+
} catch (error: any) {
|
|
33
|
+
getConfig().LOGGER.error("Failed to get hubs:", error);
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Failed to get hubs: ${error.message || "Unknown error"}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
async getHub(hubId: string): Promise<IDevice> {
|
|
32
|
-
|
|
41
|
+
try {
|
|
42
|
+
const response = await this.axiosInstance.get(`/devices/hubs/${hubId}`);
|
|
43
|
+
return response.data;
|
|
44
|
+
} catch (error: any) {
|
|
45
|
+
getConfig().LOGGER.error(`Failed to get hub ${hubId}:`, error);
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Failed to get hub ${hubId}: ${error.message || "Unknown error"}`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
33
50
|
}
|
|
34
51
|
|
|
35
52
|
async updateHub(hubId: string, body: any): Promise<IDevice> {
|
|
36
|
-
|
|
53
|
+
try {
|
|
54
|
+
const response = await this.axiosInstance.put(
|
|
55
|
+
`/devices/hubs/${hubId}`,
|
|
56
|
+
body
|
|
57
|
+
);
|
|
58
|
+
return response.data;
|
|
59
|
+
} catch (error: any) {
|
|
60
|
+
getConfig().LOGGER.error(`Failed to update hub ${hubId}:`, error);
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Failed to update hub ${hubId}: ${error.message || "Unknown error"}`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
37
65
|
}
|
|
38
66
|
|
|
39
67
|
async getStatus(hubId: string): Promise<"ONLINE" | "OFFLINE" | "UNKNOWN"> {
|
|
40
|
-
|
|
68
|
+
try {
|
|
69
|
+
const response = await this.axiosInstance.get(
|
|
70
|
+
`/devices/hubs/${hubId}/status`
|
|
71
|
+
);
|
|
72
|
+
return response.data;
|
|
73
|
+
} catch (error: any) {
|
|
74
|
+
getConfig().LOGGER.error(`Failed to get hub status ${hubId}:`, error);
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Failed to get hub status ${hubId}: ${error.message || "Unknown error"}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
41
79
|
}
|
|
42
80
|
|
|
43
81
|
async deleteHub(hubId: string): Promise<any> {
|
|
44
|
-
|
|
82
|
+
try {
|
|
83
|
+
const response = await this.axiosInstance.delete(
|
|
84
|
+
`/devices/hubs/${hubId}`
|
|
85
|
+
);
|
|
86
|
+
return response.data;
|
|
87
|
+
} catch (error: any) {
|
|
88
|
+
getConfig().LOGGER.error(`Failed to delete hub ${hubId}:`, error);
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Failed to delete hub ${hubId}: ${error.message || "Unknown error"}`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
45
93
|
}
|
|
46
94
|
|
|
47
95
|
async deleteAllHubs(hubIds: string[]): Promise<any> {
|
|
48
|
-
|
|
49
|
-
|
|
96
|
+
try {
|
|
97
|
+
const query = hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
98
|
+
const response = await this.axiosInstance.delete(`/devices/hubs${query}`);
|
|
99
|
+
return response.data;
|
|
100
|
+
} catch (error: any) {
|
|
101
|
+
getConfig().LOGGER.error("Failed to delete all hubs:", error);
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Failed to delete all hubs: ${error.message || "Unknown error"}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
50
106
|
}
|
|
51
107
|
}
|