dt-common-device 7.10.11 → 8.0.0

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.
@@ -4,6 +4,7 @@ export declare function initialize(cfg: IConfig): Promise<void>;
4
4
  export declare function getConfig(): IConfig;
5
5
  export declare function getDeviceServiceUrl(): string;
6
6
  export declare function getAdminServiceUrl(): string;
7
+ export declare function getDeviceProfileServiceUrl(): string;
7
8
  export declare function getSqsQueueUrl(): string;
8
9
  export declare function getReservationSqsQueueUrl(): string;
9
10
  export declare function getEventSubscription(): InternalEventSubscription | null;
@@ -7,6 +7,7 @@ exports.initialize = initialize;
7
7
  exports.getConfig = getConfig;
8
8
  exports.getDeviceServiceUrl = getDeviceServiceUrl;
9
9
  exports.getAdminServiceUrl = getAdminServiceUrl;
10
+ exports.getDeviceProfileServiceUrl = getDeviceProfileServiceUrl;
10
11
  exports.getSqsQueueUrl = getSqsQueueUrl;
11
12
  exports.getReservationSqsQueueUrl = getReservationSqsQueueUrl;
12
13
  exports.getEventSubscription = getEventSubscription;
@@ -139,6 +140,17 @@ function getAdminServiceUrl() {
139
140
  }
140
141
  throw new Error("dt-common-device: ADMIN_SERVICE is not configured for this service");
141
142
  }
143
+ function getDeviceProfileServiceUrl() {
144
+ if (constants_1.CONFIG_KEYS[sourceKey].env.includes("MONITORING_SERVICE_PYTHON")) {
145
+ const deviceProfileServiceUrl = process.env.MONITORING_SERVICE_PYTHON;
146
+ if (!deviceProfileServiceUrl) {
147
+ getConfig().LOGGER.error("MONITORING_SERVICE_PYTHON must be set in environment variables");
148
+ throw new Error("dt-common-device: MONITORING_SERVICE_PYTHON must be set in environment variables");
149
+ }
150
+ return deviceProfileServiceUrl;
151
+ }
152
+ throw new Error("dt-common-device: DEVICE_PROFILE_SERVICE is not configured for this service");
153
+ }
142
154
  function getSqsQueueUrl() {
143
155
  if (constants_1.CONFIG_KEYS[sourceKey].env.includes("AWS_SQS_URL")) {
144
156
  const sqsQueueUrl = process.env.AWS_SQS_URL;
@@ -13,6 +13,7 @@ exports.REQUIRED = {
13
13
  "POSTHOG_HOST",
14
14
  "DEVICE_SERVICE",
15
15
  "DT_API_KEY",
16
+ "MONITORING_SERVICE_PYTHON",
16
17
  ],
17
18
  };
18
19
  exports.CONFIG_KEYS = {
@@ -194,7 +194,7 @@ let DeviceRepository = (() => {
194
194
  }
195
195
  async setBatteryLevel(deviceId, newBatteryLevel) {
196
196
  try {
197
- const response = await this.axiosInstance.put(`/devices/${deviceId}/battery-level`, { newBatteryLevel });
197
+ const response = await this.axiosInstance.put(`/devices/${deviceId}/battery-level`, { batteryLevel: newBatteryLevel });
198
198
  return response.data;
199
199
  }
200
200
  catch (error) {
@@ -49,7 +49,7 @@ let DeviceProfileRepository = (() => {
49
49
  let _classThis;
50
50
  var DeviceProfileRepository = _classThis = class {
51
51
  constructor() {
52
- this.axiosInstance = (0, utils_1.getDeviceServiceAxiosInstance)();
52
+ this.axiosInstance = (0, utils_1.getDeviceProfileServiceAxiosInstance)();
53
53
  }
54
54
  async getDeviceBaseLine(deviceId) {
55
55
  try {
@@ -4,9 +4,9 @@ import { IAuditProperties } from "../../../../audit/IAuditProperties";
4
4
  export declare class LocalDeviceService {
5
5
  private readonly eventHandler;
6
6
  private readonly deviceRepository;
7
- private readonly alertService;
8
7
  private readonly issueService;
9
8
  private readonly localPropertyService;
9
+ private readonly deviceProfileRepository;
10
10
  constructor();
11
11
  createDevice(body: IDevice): Promise<IDevice>;
12
12
  getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
@@ -75,7 +75,6 @@ exports.LocalDeviceService = void 0;
75
75
  const EventHandler_1 = require("../../../../events/EventHandler");
76
76
  const lodash_1 = require("lodash");
77
77
  const Device_repository_1 = require("../repository/Device.repository");
78
- const Alert_service_1 = require("../../../../alerts/Alert.service");
79
78
  const Issue_service_1 = require("../../../../issues/Issue.service");
80
79
  const Service_1 = require("../../../../constants/Service");
81
80
  const typedi_1 = __importStar(require("typedi"));
@@ -84,6 +83,7 @@ const constants_1 = require("../../../../constants");
84
83
  const audit_1 = require("../../../../audit");
85
84
  const issue_types_1 = require("../../../../issues/issue.types");
86
85
  const property_1 = require("../../../property");
86
+ const DeviceProfile_repository_1 = require("../repository/DeviceProfile.repository");
87
87
  let LocalDeviceService = (() => {
88
88
  let _classDecorators = [(0, typedi_1.Service)()];
89
89
  let _classDescriptor;
@@ -94,9 +94,9 @@ let LocalDeviceService = (() => {
94
94
  // Use dependency injection instead of creating new instances
95
95
  this.eventHandler = typedi_1.default.get(EventHandler_1.EventHandler);
96
96
  this.deviceRepository = typedi_1.default.get(Device_repository_1.DeviceRepository);
97
- this.alertService = typedi_1.default.get(Alert_service_1.AlertService);
98
97
  this.issueService = typedi_1.default.get(Issue_service_1.IssueService);
99
98
  this.localPropertyService = typedi_1.default.get(property_1.LocalPropertyService);
99
+ this.deviceProfileRepository = typedi_1.default.get(DeviceProfile_repository_1.DeviceProfileRepository);
100
100
  }
101
101
  async createDevice(body) {
102
102
  const device = await this.deviceRepository.createDevice(body);
@@ -136,11 +136,20 @@ let LocalDeviceService = (() => {
136
136
  else {
137
137
  await this.handleOfflineStatus(device, device.status, body.status, auditBody?.source || Service_1.Source.SYSTEM, auditBody, body.status.error?.message);
138
138
  }
139
- // delete body.status;
139
+ // delete the status from the body, so it doesn't get updated in the database again!
140
+ delete body.status;
140
141
  }
141
- if (body.state?.batteryPercentage?.value) {
142
+ if (body.state?.batteryPercentage?.value ||
143
+ body.state?.batteryPercentage?.value === 0) {
142
144
  await this.setBatteryLevel(deviceId, body.state?.batteryPercentage?.value, auditBody?.source || Service_1.Source.SYSTEM, auditBody);
143
- // delete body.state?.batteryPercentage;
145
+ // delete the battery percentage and battery state from the body, so it doesn't get updated in the database again!
146
+ delete body.state?.batteryPercentage;
147
+ delete body.state?.batteryState;
148
+ }
149
+ if (body.state?.lockState?.value) {
150
+ await this.setState(deviceId, { lockState: body.state.lockState }, auditBody);
151
+ // delete the lock state from the body, so it doesn't get updated in the database again!
152
+ delete body.state?.lockState;
144
153
  }
145
154
  await this.deviceRepository.updateDevice(deviceId, body);
146
155
  return await this.eventHandler.onDeviceUpdate(deviceId, body, auditBody);
@@ -206,7 +215,7 @@ let LocalDeviceService = (() => {
206
215
  }
207
216
  // If old state and new state are different
208
217
  const oldState = (await this.getState(deviceId))?.state ?? {};
209
- const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
218
+ const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key].value.toLowerCase(), newState[key].value.toLowerCase()));
210
219
  if (changedKeys.length > 0) {
211
220
  await this.deviceRepository.setState(deviceId, newState);
212
221
  return await this.eventHandler.onStateChange(deviceId, newState, auditProperties, eventType);
@@ -342,10 +351,9 @@ let LocalDeviceService = (() => {
342
351
  }
343
352
  }
344
353
  async getDeviceBaseline(deviceId) {
345
- // TODO: Implement device baseline retrieval
346
- // This should return the baseline time in milliseconds for the specific device
347
- // In a real implementation, this would fetch from device configuration or settings
348
- return 3600000; // 1 hour in milliseconds
354
+ const response = await this.deviceProfileRepository.getDeviceBaseLine(deviceId);
355
+ const baselineInMilliseconds = response?.baseline && response?.baseline * 60 * 1000;
356
+ return baselineInMilliseconds ?? 3600000; // 1 hour in milliseconds
349
357
  }
350
358
  async getBatteryLevel(deviceId) {
351
359
  if (!deviceId) {
@@ -379,7 +387,7 @@ let LocalDeviceService = (() => {
379
387
  lastUpdated: new Date().toISOString(),
380
388
  },
381
389
  };
382
- await this.updateBatteryState(deviceId, batteryState);
390
+ await this.setState(deviceId, batteryState, auditBody);
383
391
  await this.eventHandler.onBatteryReplaced(deviceId, batteryLevel, auditBody);
384
392
  // Cancel any existing battery issue
385
393
  await this.issueService.performIssueAction({
@@ -8,6 +8,7 @@ export declare function validateServiceUrl(url: string): boolean;
8
8
  export declare function createAxiosInstance(baseURL?: string): import("axios").AxiosInstance;
9
9
  export declare function getDeviceServiceAxiosInstance(): any;
10
10
  export declare function getAdminServiceAxiosInstance(): any;
11
+ export declare function getDeviceProfileServiceAxiosInstance(): any;
11
12
  /**
12
13
  * Retry function for failed HTTP requests
13
14
  */
@@ -7,6 +7,7 @@ exports.validateServiceUrl = validateServiceUrl;
7
7
  exports.createAxiosInstance = createAxiosInstance;
8
8
  exports.getDeviceServiceAxiosInstance = getDeviceServiceAxiosInstance;
9
9
  exports.getAdminServiceAxiosInstance = getAdminServiceAxiosInstance;
10
+ exports.getDeviceProfileServiceAxiosInstance = getDeviceProfileServiceAxiosInstance;
10
11
  exports.retryRequest = retryRequest;
11
12
  const config_1 = require("../config/config");
12
13
  const axios_1 = __importDefault(require("axios"));
@@ -97,6 +98,13 @@ function getAdminServiceAxiosInstance() {
97
98
  }
98
99
  return cloudServiceAxiosInstance;
99
100
  }
101
+ let deviceProfileServiceAxiosInstance = null;
102
+ function getDeviceProfileServiceAxiosInstance() {
103
+ if (!deviceProfileServiceAxiosInstance) {
104
+ deviceProfileServiceAxiosInstance = createAxiosInstance((0, config_1.getDeviceProfileServiceUrl)());
105
+ }
106
+ return deviceProfileServiceAxiosInstance;
107
+ }
100
108
  /**
101
109
  * Retry function for failed HTTP requests
102
110
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "7.10.11",
3
+ "version": "8.0.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [