dt-common-device 1.2.4 → 1.2.5
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/CloudDevice.d.ts +1 -1
- package/dist/device/cloud/entities/CloudDeviceService.d.ts +1 -1
- package/dist/device/cloud/entities/index.d.ts +1 -0
- package/dist/device/cloud/entities/index.js +1 -0
- package/dist/device/cloud/interface.d.ts +101 -0
- package/dist/device/cloud/interface.js +3 -0
- package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +1 -1
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -0
- package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
- package/dist/device/cloud/interfaces/IDevicesService.js +2 -0
- package/dist/device/cloud/interfaces/index.d.ts +1 -1
- package/dist/device/cloud/interfaces/index.js +1 -1
- package/dist/device/cloud/services/Device.service.d.ts +39 -0
- package/dist/device/cloud/services/Device.service.js +9 -0
- package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
- package/dist/device/cloud/services/DeviceCloudService.js +59 -0
- package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
- package/dist/device/cloud/services/DeviceHub.service.js +6 -0
- package/dist/device/cloud/services/Hub.service.d.ts +25 -0
- package/dist/device/cloud/services/Hub.service.js +9 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
- package/dist/device/index.d.ts +4 -0
- package/dist/device/index.js +20 -0
- package/dist/device/local/events/EventHandler.d.ts +0 -1
- package/dist/device/local/events/EventHandler.js +0 -11
- package/dist/device/local/interface.d.ts +0 -0
- package/dist/device/local/interface.js +1 -0
- package/dist/device/local/interfaces/IConnection.d.ts +1 -11
- package/dist/device/local/interfaces/IConnection.js +0 -12
- package/dist/device/local/repository/Connection.repository.d.ts +1 -2
- package/dist/device/local/repository/Connection.repository.js +0 -15
- package/dist/device/local/repository/Hub.repository.d.ts +1 -1
- package/dist/device/local/repository/Property.repository.d.ts +1 -0
- package/dist/device/local/repository/Property.repository.js +12 -1
- package/dist/device/local/services/Connection.service.d.ts +2 -3
- package/dist/device/local/services/Connection.service.js +0 -15
- package/dist/device/local/services/Device.service.d.ts +1 -0
- package/dist/device/local/services/Device.service.js +13 -46
- package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
- package/dist/device/local/services/DeviceHub.service.js +40 -0
- package/dist/device/local/services/Hub.service.d.ts +1 -1
- package/dist/device/local/services/Hub.service.js +0 -18
- package/dist/device/local/services/Property.service.d.ts +1 -0
- package/dist/device/local/services/Property.service.js +4 -9
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -2
- package/package.json +4 -4
- package/src/device/cloud/entities/CloudConnection.ts +13 -0
- package/src/device/cloud/entities/CloudDevice.ts +1 -1
- package/src/device/cloud/entities/CloudDeviceService.ts +1 -1
- package/src/device/cloud/entities/index.ts +1 -0
- package/src/device/cloud/interfaces/ICloudConnection.ts +6 -0
- package/src/device/cloud/interfaces/ICloudDeviceService.ts +1 -1
- package/src/device/cloud/interfaces/index.ts +1 -1
- package/src/device/cloud/types.ts +58 -0
- package/src/device/local/events/EventHandler.ts +0 -16
- package/src/device/local/repository/Connection.repository.ts +1 -21
- package/src/device/local/repository/Hub.repository.ts +1 -1
- package/src/device/local/repository/Property.repository.ts +14 -1
- package/src/device/local/services/Connection.service.ts +2 -23
- package/src/device/local/services/Device.service.ts +25 -47
- package/src/device/local/services/Hub.service.ts +1 -20
- package/src/device/local/services/Property.service.ts +5 -9
- package/src/index.ts +3 -1
- package/src/device/local/interfaces/IConnection.ts +0 -27
|
@@ -64,12 +64,23 @@ let PropertyRepository = (() => {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
async getProperty(propertyId) {
|
|
67
|
-
const property = await this.postgres.query("SELECT * FROM
|
|
67
|
+
const property = await this.postgres.query("SELECT * FROM dt_properties WHERE id = $1", [propertyId]);
|
|
68
68
|
if (property.rows.length > 0) {
|
|
69
69
|
return property.rows[0];
|
|
70
70
|
}
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
73
|
+
async getAllProperties() {
|
|
74
|
+
try {
|
|
75
|
+
//Retrieve all the properties ids from the database
|
|
76
|
+
const properties = await this.postgres.query("SELECT id FROM dt_properties");
|
|
77
|
+
return properties.rows.map((property) => property.id);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
console.log(error);
|
|
81
|
+
throw new Error("Failed to get all properties");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
73
84
|
};
|
|
74
85
|
__setFunctionName(_classThis, "PropertyRepository");
|
|
75
86
|
(() => {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { IConnection } from "
|
|
1
|
+
import { IConnection } from "../../cloud/types";
|
|
2
2
|
export declare class LocalConnectionService {
|
|
3
3
|
private readonly connectionRepository;
|
|
4
4
|
constructor();
|
|
5
|
-
createConnection(data: Partial<IConnection>): Promise<IConnection>;
|
|
6
5
|
getConnection(connectionId: string): Promise<IConnection>;
|
|
7
|
-
updateConnection(connectionId: string, data:
|
|
6
|
+
updateConnection(connectionId: string, data: any): Promise<any>;
|
|
8
7
|
}
|
|
@@ -7,25 +7,10 @@ class LocalConnectionService {
|
|
|
7
7
|
constructor() {
|
|
8
8
|
this.connectionRepository = typedi_1.Container.get(Connection_repository_1.ConnectionRepository);
|
|
9
9
|
}
|
|
10
|
-
async createConnection(data) {
|
|
11
|
-
if (!data.connectionName ||
|
|
12
|
-
!data.connectionRefId ||
|
|
13
|
-
!data.propertyId ||
|
|
14
|
-
!data.connectionProvider) {
|
|
15
|
-
throw new Error("Missing required fields");
|
|
16
|
-
}
|
|
17
|
-
return await this.connectionRepository.createConnection(data);
|
|
18
|
-
}
|
|
19
10
|
async getConnection(connectionId) {
|
|
20
|
-
if (!connectionId) {
|
|
21
|
-
throw new Error("Connection ID is required");
|
|
22
|
-
}
|
|
23
11
|
return await this.connectionRepository.getConnectionById(connectionId);
|
|
24
12
|
}
|
|
25
13
|
async updateConnection(connectionId, data) {
|
|
26
|
-
if (!connectionId) {
|
|
27
|
-
throw new Error("Connection ID is required");
|
|
28
|
-
}
|
|
29
14
|
return await this.connectionRepository.updateConnection(connectionId, data);
|
|
30
15
|
}
|
|
31
16
|
}
|
|
@@ -5,6 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.LocalDeviceService = void 0;
|
|
7
7
|
const config_1 = require("../../../config/config");
|
|
8
|
+
const dt_pub_sub_1 = require("dt-pub-sub");
|
|
9
|
+
const dt_audit_library_1 = require("dt-audit-library");
|
|
8
10
|
const EventHandler_1 = require("../events/EventHandler");
|
|
9
11
|
const lodash_1 = require("lodash");
|
|
10
12
|
const Alert_service_1 = require("./Alert.service");
|
|
@@ -12,6 +14,8 @@ const Device_repository_1 = require("../repository/Device.repository");
|
|
|
12
14
|
const typedi_1 = __importDefault(require("typedi"));
|
|
13
15
|
class LocalDeviceService {
|
|
14
16
|
constructor() {
|
|
17
|
+
this.source = "dt-common-device";
|
|
18
|
+
// this.redis = getRedisClient();
|
|
15
19
|
(0, config_1.checkAwsEnv)();
|
|
16
20
|
(0, config_1.ensureAuditInitialized)();
|
|
17
21
|
this.eventHandler = new EventHandler_1.EventHandler();
|
|
@@ -22,45 +26,24 @@ class LocalDeviceService {
|
|
|
22
26
|
return await this.eventHandler.onDeviceCreate(body);
|
|
23
27
|
}
|
|
24
28
|
async getDevice(deviceId, withHubDetails = false) {
|
|
25
|
-
if (!deviceId) {
|
|
26
|
-
throw new Error("Device ID is required");
|
|
27
|
-
}
|
|
28
29
|
return await this.deviceRepository.getDevice(deviceId, withHubDetails);
|
|
29
30
|
}
|
|
30
31
|
async getDevices(deviceIds, withHubDetails = false) {
|
|
31
|
-
if (!deviceIds.length) {
|
|
32
|
-
throw new Error("At least one device ID is required");
|
|
33
|
-
}
|
|
34
32
|
return await this.deviceRepository.getDevices(deviceIds, withHubDetails);
|
|
35
33
|
}
|
|
36
34
|
async getPropertyDevices(propertyId, withHubDetails = false) {
|
|
37
|
-
if (!propertyId) {
|
|
38
|
-
throw new Error("Property ID is required");
|
|
39
|
-
}
|
|
40
35
|
return await this.deviceRepository.getPropertyDevices(propertyId, withHubDetails);
|
|
41
36
|
}
|
|
42
37
|
async updateDevice(deviceId, body) {
|
|
43
|
-
if (!deviceId) {
|
|
44
|
-
throw new Error("Device ID is required");
|
|
45
|
-
}
|
|
46
38
|
return await this.eventHandler.onDeviceUpdate(deviceId, body);
|
|
47
39
|
}
|
|
48
40
|
async deleteDevice(deviceId) {
|
|
49
|
-
if (!deviceId) {
|
|
50
|
-
throw new Error("Device ID is required");
|
|
51
|
-
}
|
|
52
41
|
return await this.eventHandler.onDeviceDelete(deviceId);
|
|
53
42
|
}
|
|
54
43
|
async getState(deviceId) {
|
|
55
|
-
if (!deviceId) {
|
|
56
|
-
throw new Error("Device ID is required");
|
|
57
|
-
}
|
|
58
44
|
return await this.deviceRepository.getState(deviceId);
|
|
59
45
|
}
|
|
60
46
|
async setState(deviceId, newState) {
|
|
61
|
-
if (!deviceId || !newState) {
|
|
62
|
-
throw new Error("Device ID and new state are required");
|
|
63
|
-
}
|
|
64
47
|
// If old state and new state are different
|
|
65
48
|
const oldState = (await this.getState(deviceId))?.data?.state || {};
|
|
66
49
|
const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
|
|
@@ -69,15 +52,9 @@ class LocalDeviceService {
|
|
|
69
52
|
}
|
|
70
53
|
}
|
|
71
54
|
async getStatus(deviceId) {
|
|
72
|
-
if (!deviceId) {
|
|
73
|
-
throw new Error("Device ID is required");
|
|
74
|
-
}
|
|
75
55
|
return await this.deviceRepository.getStatus(deviceId);
|
|
76
56
|
}
|
|
77
57
|
async setStatus(deviceId, newStatus) {
|
|
78
|
-
if (!deviceId || !newStatus) {
|
|
79
|
-
throw new Error("Device ID and new status are required");
|
|
80
|
-
}
|
|
81
58
|
// If old status and new status are different
|
|
82
59
|
const oldStatus = await this.getStatus(deviceId);
|
|
83
60
|
const changedKeys = Object.keys(newStatus).filter((key) => !(0, lodash_1.isEqual)(oldStatus?.data?.[key], newStatus?.[key]));
|
|
@@ -98,15 +75,9 @@ class LocalDeviceService {
|
|
|
98
75
|
}
|
|
99
76
|
}
|
|
100
77
|
async getBatteryLevel(deviceId) {
|
|
101
|
-
if (!deviceId) {
|
|
102
|
-
throw new Error("Device ID is required");
|
|
103
|
-
}
|
|
104
78
|
return await this.deviceRepository.getBatteryLevel(deviceId);
|
|
105
79
|
}
|
|
106
80
|
async setBatteryLevel(deviceId, newBatteryLevel) {
|
|
107
|
-
if (!deviceId || !newBatteryLevel) {
|
|
108
|
-
throw new Error("Device ID and new battery level are required");
|
|
109
|
-
}
|
|
110
81
|
// If old battery level and new battery level are different
|
|
111
82
|
const oldBatteryLevel = await this.getBatteryLevel(deviceId);
|
|
112
83
|
const changedKeys = Object.keys(newBatteryLevel).filter((key) => !(0, lodash_1.isEqual)(oldBatteryLevel?.data?.[key], newBatteryLevel?.[key]));
|
|
@@ -123,27 +94,23 @@ class LocalDeviceService {
|
|
|
123
94
|
}
|
|
124
95
|
}
|
|
125
96
|
async getMetaData(deviceId) {
|
|
126
|
-
if (!deviceId) {
|
|
127
|
-
throw new Error("Device ID is required");
|
|
128
|
-
}
|
|
129
97
|
return await this.deviceRepository.getMetaData(deviceId);
|
|
130
98
|
}
|
|
131
99
|
async setMetaData(deviceId, metaData) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
100
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent("device.metaData.set", { deviceId, metaData }, this.source);
|
|
101
|
+
const payload = {
|
|
102
|
+
eventType: "device.metaData.set",
|
|
103
|
+
properties: {
|
|
104
|
+
deviceId,
|
|
105
|
+
metaData,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
136
109
|
}
|
|
137
110
|
async getDevicesByZone(zoneId) {
|
|
138
|
-
if (!zoneId) {
|
|
139
|
-
throw new Error("Zone ID is required");
|
|
140
|
-
}
|
|
141
111
|
return await this.deviceRepository.getDevicesByZone(zoneId);
|
|
142
112
|
}
|
|
143
113
|
async getDevicesByAccessGroup(accessGroupId) {
|
|
144
|
-
if (!accessGroupId) {
|
|
145
|
-
throw new Error("Access Group ID is required");
|
|
146
|
-
}
|
|
147
114
|
return await this.deviceRepository.getDevicesByAccessGroup(accessGroupId);
|
|
148
115
|
}
|
|
149
116
|
}
|
|
@@ -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,7 +2,7 @@ import { IDevice } from "../interfaces";
|
|
|
2
2
|
export declare class LocalHubService {
|
|
3
3
|
private readonly hubRepository;
|
|
4
4
|
constructor();
|
|
5
|
-
addHub(body:
|
|
5
|
+
addHub(body: IDevice): Promise<IDevice>;
|
|
6
6
|
getHubs(hubIds: string[]): Promise<IDevice[]>;
|
|
7
7
|
getHub(hubId: string): Promise<IDevice>;
|
|
8
8
|
updateHub(hubId: string, body: Partial<IDevice>): Promise<IDevice>;
|
|
@@ -14,39 +14,21 @@ class LocalHubService {
|
|
|
14
14
|
return await this.hubRepository.addHub(body);
|
|
15
15
|
}
|
|
16
16
|
async getHubs(hubIds) {
|
|
17
|
-
if (!hubIds.length) {
|
|
18
|
-
throw new Error("At least one hub ID is required");
|
|
19
|
-
}
|
|
20
17
|
return await this.hubRepository.getHubs(hubIds);
|
|
21
18
|
}
|
|
22
19
|
async getHub(hubId) {
|
|
23
|
-
if (!hubId) {
|
|
24
|
-
throw new Error("Hub ID is required");
|
|
25
|
-
}
|
|
26
20
|
return await this.hubRepository.getHub(hubId);
|
|
27
21
|
}
|
|
28
22
|
async updateHub(hubId, body) {
|
|
29
|
-
if (!hubId) {
|
|
30
|
-
throw new Error("Hub ID is required");
|
|
31
|
-
}
|
|
32
23
|
return await this.hubRepository.updateHub(hubId, body);
|
|
33
24
|
}
|
|
34
25
|
async getStatus(hubId) {
|
|
35
|
-
if (!hubId) {
|
|
36
|
-
throw new Error("Hub ID is required");
|
|
37
|
-
}
|
|
38
26
|
return await this.hubRepository.getStatus(hubId);
|
|
39
27
|
}
|
|
40
28
|
async deleteHub(hubId) {
|
|
41
|
-
if (!hubId) {
|
|
42
|
-
throw new Error("Hub ID is required");
|
|
43
|
-
}
|
|
44
29
|
return await this.hubRepository.deleteHub(hubId);
|
|
45
30
|
}
|
|
46
31
|
async deleteAllHubs(hubIds) {
|
|
47
|
-
if (!hubIds.length) {
|
|
48
|
-
throw new Error("At least one hub ID is required");
|
|
49
|
-
}
|
|
50
32
|
return await this.hubRepository.deleteAllHubs(hubIds);
|
|
51
33
|
}
|
|
52
34
|
}
|
|
@@ -4,4 +4,5 @@ export declare class LocalPropertyService {
|
|
|
4
4
|
getPropertyPreferences(propertyId: string): Promise<import("../interfaces/IProperty").IPropertySettings | null>;
|
|
5
5
|
getProperty(propertyId: string): Promise<import("../interfaces/IProperty").IProperty | null>;
|
|
6
6
|
getPropertyTimeZone(propertyId: string): Promise<string>;
|
|
7
|
+
getAllProperties(): Promise<any[]>;
|
|
7
8
|
}
|
|
@@ -7,26 +7,21 @@ class LocalPropertyService {
|
|
|
7
7
|
this.propertyRepository = new Property_repository_1.PropertyRepository();
|
|
8
8
|
}
|
|
9
9
|
async getPropertyPreferences(propertyId) {
|
|
10
|
-
if (!propertyId) {
|
|
11
|
-
throw new Error("Property ID is required");
|
|
12
|
-
}
|
|
13
10
|
return await this.propertyRepository.getPropertyPreferences(propertyId);
|
|
14
11
|
}
|
|
15
12
|
async getProperty(propertyId) {
|
|
16
|
-
if (!propertyId) {
|
|
17
|
-
throw new Error("Property ID is required");
|
|
18
|
-
}
|
|
19
13
|
return await this.propertyRepository.getProperty(propertyId);
|
|
20
14
|
}
|
|
21
15
|
async getPropertyTimeZone(propertyId) {
|
|
22
|
-
if (!propertyId) {
|
|
23
|
-
throw new Error("Property ID is required");
|
|
24
|
-
}
|
|
25
16
|
const property = await this.propertyRepository.getProperty(propertyId);
|
|
26
17
|
if (!property) {
|
|
27
18
|
throw new Error("Property not found");
|
|
28
19
|
}
|
|
29
20
|
return property.timezone;
|
|
30
21
|
}
|
|
22
|
+
async getAllProperties() {
|
|
23
|
+
const properties = await this.propertyRepository.getAllProperties();
|
|
24
|
+
return properties;
|
|
25
|
+
}
|
|
31
26
|
}
|
|
32
27
|
exports.LocalPropertyService = LocalPropertyService;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { CloudDevice,
|
|
1
|
+
export { CloudDevice, DeviceFactory, CloudConnection, CloudDeviceService, } from "./device/cloud/entities";
|
|
2
2
|
export { LocalDeviceService, LocalHubService, LocalConnectionService, LocalPropertyService, LocalScheduleService, } from "./device/local/services";
|
|
3
3
|
export * from "./device/cloud/interfaces";
|
|
4
|
+
export * from "./device/cloud/types";
|
|
4
5
|
export * from "./device/local/interfaces";
|
|
5
6
|
export { initialize, getConfig } from "./config/config";
|
package/dist/index.js
CHANGED
|
@@ -15,12 +15,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
16
|
};
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.getConfig = exports.initialize = exports.LocalScheduleService = exports.LocalPropertyService = exports.LocalConnectionService = exports.LocalHubService = exports.LocalDeviceService = exports.
|
|
18
|
+
exports.getConfig = exports.initialize = exports.LocalScheduleService = exports.LocalPropertyService = exports.LocalConnectionService = exports.LocalHubService = exports.LocalDeviceService = exports.CloudDeviceService = exports.CloudConnection = exports.DeviceFactory = exports.CloudDevice = void 0;
|
|
19
19
|
// Cloud exports
|
|
20
20
|
var entities_1 = require("./device/cloud/entities");
|
|
21
21
|
Object.defineProperty(exports, "CloudDevice", { enumerable: true, get: function () { return entities_1.CloudDevice; } });
|
|
22
|
-
Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return entities_1.CloudDeviceService; } });
|
|
23
22
|
Object.defineProperty(exports, "DeviceFactory", { enumerable: true, get: function () { return entities_1.DeviceFactory; } });
|
|
23
|
+
Object.defineProperty(exports, "CloudConnection", { enumerable: true, get: function () { return entities_1.CloudConnection; } });
|
|
24
|
+
Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return entities_1.CloudDeviceService; } });
|
|
24
25
|
var services_1 = require("./device/local/services");
|
|
25
26
|
Object.defineProperty(exports, "LocalDeviceService", { enumerable: true, get: function () { return services_1.LocalDeviceService; } });
|
|
26
27
|
Object.defineProperty(exports, "LocalHubService", { enumerable: true, get: function () { return services_1.LocalHubService; } });
|
|
@@ -28,6 +29,7 @@ Object.defineProperty(exports, "LocalConnectionService", { enumerable: true, get
|
|
|
28
29
|
Object.defineProperty(exports, "LocalPropertyService", { enumerable: true, get: function () { return services_1.LocalPropertyService; } });
|
|
29
30
|
Object.defineProperty(exports, "LocalScheduleService", { enumerable: true, get: function () { return services_1.LocalScheduleService; } });
|
|
30
31
|
__exportStar(require("./device/cloud/interfaces"), exports);
|
|
32
|
+
__exportStar(require("./device/cloud/types"), exports);
|
|
31
33
|
// Local exports
|
|
32
34
|
__exportStar(require("./device/local/interfaces"), exports);
|
|
33
35
|
//initialize export
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-common-device",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
7
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
|
+
"patch": "npm version patch && npm run build",
|
|
9
|
+
"minor": "npm version minor && npm run build",
|
|
10
|
+
"major": "npm version major && npm run build",
|
|
11
11
|
"security:audit": "npm audit --audit-level=moderate",
|
|
12
12
|
"security:fix": "npm audit fix",
|
|
13
13
|
"security:check": "npm audit && npm outdated",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Device Cloud Service - Class Implementation
|
|
2
|
+
import { ICloudConnection } from "../interfaces";
|
|
3
|
+
import { IDeviceAccountResponse, IConnection } from "../types";
|
|
4
|
+
|
|
5
|
+
export abstract class CloudConnection implements ICloudConnection {
|
|
6
|
+
abstract createConnection(
|
|
7
|
+
data: IConnection,
|
|
8
|
+
userId: string
|
|
9
|
+
): Promise<IConnection>;
|
|
10
|
+
abstract getDeviceAccount(
|
|
11
|
+
connection: IConnection
|
|
12
|
+
): Promise<IDeviceAccountResponse>;
|
|
13
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IDevice } from "../../local/interfaces";
|
|
2
2
|
import { ICloudDevice } from "../interfaces/ICloudDevice";
|
|
3
3
|
import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
|
|
4
|
-
import { IConnection } from "
|
|
4
|
+
import { IConnection } from "../types";
|
|
5
5
|
|
|
6
6
|
export abstract class CloudDevice implements ICloudDevice {
|
|
7
7
|
deviceId: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
|
|
2
|
-
import { IConnection } from "
|
|
2
|
+
import { IConnection } from "../types";
|
|
3
3
|
|
|
4
4
|
export class CloudDeviceService implements ICloudDeviceService {
|
|
5
5
|
async getConnection(deviceId: string): Promise<IConnection> {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Device Cloud Type Interfaces for DeviceThread Common Library
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a connection to a device provider.
|
|
5
|
+
* WARNING: Do not log or expose sensitive fields (accessToken, clientSecret).
|
|
6
|
+
*/
|
|
7
|
+
export interface IConnection {
|
|
8
|
+
id?: string;
|
|
9
|
+
createdAt?: Date;
|
|
10
|
+
updatedAt?: Date;
|
|
11
|
+
isDeleted?: boolean;
|
|
12
|
+
connectionName: string;
|
|
13
|
+
connectionRefId: string;
|
|
14
|
+
propertyId: string;
|
|
15
|
+
connectionProvider: ConnectionProvider;
|
|
16
|
+
accessToken?: string;
|
|
17
|
+
refreshToken?: string;
|
|
18
|
+
clientId?: string;
|
|
19
|
+
clientSecret: string;
|
|
20
|
+
isActive?: boolean;
|
|
21
|
+
metaData?: any;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface IConnectionPagination {
|
|
25
|
+
page: number;
|
|
26
|
+
limit: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Device account response from provider.
|
|
31
|
+
* WARNING: Do not log or expose connection_access_token.
|
|
32
|
+
*/
|
|
33
|
+
export interface IDeviceAccountResponse {
|
|
34
|
+
id: string;
|
|
35
|
+
connection_name: string;
|
|
36
|
+
connection_access_token: string;
|
|
37
|
+
connection_provider: string;
|
|
38
|
+
totalDevices: number;
|
|
39
|
+
connection_ref_id: string;
|
|
40
|
+
isActive: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface IConnectionConnectParams {
|
|
44
|
+
code?: string;
|
|
45
|
+
propertyId?: string;
|
|
46
|
+
[key: string]: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export enum ConnectionProvider {
|
|
50
|
+
Smartthings = "Smartthings",
|
|
51
|
+
SaltoKS = "SaltoKS",
|
|
52
|
+
TTLock = "TTLock",
|
|
53
|
+
Tuya = "Tuya",
|
|
54
|
+
Schlage = "Schlage",
|
|
55
|
+
YaleWifi = "YaleWifi",
|
|
56
|
+
Sensibo = "Sensibo",
|
|
57
|
+
Devicethread = "Devicethread",
|
|
58
|
+
}
|
|
@@ -98,20 +98,4 @@ export class EventHandler {
|
|
|
98
98
|
};
|
|
99
99
|
await publishAudit(payload);
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
async onDeviceMetaChange(deviceId: string, metaData: Record<string, any>) {
|
|
103
|
-
await eventDispatcher.publishEvent(
|
|
104
|
-
DeviceEvents.DEVICE_META_DATA_SET,
|
|
105
|
-
{ deviceId, metaData },
|
|
106
|
-
this.source
|
|
107
|
-
);
|
|
108
|
-
const payload = {
|
|
109
|
-
eventType: DeviceEvents.DEVICE_META_DATA_SET,
|
|
110
|
-
properties: {
|
|
111
|
-
deviceId,
|
|
112
|
-
metaData,
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
await publishAudit(payload);
|
|
116
|
-
}
|
|
117
101
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getPostgresClient } from "../../../db";
|
|
2
|
-
import { IConnection } from "
|
|
2
|
+
import { IConnection } from "../../cloud/types";
|
|
3
3
|
import { Service } from "typedi";
|
|
4
4
|
|
|
5
5
|
@Service()
|
|
@@ -8,26 +8,6 @@ export class ConnectionRepository {
|
|
|
8
8
|
constructor() {
|
|
9
9
|
this.pool = getPostgresClient();
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
async createConnection(data: Partial<IConnection>): Promise<IConnection> {
|
|
13
|
-
const result = await this.pool.query(
|
|
14
|
-
"INSERT INTO dt_connections (connectionName, connectionRefId, propertyId, connectionProvider, accessToken, refreshToken, clientId, clientSecret, isActive, metaData) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *",
|
|
15
|
-
[
|
|
16
|
-
data.connectionName,
|
|
17
|
-
data.connectionRefId,
|
|
18
|
-
data.propertyId,
|
|
19
|
-
data.connectionProvider,
|
|
20
|
-
data.accessToken,
|
|
21
|
-
data.refreshToken,
|
|
22
|
-
data.clientId,
|
|
23
|
-
data.clientSecret,
|
|
24
|
-
data.isActive,
|
|
25
|
-
data.metaData,
|
|
26
|
-
]
|
|
27
|
-
);
|
|
28
|
-
return result.rows[0];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
11
|
async getConnectionById(connectionId: string): Promise<IConnection> {
|
|
32
12
|
const result = await this.pool.query(
|
|
33
13
|
"SELECT * FROM dt_connections WHERE id = $1",
|
|
@@ -29,7 +29,7 @@ export class PropertyRepository {
|
|
|
29
29
|
|
|
30
30
|
async getProperty(propertyId: string): Promise<IProperty | null> {
|
|
31
31
|
const property = await this.postgres.query(
|
|
32
|
-
"SELECT * FROM
|
|
32
|
+
"SELECT * FROM dt_properties WHERE id = $1",
|
|
33
33
|
[propertyId]
|
|
34
34
|
);
|
|
35
35
|
if (property.rows.length > 0) {
|
|
@@ -37,4 +37,17 @@ export class PropertyRepository {
|
|
|
37
37
|
}
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
async getAllProperties() {
|
|
42
|
+
try {
|
|
43
|
+
//Retrieve all the properties ids from the database
|
|
44
|
+
const properties = await this.postgres.query(
|
|
45
|
+
"SELECT id FROM dt_properties"
|
|
46
|
+
);
|
|
47
|
+
return properties.rows.map((property) => property.id);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.log(error);
|
|
50
|
+
throw new Error("Failed to get all properties");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
40
53
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Container } from "typedi";
|
|
2
|
+
import { IConnection } from "../../cloud/types";
|
|
2
3
|
import { ConnectionRepository } from "../repository/Connection.repository";
|
|
3
|
-
import { IConnection } from "../interfaces/IConnection";
|
|
4
4
|
|
|
5
5
|
export class LocalConnectionService {
|
|
6
6
|
private readonly connectionRepository: ConnectionRepository;
|
|
@@ -8,32 +8,11 @@ export class LocalConnectionService {
|
|
|
8
8
|
this.connectionRepository = Container.get(ConnectionRepository);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
async createConnection(data: Partial<IConnection>): Promise<IConnection> {
|
|
12
|
-
if (
|
|
13
|
-
!data.connectionName ||
|
|
14
|
-
!data.connectionRefId ||
|
|
15
|
-
!data.propertyId ||
|
|
16
|
-
!data.connectionProvider
|
|
17
|
-
) {
|
|
18
|
-
throw new Error("Missing required fields");
|
|
19
|
-
}
|
|
20
|
-
return await this.connectionRepository.createConnection(data);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
11
|
async getConnection(connectionId: string): Promise<IConnection> {
|
|
24
|
-
if (!connectionId) {
|
|
25
|
-
throw new Error("Connection ID is required");
|
|
26
|
-
}
|
|
27
12
|
return await this.connectionRepository.getConnectionById(connectionId);
|
|
28
13
|
}
|
|
29
14
|
|
|
30
|
-
async updateConnection(
|
|
31
|
-
connectionId: string,
|
|
32
|
-
data: Partial<IConnection>
|
|
33
|
-
): Promise<IConnection> {
|
|
34
|
-
if (!connectionId) {
|
|
35
|
-
throw new Error("Connection ID is required");
|
|
36
|
-
}
|
|
15
|
+
async updateConnection(connectionId: string, data: any) {
|
|
37
16
|
return await this.connectionRepository.updateConnection(connectionId, data);
|
|
38
17
|
}
|
|
39
18
|
}
|