dt-common-device 1.0.5 → 1.0.7
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/README.md +16 -0
- package/dist/config/config.d.ts +2 -0
- package/dist/config/config.js +27 -0
- package/dist/device/cloud/interfaces/IConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IConnectionService.js +3 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +2 -3
- package/dist/device/cloud/interfaces/IDeviceService.d.ts +8 -0
- package/dist/device/cloud/interfaces/IDeviceService.js +2 -0
- package/dist/device/cloud/interfaces/IHubService.d.ts +5 -0
- package/dist/device/cloud/interfaces/IHubService.js +2 -0
- package/dist/device/cloud/interfaces/index.d.ts +3 -2
- package/dist/device/cloud/interfaces/index.js +3 -2
- package/dist/device/cloud/services/Connection.service.d.ts +4 -5
- package/dist/device/cloud/services/Device.service.d.ts +28 -5
- package/dist/device/cloud/services/Device.service.js +3 -0
- package/dist/device/cloud/services/Hub.service.d.ts +6 -0
- package/dist/device/cloud/services/Hub.service.js +6 -0
- package/dist/device/cloud/services/index.d.ts +1 -1
- package/dist/device/cloud/services/index.js +3 -3
- package/dist/device/cloud/types.d.ts +1 -1
- package/dist/device/cloud/types.js +1 -1
- package/dist/device/local/services/Device.service.d.ts +8 -0
- package/dist/device/local/services/Device.service.js +75 -3
- package/dist/device/local/services/Hub.service.d.ts +12 -0
- package/dist/device/local/services/Hub.service.js +43 -0
- package/dist/device/local/services/index.d.ts +1 -1
- package/dist/device/local/services/index.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/package.json +23 -21
- package/src/config/config.ts +35 -0
- package/src/device/cloud/interfaces/{IDeviceConnectionService.ts → IConnectionService.ts} +17 -26
- package/src/device/cloud/interfaces/{IDevicesService.ts → IDeviceService.ts} +3 -4
- package/src/device/cloud/interfaces/IHubService.ts +5 -0
- package/src/device/cloud/interfaces/index.ts +3 -2
- package/src/device/cloud/services/Connection.service.ts +3 -12
- package/src/device/cloud/services/Device.service.ts +30 -11
- package/src/device/cloud/services/Hub.service.ts +10 -0
- package/src/device/cloud/services/index.ts +1 -1
- package/src/device/cloud/types.ts +1 -1
- package/src/device/local/services/Device.service.ts +104 -4
- package/src/device/local/services/{DeviceHub.service.ts → Hub.service.ts} +5 -1
- package/src/device/local/services/index.ts +1 -1
- package/src/index.ts +2 -2
- 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
|
|
package/dist/config/config.d.ts
CHANGED
package/dist/config/config.js
CHANGED
|
@@ -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
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { IConnection, IConnectionConnectParams,
|
|
1
|
+
import { IConnection, IConnectionConnectParams, IDeviceAccountResponse } from "../types";
|
|
2
2
|
export interface IDeviceConnectionService {
|
|
3
3
|
createConnection(data: IConnection, userId: string): Promise<any>;
|
|
4
4
|
getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
|
|
5
|
-
getDevices(connection: IConnection): Promise<
|
|
6
|
-
filterDevices(connection: IConnection, devices: Record<string, any>[]): Promise<IDevice[]>;
|
|
5
|
+
getDevices(connection: IConnection): Promise<any>;
|
|
7
6
|
connect(connection: IConnection, connectionConnect: IConnectionConnectParams): Promise<any>;
|
|
8
7
|
}
|
|
@@ -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
|
+
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
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("./
|
|
18
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./IConnectionService"), exports);
|
|
18
|
+
__exportStar(require("./IDeviceService"), exports);
|
|
19
|
+
__exportStar(require("./IHubService"), exports);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IConnection,
|
|
3
|
-
export declare abstract class ConnectionService implements
|
|
1
|
+
import { IConnectionService } from "../interfaces";
|
|
2
|
+
import { IConnection, IDeviceAccountResponse, IConnectionConnectParams } from "../types";
|
|
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
|
-
abstract getDevices(connection: IConnection): Promise<
|
|
7
|
-
abstract filterDevices(connection: IConnection, devices: Record<string, any>[]): Promise<IDevice[]>;
|
|
6
|
+
abstract getDevices(connection: IConnection): Promise<any>;
|
|
8
7
|
abstract connect(connection: IConnection, connectionConnect: IConnectionConnectParams): Promise<any>;
|
|
9
8
|
}
|
|
@@ -1,10 +1,33 @@
|
|
|
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
|
+
};
|
|
26
|
+
connection: IConnection;
|
|
27
|
+
constructor(device: IDevice);
|
|
4
28
|
abstract getDevices(connection: IConnection): Promise<Record<string, any>[]>;
|
|
5
29
|
abstract getDevice(connectionId: string, deviceId: string): Promise<any>;
|
|
6
|
-
abstract
|
|
7
|
-
abstract
|
|
8
|
-
abstract
|
|
9
|
-
abstract getGatewayDetails(connectionId: string, gatewayId: string): Promise<Record<string, any>>;
|
|
30
|
+
abstract getBattery(deviceId: string): Promise<number | string>;
|
|
31
|
+
abstract getStatus(connectionId: string, deviceId: string): Promise<string>;
|
|
32
|
+
abstract getState(deviceId: string): Promise<string>;
|
|
10
33
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IHubService } from "../interfaces";
|
|
2
|
+
export declare abstract class HubService implements IHubService {
|
|
3
|
+
abstract getHubs(connectionId: string): Promise<any[] | null>;
|
|
4
|
+
abstract getHub(connectionId: string, hubId: string): Promise<Record<string, any>>;
|
|
5
|
+
abstract getStatus(connectionId: string, hubId: string): Promise<string>;
|
|
6
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { DeviceService as CloudDeviceService } from "./Device.service";
|
|
2
2
|
export { ConnectionService as CloudConnectionService } from "./Connection.service";
|
|
3
|
-
export {
|
|
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.
|
|
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
|
|
10
|
-
Object.defineProperty(exports, "
|
|
9
|
+
var Hub_service_1 = require("./Hub.service");
|
|
10
|
+
Object.defineProperty(exports, "CloudHubService", { enumerable: true, get: function () { return Hub_service_1.HubService; } });
|
|
@@ -98,12 +98,12 @@ export interface IConnectionConnectParams {
|
|
|
98
98
|
[key: string]: unknown;
|
|
99
99
|
}
|
|
100
100
|
export declare enum ConnectionProvider {
|
|
101
|
+
Devicethread = "Devicethread",
|
|
101
102
|
Smartthings = "Smartthings",
|
|
102
103
|
SaltoKS = "SaltoKS",
|
|
103
104
|
Tuya = "Tuya",
|
|
104
105
|
TTLock = "TTLock",
|
|
105
106
|
Schlage = "Schlage",
|
|
106
107
|
YaleWifi = "YaleWifi",
|
|
107
|
-
Devicethread = "Devicethread",
|
|
108
108
|
Sensibo = "Sensibo"
|
|
109
109
|
}
|
|
@@ -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,10 +1,18 @@
|
|
|
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>;
|
|
7
8
|
getPropertyDevices(propertyId: string): Promise<any>;
|
|
8
9
|
updateDevice(deviceId: string, body: any): Promise<any>;
|
|
9
10
|
deleteDevice(deviceId: string): Promise<any>;
|
|
11
|
+
getState(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
12
|
+
getStatus(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
13
|
+
setStatus(deviceId: string, status: any): Promise<void>;
|
|
14
|
+
getBatteryLevel(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
15
|
+
setBatteryLevel(deviceId: string, batteryLevel: number): Promise<void>;
|
|
16
|
+
getMetaData(deviceId: string): Promise<import("axios").AxiosResponse<any, any>>;
|
|
17
|
+
setMetaData(deviceId: string, metaData: Record<string, any>): Promise<void>;
|
|
10
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
|
|
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,70 @@ class DeviceService {
|
|
|
27
39
|
});
|
|
28
40
|
}
|
|
29
41
|
async updateDevice(deviceId, body) {
|
|
30
|
-
return await
|
|
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
|
-
|
|
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);
|
|
61
|
+
}
|
|
62
|
+
async getState(deviceId) {
|
|
63
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
64
|
+
}
|
|
65
|
+
async getStatus(deviceId) {
|
|
66
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
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
|
+
}
|
|
79
|
+
async getBatteryLevel(deviceId) {
|
|
80
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
81
|
+
}
|
|
82
|
+
async setBatteryLevel(deviceId, batteryLevel) {
|
|
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: {
|
|
87
|
+
deviceId,
|
|
88
|
+
batteryLevel,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
92
|
+
}
|
|
93
|
+
async getMetaData(deviceId) {
|
|
94
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
|
|
95
|
+
}
|
|
96
|
+
async setMetaData(deviceId, metaData) {
|
|
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: {
|
|
101
|
+
deviceId,
|
|
102
|
+
metaData,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
34
106
|
}
|
|
35
107
|
}
|
|
36
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 {
|
|
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.
|
|
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
|
|
7
|
-
Object.defineProperty(exports, "
|
|
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,
|
|
2
|
-
export { LocalDeviceService,
|
|
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.
|
|
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, "
|
|
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, "
|
|
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.
|
|
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.7",
|
|
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
|
+
}
|
package/src/config/config.ts
CHANGED
|
@@ -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,26 +1,17 @@
|
|
|
1
|
-
// Device Cloud Class Interface for DeviceThread Common Library
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
IConnection,
|
|
5
|
-
IConnectionConnectParams,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
connection: IConnection,
|
|
19
|
-
devices: Record<string, any>[]
|
|
20
|
-
): Promise<IDevice[]>;
|
|
21
|
-
|
|
22
|
-
connect(
|
|
23
|
-
connection: IConnection,
|
|
24
|
-
connectionConnect: IConnectionConnectParams
|
|
25
|
-
): Promise<any>;
|
|
26
|
-
}
|
|
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
|
-
|
|
10
|
-
getState(deviceId: string): Promise<
|
|
11
|
-
|
|
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
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./IConnectionService";
|
|
2
|
+
export * from "./IDeviceService";
|
|
3
|
+
export * from "./IHubService";
|
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
// Device Cloud Service - Class Implementation
|
|
2
|
-
import {
|
|
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
|
|
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
|
-
abstract getDevices(connection: IConnection): Promise<IDevice[]>;
|
|
18
|
-
|
|
19
|
-
abstract filterDevices(
|
|
20
|
-
connection: IConnection,
|
|
21
|
-
devices: Record<string, any>[]
|
|
22
|
-
): Promise<IDevice[]>;
|
|
23
|
-
|
|
14
|
+
abstract getDevices(connection: IConnection): Promise<any>;
|
|
24
15
|
abstract connect(
|
|
25
16
|
connection: IConnection,
|
|
26
17
|
connectionConnect: IConnectionConnectParams
|
|
@@ -1,17 +1,36 @@
|
|
|
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?: { id: string; name: string };
|
|
25
|
+
connection!: IConnection;
|
|
26
|
+
|
|
27
|
+
constructor(device: IDevice) {
|
|
28
|
+
Object.assign(this, device);
|
|
29
|
+
}
|
|
30
|
+
|
|
5
31
|
abstract getDevices(connection: IConnection): Promise<Record<string, any>[]>;
|
|
6
32
|
abstract getDevice(connectionId: string, deviceId: string): Promise<any>;
|
|
7
|
-
abstract
|
|
8
|
-
|
|
9
|
-
|
|
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>>;
|
|
33
|
+
abstract getBattery(deviceId: string): Promise<number | string>;
|
|
34
|
+
abstract getStatus(connectionId: string, deviceId: string): Promise<string>;
|
|
35
|
+
abstract getState(deviceId: string): Promise<string>;
|
|
17
36
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IHubService } from "../interfaces";
|
|
2
|
+
|
|
3
|
+
export abstract class HubService implements IHubService {
|
|
4
|
+
abstract getHubs(connectionId: string): Promise<any[] | null>;
|
|
5
|
+
abstract getHub(
|
|
6
|
+
connectionId: string,
|
|
7
|
+
hubId: string
|
|
8
|
+
): Promise<Record<string, any>>;
|
|
9
|
+
abstract getStatus(connectionId: string, hubId: string): Promise<string>;
|
|
10
|
+
}
|
|
@@ -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 {
|
|
5
|
+
export { HubService as CloudHubService } from "./Hub.service";
|
|
@@ -99,12 +99,12 @@ export interface IConnectionConnectParams {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
export enum ConnectionProvider {
|
|
102
|
+
Devicethread = "Devicethread",
|
|
102
103
|
Smartthings = "Smartthings",
|
|
103
104
|
SaltoKS = "SaltoKS",
|
|
104
105
|
Tuya = "Tuya",
|
|
105
106
|
TTLock = "TTLock",
|
|
106
107
|
Schlage = "Schlage",
|
|
107
108
|
YaleWifi = "YaleWifi",
|
|
108
|
-
Devicethread = "Devicethread",
|
|
109
109
|
Sensibo = "Sensibo",
|
|
110
110
|
}
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// src/device/local/services/Device.service.ts
|
|
2
2
|
|
|
3
3
|
import axios from "axios";
|
|
4
|
-
import {
|
|
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
|
|
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,10 +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
|
-
|
|
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);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async getState(deviceId: string) {
|
|
82
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async getStatus(deviceId: string) {
|
|
86
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
87
|
+
}
|
|
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
|
+
|
|
105
|
+
async getBatteryLevel(deviceId: string) {
|
|
106
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
107
|
+
}
|
|
108
|
+
async setBatteryLevel(deviceId: string, batteryLevel: number) {
|
|
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: {
|
|
117
|
+
deviceId,
|
|
118
|
+
batteryLevel,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
await publishAudit(payload);
|
|
122
|
+
}
|
|
123
|
+
async getMetaData(deviceId: string) {
|
|
124
|
+
return await axios.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
|
|
125
|
+
}
|
|
126
|
+
async setMetaData(deviceId: string, metaData: Record<string, any>) {
|
|
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: {
|
|
135
|
+
deviceId,
|
|
136
|
+
metaData,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
await publishAudit(payload);
|
|
40
140
|
}
|
|
41
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
|
|
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 {
|
|
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
|
-
|
|
6
|
+
CloudHubService,
|
|
7
7
|
CloudConnectionService,
|
|
8
8
|
} from "./device/cloud/services";
|
|
9
9
|
export {
|
|
10
10
|
LocalDeviceService,
|
|
11
|
-
|
|
11
|
+
LocalHubService,
|
|
12
12
|
} from "./device/local/services";
|
|
13
13
|
|
|
14
14
|
export * as cloudInterfaces from "./device/cloud/interfaces";
|