dt-common-device 1.0.6 → 1.0.8

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/README.md +16 -0
  2. package/dist/config/config.d.ts +2 -0
  3. package/dist/config/config.js +27 -0
  4. package/dist/device/cloud/interfaces/IConnectionService.d.ts +7 -0
  5. package/dist/device/cloud/interfaces/IConnectionService.js +3 -0
  6. package/dist/device/cloud/interfaces/IDeviceService.d.ts +8 -0
  7. package/dist/device/cloud/interfaces/IDeviceService.js +2 -0
  8. package/dist/device/cloud/interfaces/IHubService.d.ts +5 -0
  9. package/dist/device/cloud/interfaces/IHubService.js +2 -0
  10. package/dist/device/cloud/interfaces/index.d.ts +3 -2
  11. package/dist/device/cloud/interfaces/index.js +3 -2
  12. package/dist/device/cloud/services/Connection.service.d.ts +2 -2
  13. package/dist/device/cloud/services/Device.service.d.ts +34 -5
  14. package/dist/device/cloud/services/Device.service.js +3 -0
  15. package/dist/device/cloud/services/Hub.service.d.ts +25 -0
  16. package/dist/device/cloud/services/Hub.service.js +9 -0
  17. package/dist/device/cloud/services/index.d.ts +1 -1
  18. package/dist/device/cloud/services/index.js +3 -3
  19. package/dist/device/cloud/types.d.ts +2 -1
  20. package/dist/device/cloud/types.js +1 -1
  21. package/dist/device/local/services/Device.service.d.ts +4 -2
  22. package/dist/device/local/services/Device.service.js +54 -10
  23. package/dist/device/local/services/Hub.service.d.ts +12 -0
  24. package/dist/device/local/services/Hub.service.js +43 -0
  25. package/dist/device/local/services/index.d.ts +1 -1
  26. package/dist/device/local/services/index.js +3 -3
  27. package/dist/index.d.ts +2 -2
  28. package/dist/index.js +3 -3
  29. package/package.json +23 -21
  30. package/src/config/config.ts +35 -0
  31. package/src/device/cloud/interfaces/{IDeviceConnectionService.ts → IConnectionService.ts} +17 -21
  32. package/src/device/cloud/interfaces/{IDevicesService.ts → IDeviceService.ts} +3 -4
  33. package/src/device/cloud/interfaces/IHubService.ts +5 -0
  34. package/src/device/cloud/interfaces/index.ts +3 -2
  35. package/src/device/cloud/services/Connection.service.ts +2 -6
  36. package/src/device/cloud/services/Device.service.ts +35 -11
  37. package/src/device/cloud/services/Hub.service.ts +34 -0
  38. package/src/device/cloud/services/index.ts +1 -1
  39. package/src/device/cloud/types.ts +2 -1
  40. package/src/device/local/services/Device.service.ts +81 -11
  41. package/src/device/local/services/{DeviceHub.service.ts → Hub.service.ts} +5 -1
  42. package/src/device/local/services/index.ts +1 -1
  43. package/src/index.ts +2 -2
  44. package/src/device/cloud/services/DeviceHub.service.ts +0 -3
package/README.md CHANGED
@@ -8,6 +8,20 @@ A TypeScript library for device management, supporting both cloud and local devi
8
8
  npm install dt-common-device
9
9
  ```
10
10
 
11
+ ## Environment Variables Required
12
+
13
+ **For audit logging to work (used in local device services), you must set the following environment variables in your application:**
14
+
15
+ - `POSTHOG_API_KEY` — Your PostHog API key
16
+ - `POSTHOG_HOST` — The PostHog host URL
17
+
18
+ **You do NOT need to call `initializeAudit()` yourself.**
19
+
20
+ - `dt-common-device` will automatically initialize audit logging using these environment variables when you create a local device service.
21
+ - If either variable is missing, the library will throw a clear error at runtime.
22
+
23
+ ---
24
+
11
25
  ## Initialization (Required)
12
26
 
13
27
  Before using any service, you **must** call `initialize()` in your main entry file:
@@ -161,6 +175,8 @@ import { IDevice, IHubCreateParams, IConnection } from "dt-common-device";
161
175
  ## Notes
162
176
 
163
177
  - You **must** call `initialize()` before using any service. If not, you will get a runtime error.
178
+ - **You must set `POSTHOG_API_KEY` and `POSTHOG_HOST` in your environment before using any local device service.**
179
+ - You do **not** need to call `initializeAudit()`; the library will handle audit initialization automatically.
164
180
  - Cloud service methods are stubs and must be implemented by the consuming project.
165
181
  - All types are strongly typed for TypeScript support.
166
182
 
@@ -1,3 +1,5 @@
1
1
  import { DeviceConfig } from "../types";
2
2
  export declare function initialize(cfg: DeviceConfig): void;
3
3
  export declare function getConfig(): DeviceConfig;
4
+ export declare function checkAwsEnv(): void;
5
+ export declare function ensureAuditInitialized(): void;
@@ -2,7 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.initialize = initialize;
4
4
  exports.getConfig = getConfig;
5
+ exports.checkAwsEnv = checkAwsEnv;
6
+ exports.ensureAuditInitialized = ensureAuditInitialized;
7
+ const dt_audit_library_1 = require("dt-audit-library");
5
8
  let config = null;
9
+ let auditInitialized = false;
6
10
  function initialize(cfg) {
7
11
  // Check if at least one service is provided
8
12
  const hasAtLeastOneService = [
@@ -23,3 +27,26 @@ function getConfig() {
23
27
  }
24
28
  return config;
25
29
  }
30
+ function checkAwsEnv() {
31
+ const requiredEnv = [
32
+ "AWS_SECRET_ACCESS_KEY",
33
+ "AWS_REGION",
34
+ "AWS_ACCESS_KEY_ID",
35
+ "EVENT_BUS_NAME",
36
+ ];
37
+ const missing = requiredEnv.filter((key) => !process.env[key]);
38
+ if (missing.length > 0) {
39
+ throw new Error(`Missing required AWS environment variables for dt-pub-sub: ${missing.join(", ")}`);
40
+ }
41
+ }
42
+ function ensureAuditInitialized() {
43
+ if (auditInitialized)
44
+ return;
45
+ const apiKey = process.env.POSTHOG_API_KEY;
46
+ const host = process.env.POSTHOG_HOST;
47
+ if (!apiKey || !host) {
48
+ throw new Error("dt-common-device: POSTHOG_API_KEY and POSTHOG_HOST must be set in environment variables");
49
+ }
50
+ (0, dt_audit_library_1.initializeAudit)(apiKey, host);
51
+ auditInitialized = true;
52
+ }
@@ -0,0 +1,7 @@
1
+ import { IConnection, IConnectionConnectParams, IDeviceAccountResponse } from "../types";
2
+ export interface IConnectionService {
3
+ createConnection(data: IConnection, userId: string): Promise<any>;
4
+ getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
5
+ getDevices(connection: IConnection): Promise<any>;
6
+ connect(connection: IConnection, connectionConnect: IConnectionConnectParams): Promise<any>;
7
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // Device Cloud Class Interface for DeviceThread Common Library
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { IConnection } from "../types";
2
+ export interface IDeviceService {
3
+ getDevices(connection: IConnection): Promise<Record<string, any>[]>;
4
+ getDevice(connectionId: string, deviceId: string): Promise<Record<string, any>>;
5
+ getBattery(deviceId: string): Promise<number | string>;
6
+ getState(deviceId: string): Promise<string>;
7
+ getStatus(connectionId: string, deviceId: string): Promise<string>;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface IHubService {
2
+ getHubs(connectionId: string): Promise<any[] | null>;
3
+ getHub(connectionId: string, hubId: string): Promise<Record<string, any>>;
4
+ getStatus(connectionId: string, hubId: string): Promise<string>;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +1,3 @@
1
- export * from "./IDeviceConnectionService";
2
- export * from "./IDevicesService";
1
+ export * from "./IConnectionService";
2
+ export * from "./IDeviceService";
3
+ export * from "./IHubService";
@@ -14,5 +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("./IDeviceConnectionService"), exports);
18
- __exportStar(require("./IDevicesService"), exports);
17
+ __exportStar(require("./IConnectionService"), exports);
18
+ __exportStar(require("./IDeviceService"), exports);
19
+ __exportStar(require("./IHubService"), exports);
@@ -1,6 +1,6 @@
1
- import { IDeviceConnectionService } from "../interfaces";
1
+ import { IConnectionService } from "../interfaces";
2
2
  import { IConnection, IDeviceAccountResponse, IConnectionConnectParams } from "../types";
3
- export declare abstract class ConnectionService implements IDeviceConnectionService {
3
+ export declare abstract class ConnectionService implements IConnectionService {
4
4
  abstract createConnection(data: IConnection, userId: string): Promise<any>;
5
5
  abstract getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
6
6
  abstract getDevices(connection: IConnection): Promise<any>;
@@ -1,10 +1,39 @@
1
1
  import { IDeviceService } from "../interfaces";
2
- import { IConnection } from "../types";
2
+ import { IConnection, IDevice } from "../types";
3
3
  export declare abstract class DeviceService implements IDeviceService {
4
+ deviceId: string;
5
+ propertyId: string;
6
+ name: string;
7
+ hubId: string[];
8
+ deviceType: {
9
+ id: string;
10
+ type: string;
11
+ };
12
+ status: {
13
+ online: boolean;
14
+ error?: {
15
+ type?: string;
16
+ message?: string;
17
+ };
18
+ lastUpdated?: string;
19
+ };
20
+ state?: Record<string, any>;
21
+ metaData?: Record<string, any>;
22
+ zone?: {
23
+ id: string;
24
+ name: string;
25
+ zoneType: string;
26
+ parentZone?: {
27
+ id: string;
28
+ name: string;
29
+ zoneType: string;
30
+ };
31
+ };
32
+ connection: IConnection;
33
+ constructor(device: IDevice);
4
34
  abstract getDevices(connection: IConnection): Promise<Record<string, any>[]>;
5
35
  abstract getDevice(connectionId: string, deviceId: string): Promise<any>;
6
- abstract getStatus(connectionId: string, deviceId: string): Promise<string | null>;
7
- abstract getState(deviceId: string): Promise<Record<string, any>>;
8
- abstract getGateways(connectionId: string): Promise<any[] | null>;
9
- abstract getGatewayDetails(connectionId: string, gatewayId: string): Promise<Record<string, any>>;
36
+ abstract getBattery(deviceId: string): Promise<number | string>;
37
+ abstract getStatus(connectionId: string, deviceId: string): Promise<string>;
38
+ abstract getState(deviceId: string): Promise<string>;
10
39
  }
@@ -2,5 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DeviceService = void 0;
4
4
  class DeviceService {
5
+ constructor(device) {
6
+ Object.assign(this, device);
7
+ }
5
8
  }
6
9
  exports.DeviceService = DeviceService;
@@ -0,0 +1,25 @@
1
+ import { IHubService } from "../interfaces";
2
+ import { IConnection, IDevice } from "../types";
3
+ export declare abstract class HubService implements IHubService {
4
+ deviceId: string;
5
+ propertyId: string;
6
+ name: string;
7
+ deviceType: {
8
+ id: string;
9
+ type: string;
10
+ };
11
+ status: {
12
+ online: boolean;
13
+ error?: {
14
+ type?: string;
15
+ message?: string;
16
+ };
17
+ lastUpdated?: string;
18
+ };
19
+ metaData?: Record<string, any>;
20
+ connection: IConnection;
21
+ constructor(hub: IDevice);
22
+ abstract getHubs(connectionId: string): Promise<any[] | null>;
23
+ abstract getHub(connectionId: string, hubId: string): Promise<Record<string, any>>;
24
+ abstract getStatus(connectionId: string, hubId: string): Promise<string>;
25
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HubService = void 0;
4
+ class HubService {
5
+ constructor(hub) {
6
+ Object.assign(this, hub);
7
+ }
8
+ }
9
+ exports.HubService = HubService;
@@ -1,3 +1,3 @@
1
1
  export { DeviceService as CloudDeviceService } from "./Device.service";
2
2
  export { ConnectionService as CloudConnectionService } from "./Connection.service";
3
- export { DeviceHubService as CloudDeviceHubService } from "./DeviceHub.service";
3
+ export { HubService as CloudHubService } from "./Hub.service";
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  // Device Cloud Services - Export all service classes
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.CloudDeviceHubService = exports.CloudConnectionService = exports.CloudDeviceService = void 0;
4
+ exports.CloudHubService = exports.CloudConnectionService = exports.CloudDeviceService = void 0;
5
5
  var Device_service_1 = require("./Device.service");
6
6
  Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return Device_service_1.DeviceService; } });
7
7
  var Connection_service_1 = require("./Connection.service");
8
8
  Object.defineProperty(exports, "CloudConnectionService", { enumerable: true, get: function () { return Connection_service_1.ConnectionService; } });
9
- var DeviceHub_service_1 = require("./DeviceHub.service");
10
- Object.defineProperty(exports, "CloudDeviceHubService", { enumerable: true, get: function () { return DeviceHub_service_1.DeviceHubService; } });
9
+ var Hub_service_1 = require("./Hub.service");
10
+ Object.defineProperty(exports, "CloudHubService", { enumerable: true, get: function () { return Hub_service_1.HubService; } });
@@ -15,6 +15,7 @@ export interface IConnection {
15
15
  clientId?: string;
16
16
  clientSecret?: string;
17
17
  isActive?: boolean;
18
+ metaData?: any;
18
19
  }
19
20
  export interface IConnectionPagination {
20
21
  page: number;
@@ -98,12 +99,12 @@ export interface IConnectionConnectParams {
98
99
  [key: string]: unknown;
99
100
  }
100
101
  export declare enum ConnectionProvider {
102
+ Devicethread = "Devicethread",
101
103
  Smartthings = "Smartthings",
102
104
  SaltoKS = "SaltoKS",
103
105
  Tuya = "Tuya",
104
106
  TTLock = "TTLock",
105
107
  Schlage = "Schlage",
106
108
  YaleWifi = "YaleWifi",
107
- Devicethread = "Devicethread",
108
109
  Sensibo = "Sensibo"
109
110
  }
@@ -4,12 +4,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.ConnectionProvider = void 0;
5
5
  var ConnectionProvider;
6
6
  (function (ConnectionProvider) {
7
+ ConnectionProvider["Devicethread"] = "Devicethread";
7
8
  ConnectionProvider["Smartthings"] = "Smartthings";
8
9
  ConnectionProvider["SaltoKS"] = "SaltoKS";
9
10
  ConnectionProvider["Tuya"] = "Tuya";
10
11
  ConnectionProvider["TTLock"] = "TTLock";
11
12
  ConnectionProvider["Schlage"] = "Schlage";
12
13
  ConnectionProvider["YaleWifi"] = "YaleWifi";
13
- ConnectionProvider["Devicethread"] = "Devicethread";
14
14
  ConnectionProvider["Sensibo"] = "Sensibo";
15
15
  })(ConnectionProvider || (exports.ConnectionProvider = ConnectionProvider = {}));
@@ -1,6 +1,7 @@
1
1
  import { IDeviceCreateParams } from "../interfaces";
2
2
  export declare class DeviceService {
3
3
  private readonly baseUrl;
4
+ private readonly source;
4
5
  constructor();
5
6
  createDevice(body: IDeviceCreateParams): Promise<void>;
6
7
  getDevice(deviceId: string): Promise<any>;
@@ -9,8 +10,9 @@ export declare class DeviceService {
9
10
  deleteDevice(deviceId: string): Promise<any>;
10
11
  getState(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
11
12
  getStatus(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
13
+ setStatus(deviceId: string, status: any): Promise<void>;
12
14
  getBatteryLevel(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
13
- setBatteryLevel(deviceId: string, batteryLevel: number): Promise<import("axios").AxiosResponse<any, any>>;
15
+ setBatteryLevel(deviceId: string, batteryLevel: number): Promise<void>;
14
16
  getMetaData(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
15
- setMetaData(deviceId: string, metaData: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
17
+ setMetaData(deviceId: string, metaData: Record<string, any>): Promise<void>;
16
18
  }
@@ -7,16 +7,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.DeviceService = void 0;
8
8
  const axios_1 = __importDefault(require("axios"));
9
9
  const config_1 = require("../../../config/config");
10
+ const dt_pub_sub_1 = require("dt-pub-sub");
11
+ const dt_audit_library_1 = require("dt-audit-library");
10
12
  class DeviceService {
11
13
  constructor() {
14
+ this.source = "dt-common-device";
12
15
  const { DEVICE_SERVICE } = (0, config_1.getConfig)();
13
16
  if (!DEVICE_SERVICE) {
14
17
  throw new Error("DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE.");
15
18
  }
16
19
  this.baseUrl = DEVICE_SERVICE;
20
+ (0, config_1.checkAwsEnv)();
21
+ (0, config_1.ensureAuditInitialized)();
17
22
  }
18
23
  async createDevice(body) {
19
- await axios_1.default.post(`${this.baseUrl}/devices`, body);
24
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.created", body, this.source);
25
+ const payload = {
26
+ eventType: "device.created",
27
+ properties: {
28
+ ...body,
29
+ },
30
+ };
31
+ await (0, dt_audit_library_1.publishAudit)(payload);
20
32
  }
21
33
  async getDevice(deviceId) {
22
34
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}`);
@@ -27,10 +39,25 @@ class DeviceService {
27
39
  });
28
40
  }
29
41
  async updateDevice(deviceId, body) {
30
- return await axios_1.default.put(`${this.baseUrl}/devices/${deviceId}`, body);
42
+ //return await axios.put(`${this.baseUrl}/devices/${deviceId}`, body);
43
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.updated", { deviceId, body }, this.source);
44
+ const payload = {
45
+ eventType: "device.updated",
46
+ properties: {
47
+ ...body,
48
+ },
49
+ };
50
+ await (0, dt_audit_library_1.publishAudit)(payload);
31
51
  }
32
52
  async deleteDevice(deviceId) {
33
- return await axios_1.default.delete(`${this.baseUrl}/devices/${deviceId}`);
53
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.deleted", { deviceId }, this.source);
54
+ const payload = {
55
+ eventType: "device.deleted",
56
+ properties: {
57
+ deviceId,
58
+ },
59
+ };
60
+ await (0, dt_audit_library_1.publishAudit)(payload);
34
61
  }
35
62
  async getState(deviceId) {
36
63
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/state`);
@@ -38,27 +65,44 @@ class DeviceService {
38
65
  async getStatus(deviceId) {
39
66
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/status`);
40
67
  }
68
+ async setStatus(deviceId, status) {
69
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.status.set", { deviceId, status }, this.source);
70
+ const payload = {
71
+ eventType: "device.status.set",
72
+ properties: {
73
+ deviceId,
74
+ status,
75
+ },
76
+ };
77
+ await (0, dt_audit_library_1.publishAudit)(payload);
78
+ }
41
79
  async getBatteryLevel(deviceId) {
42
80
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
43
81
  }
44
82
  async setBatteryLevel(deviceId, batteryLevel) {
45
- return await axios_1.default.put(`${this.baseUrl}/devices/battery-level`, {
46
- body: {
83
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.battery.set", { deviceId, batteryLevel }, this.source);
84
+ const payload = {
85
+ eventType: "device.battery.set",
86
+ properties: {
47
87
  deviceId,
48
88
  batteryLevel,
49
89
  },
50
- });
90
+ };
91
+ await (0, dt_audit_library_1.publishAudit)(payload);
51
92
  }
52
93
  async getMetaData(deviceId) {
53
- return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/meta-data`);
94
+ return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
54
95
  }
55
96
  async setMetaData(deviceId, metaData) {
56
- return await axios_1.default.put(`${this.baseUrl}/devices/meta-data`, {
57
- body: {
97
+ await dt_pub_sub_1.eventDispatcher.publishEvent("device.metaData.set", { deviceId, metaData }, this.source);
98
+ const payload = {
99
+ eventType: "device.metaData.set",
100
+ properties: {
58
101
  deviceId,
59
102
  metaData,
60
103
  },
61
- });
104
+ };
105
+ await (0, dt_audit_library_1.publishAudit)(payload);
62
106
  }
63
107
  }
64
108
  exports.DeviceService = DeviceService;
@@ -0,0 +1,12 @@
1
+ import { IHubCreateParams } from "../interfaces";
2
+ export declare class HubService {
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
+ getStatus(hubId: string): Promise<any>;
10
+ deleteHub(hubId: string): Promise<any>;
11
+ deleteAllHubs(hubIds: string[]): Promise<any>;
12
+ }
@@ -0,0 +1,43 @@
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.HubService = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const config_1 = require("../../../config/config");
9
+ class HubService {
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 getStatus(hubId) {
33
+ return await axios_1.default.get(`${this.baseUrl}/devices/hubs/${hubId}/status`);
34
+ }
35
+ async deleteHub(hubId) {
36
+ return await axios_1.default.delete(`${this.baseUrl}/devices/hubs/${hubId}`);
37
+ }
38
+ async deleteAllHubs(hubIds) {
39
+ const query = hubIds.length ? `?ids=${hubIds.join(",")}` : "";
40
+ return await axios_1.default.delete(`${this.baseUrl}/devices/hubs${query}`);
41
+ }
42
+ }
43
+ exports.HubService = HubService;
@@ -1,2 +1,2 @@
1
1
  export { DeviceService as LocalDeviceService } from "./Device.service";
2
- export { DeviceHubService as LocalDeviceHubService } from "./DeviceHub.service";
2
+ export { HubService as LocalHubService } from "./Hub.service";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LocalDeviceHubService = exports.LocalDeviceService = void 0;
3
+ exports.LocalHubService = exports.LocalDeviceService = void 0;
4
4
  var Device_service_1 = require("./Device.service");
5
5
  Object.defineProperty(exports, "LocalDeviceService", { enumerable: true, get: function () { return Device_service_1.DeviceService; } });
6
- var DeviceHub_service_1 = require("./DeviceHub.service");
7
- Object.defineProperty(exports, "LocalDeviceHubService", { enumerable: true, get: function () { return DeviceHub_service_1.DeviceHubService; } });
6
+ var Hub_service_1 = require("./Hub.service");
7
+ Object.defineProperty(exports, "LocalHubService", { enumerable: true, get: function () { return Hub_service_1.HubService; } });
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { CloudDeviceService, CloudDeviceHubService, CloudConnectionService, } from "./device/cloud/services";
2
- export { LocalDeviceService, LocalDeviceHubService, } from "./device/local/services";
1
+ export { CloudDeviceService, CloudHubService, CloudConnectionService, } from "./device/cloud/services";
2
+ export { LocalDeviceService, LocalHubService, } from "./device/local/services";
3
3
  export * as cloudInterfaces from "./device/cloud/interfaces";
4
4
  export * from "./device/cloud/types";
5
5
  export * as localInterfaces from "./device/local/interfaces";
package/dist/index.js CHANGED
@@ -37,15 +37,15 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
37
37
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
38
38
  };
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.getConfig = exports.initialize = exports.localInterfaces = exports.cloudInterfaces = exports.LocalDeviceHubService = exports.LocalDeviceService = exports.CloudConnectionService = exports.CloudDeviceHubService = exports.CloudDeviceService = void 0;
40
+ exports.getConfig = exports.initialize = exports.localInterfaces = exports.cloudInterfaces = exports.LocalHubService = exports.LocalDeviceService = exports.CloudConnectionService = exports.CloudHubService = exports.CloudDeviceService = void 0;
41
41
  // Cloud exports
42
42
  var services_1 = require("./device/cloud/services");
43
43
  Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return services_1.CloudDeviceService; } });
44
- Object.defineProperty(exports, "CloudDeviceHubService", { enumerable: true, get: function () { return services_1.CloudDeviceHubService; } });
44
+ Object.defineProperty(exports, "CloudHubService", { enumerable: true, get: function () { return services_1.CloudHubService; } });
45
45
  Object.defineProperty(exports, "CloudConnectionService", { enumerable: true, get: function () { return services_1.CloudConnectionService; } });
46
46
  var services_2 = require("./device/local/services");
47
47
  Object.defineProperty(exports, "LocalDeviceService", { enumerable: true, get: function () { return services_2.LocalDeviceService; } });
48
- Object.defineProperty(exports, "LocalDeviceHubService", { enumerable: true, get: function () { return services_2.LocalDeviceHubService; } });
48
+ Object.defineProperty(exports, "LocalHubService", { enumerable: true, get: function () { return services_2.LocalHubService; } });
49
49
  exports.cloudInterfaces = __importStar(require("./device/cloud/interfaces"));
50
50
  __exportStar(require("./device/cloud/types"), exports);
51
51
  // Local exports
package/package.json CHANGED
@@ -1,21 +1,23 @@
1
- {
2
- "name": "dt-common-device",
3
- "version": "1.0.6",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "build": "tsc"
9
- },
10
- "keywords": [],
11
- "author": "",
12
- "license": "ISC",
13
- "description": "",
14
- "devDependencies": {
15
- "ts-node": "^10.9.2",
16
- "typescript": "^5.8.3"
17
- },
18
- "dependencies": {
19
- "axios": "^1.10.0"
20
- }
21
- }
1
+ {
2
+ "name": "dt-common-device",
3
+ "version": "1.0.8",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "description": "",
14
+ "devDependencies": {
15
+ "ts-node": "^10.9.2",
16
+ "typescript": "^5.8.3"
17
+ },
18
+ "dependencies": {
19
+ "axios": "^1.10.0",
20
+ "dt-audit-library": "^1.0.3",
21
+ "dt-pub-sub": "^1.0.0"
22
+ }
23
+ }
@@ -1,6 +1,8 @@
1
1
  import { DeviceConfig } from "../types";
2
+ import { initializeAudit } from "dt-audit-library";
2
3
 
3
4
  let config: DeviceConfig | null = null;
5
+ let auditInitialized = false;
4
6
 
5
7
  export function initialize(cfg: DeviceConfig) {
6
8
  // Check if at least one service is provided
@@ -26,3 +28,36 @@ export function getConfig(): DeviceConfig {
26
28
  }
27
29
  return config;
28
30
  }
31
+
32
+ export function checkAwsEnv() {
33
+ const requiredEnv = [
34
+ "AWS_SECRET_ACCESS_KEY",
35
+ "AWS_REGION",
36
+ "AWS_ACCESS_KEY_ID",
37
+ "EVENT_BUS_NAME",
38
+ ];
39
+ const missing = requiredEnv.filter((key) => !process.env[key]);
40
+ if (missing.length > 0) {
41
+ throw new Error(
42
+ `Missing required AWS environment variables for dt-pub-sub: ${missing.join(
43
+ ", "
44
+ )}`
45
+ );
46
+ }
47
+ }
48
+
49
+ export function ensureAuditInitialized() {
50
+ if (auditInitialized) return;
51
+
52
+ const apiKey = process.env.POSTHOG_API_KEY;
53
+ const host = process.env.POSTHOG_HOST;
54
+
55
+ if (!apiKey || !host) {
56
+ throw new Error(
57
+ "dt-common-device: POSTHOG_API_KEY and POSTHOG_HOST must be set in environment variables"
58
+ );
59
+ }
60
+
61
+ initializeAudit(apiKey, host);
62
+ auditInitialized = true;
63
+ }
@@ -1,21 +1,17 @@
1
- // Device Cloud Class Interface for DeviceThread Common Library
2
-
3
- import {
4
- IConnection,
5
- IConnectionConnectParams,
6
- IDevice,
7
- IDeviceAccountResponse,
8
- } from "../types";
9
-
10
- export interface IDeviceConnectionService {
11
- createConnection(data: IConnection, userId: string): Promise<any>;
12
-
13
- getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
14
-
15
- getDevices(connection: IConnection): Promise<any>;
16
-
17
- connect(
18
- connection: IConnection,
19
- connectionConnect: IConnectionConnectParams
20
- ): Promise<any>;
21
- }
1
+ // Device Cloud Class Interface for DeviceThread Common Library
2
+
3
+ import {
4
+ IConnection,
5
+ IConnectionConnectParams,
6
+ IDeviceAccountResponse,
7
+ } from "../types";
8
+
9
+ export interface IConnectionService {
10
+ createConnection(data: IConnection, userId: string): Promise<any>;
11
+ getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
12
+ getDevices(connection: IConnection): Promise<any>;
13
+ connect(
14
+ connection: IConnection,
15
+ connectionConnect: IConnectionConnectParams
16
+ ): Promise<any>;
17
+ }
@@ -6,8 +6,7 @@ export interface IDeviceService {
6
6
  connectionId: string,
7
7
  deviceId: string
8
8
  ): Promise<Record<string, any>>;
9
- getStatus(connectionId: string, deviceId: string): Promise<string | null>;
10
- getState(deviceId: string): Promise<Record<string, any>>;
11
- getGateways(connectionId: string): Promise<any[] | null>;
12
- getGatewayDetails(connectionId: string, gatewayId: string): Promise<any>;
9
+ getBattery(deviceId: string): Promise<number | string>;
10
+ getState(deviceId: string): Promise<string>;
11
+ getStatus(connectionId: string, deviceId: string): Promise<string>;
13
12
  }
@@ -0,0 +1,5 @@
1
+ export interface IHubService {
2
+ getHubs(connectionId: string): Promise<any[] | null>;
3
+ getHub(connectionId: string, hubId: string): Promise<Record<string, any>>;
4
+ getStatus(connectionId: string, hubId: string): Promise<string>;
5
+ }
@@ -1,2 +1,3 @@
1
- export * from "./IDeviceConnectionService";
2
- export * from "./IDevicesService";
1
+ export * from "./IConnectionService";
2
+ export * from "./IDeviceService";
3
+ export * from "./IHubService";
@@ -1,21 +1,17 @@
1
1
  // Device Cloud Service - Class Implementation
2
- import { IDeviceConnectionService } from "../interfaces";
2
+ import { IConnectionService } from "../interfaces";
3
3
  import {
4
4
  IConnection,
5
- IDevice,
6
5
  IDeviceAccountResponse,
7
6
  IConnectionConnectParams,
8
7
  } from "../types";
9
8
 
10
- export abstract class ConnectionService implements IDeviceConnectionService {
9
+ export abstract class ConnectionService implements IConnectionService {
11
10
  abstract createConnection(data: IConnection, userId: string): Promise<any>;
12
-
13
11
  abstract getDeviceAccount(
14
12
  connection: IConnection
15
13
  ): Promise<IDeviceAccountResponse>;
16
-
17
14
  abstract getDevices(connection: IConnection): Promise<any>;
18
-
19
15
  abstract connect(
20
16
  connection: IConnection,
21
17
  connectionConnect: IConnectionConnectParams
@@ -1,17 +1,41 @@
1
1
  import { IDeviceService } from "../interfaces";
2
- import { IConnection } from "../types";
2
+ import { IConnection, IDevice } from "../types";
3
3
 
4
4
  export abstract class DeviceService implements IDeviceService {
5
+ // IDevice properties
6
+ deviceId!: string;
7
+ propertyId!: string;
8
+ name!: string;
9
+ hubId!: string[];
10
+ deviceType!: {
11
+ id: string;
12
+ type: string;
13
+ };
14
+ status!: {
15
+ online: boolean;
16
+ error?: {
17
+ type?: string;
18
+ message?: string;
19
+ };
20
+ lastUpdated?: string;
21
+ };
22
+ state?: Record<string, any>;
23
+ metaData?: Record<string, any>;
24
+ zone?: {
25
+ id: string;
26
+ name: string;
27
+ zoneType: string;
28
+ parentZone?: { id: string; name: string; zoneType: string };
29
+ };
30
+ connection!: IConnection;
31
+
32
+ constructor(device: IDevice) {
33
+ Object.assign(this, device);
34
+ }
35
+
5
36
  abstract getDevices(connection: IConnection): Promise<Record<string, any>[]>;
6
37
  abstract getDevice(connectionId: string, deviceId: string): Promise<any>;
7
- abstract getStatus(
8
- connectionId: string,
9
- deviceId: string
10
- ): Promise<string | null>;
11
- abstract getState(deviceId: string): Promise<Record<string, any>>;
12
- abstract getGateways(connectionId: string): Promise<any[] | null>;
13
- abstract getGatewayDetails(
14
- connectionId: string,
15
- gatewayId: string
16
- ): Promise<Record<string, any>>;
38
+ abstract getBattery(deviceId: string): Promise<number | string>;
39
+ abstract getStatus(connectionId: string, deviceId: string): Promise<string>;
40
+ abstract getState(deviceId: string): Promise<string>;
17
41
  }
@@ -0,0 +1,34 @@
1
+ import { IHubService } from "../interfaces";
2
+ import { IConnection, IDevice } from "../types";
3
+
4
+ export abstract class HubService implements IHubService {
5
+ // IDevice properties
6
+ deviceId!: string;
7
+ propertyId!: string;
8
+ name!: string;
9
+ deviceType!: {
10
+ id: string;
11
+ type: string;
12
+ };
13
+ status!: {
14
+ online: boolean;
15
+ error?: {
16
+ type?: string;
17
+ message?: string;
18
+ };
19
+ lastUpdated?: string;
20
+ };
21
+ metaData?: Record<string, any>;
22
+ connection!: IConnection;
23
+
24
+ constructor(hub: IDevice) {
25
+ Object.assign(this, hub);
26
+ }
27
+
28
+ abstract getHubs(connectionId: string): Promise<any[] | null>;
29
+ abstract getHub(
30
+ connectionId: string,
31
+ hubId: string
32
+ ): Promise<Record<string, any>>;
33
+ abstract getStatus(connectionId: string, hubId: string): Promise<string>;
34
+ }
@@ -2,4 +2,4 @@
2
2
 
3
3
  export { DeviceService as CloudDeviceService } from "./Device.service";
4
4
  export { ConnectionService as CloudConnectionService } from "./Connection.service";
5
- export { DeviceHubService as CloudDeviceHubService } from "./DeviceHub.service";
5
+ export { HubService as CloudHubService } from "./Hub.service";
@@ -17,6 +17,7 @@ export interface IConnection {
17
17
  clientId?: string;
18
18
  clientSecret?: string;
19
19
  isActive?: boolean;
20
+ metaData?: any;
20
21
  }
21
22
 
22
23
  export interface IConnectionPagination {
@@ -99,12 +100,12 @@ export interface IConnectionConnectParams {
99
100
  }
100
101
 
101
102
  export enum ConnectionProvider {
103
+ Devicethread = "Devicethread",
102
104
  Smartthings = "Smartthings",
103
105
  SaltoKS = "SaltoKS",
104
106
  Tuya = "Tuya",
105
107
  TTLock = "TTLock",
106
108
  Schlage = "Schlage",
107
109
  YaleWifi = "YaleWifi",
108
- Devicethread = "Devicethread",
109
110
  Sensibo = "Sensibo",
110
111
  }
@@ -1,11 +1,18 @@
1
1
  // src/device/local/services/Device.service.ts
2
2
 
3
3
  import axios from "axios";
4
- import { getConfig } from "../../../config/config";
4
+ import {
5
+ getConfig,
6
+ checkAwsEnv,
7
+ ensureAuditInitialized,
8
+ } from "../../../config/config";
5
9
  import { IDeviceCreateParams } from "../interfaces";
10
+ import { eventDispatcher } from "dt-pub-sub";
11
+ import { publishAudit } from "dt-audit-library";
6
12
 
7
13
  export class DeviceService {
8
14
  private readonly baseUrl: string;
15
+ private readonly source = "dt-common-device";
9
16
 
10
17
  constructor() {
11
18
  const { DEVICE_SERVICE } = getConfig();
@@ -15,10 +22,19 @@ export class DeviceService {
15
22
  );
16
23
  }
17
24
  this.baseUrl = DEVICE_SERVICE;
25
+ checkAwsEnv();
26
+ ensureAuditInitialized();
18
27
  }
19
28
 
20
29
  async createDevice(body: IDeviceCreateParams): Promise<void> {
21
- await axios.post(`${this.baseUrl}/devices`, body);
30
+ await eventDispatcher.publishEvent("device.created", body, this.source);
31
+ const payload = {
32
+ eventType: "device.created",
33
+ properties: {
34
+ ...body,
35
+ },
36
+ };
37
+ await publishAudit(payload);
22
38
  }
23
39
 
24
40
  async getDevice(deviceId: string): Promise<any> {
@@ -32,40 +48,94 @@ export class DeviceService {
32
48
  }
33
49
 
34
50
  async updateDevice(deviceId: string, body: any): Promise<any> {
35
- return await axios.put(`${this.baseUrl}/devices/${deviceId}`, body);
51
+ //return await axios.put(`${this.baseUrl}/devices/${deviceId}`, body);
52
+ await eventDispatcher.publishEvent(
53
+ "device.updated",
54
+ { deviceId, body },
55
+ this.source
56
+ );
57
+ const payload = {
58
+ eventType: "device.updated",
59
+ properties: {
60
+ ...body,
61
+ },
62
+ };
63
+ await publishAudit(payload);
36
64
  }
37
65
 
38
66
  async deleteDevice(deviceId: string): Promise<any> {
39
- return await axios.delete(`${this.baseUrl}/devices/${deviceId}`);
67
+ await eventDispatcher.publishEvent(
68
+ "device.deleted",
69
+ { deviceId },
70
+ this.source
71
+ );
72
+ const payload = {
73
+ eventType: "device.deleted",
74
+ properties: {
75
+ deviceId,
76
+ },
77
+ };
78
+ await publishAudit(payload);
40
79
  }
41
80
 
42
81
  async getState(deviceId: string) {
43
82
  return await axios.get(`${this.baseUrl}/devices/${deviceId}/state`);
44
83
  }
84
+
45
85
  async getStatus(deviceId: string) {
46
86
  return await axios.get(`${this.baseUrl}/devices/${deviceId}/status`);
47
87
  }
48
88
 
89
+ async setStatus(deviceId: string, status: any) {
90
+ await eventDispatcher.publishEvent(
91
+ "device.status.set",
92
+ { deviceId, status },
93
+ this.source
94
+ );
95
+ const payload = {
96
+ eventType: "device.status.set",
97
+ properties: {
98
+ deviceId,
99
+ status,
100
+ },
101
+ };
102
+ await publishAudit(payload);
103
+ }
104
+
49
105
  async getBatteryLevel(deviceId: string) {
50
106
  return await axios.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
51
107
  }
52
108
  async setBatteryLevel(deviceId: string, batteryLevel: number) {
53
- return await axios.put(`${this.baseUrl}/devices/battery-level`, {
54
- body: {
109
+ await eventDispatcher.publishEvent(
110
+ "device.battery.set",
111
+ { deviceId, batteryLevel },
112
+ this.source
113
+ );
114
+ const payload = {
115
+ eventType: "device.battery.set",
116
+ properties: {
55
117
  deviceId,
56
118
  batteryLevel,
57
119
  },
58
- });
120
+ };
121
+ await publishAudit(payload);
59
122
  }
60
123
  async getMetaData(deviceId: string) {
61
- return await axios.get(`${this.baseUrl}/devices/${deviceId}/meta-data`);
124
+ return await axios.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
62
125
  }
63
126
  async setMetaData(deviceId: string, metaData: Record<string, any>) {
64
- return await axios.put(`${this.baseUrl}/devices/meta-data`, {
65
- body: {
127
+ await eventDispatcher.publishEvent(
128
+ "device.metaData.set",
129
+ { deviceId, metaData },
130
+ this.source
131
+ );
132
+ const payload = {
133
+ eventType: "device.metaData.set",
134
+ properties: {
66
135
  deviceId,
67
136
  metaData,
68
137
  },
69
- });
138
+ };
139
+ await publishAudit(payload);
70
140
  }
71
141
  }
@@ -2,7 +2,7 @@ import axios from "axios";
2
2
  import { getConfig } from "../../../config/config";
3
3
  import { IHubCreateParams } from "../interfaces";
4
4
 
5
- export class DeviceHubService {
5
+ export class HubService {
6
6
  private readonly baseUrl: string;
7
7
 
8
8
  constructor() {
@@ -34,6 +34,10 @@ export class DeviceHubService {
34
34
  return await axios.put(`${this.baseUrl}/devices/hubs/${hubId}`, body);
35
35
  }
36
36
 
37
+ async getStatus(hubId: string): Promise<any> {
38
+ return await axios.get(`${this.baseUrl}/devices/hubs/${hubId}/status`);
39
+ }
40
+
37
41
  async deleteHub(hubId: string): Promise<any> {
38
42
  return await axios.delete(`${this.baseUrl}/devices/hubs/${hubId}`);
39
43
  }
@@ -1,2 +1,2 @@
1
1
  export { DeviceService as LocalDeviceService } from "./Device.service";
2
- export { DeviceHubService as LocalDeviceHubService } from "./DeviceHub.service";
2
+ export { HubService as LocalHubService } from "./Hub.service";
package/src/index.ts CHANGED
@@ -3,12 +3,12 @@
3
3
  // Cloud exports
4
4
  export {
5
5
  CloudDeviceService,
6
- CloudDeviceHubService,
6
+ CloudHubService,
7
7
  CloudConnectionService,
8
8
  } from "./device/cloud/services";
9
9
  export {
10
10
  LocalDeviceService,
11
- LocalDeviceHubService,
11
+ LocalHubService,
12
12
  } from "./device/local/services";
13
13
 
14
14
  export * as cloudInterfaces from "./device/cloud/interfaces";
@@ -1,3 +0,0 @@
1
- export abstract class DeviceHubService {
2
- abstract getHubs(): Promise<any>;
3
- }