dt-common-device 1.0.20 → 1.2.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.
- package/dist/device/cloud/entities/CloudConnection.d.ts +6 -0
- package/dist/device/cloud/entities/CloudConnection.js +6 -0
- package/dist/device/cloud/entities/CloudDevice.js +0 -19
- package/dist/device/cloud/entities/CloudDeviceService.d.ts +5 -0
- package/dist/device/cloud/entities/CloudDeviceService.js +9 -0
- package/dist/device/cloud/entities/DeviceFactory.d.ts +2 -1
- package/dist/device/cloud/entities/index.d.ts +2 -0
- package/dist/device/cloud/entities/index.js +2 -0
- package/dist/device/cloud/interfaces/ICloudConnection.d.ts +5 -0
- package/dist/device/cloud/interfaces/ICloudConnection.js +2 -0
- package/dist/device/cloud/interfaces/IDeviceFactory.d.ts +4 -0
- package/dist/device/cloud/interfaces/IDeviceFactory.js +2 -0
- package/dist/device/cloud/interfaces/index.d.ts +1 -3
- package/dist/device/cloud/interfaces/index.js +1 -3
- package/dist/device/cloud/types.d.ts +1 -1
- package/dist/device/local/events/EventHandler.d.ts +10 -0
- package/dist/device/local/events/EventHandler.js +75 -0
- package/dist/device/local/events/Events.d.ts +21 -0
- package/dist/device/local/events/Events.js +24 -0
- package/dist/device/local/interfaces/IDtDevice.d.ts +16 -0
- package/dist/device/local/interfaces/IDtDevice.js +2 -0
- package/dist/device/local/interfaces/index.d.ts +0 -1
- package/dist/device/local/interfaces/index.js +0 -1
- package/dist/device/local/repository/Connection.repository.d.ts +7 -0
- package/dist/device/local/repository/Connection.repository.js +77 -0
- package/dist/device/local/repository/Device.repository.d.ts +16 -0
- package/dist/device/local/repository/Device.repository.js +134 -0
- package/dist/device/local/repository/Hub.repository.d.ts +13 -0
- package/dist/device/local/repository/Hub.repository.js +97 -0
- package/dist/device/local/repository/Property.repository.d.ts +9 -0
- package/dist/device/local/repository/Property.repository.js +81 -0
- package/dist/device/local/services/Connection.service.d.ts +3 -2
- package/dist/device/local/services/Connection.service.js +7 -4
- package/dist/device/local/services/Device.service.d.ts +7 -9
- package/dist/device/local/services/Device.service.js +17 -91
- package/dist/device/local/services/Hub.service.d.ts +7 -7
- package/dist/device/local/services/Hub.service.js +13 -21
- package/dist/device/local/services/Property.service.d.ts +9 -0
- package/dist/device/local/services/Property.service.js +13 -0
- package/dist/device/local/services/index.d.ts +2 -1
- package/dist/device/local/services/index.js +4 -2
- package/dist/index.d.ts +2 -3
- package/dist/index.js +8 -8
- package/package.json +4 -3
- package/src/device/cloud/entities/CloudConnection.ts +13 -0
- package/src/device/cloud/entities/CloudDevice.ts +1 -21
- package/src/device/cloud/entities/DeviceFactory.ts +2 -1
- package/src/device/cloud/entities/index.ts +2 -0
- package/src/device/cloud/interfaces/ICloudConnection.ts +6 -0
- package/src/device/cloud/interfaces/ICloudDevice.ts +0 -1
- package/src/device/cloud/interfaces/IDeviceFactory.ts +5 -0
- package/src/device/cloud/interfaces/index.ts +1 -3
- package/src/device/cloud/types.ts +1 -1
- package/src/device/local/events/EventHandler.ts +101 -0
- package/src/device/local/events/Events.ts +23 -0
- package/src/device/local/interfaces/IDtDevice.ts +16 -0
- package/src/device/local/interfaces/index.ts +0 -1
- package/src/device/local/repository/Connection.repository.ts +32 -0
- package/src/device/local/repository/Device.repository.ts +106 -0
- package/src/device/local/repository/Hub.repository.ts +51 -0
- package/src/device/local/repository/Property.repository.ts +32 -0
- package/src/device/local/services/Connection.service.ts +11 -9
- package/src/device/local/services/Device.service.ts +21 -109
- package/src/device/local/services/Hub.service.ts +17 -28
- package/src/device/local/services/Property.service.ts +12 -0
- package/src/device/local/services/index.ts +2 -1
- package/src/index.ts +5 -3
- package/src/device/cloud/interfaces/IConnectionService.ts +0 -15
- package/src/device/cloud/interfaces/IDeviceService.ts +0 -12
- package/src/device/cloud/interfaces/IHubService.ts +0 -5
- package/src/device/cloud/services/Connection.service.ts +0 -19
- package/src/device/cloud/services/index.ts +0 -2
- package/src/device/local/handler/EventHandler.ts +0 -52
- package/src/device/local/interfaces/IConnection.ts +0 -17
- package/src/device/local/interfaces/IHub.ts +0 -46
- /package/src/device/cloud/{services/CloudDevice.service.ts → entities/CloudDeviceService.ts} +0 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { eventDispatcher } from "dt-pub-sub";
|
|
2
|
+
import { publishAudit } from "dt-audit-library";
|
|
3
|
+
import { DeviceEvents } from "./Events";
|
|
4
|
+
|
|
5
|
+
export class EventHandler {
|
|
6
|
+
private readonly source = "dt-common-device";
|
|
7
|
+
constructor() {}
|
|
8
|
+
|
|
9
|
+
async onDeviceCreate(body: any) {
|
|
10
|
+
await eventDispatcher.publishEvent(
|
|
11
|
+
DeviceEvents.DEVICE_CREATED,
|
|
12
|
+
body,
|
|
13
|
+
this.source
|
|
14
|
+
);
|
|
15
|
+
const payload = {
|
|
16
|
+
eventType: DeviceEvents.DEVICE_CREATED,
|
|
17
|
+
properties: {
|
|
18
|
+
...body,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
await publishAudit(payload);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async onDeviceUpdate(deviceId: string, body: any) {
|
|
25
|
+
await eventDispatcher.publishEvent(
|
|
26
|
+
DeviceEvents.DEVICE_UPDATED,
|
|
27
|
+
{ deviceId, body },
|
|
28
|
+
this.source
|
|
29
|
+
);
|
|
30
|
+
const payload = {
|
|
31
|
+
eventType: DeviceEvents.DEVICE_UPDATED,
|
|
32
|
+
properties: {
|
|
33
|
+
...body,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
await publishAudit(payload);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async onDeviceDelete(deviceId: string) {
|
|
40
|
+
await eventDispatcher.publishEvent(
|
|
41
|
+
DeviceEvents.DEVICE_DELETED,
|
|
42
|
+
{ deviceId },
|
|
43
|
+
this.source
|
|
44
|
+
);
|
|
45
|
+
const payload = {
|
|
46
|
+
eventType: DeviceEvents.DEVICE_DELETED,
|
|
47
|
+
properties: {
|
|
48
|
+
deviceId,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
await publishAudit(payload);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async onStateChange(deviceId: string, state: any) {
|
|
55
|
+
await eventDispatcher.publishEvent(
|
|
56
|
+
DeviceEvents.DEVICE_STATE_SET,
|
|
57
|
+
{ deviceId, state },
|
|
58
|
+
this.source
|
|
59
|
+
);
|
|
60
|
+
const payload = {
|
|
61
|
+
eventType: DeviceEvents.DEVICE_STATE_SET,
|
|
62
|
+
properties: {
|
|
63
|
+
deviceId,
|
|
64
|
+
state,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
await publishAudit(payload);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async onStatusChange(deviceId: string, status: any) {
|
|
71
|
+
await eventDispatcher.publishEvent(
|
|
72
|
+
DeviceEvents.DEVICE_STATUS_SET,
|
|
73
|
+
{ deviceId, status },
|
|
74
|
+
this.source
|
|
75
|
+
);
|
|
76
|
+
const payload = {
|
|
77
|
+
eventType: DeviceEvents.DEVICE_STATUS_SET,
|
|
78
|
+
properties: {
|
|
79
|
+
deviceId,
|
|
80
|
+
status,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
await publishAudit(payload);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async onBatteryLevelChange(deviceId: string, batteryLevel: number) {
|
|
87
|
+
await eventDispatcher.publishEvent(
|
|
88
|
+
DeviceEvents.DEVICE_BATTERY_SET,
|
|
89
|
+
{ deviceId, batteryLevel },
|
|
90
|
+
this.source
|
|
91
|
+
);
|
|
92
|
+
const payload = {
|
|
93
|
+
eventType: DeviceEvents.DEVICE_BATTERY_SET,
|
|
94
|
+
properties: {
|
|
95
|
+
deviceId,
|
|
96
|
+
batteryLevel,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
await publishAudit(payload);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface IDtDevice {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
zoneId: string;
|
|
6
|
+
deviceType: string;
|
|
7
|
+
deviceId: string;
|
|
8
|
+
deviceNetworkId: string;
|
|
9
|
+
propertyId: string;
|
|
10
|
+
connectionId: string;
|
|
11
|
+
hubId: string;
|
|
12
|
+
isBatteryLowAlertSent: boolean;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
isActive: boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getPostgresClient } from "../../../db";
|
|
2
|
+
import { IConnection } from "../../cloud/types";
|
|
3
|
+
import { Service } from "typedi";
|
|
4
|
+
|
|
5
|
+
@Service()
|
|
6
|
+
export class ConnectionRepository {
|
|
7
|
+
private readonly pool;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.pool = getPostgresClient();
|
|
10
|
+
}
|
|
11
|
+
async getConnectionById(connectionId: string): Promise<IConnection> {
|
|
12
|
+
const result = await this.pool.query(
|
|
13
|
+
"SELECT * FROM dt_connections WHERE id = $1",
|
|
14
|
+
[connectionId]
|
|
15
|
+
);
|
|
16
|
+
return result.rows[0];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async updateConnection(connectionId: string, data: Partial<IConnection>) {
|
|
20
|
+
// Build dynamic SET clause
|
|
21
|
+
const setClause = Object.keys(data)
|
|
22
|
+
.map((key, index) => `${key} = $${index + 2}`)
|
|
23
|
+
.join(", ");
|
|
24
|
+
|
|
25
|
+
const values = Object.values(data);
|
|
26
|
+
const result = await this.pool.query(
|
|
27
|
+
`UPDATE dt_connections SET ${setClause}, updated_at = NOW() WHERE id = $1 RETURNING *`,
|
|
28
|
+
[connectionId, ...values]
|
|
29
|
+
);
|
|
30
|
+
return result.rows[0];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { IDevice } from "../interfaces/IDevice";
|
|
3
|
+
import { getConfig } from "../../../config/config";
|
|
4
|
+
import { getPostgresClient } from "../../../db";
|
|
5
|
+
import { Service } from "typedi";
|
|
6
|
+
import { IDtDevice } from "../interfaces/IDtDevice";
|
|
7
|
+
|
|
8
|
+
@Service()
|
|
9
|
+
export class DeviceRepository {
|
|
10
|
+
private readonly baseUrl: string;
|
|
11
|
+
private readonly postgres;
|
|
12
|
+
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
|
+
this.postgres = getPostgresClient();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async getDevice(
|
|
24
|
+
deviceId: string,
|
|
25
|
+
withHubDetails: boolean = false
|
|
26
|
+
): Promise<IDevice> {
|
|
27
|
+
try {
|
|
28
|
+
const response = await axios.get(
|
|
29
|
+
`${this.baseUrl}/devices/${deviceId}?withHubDetails=${withHubDetails} `
|
|
30
|
+
);
|
|
31
|
+
return response.data;
|
|
32
|
+
} catch (error: any) {
|
|
33
|
+
console.log(error);
|
|
34
|
+
throw new Error(`Failed to get device: ${error.message}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async getDevices(
|
|
39
|
+
deviceIds: string[],
|
|
40
|
+
withHubDetails: boolean = false
|
|
41
|
+
): Promise<IDevice[]> {
|
|
42
|
+
return await axios.get(`${this.baseUrl}/devices`, {
|
|
43
|
+
params: { deviceIds, withHubDetails },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async getPropertyDevices(
|
|
48
|
+
propertyId: string,
|
|
49
|
+
withHubDetails: boolean = false
|
|
50
|
+
): Promise<IDevice[]> {
|
|
51
|
+
return await axios.get(`${this.baseUrl}/devices`, {
|
|
52
|
+
params: { propertyId, withHubDetails },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async getState(deviceId: string): Promise<any> {
|
|
57
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async getStatus(deviceId: string): Promise<Record<string, any>> {
|
|
61
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async getBatteryLevel(deviceId: string): Promise<Record<string, any>> {
|
|
65
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async getMetaData(deviceId: string): Promise<any> {
|
|
69
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getDevicesByAccessGroup(accessGroupId: string): Promise<IDtDevice[]> {
|
|
73
|
+
try {
|
|
74
|
+
const result = await this.postgres.query(
|
|
75
|
+
`SELECT d.* FROM dt_devices d
|
|
76
|
+
INNER JOIN dt_zones_collection_map zcm ON d.zoneId = zcm.zoneId
|
|
77
|
+
WHERE zcm.collectionId = $1`,
|
|
78
|
+
[accessGroupId]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
if (result.rows.length > 0) {
|
|
82
|
+
return result.rows;
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.log(error);
|
|
87
|
+
throw new Error("Failed to get devices by access group");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async getDevicesByZone(zoneId: string): Promise<IDtDevice[]> {
|
|
92
|
+
try {
|
|
93
|
+
const result = await this.postgres.query(
|
|
94
|
+
"SELECT * FROM dt_devices WHERE zoneId = $1",
|
|
95
|
+
[zoneId]
|
|
96
|
+
);
|
|
97
|
+
if (result.rows.length > 0) {
|
|
98
|
+
return result.rows;
|
|
99
|
+
}
|
|
100
|
+
return [];
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.log(error);
|
|
103
|
+
throw new Error("Failed to get device by zone");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { getConfig } from "../../../config/config";
|
|
3
|
+
import { getPostgresClient } from "../../../db";
|
|
4
|
+
import { Service } from "typedi";
|
|
5
|
+
import { IDevice } from "../interfaces";
|
|
6
|
+
|
|
7
|
+
@Service()
|
|
8
|
+
export class HubRepository {
|
|
9
|
+
private readonly baseUrl: string;
|
|
10
|
+
private readonly postgres;
|
|
11
|
+
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
|
+
this.postgres = getPostgresClient();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async addHub(body: any): Promise<IDevice> {
|
|
23
|
+
return await axios.post(`${this.baseUrl}/devices/hubs`, body);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async getHubs(hubIds: string[]): Promise<IDevice[]> {
|
|
27
|
+
const query = hubIds && hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
28
|
+
return await axios.get(`${this.baseUrl}/devices/hubs${query}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async getHub(hubId: string): Promise<IDevice> {
|
|
32
|
+
return await axios.get(`${this.baseUrl}/devices/hubs/${hubId}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async updateHub(hubId: string, body: any): Promise<IDevice> {
|
|
36
|
+
return await axios.put(`${this.baseUrl}/devices/hubs/${hubId}`, body);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async getStatus(hubId: string): Promise<"ONLINE" | "OFFLINE" | "UNKNOWN"> {
|
|
40
|
+
return await axios.get(`${this.baseUrl}/devices/hubs/${hubId}/status`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async deleteHub(hubId: string): Promise<any> {
|
|
44
|
+
return await axios.delete(`${this.baseUrl}/devices/hubs/${hubId}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async deleteAllHubs(hubIds: string[]): Promise<any> {
|
|
48
|
+
const query = hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
49
|
+
return await axios.delete(`${this.baseUrl}/devices/hubs${query}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getPostgresClient } from "../../../db";
|
|
2
|
+
import { Service } from "typedi";
|
|
3
|
+
|
|
4
|
+
@Service()
|
|
5
|
+
export class PropertyRepository {
|
|
6
|
+
private readonly postgres;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.postgres = getPostgresClient();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async getPropertyPreferences(
|
|
12
|
+
propertyId: string
|
|
13
|
+
): Promise<{ id: string; propertyId: string; settings: string } | null> {
|
|
14
|
+
try {
|
|
15
|
+
const propertyPreferences = await this.postgres.query(
|
|
16
|
+
"SELECT * FROM dt_property_settings WHERE propertyId = $1",
|
|
17
|
+
[propertyId]
|
|
18
|
+
);
|
|
19
|
+
if (propertyPreferences.rows.length > 0) {
|
|
20
|
+
return {
|
|
21
|
+
id: propertyPreferences.rows[0].id,
|
|
22
|
+
propertyId: propertyPreferences.rows[0].propertyId,
|
|
23
|
+
settings: JSON.parse(propertyPreferences.rows[0].settings),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.log(error);
|
|
29
|
+
throw new Error("Failed to get property preferences");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IConnection } from "
|
|
1
|
+
import { Container } from "typedi";
|
|
2
|
+
import { IConnection } from "../../cloud/types";
|
|
3
|
+
import { ConnectionRepository } from "../repository/Connection.repository";
|
|
4
|
+
|
|
3
5
|
export class LocalConnectionService {
|
|
4
|
-
private readonly
|
|
6
|
+
private readonly connectionRepository: ConnectionRepository;
|
|
5
7
|
constructor() {
|
|
6
|
-
this.
|
|
8
|
+
this.connectionRepository = Container.get(ConnectionRepository);
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
async getConnection(connectionId: string): Promise<IConnection> {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return
|
|
12
|
+
return await this.connectionRepository.getConnectionById(connectionId);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async updateConnection(connectionId: string, data: any) {
|
|
16
|
+
return await this.connectionRepository.updateConnection(connectionId, data);
|
|
15
17
|
}
|
|
16
18
|
}
|
|
@@ -7,111 +7,66 @@ import {
|
|
|
7
7
|
import { IDevice } from "../interfaces";
|
|
8
8
|
import { eventDispatcher } from "dt-pub-sub";
|
|
9
9
|
import { publishAudit } from "dt-audit-library";
|
|
10
|
-
import { EventHandler } from "../
|
|
10
|
+
import { EventHandler } from "../events/EventHandler";
|
|
11
11
|
import { isEqual } from "lodash";
|
|
12
12
|
import { AlertService } from "./Alert.service";
|
|
13
13
|
import { getPostgresClient } from "../../../db";
|
|
14
|
+
import { DeviceRepository } from "../repository/Device.repository";
|
|
15
|
+
import Container from "typedi";
|
|
14
16
|
|
|
15
17
|
export class LocalDeviceService {
|
|
16
|
-
private readonly baseUrl: string;
|
|
17
18
|
private readonly source = "dt-common-device";
|
|
18
19
|
private readonly eventHandler: EventHandler;
|
|
19
20
|
private readonly alertService: AlertService;
|
|
20
21
|
//private readonly redis;
|
|
21
|
-
private readonly
|
|
22
|
+
private readonly deviceRepository: DeviceRepository;
|
|
22
23
|
constructor() {
|
|
23
|
-
const { DEVICE_SERVICE } = getConfig();
|
|
24
|
-
if (!DEVICE_SERVICE) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
24
|
// this.redis = getRedisClient();
|
|
30
|
-
this.postgres = getPostgresClient();
|
|
31
|
-
this.baseUrl = DEVICE_SERVICE;
|
|
32
25
|
checkAwsEnv();
|
|
33
26
|
ensureAuditInitialized();
|
|
34
27
|
this.eventHandler = new EventHandler();
|
|
35
28
|
this.alertService = new AlertService();
|
|
29
|
+
this.deviceRepository = Container.get(DeviceRepository);
|
|
36
30
|
}
|
|
37
31
|
|
|
38
32
|
async createDevice(body: IDevice): Promise<void> {
|
|
39
|
-
await
|
|
40
|
-
const payload = {
|
|
41
|
-
eventType: "device.created",
|
|
42
|
-
properties: {
|
|
43
|
-
...body,
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
await publishAudit(payload);
|
|
33
|
+
return await this.eventHandler.onDeviceCreate(body);
|
|
47
34
|
}
|
|
48
35
|
|
|
49
36
|
async getDevice(
|
|
50
37
|
deviceId: string,
|
|
51
38
|
withHubDetails: boolean = false
|
|
52
39
|
): Promise<IDevice> {
|
|
53
|
-
|
|
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
|
-
}
|
|
40
|
+
return await this.deviceRepository.getDevice(deviceId, withHubDetails);
|
|
62
41
|
}
|
|
63
42
|
|
|
64
43
|
async getDevices(
|
|
65
44
|
deviceIds: string[],
|
|
66
45
|
withHubDetails: boolean = false
|
|
67
46
|
): Promise<IDevice[]> {
|
|
68
|
-
return await
|
|
69
|
-
params: { deviceIds, withHubDetails },
|
|
70
|
-
});
|
|
47
|
+
return await this.deviceRepository.getDevices(deviceIds, withHubDetails);
|
|
71
48
|
}
|
|
72
49
|
|
|
73
50
|
async getPropertyDevices(
|
|
74
51
|
propertyId: string,
|
|
75
52
|
withHubDetails: boolean = false
|
|
76
53
|
): Promise<IDevice[]> {
|
|
77
|
-
return await
|
|
78
|
-
|
|
79
|
-
|
|
54
|
+
return await this.deviceRepository.getPropertyDevices(
|
|
55
|
+
propertyId,
|
|
56
|
+
withHubDetails
|
|
57
|
+
);
|
|
80
58
|
}
|
|
81
59
|
|
|
82
60
|
async updateDevice(deviceId: string, body: any): Promise<any> {
|
|
83
|
-
|
|
84
|
-
await eventDispatcher.publishEvent(
|
|
85
|
-
"device.updated",
|
|
86
|
-
{ deviceId, body },
|
|
87
|
-
this.source
|
|
88
|
-
);
|
|
89
|
-
const payload = {
|
|
90
|
-
eventType: "device.updated",
|
|
91
|
-
properties: {
|
|
92
|
-
...body,
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
await publishAudit(payload);
|
|
61
|
+
return await this.eventHandler.onDeviceUpdate(deviceId, body);
|
|
96
62
|
}
|
|
97
63
|
|
|
98
64
|
async deleteDevice(deviceId: string): Promise<any> {
|
|
99
|
-
await
|
|
100
|
-
"device.deleted",
|
|
101
|
-
{ deviceId },
|
|
102
|
-
this.source
|
|
103
|
-
);
|
|
104
|
-
const payload = {
|
|
105
|
-
eventType: "device.deleted",
|
|
106
|
-
properties: {
|
|
107
|
-
deviceId,
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
await publishAudit(payload);
|
|
65
|
+
return await this.eventHandler.onDeviceDelete(deviceId);
|
|
111
66
|
}
|
|
112
67
|
|
|
113
68
|
async getState(deviceId: string) {
|
|
114
|
-
return await
|
|
69
|
+
return await this.deviceRepository.getState(deviceId);
|
|
115
70
|
}
|
|
116
71
|
|
|
117
72
|
async setState(deviceId: string, newState: any) {
|
|
@@ -126,7 +81,7 @@ export class LocalDeviceService {
|
|
|
126
81
|
}
|
|
127
82
|
|
|
128
83
|
async getStatus(deviceId: string) {
|
|
129
|
-
return await
|
|
84
|
+
return await this.deviceRepository.getStatus(deviceId);
|
|
130
85
|
}
|
|
131
86
|
|
|
132
87
|
async setStatus(deviceId: string, newStatus: any) {
|
|
@@ -155,7 +110,7 @@ export class LocalDeviceService {
|
|
|
155
110
|
}
|
|
156
111
|
|
|
157
112
|
async getBatteryLevel(deviceId: string) {
|
|
158
|
-
return await
|
|
113
|
+
return await this.deviceRepository.getBatteryLevel(deviceId);
|
|
159
114
|
}
|
|
160
115
|
async setBatteryLevel(deviceId: string, newBatteryLevel: any) {
|
|
161
116
|
// If old battery level and new battery level are different
|
|
@@ -181,7 +136,7 @@ export class LocalDeviceService {
|
|
|
181
136
|
}
|
|
182
137
|
}
|
|
183
138
|
async getMetaData(deviceId: string) {
|
|
184
|
-
return await
|
|
139
|
+
return await this.deviceRepository.getMetaData(deviceId);
|
|
185
140
|
}
|
|
186
141
|
async setMetaData(deviceId: string, metaData: Record<string, any>) {
|
|
187
142
|
await eventDispatcher.publishEvent(
|
|
@@ -199,54 +154,11 @@ export class LocalDeviceService {
|
|
|
199
154
|
await publishAudit(payload);
|
|
200
155
|
}
|
|
201
156
|
|
|
202
|
-
async
|
|
203
|
-
|
|
204
|
-
// If not available, fetch from PostgreSQL DB
|
|
205
|
-
const result = await this.postgres.query(
|
|
206
|
-
"SELECT * FROM dt_devices WHERE zoneId = $1",
|
|
207
|
-
[zoneId]
|
|
208
|
-
);
|
|
209
|
-
if (result.rows.length > 0) {
|
|
210
|
-
return result.rows[0];
|
|
211
|
-
}
|
|
212
|
-
// If data is not available, return null
|
|
213
|
-
return null;
|
|
214
|
-
} catch (error) {
|
|
215
|
-
console.log(error);
|
|
216
|
-
throw new Error("Failed to get device by zone");
|
|
217
|
-
}
|
|
157
|
+
async getDevicesByZone(zoneId: string) {
|
|
158
|
+
return await this.deviceRepository.getDevicesByZone(zoneId);
|
|
218
159
|
}
|
|
219
160
|
|
|
220
161
|
async getDevicesByAccessGroup(accessGroupId: string) {
|
|
221
|
-
|
|
222
|
-
// Fetch from Postgres if not in cache
|
|
223
|
-
const result = await this.postgres.query(
|
|
224
|
-
"SELECT * FROM dt_group WHERE id = $1",
|
|
225
|
-
[accessGroupId]
|
|
226
|
-
);
|
|
227
|
-
if (result.rows.length > 0) {
|
|
228
|
-
return result.rows[0];
|
|
229
|
-
}
|
|
230
|
-
return null;
|
|
231
|
-
} catch (error) {
|
|
232
|
-
console.log(error);
|
|
233
|
-
throw new Error("Failed to get devices by access group");
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async getPropertyPreferences(propertyId: string) {
|
|
238
|
-
try {
|
|
239
|
-
const propertyPreferences = await this.postgres.query(
|
|
240
|
-
"SELECT * FROM dt_property_settings WHERE propertyId = $1",
|
|
241
|
-
[propertyId]
|
|
242
|
-
);
|
|
243
|
-
if (propertyPreferences.rows.length > 0) {
|
|
244
|
-
return propertyPreferences.rows[0];
|
|
245
|
-
}
|
|
246
|
-
return null;
|
|
247
|
-
} catch (error) {
|
|
248
|
-
console.log(error);
|
|
249
|
-
throw new Error("Failed to get property preferences");
|
|
250
|
-
}
|
|
162
|
+
return await this.deviceRepository.getDevicesByAccessGroup(accessGroupId);
|
|
251
163
|
}
|
|
252
164
|
}
|
|
@@ -1,49 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
export class HubService {
|
|
6
|
-
private readonly baseUrl: string;
|
|
1
|
+
import { IDevice } from "../interfaces";
|
|
2
|
+
import { HubRepository } from "../repository/Hub.repository";
|
|
3
|
+
import Container from "typedi";
|
|
7
4
|
|
|
5
|
+
export class LocalHubService {
|
|
6
|
+
private readonly hubRepository: HubRepository;
|
|
8
7
|
constructor() {
|
|
9
|
-
|
|
10
|
-
if (!DEVICE_SERVICE) {
|
|
11
|
-
throw new Error(
|
|
12
|
-
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
this.baseUrl = DEVICE_SERVICE;
|
|
8
|
+
this.hubRepository = Container.get(HubRepository);
|
|
16
9
|
}
|
|
17
10
|
|
|
18
|
-
async addHub(body:
|
|
19
|
-
return await
|
|
11
|
+
async addHub(body: IDevice): Promise<IDevice> {
|
|
12
|
+
return await this.hubRepository.addHub(body);
|
|
20
13
|
}
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const query = hubIds && hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
25
|
-
return await axios.get(`${this.baseUrl}/devices/hubs${query}`);
|
|
15
|
+
async getHubs(hubIds: string[]): Promise<IDevice[]> {
|
|
16
|
+
return await this.hubRepository.getHubs(hubIds);
|
|
26
17
|
}
|
|
27
18
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return await axios.get(`${this.baseUrl}/devices/hubs/${hubId}`);
|
|
19
|
+
async getHub(hubId: string): Promise<IDevice> {
|
|
20
|
+
return await this.hubRepository.getHub(hubId);
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
async updateHub(hubId: string, body:
|
|
34
|
-
return await
|
|
23
|
+
async updateHub(hubId: string, body: Partial<IDevice>): Promise<IDevice> {
|
|
24
|
+
return await this.hubRepository.updateHub(hubId, body);
|
|
35
25
|
}
|
|
36
26
|
|
|
37
27
|
async getStatus(hubId: string): Promise<any> {
|
|
38
|
-
return await
|
|
28
|
+
return await this.hubRepository.getStatus(hubId);
|
|
39
29
|
}
|
|
40
30
|
|
|
41
31
|
async deleteHub(hubId: string): Promise<any> {
|
|
42
|
-
return await
|
|
32
|
+
return await this.hubRepository.deleteHub(hubId);
|
|
43
33
|
}
|
|
44
34
|
|
|
45
35
|
async deleteAllHubs(hubIds: string[]): Promise<any> {
|
|
46
|
-
|
|
47
|
-
return await axios.delete(`${this.baseUrl}/devices/hubs${query}`);
|
|
36
|
+
return await this.hubRepository.deleteAllHubs(hubIds);
|
|
48
37
|
}
|
|
49
38
|
}
|