dt-common-device 1.2.5 → 1.2.6

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.
Files changed (44) hide show
  1. package/dist/device/cloud/entities/CloudDevice.d.ts +1 -1
  2. package/dist/device/cloud/entities/CloudDeviceService.d.ts +1 -1
  3. package/dist/device/cloud/entities/index.d.ts +0 -1
  4. package/dist/device/cloud/entities/index.js +0 -1
  5. package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +1 -1
  6. package/dist/device/cloud/interfaces/index.d.ts +1 -1
  7. package/dist/device/cloud/interfaces/index.js +1 -1
  8. package/dist/device/local/events/EventHandler.d.ts +1 -0
  9. package/dist/device/local/events/EventHandler.js +11 -0
  10. package/dist/device/local/interfaces/IConnection.d.ts +11 -1
  11. package/dist/device/local/interfaces/IConnection.js +12 -0
  12. package/dist/device/local/repository/Connection.repository.d.ts +2 -1
  13. package/dist/device/local/repository/Connection.repository.js +15 -0
  14. package/dist/device/local/repository/Device.repository.d.ts +1 -1
  15. package/dist/device/local/repository/Device.repository.js +2 -2
  16. package/dist/device/local/repository/Hub.repository.d.ts +1 -1
  17. package/dist/device/local/services/Connection.service.d.ts +3 -2
  18. package/dist/device/local/services/Connection.service.js +15 -0
  19. package/dist/device/local/services/Device.service.d.ts +1 -2
  20. package/dist/device/local/services/Device.service.js +48 -15
  21. package/dist/device/local/services/Hub.service.d.ts +1 -1
  22. package/dist/device/local/services/Hub.service.js +18 -0
  23. package/dist/device/local/services/Property.service.js +9 -0
  24. package/dist/index.d.ts +1 -2
  25. package/dist/index.js +2 -4
  26. package/package.json +4 -4
  27. package/src/device/cloud/entities/CloudDevice.ts +1 -1
  28. package/src/device/cloud/entities/CloudDeviceService.ts +1 -1
  29. package/src/device/cloud/entities/index.ts +0 -1
  30. package/src/device/cloud/interfaces/ICloudDeviceService.ts +1 -1
  31. package/src/device/cloud/interfaces/index.ts +1 -1
  32. package/src/device/local/events/EventHandler.ts +16 -0
  33. package/src/device/local/interfaces/IConnection.ts +27 -0
  34. package/src/device/local/repository/Connection.repository.ts +21 -1
  35. package/src/device/local/repository/Device.repository.ts +3 -1
  36. package/src/device/local/repository/Hub.repository.ts +1 -1
  37. package/src/device/local/services/Connection.service.ts +23 -2
  38. package/src/device/local/services/Device.service.ts +51 -25
  39. package/src/device/local/services/Hub.service.ts +20 -1
  40. package/src/device/local/services/Property.service.ts +9 -0
  41. package/src/index.ts +1 -3
  42. package/src/device/cloud/entities/CloudConnection.ts +0 -13
  43. package/src/device/cloud/interfaces/ICloudConnection.ts +0 -6
  44. package/src/device/cloud/types.ts +0 -58
@@ -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 "../types";
4
+ import { IConnection } from "../../local/interfaces/IConnection";
5
5
  export declare abstract class CloudDevice implements ICloudDevice {
6
6
  deviceId: string;
7
7
  localDevice?: IDevice;
@@ -1,5 +1,5 @@
1
1
  import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
2
- import { IConnection } from "../types";
2
+ import { IConnection } from "../../local/interfaces/IConnection";
3
3
  export declare class CloudDeviceService implements ICloudDeviceService {
4
4
  getConnection(deviceId: string): Promise<IConnection>;
5
5
  }
@@ -1,4 +1,3 @@
1
1
  export * from "./CloudDevice";
2
2
  export * from "./DeviceFactory";
3
- export * from "./CloudConnection";
4
3
  export * from "./CloudDeviceService";
@@ -16,5 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./CloudDevice"), exports);
18
18
  __exportStar(require("./DeviceFactory"), exports);
19
- __exportStar(require("./CloudConnection"), exports);
20
19
  __exportStar(require("./CloudDeviceService"), exports);
@@ -1,4 +1,4 @@
1
- import { IConnection } from "../types";
1
+ import { IConnection } from "../../local/interfaces/IConnection";
2
2
  export interface ICloudDeviceService {
3
3
  getConnection(deviceId: string): Promise<IConnection>;
4
4
  }
@@ -1,3 +1,3 @@
1
- export * from "./ICloudConnection";
2
1
  export * from "./ICloudDevice";
3
2
  export * from "./ICloudDeviceService";
3
+ export * from "./IDeviceFactory";
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ICloudConnection"), exports);
18
17
  __exportStar(require("./ICloudDevice"), exports);
19
18
  __exportStar(require("./ICloudDeviceService"), exports);
19
+ __exportStar(require("./IDeviceFactory"), exports);
@@ -7,4 +7,5 @@ export declare class EventHandler {
7
7
  onStateChange(deviceId: string, state: any): Promise<void>;
8
8
  onStatusChange(deviceId: string, status: any): Promise<void>;
9
9
  onBatteryLevelChange(deviceId: string, batteryLevel: number): Promise<void>;
10
+ onDeviceMetaChange(deviceId: string, metaData: Record<string, any>): Promise<void>;
10
11
  }
@@ -71,5 +71,16 @@ class EventHandler {
71
71
  };
72
72
  await (0, dt_audit_library_1.publishAudit)(payload);
73
73
  }
74
+ async onDeviceMetaChange(deviceId, metaData) {
75
+ await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_META_DATA_SET, { deviceId, metaData }, this.source);
76
+ const payload = {
77
+ eventType: Events_1.DeviceEvents.DEVICE_META_DATA_SET,
78
+ properties: {
79
+ deviceId,
80
+ metaData,
81
+ },
82
+ };
83
+ await (0, dt_audit_library_1.publishAudit)(payload);
84
+ }
74
85
  }
75
86
  exports.EventHandler = EventHandler;
@@ -1,4 +1,3 @@
1
- import { ConnectionProvider } from "../../cloud/types";
2
1
  export interface IConnection {
3
2
  id?: string;
4
3
  createdAt?: Date;
@@ -9,8 +8,19 @@ export interface IConnection {
9
8
  propertyId: string;
10
9
  connectionProvider: ConnectionProvider;
11
10
  accessToken?: string;
11
+ refreshToken?: string;
12
12
  clientId?: string;
13
13
  clientSecret: string;
14
14
  isActive?: boolean;
15
15
  metaData?: any;
16
16
  }
17
+ export declare enum ConnectionProvider {
18
+ Smartthings = "Smartthings",
19
+ SaltoKS = "SaltoKS",
20
+ TTLock = "TTLock",
21
+ Tuya = "Tuya",
22
+ Schlage = "Schlage",
23
+ YaleWifi = "YaleWifi",
24
+ Sensibo = "Sensibo",
25
+ Devicethread = "Devicethread"
26
+ }
@@ -1,2 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectionProvider = void 0;
4
+ var ConnectionProvider;
5
+ (function (ConnectionProvider) {
6
+ ConnectionProvider["Smartthings"] = "Smartthings";
7
+ ConnectionProvider["SaltoKS"] = "SaltoKS";
8
+ ConnectionProvider["TTLock"] = "TTLock";
9
+ ConnectionProvider["Tuya"] = "Tuya";
10
+ ConnectionProvider["Schlage"] = "Schlage";
11
+ ConnectionProvider["YaleWifi"] = "YaleWifi";
12
+ ConnectionProvider["Sensibo"] = "Sensibo";
13
+ ConnectionProvider["Devicethread"] = "Devicethread";
14
+ })(ConnectionProvider || (exports.ConnectionProvider = ConnectionProvider = {}));
@@ -1,7 +1,8 @@
1
- import { IConnection } from "../../cloud/types";
1
+ import { IConnection } from "../interfaces/IConnection";
2
2
  export declare class ConnectionRepository {
3
3
  private readonly pool;
4
4
  constructor();
5
+ createConnection(data: Partial<IConnection>): Promise<IConnection>;
5
6
  getConnectionById(connectionId: string): Promise<IConnection>;
6
7
  updateConnection(connectionId: string, data: Partial<IConnection>): Promise<any>;
7
8
  }
@@ -50,6 +50,21 @@ let ConnectionRepository = (() => {
50
50
  constructor() {
51
51
  this.pool = (0, db_1.getPostgresClient)();
52
52
  }
53
+ async createConnection(data) {
54
+ const result = await this.pool.query("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 *", [
55
+ data.connectionName,
56
+ data.connectionRefId,
57
+ data.propertyId,
58
+ data.connectionProvider,
59
+ data.accessToken,
60
+ data.refreshToken,
61
+ data.clientId,
62
+ data.clientSecret,
63
+ data.isActive,
64
+ data.metaData,
65
+ ]);
66
+ return result.rows[0];
67
+ }
53
68
  async getConnectionById(connectionId) {
54
69
  const result = await this.pool.query("SELECT * FROM dt_connections WHERE id = $1", [connectionId]);
55
70
  return result.rows[0];
@@ -6,7 +6,7 @@ export declare class DeviceRepository {
6
6
  constructor();
7
7
  getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
8
8
  getDevices(deviceIds: string[], withHubDetails?: boolean): Promise<IDevice[]>;
9
- getPropertyDevices(propertyId: string, withHubDetails?: boolean): Promise<IDevice[]>;
9
+ getPropertyDevices(propertyId: string, selectDeviceId?: boolean, type?: string, withHubDetails?: boolean): Promise<IDevice[]>;
10
10
  getState(deviceId: string): Promise<any>;
11
11
  getStatus(deviceId: string): Promise<Record<string, any>>;
12
12
  getBatteryLevel(deviceId: string): Promise<Record<string, any>>;
@@ -75,9 +75,9 @@ let DeviceRepository = (() => {
75
75
  params: { deviceIds, withHubDetails },
76
76
  });
77
77
  }
78
- async getPropertyDevices(propertyId, withHubDetails = false) {
78
+ async getPropertyDevices(propertyId, selectDeviceId = false, type, withHubDetails = false) {
79
79
  return await axios_1.default.get(`${this.baseUrl}/devices`, {
80
- params: { propertyId, withHubDetails },
80
+ params: { propertyId, selectDeviceId, type, withHubDetails },
81
81
  });
82
82
  }
83
83
  async getState(deviceId) {
@@ -3,7 +3,7 @@ export declare class HubRepository {
3
3
  private readonly baseUrl;
4
4
  private readonly postgres;
5
5
  constructor();
6
- addHub(body: any): Promise<IDevice>;
6
+ addHub(body: Partial<IDevice>): Promise<IDevice>;
7
7
  getHubs(hubIds: string[]): Promise<IDevice[]>;
8
8
  getHub(hubId: string): Promise<IDevice>;
9
9
  updateHub(hubId: string, body: any): Promise<IDevice>;
@@ -1,7 +1,8 @@
1
- import { IConnection } from "../../cloud/types";
1
+ import { IConnection } from "../interfaces/IConnection";
2
2
  export declare class LocalConnectionService {
3
3
  private readonly connectionRepository;
4
4
  constructor();
5
+ createConnection(data: Partial<IConnection>): Promise<IConnection>;
5
6
  getConnection(connectionId: string): Promise<IConnection>;
6
- updateConnection(connectionId: string, data: any): Promise<any>;
7
+ updateConnection(connectionId: string, data: Partial<IConnection>): Promise<IConnection>;
7
8
  }
@@ -7,10 +7,25 @@ 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
+ }
10
19
  async getConnection(connectionId) {
20
+ if (!connectionId) {
21
+ throw new Error("Connection ID is required");
22
+ }
11
23
  return await this.connectionRepository.getConnectionById(connectionId);
12
24
  }
13
25
  async updateConnection(connectionId, data) {
26
+ if (!connectionId) {
27
+ throw new Error("Connection ID is required");
28
+ }
14
29
  return await this.connectionRepository.updateConnection(connectionId, data);
15
30
  }
16
31
  }
@@ -1,6 +1,5 @@
1
1
  import { IDevice } from "../interfaces";
2
2
  export declare class LocalDeviceService {
3
- private readonly source;
4
3
  private readonly eventHandler;
5
4
  private readonly alertService;
6
5
  private readonly deviceRepository;
@@ -8,7 +7,7 @@ export declare class LocalDeviceService {
8
7
  createDevice(body: IDevice): Promise<void>;
9
8
  getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
10
9
  getDevices(deviceIds: string[], withHubDetails?: boolean): Promise<IDevice[]>;
11
- getPropertyDevices(propertyId: string, withHubDetails?: boolean): Promise<IDevice[]>;
10
+ getPropertyDevices(propertyId: string, selectDeviceId?: boolean, type?: string, withHubDetails?: boolean): Promise<IDevice[]>;
12
11
  updateDevice(deviceId: string, body: any): Promise<any>;
13
12
  deleteDevice(deviceId: string): Promise<any>;
14
13
  getState(deviceId: string): Promise<any>;
@@ -5,8 +5,6 @@ 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");
10
8
  const EventHandler_1 = require("../events/EventHandler");
11
9
  const lodash_1 = require("lodash");
12
10
  const Alert_service_1 = require("./Alert.service");
@@ -14,8 +12,6 @@ const Device_repository_1 = require("../repository/Device.repository");
14
12
  const typedi_1 = __importDefault(require("typedi"));
15
13
  class LocalDeviceService {
16
14
  constructor() {
17
- this.source = "dt-common-device";
18
- // this.redis = getRedisClient();
19
15
  (0, config_1.checkAwsEnv)();
20
16
  (0, config_1.ensureAuditInitialized)();
21
17
  this.eventHandler = new EventHandler_1.EventHandler();
@@ -26,24 +22,45 @@ class LocalDeviceService {
26
22
  return await this.eventHandler.onDeviceCreate(body);
27
23
  }
28
24
  async getDevice(deviceId, withHubDetails = false) {
25
+ if (!deviceId) {
26
+ throw new Error("Device ID is required");
27
+ }
29
28
  return await this.deviceRepository.getDevice(deviceId, withHubDetails);
30
29
  }
31
30
  async getDevices(deviceIds, withHubDetails = false) {
31
+ if (!deviceIds.length) {
32
+ throw new Error("At least one device ID is required");
33
+ }
32
34
  return await this.deviceRepository.getDevices(deviceIds, withHubDetails);
33
35
  }
34
- async getPropertyDevices(propertyId, withHubDetails = false) {
35
- return await this.deviceRepository.getPropertyDevices(propertyId, withHubDetails);
36
+ async getPropertyDevices(propertyId, selectDeviceId = false, type, withHubDetails = false) {
37
+ if (!propertyId) {
38
+ throw new Error("Property ID is required");
39
+ }
40
+ return await this.deviceRepository.getPropertyDevices(propertyId, selectDeviceId, type, withHubDetails);
36
41
  }
37
42
  async updateDevice(deviceId, body) {
43
+ if (!deviceId) {
44
+ throw new Error("Device ID is required");
45
+ }
38
46
  return await this.eventHandler.onDeviceUpdate(deviceId, body);
39
47
  }
40
48
  async deleteDevice(deviceId) {
49
+ if (!deviceId) {
50
+ throw new Error("Device ID is required");
51
+ }
41
52
  return await this.eventHandler.onDeviceDelete(deviceId);
42
53
  }
43
54
  async getState(deviceId) {
55
+ if (!deviceId) {
56
+ throw new Error("Device ID is required");
57
+ }
44
58
  return await this.deviceRepository.getState(deviceId);
45
59
  }
46
60
  async setState(deviceId, newState) {
61
+ if (!deviceId || !newState) {
62
+ throw new Error("Device ID and new state are required");
63
+ }
47
64
  // If old state and new state are different
48
65
  const oldState = (await this.getState(deviceId))?.data?.state || {};
49
66
  const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
@@ -52,9 +69,15 @@ class LocalDeviceService {
52
69
  }
53
70
  }
54
71
  async getStatus(deviceId) {
72
+ if (!deviceId) {
73
+ throw new Error("Device ID is required");
74
+ }
55
75
  return await this.deviceRepository.getStatus(deviceId);
56
76
  }
57
77
  async setStatus(deviceId, newStatus) {
78
+ if (!deviceId || !newStatus) {
79
+ throw new Error("Device ID and new status are required");
80
+ }
58
81
  // If old status and new status are different
59
82
  const oldStatus = await this.getStatus(deviceId);
60
83
  const changedKeys = Object.keys(newStatus).filter((key) => !(0, lodash_1.isEqual)(oldStatus?.data?.[key], newStatus?.[key]));
@@ -75,9 +98,15 @@ class LocalDeviceService {
75
98
  }
76
99
  }
77
100
  async getBatteryLevel(deviceId) {
101
+ if (!deviceId) {
102
+ throw new Error("Device ID is required");
103
+ }
78
104
  return await this.deviceRepository.getBatteryLevel(deviceId);
79
105
  }
80
106
  async setBatteryLevel(deviceId, newBatteryLevel) {
107
+ if (!deviceId || !newBatteryLevel) {
108
+ throw new Error("Device ID and new battery level are required");
109
+ }
81
110
  // If old battery level and new battery level are different
82
111
  const oldBatteryLevel = await this.getBatteryLevel(deviceId);
83
112
  const changedKeys = Object.keys(newBatteryLevel).filter((key) => !(0, lodash_1.isEqual)(oldBatteryLevel?.data?.[key], newBatteryLevel?.[key]));
@@ -94,23 +123,27 @@ class LocalDeviceService {
94
123
  }
95
124
  }
96
125
  async getMetaData(deviceId) {
126
+ if (!deviceId) {
127
+ throw new Error("Device ID is required");
128
+ }
97
129
  return await this.deviceRepository.getMetaData(deviceId);
98
130
  }
99
131
  async setMetaData(deviceId, metaData) {
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);
132
+ if (!deviceId || !metaData) {
133
+ throw new Error("Device ID and meta data are required");
134
+ }
135
+ return await this.eventHandler.onDeviceMetaChange(deviceId, metaData);
109
136
  }
110
137
  async getDevicesByZone(zoneId) {
138
+ if (!zoneId) {
139
+ throw new Error("Zone ID is required");
140
+ }
111
141
  return await this.deviceRepository.getDevicesByZone(zoneId);
112
142
  }
113
143
  async getDevicesByAccessGroup(accessGroupId) {
144
+ if (!accessGroupId) {
145
+ throw new Error("Access Group ID is required");
146
+ }
114
147
  return await this.deviceRepository.getDevicesByAccessGroup(accessGroupId);
115
148
  }
116
149
  }
@@ -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: IDevice): Promise<IDevice>;
5
+ addHub(body: Partial<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,21 +14,39 @@ 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
+ }
17
20
  return await this.hubRepository.getHubs(hubIds);
18
21
  }
19
22
  async getHub(hubId) {
23
+ if (!hubId) {
24
+ throw new Error("Hub ID is required");
25
+ }
20
26
  return await this.hubRepository.getHub(hubId);
21
27
  }
22
28
  async updateHub(hubId, body) {
29
+ if (!hubId) {
30
+ throw new Error("Hub ID is required");
31
+ }
23
32
  return await this.hubRepository.updateHub(hubId, body);
24
33
  }
25
34
  async getStatus(hubId) {
35
+ if (!hubId) {
36
+ throw new Error("Hub ID is required");
37
+ }
26
38
  return await this.hubRepository.getStatus(hubId);
27
39
  }
28
40
  async deleteHub(hubId) {
41
+ if (!hubId) {
42
+ throw new Error("Hub ID is required");
43
+ }
29
44
  return await this.hubRepository.deleteHub(hubId);
30
45
  }
31
46
  async deleteAllHubs(hubIds) {
47
+ if (!hubIds.length) {
48
+ throw new Error("At least one hub ID is required");
49
+ }
32
50
  return await this.hubRepository.deleteAllHubs(hubIds);
33
51
  }
34
52
  }
@@ -7,12 +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
+ }
10
13
  return await this.propertyRepository.getPropertyPreferences(propertyId);
11
14
  }
12
15
  async getProperty(propertyId) {
16
+ if (!propertyId) {
17
+ throw new Error("Property ID is required");
18
+ }
13
19
  return await this.propertyRepository.getProperty(propertyId);
14
20
  }
15
21
  async getPropertyTimeZone(propertyId) {
22
+ if (!propertyId) {
23
+ throw new Error("Property ID is required");
24
+ }
16
25
  const property = await this.propertyRepository.getProperty(propertyId);
17
26
  if (!property) {
18
27
  throw new Error("Property not found");
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- export { CloudDevice, DeviceFactory, CloudConnection, CloudDeviceService, } from "./device/cloud/entities";
1
+ export { CloudDevice, CloudDeviceService, DeviceFactory, } 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";
5
4
  export * from "./device/local/interfaces";
6
5
  export { initialize, getConfig } from "./config/config";
package/dist/index.js CHANGED
@@ -15,13 +15,12 @@ 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.CloudDeviceService = exports.CloudConnection = exports.DeviceFactory = exports.CloudDevice = void 0;
18
+ exports.getConfig = exports.initialize = exports.LocalScheduleService = exports.LocalPropertyService = exports.LocalConnectionService = exports.LocalHubService = exports.LocalDeviceService = exports.DeviceFactory = exports.CloudDeviceService = 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, "DeviceFactory", { enumerable: true, get: function () { return entities_1.DeviceFactory; } });
23
- Object.defineProperty(exports, "CloudConnection", { enumerable: true, get: function () { return entities_1.CloudConnection; } });
24
22
  Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return entities_1.CloudDeviceService; } });
23
+ Object.defineProperty(exports, "DeviceFactory", { enumerable: true, get: function () { return entities_1.DeviceFactory; } });
25
24
  var services_1 = require("./device/local/services");
26
25
  Object.defineProperty(exports, "LocalDeviceService", { enumerable: true, get: function () { return services_1.LocalDeviceService; } });
27
26
  Object.defineProperty(exports, "LocalHubService", { enumerable: true, get: function () { return services_1.LocalHubService; } });
@@ -29,7 +28,6 @@ Object.defineProperty(exports, "LocalConnectionService", { enumerable: true, get
29
28
  Object.defineProperty(exports, "LocalPropertyService", { enumerable: true, get: function () { return services_1.LocalPropertyService; } });
30
29
  Object.defineProperty(exports, "LocalScheduleService", { enumerable: true, get: function () { return services_1.LocalScheduleService; } });
31
30
  __exportStar(require("./device/cloud/interfaces"), exports);
32
- __exportStar(require("./device/cloud/types"), exports);
33
31
  // Local exports
34
32
  __exportStar(require("./device/local/interfaces"), exports);
35
33
  //initialize export
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
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 && npm publish",
9
+ "minor": "npm version minor && npm run build && npm publish",
10
+ "major": "npm version major && npm run build && npm publish",
11
11
  "security:audit": "npm audit --audit-level=moderate",
12
12
  "security:fix": "npm audit fix",
13
13
  "security:check": "npm audit && npm outdated",
@@ -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 "../types";
4
+ import { IConnection } from "../../local/interfaces/IConnection";
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 "../types";
2
+ import { IConnection } from "../../local/interfaces/IConnection";
3
3
 
4
4
  export class CloudDeviceService implements ICloudDeviceService {
5
5
  async getConnection(deviceId: string): Promise<IConnection> {
@@ -1,4 +1,3 @@
1
1
  export * from "./CloudDevice";
2
2
  export * from "./DeviceFactory";
3
- export * from "./CloudConnection";
4
3
  export * from "./CloudDeviceService";
@@ -1,5 +1,5 @@
1
1
  // Interface for CloudDeviceService
2
- import { IConnection } from "../types";
2
+ import { IConnection } from "../../local/interfaces/IConnection";
3
3
 
4
4
  export interface ICloudDeviceService {
5
5
  getConnection(deviceId: string): Promise<IConnection>;
@@ -1,3 +1,3 @@
1
- export * from "./ICloudConnection";
2
1
  export * from "./ICloudDevice";
3
2
  export * from "./ICloudDeviceService";
3
+ export * from "./IDeviceFactory";
@@ -98,4 +98,20 @@ 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
+ }
101
117
  }
@@ -0,0 +1,27 @@
1
+ export interface IConnection {
2
+ id?: string;
3
+ createdAt?: Date;
4
+ updatedAt?: Date;
5
+ isDeleted?: boolean;
6
+ connectionName: string;
7
+ connectionRefId: string;
8
+ propertyId: string;
9
+ connectionProvider: ConnectionProvider;
10
+ accessToken?: string;
11
+ refreshToken?: string;
12
+ clientId?: string;
13
+ clientSecret: string;
14
+ isActive?: boolean;
15
+ metaData?: any;
16
+ }
17
+
18
+ export enum ConnectionProvider {
19
+ Smartthings = "Smartthings",
20
+ SaltoKS = "SaltoKS",
21
+ TTLock = "TTLock",
22
+ Tuya = "Tuya",
23
+ Schlage = "Schlage",
24
+ YaleWifi = "YaleWifi",
25
+ Sensibo = "Sensibo",
26
+ Devicethread = "Devicethread",
27
+ }
@@ -1,5 +1,5 @@
1
1
  import { getPostgresClient } from "../../../db";
2
- import { IConnection } from "../../cloud/types";
2
+ import { IConnection } from "../interfaces/IConnection";
3
3
  import { Service } from "typedi";
4
4
 
5
5
  @Service()
@@ -8,6 +8,26 @@ 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
+
11
31
  async getConnectionById(connectionId: string): Promise<IConnection> {
12
32
  const result = await this.pool.query(
13
33
  "SELECT * FROM dt_connections WHERE id = $1",
@@ -46,10 +46,12 @@ export class DeviceRepository {
46
46
 
47
47
  async getPropertyDevices(
48
48
  propertyId: string,
49
+ selectDeviceId: boolean = false,
50
+ type?: string,
49
51
  withHubDetails: boolean = false
50
52
  ): Promise<IDevice[]> {
51
53
  return await axios.get(`${this.baseUrl}/devices`, {
52
- params: { propertyId, withHubDetails },
54
+ params: { propertyId, selectDeviceId, type, withHubDetails },
53
55
  });
54
56
  }
55
57
 
@@ -19,7 +19,7 @@ export class HubRepository {
19
19
  this.postgres = getPostgresClient();
20
20
  }
21
21
 
22
- async addHub(body: any): Promise<IDevice> {
22
+ async addHub(body: Partial<IDevice>): Promise<IDevice> {
23
23
  return await axios.post(`${this.baseUrl}/devices/hubs`, body);
24
24
  }
25
25
 
@@ -1,6 +1,6 @@
1
1
  import { Container } from "typedi";
2
- import { IConnection } from "../../cloud/types";
3
2
  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,11 +8,32 @@ 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
+
11
23
  async getConnection(connectionId: string): Promise<IConnection> {
24
+ if (!connectionId) {
25
+ throw new Error("Connection ID is required");
26
+ }
12
27
  return await this.connectionRepository.getConnectionById(connectionId);
13
28
  }
14
29
 
15
- async updateConnection(connectionId: string, data: any) {
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
+ }
16
37
  return await this.connectionRepository.updateConnection(connectionId, data);
17
38
  }
18
39
  }
@@ -1,27 +1,16 @@
1
- import axios from "axios";
2
- import {
3
- getConfig,
4
- checkAwsEnv,
5
- ensureAuditInitialized,
6
- } from "../../../config/config";
1
+ import { checkAwsEnv, ensureAuditInitialized } from "../../../config/config";
7
2
  import { IDevice } from "../interfaces";
8
- import { eventDispatcher } from "dt-pub-sub";
9
- import { publishAudit } from "dt-audit-library";
10
3
  import { EventHandler } from "../events/EventHandler";
11
4
  import { isEqual } from "lodash";
12
5
  import { AlertService } from "./Alert.service";
13
- import { getPostgresClient } from "../../../db";
14
6
  import { DeviceRepository } from "../repository/Device.repository";
15
7
  import Container from "typedi";
16
8
 
17
9
  export class LocalDeviceService {
18
- private readonly source = "dt-common-device";
19
10
  private readonly eventHandler: EventHandler;
20
11
  private readonly alertService: AlertService;
21
- //private readonly redis;
22
12
  private readonly deviceRepository: DeviceRepository;
23
13
  constructor() {
24
- // this.redis = getRedisClient();
25
14
  checkAwsEnv();
26
15
  ensureAuditInitialized();
27
16
  this.eventHandler = new EventHandler();
@@ -37,6 +26,9 @@ export class LocalDeviceService {
37
26
  deviceId: string,
38
27
  withHubDetails: boolean = false
39
28
  ): Promise<IDevice> {
29
+ if (!deviceId) {
30
+ throw new Error("Device ID is required");
31
+ }
40
32
  return await this.deviceRepository.getDevice(deviceId, withHubDetails);
41
33
  }
42
34
 
@@ -44,32 +36,54 @@ export class LocalDeviceService {
44
36
  deviceIds: string[],
45
37
  withHubDetails: boolean = false
46
38
  ): Promise<IDevice[]> {
39
+ if (!deviceIds.length) {
40
+ throw new Error("At least one device ID is required");
41
+ }
47
42
  return await this.deviceRepository.getDevices(deviceIds, withHubDetails);
48
43
  }
49
44
 
50
45
  async getPropertyDevices(
51
46
  propertyId: string,
47
+ selectDeviceId: boolean = false,
48
+ type?: string,
52
49
  withHubDetails: boolean = false
53
50
  ): Promise<IDevice[]> {
51
+ if (!propertyId) {
52
+ throw new Error("Property ID is required");
53
+ }
54
54
  return await this.deviceRepository.getPropertyDevices(
55
55
  propertyId,
56
+ selectDeviceId,
57
+ type,
56
58
  withHubDetails
57
59
  );
58
60
  }
59
61
 
60
62
  async updateDevice(deviceId: string, body: any): Promise<any> {
63
+ if (!deviceId) {
64
+ throw new Error("Device ID is required");
65
+ }
61
66
  return await this.eventHandler.onDeviceUpdate(deviceId, body);
62
67
  }
63
68
 
64
69
  async deleteDevice(deviceId: string): Promise<any> {
70
+ if (!deviceId) {
71
+ throw new Error("Device ID is required");
72
+ }
65
73
  return await this.eventHandler.onDeviceDelete(deviceId);
66
74
  }
67
75
 
68
76
  async getState(deviceId: string) {
77
+ if (!deviceId) {
78
+ throw new Error("Device ID is required");
79
+ }
69
80
  return await this.deviceRepository.getState(deviceId);
70
81
  }
71
82
 
72
83
  async setState(deviceId: string, newState: any) {
84
+ if (!deviceId || !newState) {
85
+ throw new Error("Device ID and new state are required");
86
+ }
73
87
  // If old state and new state are different
74
88
  const oldState = (await this.getState(deviceId))?.data?.state || {};
75
89
  const changedKeys = Object.keys(newState).filter(
@@ -81,10 +95,16 @@ export class LocalDeviceService {
81
95
  }
82
96
 
83
97
  async getStatus(deviceId: string) {
98
+ if (!deviceId) {
99
+ throw new Error("Device ID is required");
100
+ }
84
101
  return await this.deviceRepository.getStatus(deviceId);
85
102
  }
86
103
 
87
104
  async setStatus(deviceId: string, newStatus: any) {
105
+ if (!deviceId || !newStatus) {
106
+ throw new Error("Device ID and new status are required");
107
+ }
88
108
  // If old status and new status are different
89
109
  const oldStatus = await this.getStatus(deviceId);
90
110
  const changedKeys = Object.keys(newStatus).filter(
@@ -110,9 +130,15 @@ export class LocalDeviceService {
110
130
  }
111
131
 
112
132
  async getBatteryLevel(deviceId: string) {
133
+ if (!deviceId) {
134
+ throw new Error("Device ID is required");
135
+ }
113
136
  return await this.deviceRepository.getBatteryLevel(deviceId);
114
137
  }
115
138
  async setBatteryLevel(deviceId: string, newBatteryLevel: any) {
139
+ if (!deviceId || !newBatteryLevel) {
140
+ throw new Error("Device ID and new battery level are required");
141
+ }
116
142
  // If old battery level and new battery level are different
117
143
  const oldBatteryLevel = await this.getBatteryLevel(deviceId);
118
144
  const changedKeys = Object.keys(newBatteryLevel).filter(
@@ -136,29 +162,29 @@ export class LocalDeviceService {
136
162
  }
137
163
  }
138
164
  async getMetaData(deviceId: string) {
165
+ if (!deviceId) {
166
+ throw new Error("Device ID is required");
167
+ }
139
168
  return await this.deviceRepository.getMetaData(deviceId);
140
169
  }
141
170
  async setMetaData(deviceId: string, metaData: Record<string, any>) {
142
- await eventDispatcher.publishEvent(
143
- "device.metaData.set",
144
- { deviceId, metaData },
145
- this.source
146
- );
147
- const payload = {
148
- eventType: "device.metaData.set",
149
- properties: {
150
- deviceId,
151
- metaData,
152
- },
153
- };
154
- await publishAudit(payload);
171
+ if (!deviceId || !metaData) {
172
+ throw new Error("Device ID and meta data are required");
173
+ }
174
+ return await this.eventHandler.onDeviceMetaChange(deviceId, metaData);
155
175
  }
156
176
 
157
177
  async getDevicesByZone(zoneId: string) {
178
+ if (!zoneId) {
179
+ throw new Error("Zone ID is required");
180
+ }
158
181
  return await this.deviceRepository.getDevicesByZone(zoneId);
159
182
  }
160
183
 
161
184
  async getDevicesByAccessGroup(accessGroupId: string) {
185
+ if (!accessGroupId) {
186
+ throw new Error("Access Group ID is required");
187
+ }
162
188
  return await this.deviceRepository.getDevicesByAccessGroup(accessGroupId);
163
189
  }
164
190
  }
@@ -8,31 +8,50 @@ export class LocalHubService {
8
8
  this.hubRepository = Container.get(HubRepository);
9
9
  }
10
10
 
11
- async addHub(body: IDevice): Promise<IDevice> {
11
+ async addHub(body: Partial<IDevice>): Promise<IDevice> {
12
12
  return await this.hubRepository.addHub(body);
13
13
  }
14
14
 
15
15
  async getHubs(hubIds: string[]): Promise<IDevice[]> {
16
+ if (!hubIds.length) {
17
+ throw new Error("At least one hub ID is required");
18
+ }
16
19
  return await this.hubRepository.getHubs(hubIds);
17
20
  }
18
21
 
19
22
  async getHub(hubId: string): Promise<IDevice> {
23
+ if (!hubId) {
24
+ throw new Error("Hub ID is required");
25
+ }
20
26
  return await this.hubRepository.getHub(hubId);
21
27
  }
22
28
 
23
29
  async updateHub(hubId: string, body: Partial<IDevice>): Promise<IDevice> {
30
+ if (!hubId) {
31
+ throw new Error("Hub ID is required");
32
+ }
24
33
  return await this.hubRepository.updateHub(hubId, body);
25
34
  }
26
35
 
27
36
  async getStatus(hubId: string): Promise<any> {
37
+ if (!hubId) {
38
+ throw new Error("Hub ID is required");
39
+ }
28
40
  return await this.hubRepository.getStatus(hubId);
29
41
  }
30
42
 
31
43
  async deleteHub(hubId: string): Promise<any> {
44
+ if (!hubId) {
45
+ throw new Error("Hub ID is required");
46
+ }
32
47
  return await this.hubRepository.deleteHub(hubId);
33
48
  }
34
49
 
50
+
35
51
  async deleteAllHubs(hubIds: string[]): Promise<any> {
52
+ if (!hubIds.length) {
53
+ throw new Error("At least one hub ID is required");
54
+ }
36
55
  return await this.hubRepository.deleteAllHubs(hubIds);
37
56
  }
38
57
  }
@@ -7,14 +7,23 @@ export class LocalPropertyService {
7
7
  }
8
8
 
9
9
  async getPropertyPreferences(propertyId: string) {
10
+ if (!propertyId) {
11
+ throw new Error("Property ID is required");
12
+ }
10
13
  return await this.propertyRepository.getPropertyPreferences(propertyId);
11
14
  }
12
15
 
13
16
  async getProperty(propertyId: string) {
17
+ if (!propertyId) {
18
+ throw new Error("Property ID is required");
19
+ }
14
20
  return await this.propertyRepository.getProperty(propertyId);
15
21
  }
16
22
 
17
23
  async getPropertyTimeZone(propertyId: string) {
24
+ if (!propertyId) {
25
+ throw new Error("Property ID is required");
26
+ }
18
27
  const property = await this.propertyRepository.getProperty(propertyId);
19
28
  if (!property) {
20
29
  throw new Error("Property not found");
package/src/index.ts CHANGED
@@ -3,9 +3,8 @@
3
3
  // Cloud exports
4
4
  export {
5
5
  CloudDevice,
6
- DeviceFactory,
7
- CloudConnection,
8
6
  CloudDeviceService,
7
+ DeviceFactory,
9
8
  } from "./device/cloud/entities";
10
9
  export {
11
10
  LocalDeviceService,
@@ -16,7 +15,6 @@ export {
16
15
  } from "./device/local/services";
17
16
 
18
17
  export * from "./device/cloud/interfaces";
19
- export * from "./device/cloud/types";
20
18
 
21
19
  // Local exports
22
20
  export * from "./device/local/interfaces";
@@ -1,13 +0,0 @@
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,6 +0,0 @@
1
- import { IConnection, IDeviceAccountResponse } from "../types";
2
-
3
- export interface ICloudConnection {
4
- createConnection(data: IConnection, userId: string): Promise<IConnection>;
5
- getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
6
- }
@@ -1,58 +0,0 @@
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
- }