dt-common-device 1.0.20 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/device/cloud/entities/CloudConnection.d.ts +6 -0
- package/dist/device/cloud/entities/CloudConnection.js +6 -0
- package/dist/device/cloud/entities/CloudDevice.js +0 -19
- package/dist/device/cloud/entities/CloudDeviceService.d.ts +5 -0
- package/dist/device/cloud/entities/CloudDeviceService.js +9 -0
- package/dist/device/cloud/entities/DeviceFactory.d.ts +2 -1
- package/dist/device/cloud/entities/index.d.ts +2 -0
- package/dist/device/cloud/entities/index.js +2 -0
- package/dist/device/cloud/interfaces/ICloudConnection.d.ts +5 -0
- package/dist/device/cloud/interfaces/ICloudConnection.js +2 -0
- package/dist/device/cloud/interfaces/IDeviceFactory.d.ts +4 -0
- package/dist/device/cloud/interfaces/IDeviceFactory.js +2 -0
- package/dist/device/cloud/interfaces/index.d.ts +1 -3
- package/dist/device/cloud/interfaces/index.js +1 -3
- package/dist/device/cloud/types.d.ts +1 -1
- package/dist/device/local/events/EventHandler.d.ts +10 -0
- package/dist/device/local/events/EventHandler.js +75 -0
- package/dist/device/local/events/Events.d.ts +21 -0
- package/dist/device/local/events/Events.js +24 -0
- package/dist/device/local/interfaces/IDtDevice.d.ts +16 -0
- package/dist/device/local/interfaces/IDtDevice.js +2 -0
- package/dist/device/local/interfaces/index.d.ts +0 -1
- package/dist/device/local/interfaces/index.js +0 -1
- package/dist/device/local/repository/Connection.repository.d.ts +7 -0
- package/dist/device/local/repository/Connection.repository.js +77 -0
- package/dist/device/local/repository/Device.repository.d.ts +16 -0
- package/dist/device/local/repository/Device.repository.js +134 -0
- package/dist/device/local/repository/Hub.repository.d.ts +13 -0
- package/dist/device/local/repository/Hub.repository.js +97 -0
- package/dist/device/local/repository/Property.repository.d.ts +9 -0
- package/dist/device/local/repository/Property.repository.js +81 -0
- package/dist/device/local/services/Connection.service.d.ts +3 -2
- package/dist/device/local/services/Connection.service.js +7 -4
- package/dist/device/local/services/Device.service.d.ts +7 -9
- package/dist/device/local/services/Device.service.js +17 -91
- package/dist/device/local/services/Hub.service.d.ts +7 -7
- package/dist/device/local/services/Hub.service.js +13 -21
- package/dist/device/local/services/Property.service.d.ts +9 -0
- package/dist/device/local/services/Property.service.js +13 -0
- package/dist/device/local/services/index.d.ts +2 -1
- package/dist/device/local/services/index.js +4 -2
- package/dist/index.d.ts +2 -3
- package/dist/index.js +8 -8
- package/package.json +4 -3
- package/src/device/cloud/entities/CloudConnection.ts +13 -0
- package/src/device/cloud/entities/CloudDevice.ts +1 -21
- package/src/device/cloud/entities/DeviceFactory.ts +2 -1
- package/src/device/cloud/entities/index.ts +2 -0
- package/src/device/cloud/interfaces/ICloudConnection.ts +6 -0
- package/src/device/cloud/interfaces/ICloudDevice.ts +0 -1
- package/src/device/cloud/interfaces/IDeviceFactory.ts +5 -0
- package/src/device/cloud/interfaces/index.ts +1 -3
- package/src/device/cloud/types.ts +1 -1
- package/src/device/local/events/EventHandler.ts +101 -0
- package/src/device/local/events/Events.ts +23 -0
- package/src/device/local/interfaces/IDtDevice.ts +16 -0
- package/src/device/local/interfaces/index.ts +0 -1
- package/src/device/local/repository/Connection.repository.ts +32 -0
- package/src/device/local/repository/Device.repository.ts +106 -0
- package/src/device/local/repository/Hub.repository.ts +51 -0
- package/src/device/local/repository/Property.repository.ts +32 -0
- package/src/device/local/services/Connection.service.ts +11 -9
- package/src/device/local/services/Device.service.ts +21 -109
- package/src/device/local/services/Hub.service.ts +17 -28
- package/src/device/local/services/Property.service.ts +12 -0
- package/src/device/local/services/index.ts +2 -1
- package/src/index.ts +5 -3
- package/src/device/cloud/interfaces/IConnectionService.ts +0 -15
- package/src/device/cloud/interfaces/IDeviceService.ts +0 -12
- package/src/device/cloud/interfaces/IHubService.ts +0 -5
- package/src/device/cloud/services/Connection.service.ts +0 -19
- package/src/device/cloud/services/index.ts +0 -2
- package/src/device/local/handler/EventHandler.ts +0 -52
- package/src/device/local/interfaces/IConnection.ts +0 -17
- package/src/device/local/interfaces/IHub.ts +0 -46
- /package/src/device/cloud/{services/CloudDevice.service.ts → entities/CloudDeviceService.ts} +0 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ICloudConnection } from "../interfaces";
|
|
2
|
+
import { IDeviceAccountResponse, IConnection } from "../types";
|
|
3
|
+
export declare abstract class CloudConnection implements ICloudConnection {
|
|
4
|
+
abstract createConnection(data: IConnection, userId: string): Promise<IConnection>;
|
|
5
|
+
abstract getDeviceAccount(connection: IConnection): Promise<IDeviceAccountResponse>;
|
|
6
|
+
}
|
|
@@ -21,22 +21,3 @@ class CloudDevice {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
exports.CloudDevice = CloudDevice;
|
|
24
|
-
/*
|
|
25
|
-
Usage Example:
|
|
26
|
-
|
|
27
|
-
// When extending CloudDevice, you can now do:
|
|
28
|
-
class MyCloudDevice extends CloudDevice {
|
|
29
|
-
constructor(device: IDevice, cloudDeviceService: ICloudDeviceService) {
|
|
30
|
-
super(device, cloudDeviceService);
|
|
31
|
-
// Additional initialization if needed
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Create and initialize in one step:
|
|
36
|
-
const device = await MyCloudDevice.create(device, cloudDeviceService);
|
|
37
|
-
// The device is now fully initialized with connection and localDevice loaded
|
|
38
|
-
|
|
39
|
-
// Or if you need to create without auto-initialization:
|
|
40
|
-
const device = new MyCloudDevice(device, cloudDeviceService);
|
|
41
|
-
await device.initialize(); // Manual initialization
|
|
42
|
-
*/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CloudDeviceService = void 0;
|
|
4
|
+
class CloudDeviceService {
|
|
5
|
+
async getConnection(deviceId) {
|
|
6
|
+
throw new Error("Method not implemented.");
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.CloudDeviceService = CloudDeviceService;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { IDevice } from "../../local/interfaces";
|
|
2
|
-
|
|
2
|
+
import { IDeviceFactory } from "../interfaces/IDeviceFactory";
|
|
3
|
+
export declare class DeviceFactory implements IDeviceFactory {
|
|
3
4
|
private readonly localDeviceService;
|
|
4
5
|
constructor();
|
|
5
6
|
getDevice(deviceId: string): Promise<IDevice>;
|
|
@@ -16,3 +16,5 @@ 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
|
+
__exportStar(require("./CloudDeviceService"), exports);
|
|
@@ -14,8 +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("./IDeviceService"), exports);
|
|
19
|
-
__exportStar(require("./IHubService"), exports);
|
|
17
|
+
__exportStar(require("./ICloudConnection"), exports);
|
|
20
18
|
__exportStar(require("./ICloudDevice"), exports);
|
|
21
19
|
__exportStar(require("./ICloudDeviceService"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class EventHandler {
|
|
2
|
+
private readonly source;
|
|
3
|
+
constructor();
|
|
4
|
+
onDeviceCreate(body: any): Promise<void>;
|
|
5
|
+
onDeviceUpdate(deviceId: string, body: any): Promise<void>;
|
|
6
|
+
onDeviceDelete(deviceId: string): Promise<void>;
|
|
7
|
+
onStateChange(deviceId: string, state: any): Promise<void>;
|
|
8
|
+
onStatusChange(deviceId: string, status: any): Promise<void>;
|
|
9
|
+
onBatteryLevelChange(deviceId: string, batteryLevel: number): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventHandler = void 0;
|
|
4
|
+
const dt_pub_sub_1 = require("dt-pub-sub");
|
|
5
|
+
const dt_audit_library_1 = require("dt-audit-library");
|
|
6
|
+
const Events_1 = require("./Events");
|
|
7
|
+
class EventHandler {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.source = "dt-common-device";
|
|
10
|
+
}
|
|
11
|
+
async onDeviceCreate(body) {
|
|
12
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_CREATED, body, this.source);
|
|
13
|
+
const payload = {
|
|
14
|
+
eventType: Events_1.DeviceEvents.DEVICE_CREATED,
|
|
15
|
+
properties: {
|
|
16
|
+
...body,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
20
|
+
}
|
|
21
|
+
async onDeviceUpdate(deviceId, body) {
|
|
22
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_UPDATED, { deviceId, body }, this.source);
|
|
23
|
+
const payload = {
|
|
24
|
+
eventType: Events_1.DeviceEvents.DEVICE_UPDATED,
|
|
25
|
+
properties: {
|
|
26
|
+
...body,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
30
|
+
}
|
|
31
|
+
async onDeviceDelete(deviceId) {
|
|
32
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_DELETED, { deviceId }, this.source);
|
|
33
|
+
const payload = {
|
|
34
|
+
eventType: Events_1.DeviceEvents.DEVICE_DELETED,
|
|
35
|
+
properties: {
|
|
36
|
+
deviceId,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
40
|
+
}
|
|
41
|
+
async onStateChange(deviceId, state) {
|
|
42
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_STATE_SET, { deviceId, state }, this.source);
|
|
43
|
+
const payload = {
|
|
44
|
+
eventType: Events_1.DeviceEvents.DEVICE_STATE_SET,
|
|
45
|
+
properties: {
|
|
46
|
+
deviceId,
|
|
47
|
+
state,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
51
|
+
}
|
|
52
|
+
async onStatusChange(deviceId, status) {
|
|
53
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_STATUS_SET, { deviceId, status }, this.source);
|
|
54
|
+
const payload = {
|
|
55
|
+
eventType: Events_1.DeviceEvents.DEVICE_STATUS_SET,
|
|
56
|
+
properties: {
|
|
57
|
+
deviceId,
|
|
58
|
+
status,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
62
|
+
}
|
|
63
|
+
async onBatteryLevelChange(deviceId, batteryLevel) {
|
|
64
|
+
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DeviceEvents.DEVICE_BATTERY_SET, { deviceId, batteryLevel }, this.source);
|
|
65
|
+
const payload = {
|
|
66
|
+
eventType: Events_1.DeviceEvents.DEVICE_BATTERY_SET,
|
|
67
|
+
properties: {
|
|
68
|
+
deviceId,
|
|
69
|
+
batteryLevel,
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
await (0, dt_audit_library_1.publishAudit)(payload);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.EventHandler = EventHandler;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const DeviceEvents: {
|
|
2
|
+
DEVICE_CREATED: string;
|
|
3
|
+
DEVICE_UPDATED: string;
|
|
4
|
+
DEVICE_DELETED: string;
|
|
5
|
+
DEVICE_STATE_SET: string;
|
|
6
|
+
DEVICE_STATUS_SET: string;
|
|
7
|
+
DEVICE_BATTERY_SET: string;
|
|
8
|
+
DEVICE_META_DATA_SET: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const ConnectionEvents: {
|
|
11
|
+
CONNECTION_UPDATED: string;
|
|
12
|
+
CONNECTION_DELETED: string;
|
|
13
|
+
CONNECTION_CREATED: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const PropertyEvents: {
|
|
16
|
+
PROPERTY_UPDATED: string;
|
|
17
|
+
PROPERTY_DELETED: string;
|
|
18
|
+
PROPERTY_CREATED: string;
|
|
19
|
+
PROPERTY_PREFERENCES_UPDATED: string;
|
|
20
|
+
PROPERTY_PREFERENCES_CREATED: string;
|
|
21
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PropertyEvents = exports.ConnectionEvents = exports.DeviceEvents = void 0;
|
|
4
|
+
exports.DeviceEvents = {
|
|
5
|
+
DEVICE_CREATED: "device.created",
|
|
6
|
+
DEVICE_UPDATED: "device.updated",
|
|
7
|
+
DEVICE_DELETED: "device.deleted",
|
|
8
|
+
DEVICE_STATE_SET: "device.state.set",
|
|
9
|
+
DEVICE_STATUS_SET: "device.status.set",
|
|
10
|
+
DEVICE_BATTERY_SET: "device.battery.set",
|
|
11
|
+
DEVICE_META_DATA_SET: "device.metaData.set",
|
|
12
|
+
};
|
|
13
|
+
exports.ConnectionEvents = {
|
|
14
|
+
CONNECTION_UPDATED: "connection.updated",
|
|
15
|
+
CONNECTION_DELETED: "connection.deleted",
|
|
16
|
+
CONNECTION_CREATED: "connection.created",
|
|
17
|
+
};
|
|
18
|
+
exports.PropertyEvents = {
|
|
19
|
+
PROPERTY_UPDATED: "property.updated",
|
|
20
|
+
PROPERTY_DELETED: "property.deleted",
|
|
21
|
+
PROPERTY_CREATED: "property.created",
|
|
22
|
+
PROPERTY_PREFERENCES_UPDATED: "property.preferences.updated",
|
|
23
|
+
PROPERTY_PREFERENCES_CREATED: "property.preferences.created",
|
|
24
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface IDtDevice {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
zoneId: string;
|
|
6
|
+
deviceType: string;
|
|
7
|
+
deviceId: string;
|
|
8
|
+
deviceNetworkId: string;
|
|
9
|
+
propertyId: string;
|
|
10
|
+
connectionId: string;
|
|
11
|
+
hubId: string;
|
|
12
|
+
isBatteryLowAlertSent: boolean;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
isActive: boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IConnection } from "../../cloud/types";
|
|
2
|
+
export declare class ConnectionRepository {
|
|
3
|
+
private readonly pool;
|
|
4
|
+
constructor();
|
|
5
|
+
getConnectionById(connectionId: string): Promise<IConnection>;
|
|
6
|
+
updateConnection(connectionId: string, data: Partial<IConnection>): Promise<any>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.ConnectionRepository = void 0;
|
|
42
|
+
const db_1 = require("../../../db");
|
|
43
|
+
const typedi_1 = require("typedi");
|
|
44
|
+
let ConnectionRepository = (() => {
|
|
45
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
46
|
+
let _classDescriptor;
|
|
47
|
+
let _classExtraInitializers = [];
|
|
48
|
+
let _classThis;
|
|
49
|
+
var ConnectionRepository = _classThis = class {
|
|
50
|
+
constructor() {
|
|
51
|
+
this.pool = (0, db_1.getPostgresClient)();
|
|
52
|
+
}
|
|
53
|
+
async getConnectionById(connectionId) {
|
|
54
|
+
const result = await this.pool.query("SELECT * FROM dt_connections WHERE id = $1", [connectionId]);
|
|
55
|
+
return result.rows[0];
|
|
56
|
+
}
|
|
57
|
+
async updateConnection(connectionId, data) {
|
|
58
|
+
// Build dynamic SET clause
|
|
59
|
+
const setClause = Object.keys(data)
|
|
60
|
+
.map((key, index) => `${key} = $${index + 2}`)
|
|
61
|
+
.join(", ");
|
|
62
|
+
const values = Object.values(data);
|
|
63
|
+
const result = await this.pool.query(`UPDATE dt_connections SET ${setClause}, updated_at = NOW() WHERE id = $1 RETURNING *`, [connectionId, ...values]);
|
|
64
|
+
return result.rows[0];
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
__setFunctionName(_classThis, "ConnectionRepository");
|
|
68
|
+
(() => {
|
|
69
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
70
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
71
|
+
ConnectionRepository = _classThis = _classDescriptor.value;
|
|
72
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
73
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
74
|
+
})();
|
|
75
|
+
return ConnectionRepository = _classThis;
|
|
76
|
+
})();
|
|
77
|
+
exports.ConnectionRepository = ConnectionRepository;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IDevice } from "../interfaces/IDevice";
|
|
2
|
+
import { IDtDevice } from "../interfaces/IDtDevice";
|
|
3
|
+
export declare class DeviceRepository {
|
|
4
|
+
private readonly baseUrl;
|
|
5
|
+
private readonly postgres;
|
|
6
|
+
constructor();
|
|
7
|
+
getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
|
|
8
|
+
getDevices(deviceIds: string[], withHubDetails?: boolean): Promise<IDevice[]>;
|
|
9
|
+
getPropertyDevices(propertyId: string, withHubDetails?: boolean): Promise<IDevice[]>;
|
|
10
|
+
getState(deviceId: string): Promise<any>;
|
|
11
|
+
getStatus(deviceId: string): Promise<Record<string, any>>;
|
|
12
|
+
getBatteryLevel(deviceId: string): Promise<Record<string, any>>;
|
|
13
|
+
getMetaData(deviceId: string): Promise<any>;
|
|
14
|
+
getDevicesByAccessGroup(accessGroupId: string): Promise<IDtDevice[]>;
|
|
15
|
+
getDevicesByZone(zoneId: string): Promise<IDtDevice[]>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.DeviceRepository = void 0;
|
|
45
|
+
const axios_1 = __importDefault(require("axios"));
|
|
46
|
+
const config_1 = require("../../../config/config");
|
|
47
|
+
const db_1 = require("../../../db");
|
|
48
|
+
const typedi_1 = require("typedi");
|
|
49
|
+
let DeviceRepository = (() => {
|
|
50
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
51
|
+
let _classDescriptor;
|
|
52
|
+
let _classExtraInitializers = [];
|
|
53
|
+
let _classThis;
|
|
54
|
+
var DeviceRepository = _classThis = class {
|
|
55
|
+
constructor() {
|
|
56
|
+
const { DEVICE_SERVICE } = (0, config_1.getConfig)();
|
|
57
|
+
if (!DEVICE_SERVICE) {
|
|
58
|
+
throw new Error("DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE.");
|
|
59
|
+
}
|
|
60
|
+
this.baseUrl = DEVICE_SERVICE;
|
|
61
|
+
this.postgres = (0, db_1.getPostgresClient)();
|
|
62
|
+
}
|
|
63
|
+
async getDevice(deviceId, withHubDetails = false) {
|
|
64
|
+
try {
|
|
65
|
+
const response = await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}?withHubDetails=${withHubDetails} `);
|
|
66
|
+
return response.data;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.log(error);
|
|
70
|
+
throw new Error(`Failed to get device: ${error.message}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async getDevices(deviceIds, withHubDetails = false) {
|
|
74
|
+
return await axios_1.default.get(`${this.baseUrl}/devices`, {
|
|
75
|
+
params: { deviceIds, withHubDetails },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async getPropertyDevices(propertyId, withHubDetails = false) {
|
|
79
|
+
return await axios_1.default.get(`${this.baseUrl}/devices`, {
|
|
80
|
+
params: { propertyId, withHubDetails },
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async getState(deviceId) {
|
|
84
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/state`);
|
|
85
|
+
}
|
|
86
|
+
async getStatus(deviceId) {
|
|
87
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/status`);
|
|
88
|
+
}
|
|
89
|
+
async getBatteryLevel(deviceId) {
|
|
90
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
|
|
91
|
+
}
|
|
92
|
+
async getMetaData(deviceId) {
|
|
93
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
|
|
94
|
+
}
|
|
95
|
+
async getDevicesByAccessGroup(accessGroupId) {
|
|
96
|
+
try {
|
|
97
|
+
const result = await this.postgres.query(`SELECT d.* FROM dt_devices d
|
|
98
|
+
INNER JOIN dt_zones_collection_map zcm ON d.zoneId = zcm.zoneId
|
|
99
|
+
WHERE zcm.collectionId = $1`, [accessGroupId]);
|
|
100
|
+
if (result.rows.length > 0) {
|
|
101
|
+
return result.rows;
|
|
102
|
+
}
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
console.log(error);
|
|
107
|
+
throw new Error("Failed to get devices by access group");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async getDevicesByZone(zoneId) {
|
|
111
|
+
try {
|
|
112
|
+
const result = await this.postgres.query("SELECT * FROM dt_devices WHERE zoneId = $1", [zoneId]);
|
|
113
|
+
if (result.rows.length > 0) {
|
|
114
|
+
return result.rows;
|
|
115
|
+
}
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.log(error);
|
|
120
|
+
throw new Error("Failed to get device by zone");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
__setFunctionName(_classThis, "DeviceRepository");
|
|
125
|
+
(() => {
|
|
126
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
127
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
128
|
+
DeviceRepository = _classThis = _classDescriptor.value;
|
|
129
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
130
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
131
|
+
})();
|
|
132
|
+
return DeviceRepository = _classThis;
|
|
133
|
+
})();
|
|
134
|
+
exports.DeviceRepository = DeviceRepository;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IDevice } from "../interfaces";
|
|
2
|
+
export declare class HubRepository {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly postgres;
|
|
5
|
+
constructor();
|
|
6
|
+
addHub(body: any): Promise<IDevice>;
|
|
7
|
+
getHubs(hubIds: string[]): Promise<IDevice[]>;
|
|
8
|
+
getHub(hubId: string): Promise<IDevice>;
|
|
9
|
+
updateHub(hubId: string, body: any): Promise<IDevice>;
|
|
10
|
+
getStatus(hubId: string): Promise<"ONLINE" | "OFFLINE" | "UNKNOWN">;
|
|
11
|
+
deleteHub(hubId: string): Promise<any>;
|
|
12
|
+
deleteAllHubs(hubIds: string[]): Promise<any>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.HubRepository = void 0;
|
|
45
|
+
const axios_1 = __importDefault(require("axios"));
|
|
46
|
+
const config_1 = require("../../../config/config");
|
|
47
|
+
const db_1 = require("../../../db");
|
|
48
|
+
const typedi_1 = require("typedi");
|
|
49
|
+
let HubRepository = (() => {
|
|
50
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
51
|
+
let _classDescriptor;
|
|
52
|
+
let _classExtraInitializers = [];
|
|
53
|
+
let _classThis;
|
|
54
|
+
var HubRepository = _classThis = class {
|
|
55
|
+
constructor() {
|
|
56
|
+
const { DEVICE_SERVICE } = (0, config_1.getConfig)();
|
|
57
|
+
if (!DEVICE_SERVICE) {
|
|
58
|
+
throw new Error("DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE.");
|
|
59
|
+
}
|
|
60
|
+
this.baseUrl = DEVICE_SERVICE;
|
|
61
|
+
this.postgres = (0, db_1.getPostgresClient)();
|
|
62
|
+
}
|
|
63
|
+
async addHub(body) {
|
|
64
|
+
return await axios_1.default.post(`${this.baseUrl}/devices/hubs`, body);
|
|
65
|
+
}
|
|
66
|
+
async getHubs(hubIds) {
|
|
67
|
+
const query = hubIds && hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
68
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/hubs${query}`);
|
|
69
|
+
}
|
|
70
|
+
async getHub(hubId) {
|
|
71
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/hubs/${hubId}`);
|
|
72
|
+
}
|
|
73
|
+
async updateHub(hubId, body) {
|
|
74
|
+
return await axios_1.default.put(`${this.baseUrl}/devices/hubs/${hubId}`, body);
|
|
75
|
+
}
|
|
76
|
+
async getStatus(hubId) {
|
|
77
|
+
return await axios_1.default.get(`${this.baseUrl}/devices/hubs/${hubId}/status`);
|
|
78
|
+
}
|
|
79
|
+
async deleteHub(hubId) {
|
|
80
|
+
return await axios_1.default.delete(`${this.baseUrl}/devices/hubs/${hubId}`);
|
|
81
|
+
}
|
|
82
|
+
async deleteAllHubs(hubIds) {
|
|
83
|
+
const query = hubIds.length ? `?ids=${hubIds.join(",")}` : "";
|
|
84
|
+
return await axios_1.default.delete(`${this.baseUrl}/devices/hubs${query}`);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
__setFunctionName(_classThis, "HubRepository");
|
|
88
|
+
(() => {
|
|
89
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
90
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
91
|
+
HubRepository = _classThis = _classDescriptor.value;
|
|
92
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
93
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
94
|
+
})();
|
|
95
|
+
return HubRepository = _classThis;
|
|
96
|
+
})();
|
|
97
|
+
exports.HubRepository = HubRepository;
|