dt-common-device 1.0.12 → 1.0.13

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 (35) hide show
  1. package/dist/device/cloud/entities/CloudDevice.d.ts +10 -0
  2. package/dist/device/cloud/entities/CloudDevice.js +14 -0
  3. package/dist/device/cloud/entities/DeviceFactory.d.ts +6 -0
  4. package/dist/device/cloud/entities/DeviceFactory.js +13 -0
  5. package/dist/device/cloud/entities/index.d.ts +2 -0
  6. package/dist/device/cloud/entities/index.js +18 -0
  7. package/dist/device/cloud/interfaces/ICloudDevice.d.ts +5 -0
  8. package/dist/device/cloud/interfaces/ICloudDevice.js +2 -0
  9. package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +4 -0
  10. package/dist/device/cloud/interfaces/ICloudDeviceService.js +2 -0
  11. package/dist/device/cloud/interfaces/index.d.ts +2 -0
  12. package/dist/device/cloud/interfaces/index.js +2 -0
  13. package/dist/device/cloud/services/CloudDevice.service.d.ts +5 -0
  14. package/dist/device/cloud/services/CloudDevice.service.js +9 -0
  15. package/dist/device/cloud/services/index.d.ts +2 -3
  16. package/dist/device/cloud/services/index.js +16 -8
  17. package/dist/device/cloud/types.d.ts +0 -59
  18. package/dist/device/local/services/Device.service.d.ts +1 -0
  19. package/dist/device/local/services/Device.service.js +33 -19
  20. package/dist/index.d.ts +4 -3
  21. package/dist/index.js +7 -27
  22. package/package.json +1 -1
  23. package/src/device/cloud/entities/CloudDevice.ts +19 -0
  24. package/src/device/cloud/entities/DeviceFactory.ts +14 -0
  25. package/src/device/cloud/entities/index.ts +2 -0
  26. package/src/device/cloud/interfaces/ICloudDevice.ts +7 -0
  27. package/src/device/cloud/interfaces/ICloudDeviceService.ts +6 -0
  28. package/src/device/cloud/interfaces/index.ts +2 -0
  29. package/src/device/cloud/services/CloudDevice.service.ts +8 -0
  30. package/src/device/cloud/services/index.ts +2 -5
  31. package/src/device/cloud/types.ts +0 -53
  32. package/src/device/local/services/Device.service.ts +38 -20
  33. package/src/index.ts +4 -4
  34. package/src/device/cloud/services/Device.service.ts +0 -41
  35. package/src/device/cloud/services/Hub.service.ts +0 -34
@@ -0,0 +1,10 @@
1
+ import { ICloudDevice } from "../interfaces/ICloudDevice";
2
+ import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
3
+ import { IConnection } from "../types";
4
+ export declare abstract class CloudDevice implements ICloudDevice {
5
+ deviceId: string;
6
+ connection: IConnection;
7
+ cloudDeviceService: ICloudDeviceService;
8
+ constructor(deviceId: string, cloudDeviceService: ICloudDeviceService);
9
+ getConnection(deviceId: string): IConnection;
10
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudDevice = void 0;
4
+ class CloudDevice {
5
+ constructor(deviceId, cloudDeviceService) {
6
+ this.deviceId = deviceId;
7
+ this.cloudDeviceService = cloudDeviceService;
8
+ this.connection = this.getConnection(deviceId);
9
+ }
10
+ getConnection(deviceId) {
11
+ return this.cloudDeviceService.getConnection(deviceId);
12
+ }
13
+ }
14
+ exports.CloudDevice = CloudDevice;
@@ -0,0 +1,6 @@
1
+ import { IDevice } from "../../local/interfaces";
2
+ export declare class DeviceFactory {
3
+ private readonly localDeviceService;
4
+ constructor();
5
+ getDevice(deviceId: string): Promise<IDevice>;
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceFactory = void 0;
4
+ const Device_service_1 = require("../../local/services/Device.service");
5
+ class DeviceFactory {
6
+ constructor() {
7
+ this.localDeviceService = new Device_service_1.DeviceService();
8
+ }
9
+ async getDevice(deviceId) {
10
+ return await this.localDeviceService.getDevice(deviceId);
11
+ }
12
+ }
13
+ exports.DeviceFactory = DeviceFactory;
@@ -0,0 +1,2 @@
1
+ export * from "./CloudDevice";
2
+ export * from "./DeviceFactory";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./CloudDevice"), exports);
18
+ __exportStar(require("./DeviceFactory"), exports);
@@ -0,0 +1,5 @@
1
+ import { ICloudDeviceService } from "./ICloudDeviceService";
2
+ export interface ICloudDevice {
3
+ deviceId: string;
4
+ cloudDeviceService: ICloudDeviceService;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { IConnection } from "../types";
2
+ export interface ICloudDeviceService {
3
+ getConnection(deviceId: string): IConnection;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,5 @@
1
1
  export * from "./IConnectionService";
2
2
  export * from "./IDeviceService";
3
3
  export * from "./IHubService";
4
+ export * from "./ICloudDevice";
5
+ export * from "./ICloudDeviceService";
@@ -17,3 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./IConnectionService"), exports);
18
18
  __exportStar(require("./IDeviceService"), exports);
19
19
  __exportStar(require("./IHubService"), exports);
20
+ __exportStar(require("./ICloudDevice"), exports);
21
+ __exportStar(require("./ICloudDeviceService"), exports);
@@ -0,0 +1,5 @@
1
+ import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
2
+ import { IConnection } from "../types";
3
+ export declare class CloudDeviceService implements ICloudDeviceService {
4
+ getConnection(deviceId: string): IConnection;
5
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudDeviceService = void 0;
4
+ class CloudDeviceService {
5
+ getConnection(deviceId) {
6
+ throw new Error("Method not implemented.");
7
+ }
8
+ }
9
+ exports.CloudDeviceService = CloudDeviceService;
@@ -1,3 +1,2 @@
1
- export { DeviceService as CloudDeviceService } from "./Device.service";
2
- export { ConnectionService as CloudConnectionService } from "./Connection.service";
3
- export { HubService as CloudHubService } from "./Hub.service";
1
+ export * from "./CloudDevice.service";
2
+ export * from "./Connection.service";
@@ -1,10 +1,18 @@
1
1
  "use strict";
2
- // Device Cloud Services - Export all service classes
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
3
16
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.CloudHubService = exports.CloudConnectionService = exports.CloudDeviceService = void 0;
5
- var Device_service_1 = require("./Device.service");
6
- Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return Device_service_1.DeviceService; } });
7
- var Connection_service_1 = require("./Connection.service");
8
- Object.defineProperty(exports, "CloudConnectionService", { enumerable: true, get: function () { return Connection_service_1.ConnectionService; } });
9
- var Hub_service_1 = require("./Hub.service");
10
- Object.defineProperty(exports, "CloudHubService", { enumerable: true, get: function () { return Hub_service_1.HubService; } });
17
+ __exportStar(require("./CloudDevice.service"), exports);
18
+ __exportStar(require("./Connection.service"), exports);
@@ -21,65 +21,6 @@ export interface IConnectionPagination {
21
21
  page: number;
22
22
  limit: number;
23
23
  }
24
- /**
25
- * Represents a device in the system.
26
- * All date fields must be ISO 8601 strings.
27
- */
28
- export interface IDevice {
29
- deviceId: string;
30
- propertyId: string;
31
- name: string;
32
- hubId: string[];
33
- specifications?: {
34
- manufacturer?: string;
35
- model?: string;
36
- firmware?: {
37
- version?: string;
38
- newVersionAvailable?: boolean;
39
- newVersion?: string;
40
- mandatoryUpdate?: boolean;
41
- };
42
- };
43
- protocol?: {
44
- location?: {
45
- id?: string;
46
- name?: string;
47
- city?: string;
48
- };
49
- name?: string;
50
- room?: {
51
- id?: string;
52
- name?: string;
53
- };
54
- accountId?: string;
55
- };
56
- deviceType: {
57
- id: string;
58
- type: string;
59
- };
60
- status: {
61
- online: boolean;
62
- error?: {
63
- type?: string;
64
- message?: string;
65
- };
66
- lastUpdated?: string;
67
- };
68
- state?: Record<string, any>;
69
- metaData?: Record<string, any>;
70
- createdAt?: Date;
71
- updatedAt?: Date;
72
- isDeleted?: boolean;
73
- hubDeviceDetails?: IDevice[];
74
- parentZones?: {
75
- id: string;
76
- name: string;
77
- }[];
78
- zone?: {
79
- id: string;
80
- name: string;
81
- };
82
- }
83
24
  /**
84
25
  * Device account response from provider.
85
26
  * WARNING: Do not log or expose connection_access_token.
@@ -3,6 +3,7 @@ export declare class DeviceService {
3
3
  private readonly baseUrl;
4
4
  private readonly source;
5
5
  private readonly eventHandler;
6
+ private readonly alertService;
6
7
  constructor();
7
8
  createDevice(body: IDevice): Promise<void>;
8
9
  getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
@@ -11,6 +11,7 @@ const dt_pub_sub_1 = require("dt-pub-sub");
11
11
  const dt_audit_library_1 = require("dt-audit-library");
12
12
  const EventHandler_1 = require("../handler/EventHandler");
13
13
  const lodash_1 = require("lodash");
14
+ const Alert_service_1 = require("./Alert.service");
14
15
  class DeviceService {
15
16
  constructor() {
16
17
  this.source = "dt-common-device";
@@ -22,6 +23,7 @@ class DeviceService {
22
23
  (0, config_1.checkAwsEnv)();
23
24
  (0, config_1.ensureAuditInitialized)();
24
25
  this.eventHandler = new EventHandler_1.EventHandler();
26
+ this.alertService = new Alert_service_1.AlertService();
25
27
  }
26
28
  async createDevice(body) {
27
29
  await dt_pub_sub_1.eventDispatcher.publishEvent("device.created", body, this.source);
@@ -71,14 +73,11 @@ class DeviceService {
71
73
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/state`);
72
74
  }
73
75
  async setState(deviceId, state) {
74
- // If old status and new status are different
76
+ // If old state and new state are different
75
77
  const oldState = await this.getState(deviceId);
76
- if (!(0, lodash_1.isEqual)(oldState, state)) {
77
- this.eventHandler.onStateChange(deviceId, state);
78
+ if (!(0, lodash_1.isEqual)(oldState?.data?.state, state)) {
79
+ return await this.eventHandler.onStateChange(deviceId, state);
78
80
  }
79
- // If offline, check the time difference between the last offline status and current time
80
- // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
81
- //deviceAlertService.raiseOperationalAlert();
82
81
  }
83
82
  async getStatus(deviceId) {
84
83
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/status`);
@@ -87,23 +86,38 @@ class DeviceService {
87
86
  // If old status and new status are different
88
87
  const oldStatus = await this.getStatus(deviceId);
89
88
  if (!(0, lodash_1.isEqual)(oldStatus, status)) {
90
- this.eventHandler.onStatusChange(deviceId, status);
89
+ return await this.eventHandler.onStatusChange(deviceId, status);
90
+ }
91
+ // If both old and new status are offline, check the time difference between the last offline status and current time
92
+ if (oldStatus?.data?.online === false &&
93
+ oldStatus?.data?.lastUpdated &&
94
+ status?.online === false) {
95
+ const lastOfflineTime = new Date(oldStatus?.data?.lastUpdated).getTime();
96
+ const currentTime = Date.now();
97
+ const timeDifference = currentTime - lastOfflineTime;
98
+ // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
99
+ if (timeDifference > 1000 * 60 * 60 * 24) {
100
+ return await this.alertService.raiseOperationsAlert();
101
+ }
91
102
  }
92
- // If offline, check the time difference between the last offline status and current time
93
- // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
94
- //deviceAlertService.raiseOperationalAlert();
95
103
  }
96
104
  async getBatteryLevel(deviceId) {
97
105
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
98
106
  }
99
107
  async setBatteryLevel(deviceId, batteryLevel) {
100
- // If old status and new status are different
101
- // and the last battery level received time more than 8 hours {
102
- this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel);
103
- // if(batteryLevel < propertySettingBatteryThreshold) {
104
- // deviceAlertService.raiseEnergyAlert({});
105
- // }
106
- // }
108
+ // If old battery level and new battery level are different
109
+ const oldBatteryLevel = await this.getBatteryLevel(deviceId);
110
+ if (!(0, lodash_1.isEqual)(oldBatteryLevel?.data?.value, batteryLevel)) {
111
+ this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel);
112
+ }
113
+ // If current battery level is less than 20% and lsat updated time is more than 8 hours, raise an energy alert
114
+ if (batteryLevel < 20 &&
115
+ oldBatteryLevel?.data?.value < 20 &&
116
+ new Date(oldBatteryLevel?.data?.lastUpdated).getTime() <
117
+ Date.now() - 1000 * 60 * 60 * 8) {
118
+ //TODO: Raise an energy alert
119
+ await this.alertService.raiseEnergyAlert();
120
+ }
107
121
  }
108
122
  async getMetaData(deviceId) {
109
123
  return await axios_1.default.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
@@ -122,7 +136,7 @@ class DeviceService {
122
136
  async getDeviceByZone(zoneId) {
123
137
  try {
124
138
  //Step 1: Check if zone data available in redis
125
- //Step 2: If not available, fetch uaing smart-cloud-node zone api
139
+ //Step 2: If not available, fetch using postgresql db
126
140
  //Step 3: Store the zone data in redis
127
141
  //Step 4: Return the zone data
128
142
  //Step 5: If zone data is available in redis, return the zone data
@@ -135,7 +149,7 @@ class DeviceService {
135
149
  async getDevicesByAccessGroup(accessGroupId) {
136
150
  try {
137
151
  //Step 1: Check if access group data available in redis
138
- //Step 2: If not available, fetch using smart-cloud-node access-group api
152
+ //Step 2: If not available, fetch from postgresql db
139
153
  //Step 3: Store the access group data in redis
140
154
  //Step 4: Return the access group data
141
155
  //Step 5: If access group data is available in redis, return the access group data
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export { CloudDeviceService, CloudHubService, CloudConnectionService, } from "./device/cloud/services";
1
+ export { CloudDeviceService, ConnectionService as CloudConnectionService, } from "./device/cloud/services";
2
+ export { CloudDevice, DeviceFactory } from "./device/cloud/entities";
2
3
  export { LocalDeviceService, LocalHubService } from "./device/local/services";
3
- export * as cloudInterfaces from "./device/cloud/interfaces";
4
+ export * from "./device/cloud/interfaces";
4
5
  export * from "./device/cloud/types";
5
- export * as localInterfaces from "./device/local/interfaces";
6
+ export * from "./device/local/interfaces";
6
7
  export { initialize, getConfig } from "./config/config";
package/dist/index.js CHANGED
@@ -11,45 +11,25 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
11
11
  if (k2 === undefined) k2 = k;
12
12
  o[k2] = m[k];
13
13
  }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || (function () {
20
- var ownKeys = function(o) {
21
- ownKeys = Object.getOwnPropertyNames || function (o) {
22
- var ar = [];
23
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
- return ar;
25
- };
26
- return ownKeys(o);
27
- };
28
- return function (mod) {
29
- if (mod && mod.__esModule) return mod;
30
- var result = {};
31
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
- __setModuleDefault(result, mod);
33
- return result;
34
- };
35
- })();
36
14
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
37
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
38
16
  };
39
17
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.getConfig = exports.initialize = exports.localInterfaces = exports.cloudInterfaces = exports.LocalHubService = exports.LocalDeviceService = exports.CloudConnectionService = exports.CloudHubService = exports.CloudDeviceService = void 0;
18
+ exports.getConfig = exports.initialize = exports.LocalHubService = exports.LocalDeviceService = exports.DeviceFactory = exports.CloudDevice = exports.CloudConnectionService = exports.CloudDeviceService = void 0;
41
19
  // Cloud exports
42
20
  var services_1 = require("./device/cloud/services");
43
21
  Object.defineProperty(exports, "CloudDeviceService", { enumerable: true, get: function () { return services_1.CloudDeviceService; } });
44
- Object.defineProperty(exports, "CloudHubService", { enumerable: true, get: function () { return services_1.CloudHubService; } });
45
- Object.defineProperty(exports, "CloudConnectionService", { enumerable: true, get: function () { return services_1.CloudConnectionService; } });
22
+ Object.defineProperty(exports, "CloudConnectionService", { enumerable: true, get: function () { return services_1.ConnectionService; } });
23
+ var entities_1 = require("./device/cloud/entities");
24
+ Object.defineProperty(exports, "CloudDevice", { enumerable: true, get: function () { return entities_1.CloudDevice; } });
25
+ Object.defineProperty(exports, "DeviceFactory", { enumerable: true, get: function () { return entities_1.DeviceFactory; } });
46
26
  var services_2 = require("./device/local/services");
47
27
  Object.defineProperty(exports, "LocalDeviceService", { enumerable: true, get: function () { return services_2.LocalDeviceService; } });
48
28
  Object.defineProperty(exports, "LocalHubService", { enumerable: true, get: function () { return services_2.LocalHubService; } });
49
- exports.cloudInterfaces = __importStar(require("./device/cloud/interfaces"));
29
+ __exportStar(require("./device/cloud/interfaces"), exports);
50
30
  __exportStar(require("./device/cloud/types"), exports);
51
31
  // Local exports
52
- exports.localInterfaces = __importStar(require("./device/local/interfaces"));
32
+ __exportStar(require("./device/local/interfaces"), exports);
53
33
  //initialize export
54
34
  var config_1 = require("./config/config");
55
35
  Object.defineProperty(exports, "initialize", { enumerable: true, get: function () { return config_1.initialize; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -0,0 +1,19 @@
1
+ import { ICloudDevice } from "../interfaces/ICloudDevice";
2
+ import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
3
+ import { IConnection } from "../types";
4
+
5
+ export abstract class CloudDevice implements ICloudDevice {
6
+ deviceId: string;
7
+ connection: IConnection;
8
+ cloudDeviceService: ICloudDeviceService;
9
+
10
+ constructor(deviceId: string, cloudDeviceService: ICloudDeviceService) {
11
+ this.deviceId = deviceId;
12
+ this.cloudDeviceService = cloudDeviceService;
13
+ this.connection = this.getConnection(deviceId);
14
+ }
15
+
16
+ getConnection(deviceId: string): IConnection {
17
+ return this.cloudDeviceService.getConnection(deviceId);
18
+ }
19
+ }
@@ -0,0 +1,14 @@
1
+ import { IDevice } from "../../local/interfaces";
2
+ import { DeviceService } from "../../local/services/Device.service";
3
+
4
+ export class DeviceFactory {
5
+ private readonly localDeviceService: DeviceService;
6
+
7
+ constructor() {
8
+ this.localDeviceService = new DeviceService();
9
+ }
10
+
11
+ async getDevice(deviceId: string): Promise<IDevice> {
12
+ return await this.localDeviceService.getDevice(deviceId);
13
+ }
14
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./CloudDevice";
2
+ export * from "./DeviceFactory";
@@ -0,0 +1,7 @@
1
+ // CloudDevice interface
2
+ import { ICloudDeviceService } from "./ICloudDeviceService";
3
+
4
+ export interface ICloudDevice {
5
+ deviceId: string;
6
+ cloudDeviceService: ICloudDeviceService;
7
+ }
@@ -0,0 +1,6 @@
1
+ // Interface for CloudDeviceService
2
+ import { IConnection } from "../types";
3
+
4
+ export interface ICloudDeviceService {
5
+ getConnection(deviceId: string): IConnection;
6
+ }
@@ -1,3 +1,5 @@
1
1
  export * from "./IConnectionService";
2
2
  export * from "./IDeviceService";
3
3
  export * from "./IHubService";
4
+ export * from "./ICloudDevice";
5
+ export * from "./ICloudDeviceService";
@@ -0,0 +1,8 @@
1
+ import { ICloudDeviceService } from "../interfaces/ICloudDeviceService";
2
+ import { IConnection } from "../types";
3
+
4
+ export class CloudDeviceService implements ICloudDeviceService {
5
+ getConnection(deviceId: string): IConnection {
6
+ throw new Error("Method not implemented.");
7
+ }
8
+ }
@@ -1,5 +1,2 @@
1
- // Device Cloud Services - Export all service classes
2
-
3
- export { DeviceService as CloudDeviceService } from "./Device.service";
4
- export { ConnectionService as CloudConnectionService } from "./Connection.service";
5
- export { HubService as CloudHubService } from "./Hub.service";
1
+ export * from "./CloudDevice.service";
2
+ export * from "./Connection.service";
@@ -25,59 +25,6 @@ export interface IConnectionPagination {
25
25
  limit: number;
26
26
  }
27
27
 
28
- /**
29
- * Represents a device in the system.
30
- * All date fields must be ISO 8601 strings.
31
- */
32
- export interface IDevice {
33
- deviceId: string;
34
- propertyId: string;
35
- name: string;
36
- hubId: string[];
37
- specifications?: {
38
- manufacturer?: string;
39
- model?: string;
40
- firmware?: {
41
- version?: string;
42
- newVersionAvailable?: boolean;
43
- newVersion?: string;
44
- mandatoryUpdate?: boolean;
45
- };
46
- };
47
- protocol?: {
48
- location?: {
49
- id?: string;
50
- name?: string;
51
- city?: string;
52
- };
53
- name?: string;
54
- room?: {
55
- id?: string;
56
- name?: string;
57
- };
58
- accountId?: string;
59
- };
60
- deviceType: {
61
- id: string;
62
- type: string;
63
- };
64
- status: {
65
- online: boolean;
66
- error?: {
67
- type?: string;
68
- message?: string;
69
- };
70
- lastUpdated?: string;
71
- };
72
- state?: Record<string, any>;
73
- metaData?: Record<string, any>;
74
- createdAt?: Date;
75
- updatedAt?: Date;
76
- isDeleted?: boolean;
77
- hubDeviceDetails?: IDevice[];
78
- parentZones?: { id: string; name: string }[];
79
- zone?: { id: string; name: string };
80
- }
81
28
 
82
29
  /**
83
30
  * Device account response from provider.
@@ -10,11 +10,13 @@ import { eventDispatcher } from "dt-pub-sub";
10
10
  import { publishAudit } from "dt-audit-library";
11
11
  import { EventHandler } from "../handler/EventHandler";
12
12
  import { isEqual } from "lodash";
13
+ import { AlertService } from "./Alert.service";
13
14
 
14
15
  export class DeviceService {
15
16
  private readonly baseUrl: string;
16
17
  private readonly source = "dt-common-device";
17
18
  private readonly eventHandler: EventHandler;
19
+ private readonly alertService: AlertService;
18
20
 
19
21
  constructor() {
20
22
  const { DEVICE_SERVICE } = getConfig();
@@ -27,6 +29,7 @@ export class DeviceService {
27
29
  checkAwsEnv();
28
30
  ensureAuditInitialized();
29
31
  this.eventHandler = new EventHandler();
32
+ this.alertService = new AlertService();
30
33
  }
31
34
 
32
35
  async createDevice(body: IDevice): Promise<void> {
@@ -103,14 +106,11 @@ export class DeviceService {
103
106
  }
104
107
 
105
108
  async setState(deviceId: string, state: any) {
106
- // If old status and new status are different
109
+ // If old state and new state are different
107
110
  const oldState = await this.getState(deviceId);
108
- if (!isEqual(oldState, state)) {
109
- this.eventHandler.onStateChange(deviceId, state);
111
+ if (!isEqual(oldState?.data?.state, state)) {
112
+ return await this.eventHandler.onStateChange(deviceId, state);
110
113
  }
111
- // If offline, check the time difference between the last offline status and current time
112
- // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
113
- //deviceAlertService.raiseOperationalAlert();
114
114
  }
115
115
 
116
116
  async getStatus(deviceId: string) {
@@ -121,25 +121,43 @@ export class DeviceService {
121
121
  // If old status and new status are different
122
122
  const oldStatus = await this.getStatus(deviceId);
123
123
  if (!isEqual(oldStatus, status)) {
124
- this.eventHandler.onStatusChange(deviceId, status);
124
+ return await this.eventHandler.onStatusChange(deviceId, status);
125
+ }
126
+ // If both old and new status are offline, check the time difference between the last offline status and current time
127
+ if (
128
+ oldStatus?.data?.online === false &&
129
+ oldStatus?.data?.lastUpdated &&
130
+ status?.online === false
131
+ ) {
132
+ const lastOfflineTime = new Date(oldStatus?.data?.lastUpdated).getTime();
133
+ const currentTime = Date.now();
134
+ const timeDifference = currentTime - lastOfflineTime;
135
+ // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
136
+ if (timeDifference > 1000 * 60 * 60 * 24) {
137
+ return await this.alertService.raiseOperationsAlert();
138
+ }
125
139
  }
126
- // If offline, check the time difference between the last offline status and current time
127
- // Compare it with the baseline profile time of the device. If the difference is higher, then raise an operational alert
128
- //deviceAlertService.raiseOperationalAlert();
129
140
  }
130
141
 
131
142
  async getBatteryLevel(deviceId: string) {
132
143
  return await axios.get(`${this.baseUrl}/devices/${deviceId}/battery-level`);
133
144
  }
134
145
  async setBatteryLevel(deviceId: string, batteryLevel: number) {
135
- // If old status and new status are different
136
-
137
- // and the last battery level received time more than 8 hours {
138
- this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel);
139
- // if(batteryLevel < propertySettingBatteryThreshold) {
140
- // deviceAlertService.raiseEnergyAlert({});
141
- // }
142
- // }
146
+ // If old battery level and new battery level are different
147
+ const oldBatteryLevel = await this.getBatteryLevel(deviceId);
148
+ if (!isEqual(oldBatteryLevel?.data?.value, batteryLevel)) {
149
+ this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel);
150
+ }
151
+ // If current battery level is less than 20% and lsat updated time is more than 8 hours, raise an energy alert
152
+ if (
153
+ batteryLevel < 20 &&
154
+ oldBatteryLevel?.data?.value < 20 &&
155
+ new Date(oldBatteryLevel?.data?.lastUpdated).getTime() <
156
+ Date.now() - 1000 * 60 * 60 * 8
157
+ ) {
158
+ //TODO: Raise an energy alert
159
+ await this.alertService.raiseEnergyAlert();
160
+ }
143
161
  }
144
162
  async getMetaData(deviceId: string) {
145
163
  return await axios.get(`${this.baseUrl}/devices/${deviceId}/metaData`);
@@ -163,7 +181,7 @@ export class DeviceService {
163
181
  async getDeviceByZone(zoneId: string) {
164
182
  try {
165
183
  //Step 1: Check if zone data available in redis
166
- //Step 2: If not available, fetch uaing smart-cloud-node zone api
184
+ //Step 2: If not available, fetch using postgresql db
167
185
  //Step 3: Store the zone data in redis
168
186
  //Step 4: Return the zone data
169
187
  //Step 5: If zone data is available in redis, return the zone data
@@ -176,7 +194,7 @@ export class DeviceService {
176
194
  async getDevicesByAccessGroup(accessGroupId: string) {
177
195
  try {
178
196
  //Step 1: Check if access group data available in redis
179
- //Step 2: If not available, fetch using smart-cloud-node access-group api
197
+ //Step 2: If not available, fetch from postgresql db
180
198
  //Step 3: Store the access group data in redis
181
199
  //Step 4: Return the access group data
182
200
  //Step 5: If access group data is available in redis, return the access group data
package/src/index.ts CHANGED
@@ -3,16 +3,16 @@
3
3
  // Cloud exports
4
4
  export {
5
5
  CloudDeviceService,
6
- CloudHubService,
7
- CloudConnectionService,
6
+ ConnectionService as CloudConnectionService,
8
7
  } from "./device/cloud/services";
8
+ export { CloudDevice, DeviceFactory } from "./device/cloud/entities";
9
9
  export { LocalDeviceService, LocalHubService } from "./device/local/services";
10
10
 
11
- export * as cloudInterfaces from "./device/cloud/interfaces";
11
+ export * from "./device/cloud/interfaces";
12
12
  export * from "./device/cloud/types";
13
13
 
14
14
  // Local exports
15
- export * as localInterfaces from "./device/local/interfaces";
15
+ export * from "./device/local/interfaces";
16
16
 
17
17
  //initialize export
18
18
  export { initialize, getConfig } from "./config/config";
@@ -1,41 +0,0 @@
1
- import { IDeviceService } from "../interfaces";
2
- import { IConnection, IDevice } from "../types";
3
-
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
-
36
- abstract getDevices(connection: IConnection): Promise<Record<string, any>[]>;
37
- abstract getDevice(connectionId: string, deviceId: string): Promise<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>;
41
- }
@@ -1,34 +0,0 @@
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
- }