dt-common-device 11.2.0 → 11.2.2
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/admin/Admin.repository.d.ts +7 -0
- package/dist/admin/Admin.repository.js +182 -0
- package/dist/admin/Admin.service.d.ts +5 -0
- package/dist/admin/Admin.service.js +17 -0
- package/dist/admin/index.d.ts +1 -0
- package/dist/admin/index.js +17 -0
- package/dist/audit/AuditProperties.d.ts +16 -0
- package/dist/audit/AuditProperties.js +2 -0
- package/dist/chronicle/Cronicle.service.d.ts +2 -6
- package/dist/chronicle/Cronicle.service.js +2 -19
- package/dist/chronicle/IChronicle.interface.d.ts +14 -0
- package/dist/chronicle/IChronicle.interface.js +2 -0
- package/dist/chronicle/ICronicle.interface.d.ts +0 -1
- package/dist/chronicle/chronicle.service.d.ts +4 -0
- package/dist/chronicle/chronicle.service.js +44 -0
- package/dist/config/config.js +5 -0
- package/dist/config/config.types.d.ts +1 -1
- package/dist/config/constants.d.ts +7 -0
- package/dist/config/constants.js +7 -2
- package/dist/connection/Connection.repository.d.ts +8 -0
- package/dist/connection/Connection.repository.js +92 -0
- package/dist/connection/Connection.service.d.ts +8 -0
- package/dist/connection/Connection.service.js +32 -0
- package/dist/connection/IConnection.d.ts +28 -0
- package/dist/connection/IConnection.js +16 -0
- package/dist/connection/index.d.ts +2 -0
- package/dist/connection/index.js +18 -0
- package/dist/constants/ConnectionProviders.d.ts +1 -0
- package/dist/constants/ConnectionProviders.js +1 -0
- package/dist/copilotQueue/examples/CopilotQueue.example.d.ts +37 -0
- package/dist/copilotQueue/examples/CopilotQueue.example.js +140 -0
- package/dist/copilotQueue/examples/index.d.ts +1 -0
- package/dist/copilotQueue/examples/index.js +17 -0
- 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.d.ts +16 -0
- package/dist/device/cloud/entities/CloudDevice.js +26 -0
- 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 +7 -0
- package/dist/device/cloud/entities/DeviceFactory.js +80 -0
- package/dist/device/cloud/entities/index.d.ts +3 -0
- package/dist/device/cloud/entities/index.js +19 -0
- package/dist/device/cloud/interface.d.ts +101 -0
- package/dist/device/cloud/interface.js +3 -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/ICloudDevice.d.ts +9 -0
- package/dist/device/cloud/interfaces/ICloudDevice.js +2 -0
- package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +4 -0
- package/dist/device/cloud/interfaces/ICloudDeviceService.js +2 -0
- package/dist/device/cloud/interfaces/IConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IConnectionService.js +2 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -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/IDeviceService.d.ts +8 -0
- package/dist/device/cloud/interfaces/IDeviceService.js +2 -0
- package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
- package/dist/device/cloud/interfaces/IDevicesService.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/IRawDataTransformer.d.ts +4 -0
- package/dist/device/cloud/interfaces/IRawDataTransformer.js +2 -0
- package/dist/device/cloud/interfaces/IRawDevice.d.ts +17 -0
- package/dist/device/cloud/interfaces/IRawDevice.js +11 -0
- package/dist/device/cloud/interfaces/index.d.ts +5 -0
- package/dist/device/cloud/interfaces/index.js +21 -0
- package/dist/device/cloud/services/CloudDevice.service.d.ts +5 -0
- package/dist/device/cloud/services/CloudDevice.service.js +9 -0
- package/dist/device/cloud/services/Connection.service.d.ts +8 -0
- package/dist/device/cloud/services/Connection.service.js +6 -0
- package/dist/device/cloud/services/Device.service.d.ts +39 -0
- package/dist/device/cloud/services/Device.service.js +9 -0
- package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
- package/dist/device/cloud/services/DeviceCloudService.js +59 -0
- package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
- package/dist/device/cloud/services/DeviceHub.service.js +6 -0
- package/dist/device/cloud/services/Hub.service.d.ts +25 -0
- package/dist/device/cloud/services/Hub.service.js +9 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
- package/dist/device/cloud/services/index.d.ts +2 -0
- package/dist/device/cloud/services/index.js +18 -0
- package/dist/device/cloud/types.d.ts +52 -0
- package/dist/device/cloud/types.js +15 -0
- package/dist/device/index.d.ts +4 -0
- package/dist/device/index.js +20 -0
- package/dist/device/local/entities/AlertBuilder.d.ts +87 -0
- package/dist/device/local/entities/AlertBuilder.example.d.ts +11 -0
- package/dist/device/local/entities/AlertBuilder.example.js +117 -0
- package/dist/device/local/entities/AlertBuilder.js +179 -0
- package/dist/device/local/entities/IssueBuilder.d.ts +109 -0
- package/dist/device/local/entities/IssueBuilder.example.d.ts +16 -0
- package/dist/device/local/entities/IssueBuilder.example.js +196 -0
- package/dist/device/local/entities/IssueBuilder.js +237 -0
- package/dist/device/local/entities/index.d.ts +2 -0
- package/dist/device/local/entities/index.js +7 -0
- package/dist/device/local/events/EventHandler.d.ts +11 -0
- package/dist/device/local/events/EventHandler.js +86 -0
- package/dist/device/local/events/Events.d.ts +54 -0
- package/dist/device/local/events/Events.js +57 -0
- package/dist/device/local/events/index.d.ts +2 -0
- package/dist/device/local/events/index.js +7 -0
- package/dist/device/local/handler/EventHandler.d.ts +7 -0
- package/dist/device/local/handler/EventHandler.js +44 -0
- package/dist/device/local/interface.d.ts +0 -0
- package/dist/device/local/interface.js +1 -0
- package/dist/device/local/interfaces/IConnection.d.ts +26 -0
- package/dist/device/local/interfaces/IConnection.js +14 -0
- package/dist/device/local/interfaces/IDevice.d.ts +68 -0
- package/dist/device/local/interfaces/IDevice.js +10 -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/IHub.d.ts +46 -0
- package/dist/device/local/interfaces/IHub.js +2 -0
- package/dist/device/local/interfaces/IProperty.d.ts +29 -0
- package/dist/device/local/interfaces/IProperty.js +2 -0
- package/dist/device/local/interfaces/ISchedule.d.ts +25 -0
- package/dist/device/local/interfaces/ISchedule.js +2 -0
- package/dist/device/local/interfaces/index.d.ts +3 -0
- package/dist/device/local/interfaces/index.js +19 -0
- package/dist/device/local/models/Alert.model.d.ts +28 -0
- package/dist/device/local/models/Alert.model.js +222 -0
- package/dist/device/local/models/Issue.model.d.ts +28 -0
- package/dist/device/local/models/Issue.model.js +260 -0
- package/dist/device/local/repository/Alert.repository.d.ts +106 -0
- package/dist/device/local/repository/Alert.repository.js +374 -0
- package/dist/device/local/repository/Connection.repository.d.ts +8 -0
- package/dist/device/local/repository/Connection.repository.js +92 -0
- package/dist/device/local/repository/Device.repository.d.ts +30 -0
- package/dist/device/local/repository/Device.repository.js +325 -0
- package/dist/device/local/repository/Hub.repository.d.ts +13 -0
- package/dist/device/local/repository/Hub.repository.js +139 -0
- package/dist/device/local/repository/Issue.repository.d.ts +113 -0
- package/dist/device/local/repository/Issue.repository.js +401 -0
- package/dist/device/local/repository/Property.repository.d.ts +8 -0
- package/dist/device/local/repository/Property.repository.js +95 -0
- package/dist/device/local/repository/Schedule.repository.d.ts +9 -0
- package/dist/device/local/repository/Schedule.repository.js +109 -0
- package/dist/device/local/services/Alert.service.d.ts +137 -0
- package/dist/device/local/services/Alert.service.js +475 -0
- package/dist/device/local/services/AlertService.example.d.ts +55 -0
- package/dist/device/local/services/AlertService.example.js +148 -0
- package/dist/device/local/services/Connection.service.d.ts +8 -0
- package/dist/device/local/services/Connection.service.js +32 -0
- package/dist/device/local/services/Device.service.d.ts +40 -0
- package/dist/device/local/services/Device.service.js +391 -0
- package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
- package/dist/device/local/services/DeviceHub.service.js +40 -0
- package/dist/device/local/services/Hub.service.d.ts +12 -0
- package/dist/device/local/services/Hub.service.js +107 -0
- package/dist/device/local/services/Issue.service.d.ts +168 -0
- package/dist/device/local/services/Issue.service.js +642 -0
- package/dist/device/local/services/IssueService.example.d.ts +68 -0
- package/dist/device/local/services/IssueService.example.js +177 -0
- package/dist/device/local/services/Property.service.d.ts +8 -0
- package/dist/device/local/services/Property.service.js +36 -0
- package/dist/device/local/services/Schedule.service.d.ts +9 -0
- package/dist/device/local/services/Schedule.service.js +26 -0
- package/dist/device/local/services/index.d.ts +3 -0
- package/dist/device/local/services/index.js +19 -0
- package/dist/entities/accessGroup/AccessGroup.repository.d.ts +1 -2
- package/dist/entities/accessGroup/AccessGroup.repository.js +1 -4
- package/dist/entities/accessGroup/AccessGroup.service.d.ts +1 -2
- package/dist/entities/accessGroup/AccessGroup.service.js +3 -4
- package/dist/entities/guest/Guest.repository.d.ts +1 -1
- package/dist/entities/guest/Guest.repository.js +2 -5
- package/dist/entities/guest/Guest.service.d.ts +1 -1
- package/dist/entities/guest/Guest.service.js +1 -4
- package/dist/entities/schedules/Schedule.repository.d.ts +1 -1
- package/dist/entities/schedules/Schedule.repository.js +2 -5
- package/dist/entities/schedules/Schedule.service.d.ts +1 -1
- package/dist/entities/schedules/Schedule.service.js +1 -4
- package/dist/entities/user/User.repository.js +1 -4
- package/dist/entities/user/User.service.d.ts +1 -1
- package/dist/entities/user/User.service.js +1 -4
- package/dist/entities/zone/Zone.service.js +1 -4
- package/dist/pms/IPms.d.ts +6 -0
- package/dist/pms/IPms.js +10 -0
- package/dist/pms/index.d.ts +1 -0
- package/dist/pms/index.js +18 -0
- package/dist/pms/webhookQueue/examples/index.d.ts +2 -0
- package/dist/pms/webhookQueue/examples/index.js +18 -0
- package/dist/pms/webhookQueue/examples/pms-integration.d.ts +65 -0
- package/dist/pms/webhookQueue/examples/pms-integration.js +254 -0
- package/dist/pms/webhookQueue/examples/usage.d.ts +7 -0
- package/dist/pms/webhookQueue/examples/usage.js +175 -0
- package/dist/pms/webhookQueue/index.d.ts +3 -0
- package/dist/pms/webhookQueue/index.js +20 -0
- package/dist/pms/webhookQueue/interfaces/IWebhookQueue.d.ts +33 -0
- package/dist/pms/webhookQueue/interfaces/IWebhookQueue.js +2 -0
- package/dist/pms/webhookQueue/interfaces/IWebhookWorker.d.ts +38 -0
- package/dist/pms/webhookQueue/interfaces/IWebhookWorker.js +2 -0
- package/dist/pms/webhookQueue/interfaces/index.d.ts +1 -0
- package/dist/pms/webhookQueue/interfaces/index.js +17 -0
- package/dist/pms/webhookQueue/services/WebhookQueueFactory.d.ts +38 -0
- package/dist/pms/webhookQueue/services/WebhookQueueFactory.js +131 -0
- package/dist/pms/webhookQueue/services/WebhookQueueIntegration.d.ts +70 -0
- package/dist/pms/webhookQueue/services/WebhookQueueIntegration.js +207 -0
- package/dist/pms/webhookQueue/services/WebhookQueueService.d.ts +45 -0
- package/dist/pms/webhookQueue/services/WebhookQueueService.js +270 -0
- package/dist/pms/webhookQueue/services/WebhookWorker.d.ts +37 -0
- package/dist/pms/webhookQueue/services/WebhookWorker.js +201 -0
- package/dist/pms/webhookQueue/services/index.d.ts +1 -0
- package/dist/pms/webhookQueue/services/index.js +17 -0
- package/dist/pms/webhookQueue/types/index.d.ts +1 -0
- package/dist/pms/webhookQueue/types/index.js +17 -0
- package/dist/pms/webhookQueue/types/webhook.types.d.ts +39 -0
- package/dist/pms/webhookQueue/types/webhook.types.js +2 -0
- package/dist/property/IProperty.d.ts +29 -0
- package/dist/property/IProperty.js +2 -0
- package/dist/property/Property.repository.d.ts +8 -0
- package/dist/property/Property.repository.js +109 -0
- package/dist/property/Property.service.d.ts +8 -0
- package/dist/property/Property.service.js +124 -0
- package/dist/property/index.d.ts +2 -0
- package/dist/property/index.js +18 -0
- package/dist/queue/interfaces/IHttpRequestJob.d.ts +9 -0
- package/dist/queue/interfaces/IHttpRequestJob.js +2 -0
- package/dist/queue/utils/rateLimit.utils.js +6 -0
- package/dist/types/alert.types.d.ts +57 -0
- package/dist/types/alert.types.js +22 -0
- package/dist/types/config.types.d.ts +19 -0
- package/dist/types/config.types.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/issue.types.d.ts +90 -0
- package/dist/types/issue.types.js +40 -0
- package/dist/utils/http-utils.d.ts +13 -0
- package/dist/utils/http-utils.js +117 -0
- package/package.json +1 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AlertServiceExample = void 0;
|
|
4
|
+
const AlertBuilder_1 = require("../entities/AlertBuilder");
|
|
5
|
+
const alert_types_1 = require("../../../types/alert.types");
|
|
6
|
+
/**
|
|
7
|
+
* Example usage of the updated AlertService with AlertBuilder integration
|
|
8
|
+
* This file demonstrates various ways to use the AlertService with the new AlertBuilder
|
|
9
|
+
*/
|
|
10
|
+
class AlertServiceExample {
|
|
11
|
+
constructor(alertService) {
|
|
12
|
+
this.alertService = alertService;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Example 1: Using the updated createAlert method with AlertBuilder
|
|
16
|
+
*/
|
|
17
|
+
async createAlertWithBuilder() {
|
|
18
|
+
const alertBuilder = new AlertBuilder_1.AlertBuilder()
|
|
19
|
+
.setCategory(alert_types_1.AlertCategory.OPERATIONS)
|
|
20
|
+
.setPropertyId("prop123")
|
|
21
|
+
.setTitle("Device Offline")
|
|
22
|
+
.setDescription("Device has been offline for more than 5 minutes")
|
|
23
|
+
.setEntityId("device456")
|
|
24
|
+
.setEntityType(alert_types_1.EntityType.DEVICE)
|
|
25
|
+
.setSeverity(alert_types_1.AlertSeverity.HIGH)
|
|
26
|
+
.setCreatedBy("user789");
|
|
27
|
+
// Pass the AlertBuilder directly to createAlert
|
|
28
|
+
const alert = await this.alertService.createAlert(alertBuilder);
|
|
29
|
+
return alert;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Example 2: Using convenience methods for specific alert types
|
|
33
|
+
*/
|
|
34
|
+
async createSpecificAlerts() {
|
|
35
|
+
// Create a readiness alert
|
|
36
|
+
const readinessAlert = await this.alertService.raiseReadinessAlert("prop123", "System Maintenance Required", "System maintenance is scheduled for tonight", "system456", alert_types_1.EntityType.PROPERTY, "admin");
|
|
37
|
+
// Create an operations alert
|
|
38
|
+
const operationsAlert = await this.alertService.raiseOperationsAlert("prop123", "Device Temperature High", "Device temperature exceeds normal operating range", "device789", alert_types_1.EntityType.DEVICE, "monitor");
|
|
39
|
+
// Create a security alert
|
|
40
|
+
const securityAlert = await this.alertService.raiseSecurityAlert("prop123", "Unauthorized Access Attempt", "Multiple failed login attempts detected", "user123", alert_types_1.EntityType.USER, "security-system");
|
|
41
|
+
// Create an energy alert
|
|
42
|
+
const energyAlert = await this.alertService.raiseEnergyAlert("prop123", "High Energy Consumption", "Energy usage is 20% above normal levels", "zone456", alert_types_1.EntityType.COLLECTION, "energy-monitor");
|
|
43
|
+
return { readinessAlert, operationsAlert, securityAlert, energyAlert };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Example 3: Using device-specific alert methods
|
|
47
|
+
*/
|
|
48
|
+
async createDeviceAlerts() {
|
|
49
|
+
// Create a device alert with default category and severity
|
|
50
|
+
const deviceAlert1 = await this.alertService.raiseDeviceAlert("device123", "prop456", "Device Battery Low", "Device battery level is below 20%", undefined, // Use default category
|
|
51
|
+
undefined, // Use default severity
|
|
52
|
+
"system");
|
|
53
|
+
// Create a device alert with custom category and severity
|
|
54
|
+
const deviceAlert2 = await this.alertService.raiseDeviceAlert("device789", "prop456", "Device Firmware Update Available", "New firmware version is available for installation", alert_types_1.AlertCategory.READINESS, alert_types_1.AlertSeverity.MEDIUM, "firmware-manager");
|
|
55
|
+
return { deviceAlert1, deviceAlert2 };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Example 4: Using hub-specific alert methods
|
|
59
|
+
*/
|
|
60
|
+
async createHubAlerts() {
|
|
61
|
+
// Create a hub alert with default settings
|
|
62
|
+
const hubAlert1 = await this.alertService.raiseHubAlert("hub123", "prop456", "Hub Connection Lost", "Hub has lost connection to the network", undefined, // Use default category
|
|
63
|
+
undefined, // Use default severity
|
|
64
|
+
"network-monitor");
|
|
65
|
+
// Create a hub alert with custom settings
|
|
66
|
+
const hubAlert2 = await this.alertService.raiseHubAlert("hub789", "prop456", "Hub Maintenance Required", "Hub requires scheduled maintenance", alert_types_1.AlertCategory.READINESS, alert_types_1.AlertSeverity.LOW, "maintenance-system");
|
|
67
|
+
return { hubAlert1, hubAlert2 };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Example 5: Using static factory methods with AlertBuilder
|
|
71
|
+
*/
|
|
72
|
+
async createAlertsWithStaticMethods() {
|
|
73
|
+
// Create a device alert using static factory method
|
|
74
|
+
const deviceAlert = AlertBuilder_1.AlertBuilder.createDeviceAlert("device123", "prop456")
|
|
75
|
+
.setCategory(alert_types_1.AlertCategory.OPERATIONS)
|
|
76
|
+
.setTitle("Device Offline")
|
|
77
|
+
.setDescription("Device has been offline for more than 5 minutes")
|
|
78
|
+
.setSeverity(alert_types_1.AlertSeverity.HIGH)
|
|
79
|
+
.setCreatedBy("monitor");
|
|
80
|
+
const alert1 = await this.alertService.createAlert(deviceAlert);
|
|
81
|
+
// Create a hub alert using static factory method
|
|
82
|
+
const hubAlert = AlertBuilder_1.AlertBuilder.createHubAlert("hub789", "prop202")
|
|
83
|
+
.setCategory(alert_types_1.AlertCategory.SECURITY)
|
|
84
|
+
.setTitle("Hub Security Breach")
|
|
85
|
+
.setDescription("Unauthorized access attempt detected on hub")
|
|
86
|
+
.setSeverity(alert_types_1.AlertSeverity.CRITICAL)
|
|
87
|
+
.setCreatedBy("security-system");
|
|
88
|
+
const alert2 = await this.alertService.createAlert(hubAlert);
|
|
89
|
+
return { alert1, alert2 };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Example 6: Creating multiple alerts efficiently
|
|
93
|
+
*/
|
|
94
|
+
async createMultipleAlerts() {
|
|
95
|
+
const alerts = [];
|
|
96
|
+
// Create multiple device alerts efficiently
|
|
97
|
+
const deviceIds = ["device1", "device2", "device3"];
|
|
98
|
+
const propertyId = "prop123";
|
|
99
|
+
for (const deviceId of deviceIds) {
|
|
100
|
+
const alertBuilder = AlertBuilder_1.AlertBuilder.createDeviceAlert(deviceId, propertyId)
|
|
101
|
+
.setCategory(alert_types_1.AlertCategory.OPERATIONS)
|
|
102
|
+
.setTitle(`Device ${deviceId} Status`)
|
|
103
|
+
.setDescription(`Status check for device ${deviceId}`)
|
|
104
|
+
.setSeverity(alert_types_1.AlertSeverity.MEDIUM)
|
|
105
|
+
.setCreatedBy("batch-processor");
|
|
106
|
+
const alert = await this.alertService.createAlert(alertBuilder);
|
|
107
|
+
alerts.push(alert);
|
|
108
|
+
}
|
|
109
|
+
return alerts;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Example 7: Creating alerts with snooze functionality
|
|
113
|
+
*/
|
|
114
|
+
async createSnoozedAlert() {
|
|
115
|
+
const snoozeUntil = new Date();
|
|
116
|
+
snoozeUntil.setHours(snoozeUntil.getHours() + 2); // Snooze for 2 hours
|
|
117
|
+
const alertBuilder = new AlertBuilder_1.AlertBuilder()
|
|
118
|
+
.setCategory(alert_types_1.AlertCategory.OTHER)
|
|
119
|
+
.setPropertyId("prop303")
|
|
120
|
+
.setTitle("Scheduled Maintenance")
|
|
121
|
+
.setDescription("System maintenance scheduled for tonight")
|
|
122
|
+
.setEntityType(alert_types_1.EntityType.PROPERTY)
|
|
123
|
+
.setSeverity(alert_types_1.AlertSeverity.INFO)
|
|
124
|
+
.setCreatedBy("admin")
|
|
125
|
+
.setSnoozeUntil(snoozeUntil);
|
|
126
|
+
const alert = await this.alertService.createAlert(alertBuilder);
|
|
127
|
+
return alert;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Example 8: Backward compatibility - still works with CreateAlertData
|
|
131
|
+
*/
|
|
132
|
+
async createAlertWithLegacyData() {
|
|
133
|
+
const alertData = {
|
|
134
|
+
category: alert_types_1.AlertCategory.OPERATIONS,
|
|
135
|
+
propertyId: "prop123",
|
|
136
|
+
title: "Legacy Alert",
|
|
137
|
+
description: "This alert was created using the old CreateAlertData format",
|
|
138
|
+
entityId: "device456",
|
|
139
|
+
entityType: alert_types_1.EntityType.DEVICE,
|
|
140
|
+
severity: alert_types_1.AlertSeverity.MEDIUM,
|
|
141
|
+
createdBy: "legacy-system"
|
|
142
|
+
};
|
|
143
|
+
// This still works with the updated createAlert method
|
|
144
|
+
const alert = await this.alertService.createAlert(alertData);
|
|
145
|
+
return alert;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.AlertServiceExample = AlertServiceExample;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IConnection } from "../interfaces/IConnection";
|
|
2
|
+
export declare class LocalConnectionService {
|
|
3
|
+
private readonly connectionRepository;
|
|
4
|
+
constructor();
|
|
5
|
+
createConnection(data: Partial<IConnection>): Promise<IConnection>;
|
|
6
|
+
getConnection(connectionId: string): Promise<IConnection>;
|
|
7
|
+
updateConnection(connectionId: string, data: Partial<IConnection>): Promise<IConnection>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalConnectionService = void 0;
|
|
4
|
+
const typedi_1 = require("typedi");
|
|
5
|
+
const Connection_repository_1 = require("../repository/Connection.repository");
|
|
6
|
+
class LocalConnectionService {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.connectionRepository = typedi_1.Container.get(Connection_repository_1.ConnectionRepository);
|
|
9
|
+
}
|
|
10
|
+
async createConnection(data) {
|
|
11
|
+
if (!data.connectionName ||
|
|
12
|
+
!data.connectionRefId ||
|
|
13
|
+
!data.propertyId ||
|
|
14
|
+
!data.connectionProvider) {
|
|
15
|
+
throw new Error("Missing required fields");
|
|
16
|
+
}
|
|
17
|
+
return await this.connectionRepository.createConnection(data);
|
|
18
|
+
}
|
|
19
|
+
async getConnection(connectionId) {
|
|
20
|
+
if (!connectionId) {
|
|
21
|
+
throw new Error("Connection ID is required");
|
|
22
|
+
}
|
|
23
|
+
return await this.connectionRepository.getConnectionById(connectionId);
|
|
24
|
+
}
|
|
25
|
+
async updateConnection(connectionId, data) {
|
|
26
|
+
if (!connectionId) {
|
|
27
|
+
throw new Error("Connection ID is required");
|
|
28
|
+
}
|
|
29
|
+
return await this.connectionRepository.updateConnection(connectionId, data);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.LocalConnectionService = LocalConnectionService;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IDevice, IStatus, IStatusQuery } from "../interfaces";
|
|
2
|
+
import { Source } from "../../../constants/Service";
|
|
3
|
+
import { AuditProperties } from "../../../audit/AuditProperties";
|
|
4
|
+
export declare class LocalDeviceService {
|
|
5
|
+
private readonly eventHandler;
|
|
6
|
+
private readonly deviceRepository;
|
|
7
|
+
private readonly alertService;
|
|
8
|
+
private readonly issueService;
|
|
9
|
+
private readonly logger;
|
|
10
|
+
constructor();
|
|
11
|
+
createDevice(body: IDevice): Promise<IDevice>;
|
|
12
|
+
getDevice(deviceId: string, withHubDetails?: boolean): Promise<IDevice>;
|
|
13
|
+
getDevices(deviceIds: string[], withHubDetails?: boolean): Promise<IDevice[]>;
|
|
14
|
+
getPropertyDevices(propertyId: string, selectDeviceId?: boolean, type?: string, withHubDetails?: boolean): Promise<IDevice[]>;
|
|
15
|
+
getPropertyDeviceIds(propertyId: string, selectDeviceId: boolean | undefined, type: string): Promise<any>;
|
|
16
|
+
updateDevice(deviceId: string, body: any, auditBody: AuditProperties): Promise<any>;
|
|
17
|
+
updateDevices(query: any, updateData: any): Promise<any>;
|
|
18
|
+
deleteDevice(deviceId: string, auditBody: AuditProperties): Promise<any>;
|
|
19
|
+
getState(deviceId: string): Promise<any>;
|
|
20
|
+
setState(deviceId: string, newState: any, auditProperties: AuditProperties): Promise<void>;
|
|
21
|
+
getStatus(deviceId: string): Promise<Record<string, any>>;
|
|
22
|
+
setStatus(deviceId: string, newStatus: IStatus, source: Source, auditBody: AuditProperties, reason?: string): Promise<void>;
|
|
23
|
+
setStatusMany(query: IStatusQuery, newStatus: IStatus, source: Source, auditBody: AuditProperties): Promise<void>;
|
|
24
|
+
private handleOfflineStatus;
|
|
25
|
+
private handleOnlineStatus;
|
|
26
|
+
private getDeviceBaseline;
|
|
27
|
+
getBatteryLevel(deviceId: string): Promise<Record<string, any>>;
|
|
28
|
+
setBatteryLevel(deviceId: string, batteryLevel: number, source: Source, auditBody: AuditProperties): Promise<void>;
|
|
29
|
+
private shouldUpdateBatteryLevel;
|
|
30
|
+
private getPropertyBatteryThreshold;
|
|
31
|
+
private checkForDeviceMalfunctions;
|
|
32
|
+
getMetaData(deviceId: string): Promise<any>;
|
|
33
|
+
setMetaData(deviceId: string, metaData: Record<string, any>, auditBody: AuditProperties): Promise<any>;
|
|
34
|
+
getDevicesByZone(zoneId: string): Promise<import("../interfaces").IDtDevice[]>;
|
|
35
|
+
getDevicesByAccessGroup(accessGroupId: string): Promise<import("../interfaces").IDtDevice[]>;
|
|
36
|
+
querySelect(query: any, fields: string[]): Promise<any>;
|
|
37
|
+
queryCount(query: any): Promise<number>;
|
|
38
|
+
deleteDevices(query: any): Promise<any>;
|
|
39
|
+
queryDevices(query: any): Promise<any>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,391 @@
|
|
|
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.LocalDeviceService = void 0;
|
|
45
|
+
const EventHandler_1 = require("../../../events/EventHandler");
|
|
46
|
+
const lodash_1 = require("lodash");
|
|
47
|
+
const Device_repository_1 = require("../repository/Device.repository");
|
|
48
|
+
const typedi_1 = __importDefault(require("typedi"));
|
|
49
|
+
const Alert_service_1 = require("../../../alerts/Alert.service");
|
|
50
|
+
const Issue_service_1 = require("../../../issues/Issue.service");
|
|
51
|
+
const config_1 = require("../../../config/config");
|
|
52
|
+
const typedi_2 = require("typedi");
|
|
53
|
+
let LocalDeviceService = (() => {
|
|
54
|
+
let _classDecorators = [(0, typedi_2.Service)()];
|
|
55
|
+
let _classDescriptor;
|
|
56
|
+
let _classExtraInitializers = [];
|
|
57
|
+
let _classThis;
|
|
58
|
+
var LocalDeviceService = _classThis = class {
|
|
59
|
+
constructor() {
|
|
60
|
+
// Use dependency injection instead of creating new instances
|
|
61
|
+
this.eventHandler = typedi_1.default.get(EventHandler_1.EventHandler);
|
|
62
|
+
this.deviceRepository = typedi_1.default.get(Device_repository_1.DeviceRepository);
|
|
63
|
+
this.alertService = typedi_1.default.get(Alert_service_1.AlertService);
|
|
64
|
+
this.issueService = typedi_1.default.get(Issue_service_1.IssueService);
|
|
65
|
+
this.logger = (0, config_1.getConfig)().LOGGER;
|
|
66
|
+
}
|
|
67
|
+
async createDevice(body) {
|
|
68
|
+
const device = await this.deviceRepository.createDevice(body);
|
|
69
|
+
await this.eventHandler.onDeviceCreate(device);
|
|
70
|
+
return device;
|
|
71
|
+
}
|
|
72
|
+
async getDevice(deviceId, withHubDetails = false) {
|
|
73
|
+
if (!deviceId) {
|
|
74
|
+
throw new Error("Device ID is required");
|
|
75
|
+
}
|
|
76
|
+
return await this.deviceRepository.getDevice(deviceId, withHubDetails);
|
|
77
|
+
}
|
|
78
|
+
async getDevices(deviceIds, withHubDetails = false) {
|
|
79
|
+
if (!deviceIds.length) {
|
|
80
|
+
throw new Error("At least one device ID is required");
|
|
81
|
+
}
|
|
82
|
+
return await this.deviceRepository.getDevices(deviceIds, withHubDetails);
|
|
83
|
+
}
|
|
84
|
+
async getPropertyDevices(propertyId, selectDeviceId = false, type, withHubDetails = false) {
|
|
85
|
+
if (!propertyId) {
|
|
86
|
+
throw new Error("Property ID is required");
|
|
87
|
+
}
|
|
88
|
+
return await this.deviceRepository.getPropertyDevices(propertyId, selectDeviceId, type, withHubDetails);
|
|
89
|
+
}
|
|
90
|
+
async getPropertyDeviceIds(propertyId, selectDeviceId = false, type) {
|
|
91
|
+
if (!propertyId) {
|
|
92
|
+
throw new Error("Property ID is required");
|
|
93
|
+
}
|
|
94
|
+
return await this.deviceRepository.getPropertyDeviceIds(propertyId, selectDeviceId, type);
|
|
95
|
+
}
|
|
96
|
+
async updateDevice(deviceId, body, auditBody) {
|
|
97
|
+
if (!deviceId) {
|
|
98
|
+
throw new Error("Device ID is required");
|
|
99
|
+
}
|
|
100
|
+
await this.deviceRepository.updateDevice(deviceId, body);
|
|
101
|
+
return await this.eventHandler.onDeviceUpdate(deviceId, body, auditBody);
|
|
102
|
+
}
|
|
103
|
+
async updateDevices(query, updateData) {
|
|
104
|
+
if (!query || !updateData) {
|
|
105
|
+
throw new Error("Query and update data are required");
|
|
106
|
+
}
|
|
107
|
+
return await this.deviceRepository.updateDevices(query, updateData);
|
|
108
|
+
}
|
|
109
|
+
async deleteDevice(deviceId, auditBody) {
|
|
110
|
+
if (!deviceId) {
|
|
111
|
+
throw new Error("Device ID is required");
|
|
112
|
+
}
|
|
113
|
+
await this.deviceRepository.deleteDevice(deviceId);
|
|
114
|
+
return await this.eventHandler.onDeviceDelete(deviceId, auditBody);
|
|
115
|
+
}
|
|
116
|
+
async getState(deviceId) {
|
|
117
|
+
if (!deviceId) {
|
|
118
|
+
throw new Error("Device ID is required");
|
|
119
|
+
}
|
|
120
|
+
return await this.deviceRepository.getState(deviceId);
|
|
121
|
+
}
|
|
122
|
+
async setState(deviceId, newState, auditProperties) {
|
|
123
|
+
if (!deviceId || !newState) {
|
|
124
|
+
throw new Error("Device ID and new state are required");
|
|
125
|
+
}
|
|
126
|
+
// If old state and new state are different
|
|
127
|
+
const oldState = (await this.getState(deviceId))?.state ?? {};
|
|
128
|
+
const changedKeys = Object.keys(newState).filter((key) => !(0, lodash_1.isEqual)(oldState[key], newState[key]));
|
|
129
|
+
if (changedKeys.length > 0) {
|
|
130
|
+
await this.deviceRepository.setState(deviceId, newState);
|
|
131
|
+
return await this.eventHandler.onStateChange(deviceId, newState, auditProperties);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async getStatus(deviceId) {
|
|
135
|
+
if (!deviceId) {
|
|
136
|
+
throw new Error("Device ID is required");
|
|
137
|
+
}
|
|
138
|
+
return await this.deviceRepository.getStatus(deviceId);
|
|
139
|
+
}
|
|
140
|
+
async setStatus(deviceId, newStatus, source, auditBody, reason) {
|
|
141
|
+
if (!deviceId || !newStatus) {
|
|
142
|
+
throw new Error("Device ID and new status are required");
|
|
143
|
+
}
|
|
144
|
+
const device = await this.getDevice(deviceId);
|
|
145
|
+
const oldStatus = device.status;
|
|
146
|
+
const currentTime = new Date().toISOString();
|
|
147
|
+
// Determine if the new status is ONLINE or OFFLINE
|
|
148
|
+
const isNewStatusOnline = newStatus.liveStatus === "ONLINE";
|
|
149
|
+
const isNewStatusOffline = newStatus.liveStatus === "OFFLINE";
|
|
150
|
+
if (isNewStatusOffline) {
|
|
151
|
+
// New Status = OFFLINE
|
|
152
|
+
await this.handleOfflineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime);
|
|
153
|
+
}
|
|
154
|
+
else if (isNewStatusOnline) {
|
|
155
|
+
// New Status = ONLINE
|
|
156
|
+
await this.handleOnlineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
// For any other status, just update normally
|
|
160
|
+
await this.deviceRepository.setStatus(deviceId, newStatus);
|
|
161
|
+
await this.eventHandler.onStatusChange(deviceId, newStatus, auditBody);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async setStatusMany(query, newStatus, source, auditBody) {
|
|
165
|
+
if (!query || !newStatus) {
|
|
166
|
+
throw new Error("Query and new status are required");
|
|
167
|
+
}
|
|
168
|
+
newStatus.lastUpdated = new Date().toISOString();
|
|
169
|
+
await this.deviceRepository.setStatusMany(query, newStatus);
|
|
170
|
+
await this.eventHandler.onStatusChangeMany(query, newStatus, auditBody);
|
|
171
|
+
}
|
|
172
|
+
async handleOfflineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime) {
|
|
173
|
+
const isExistingStatusOnline = oldStatus?.online === true;
|
|
174
|
+
const isExistingStatusOffline = oldStatus?.online === false;
|
|
175
|
+
if (isExistingStatusOnline) {
|
|
176
|
+
// Existing status is Online
|
|
177
|
+
newStatus.online = true;
|
|
178
|
+
newStatus.liveStatus = "OFFLINE";
|
|
179
|
+
newStatus.lastUpdated = currentTime;
|
|
180
|
+
newStatus.error = {
|
|
181
|
+
type: "offline",
|
|
182
|
+
message: reason || "Device went offline",
|
|
183
|
+
default: {},
|
|
184
|
+
};
|
|
185
|
+
await this.deviceRepository.setStatus(device.deviceId, newStatus);
|
|
186
|
+
await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
|
|
187
|
+
}
|
|
188
|
+
else if (isExistingStatusOffline) {
|
|
189
|
+
// Existing status is Offline
|
|
190
|
+
const timeLapsed = oldStatus?.lastUpdated
|
|
191
|
+
? Date.now() - new Date(oldStatus.lastUpdated).getTime()
|
|
192
|
+
: 0;
|
|
193
|
+
const baselineTime = await this.getDeviceBaseline(device.deviceId);
|
|
194
|
+
if (timeLapsed > baselineTime) {
|
|
195
|
+
// When the time lapsed is higher than the baseline time
|
|
196
|
+
newStatus.online = false;
|
|
197
|
+
newStatus.liveStatus = "OFFLINE";
|
|
198
|
+
newStatus.lastUpdated = currentTime;
|
|
199
|
+
newStatus.error = {
|
|
200
|
+
type: "offline",
|
|
201
|
+
message: reason || "Device has been offline for longer than baseline",
|
|
202
|
+
default: {},
|
|
203
|
+
};
|
|
204
|
+
await this.deviceRepository.setStatus(device.deviceId, newStatus);
|
|
205
|
+
await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
|
|
206
|
+
// Raise alert (OPERATIONAL only)
|
|
207
|
+
await this.alertService.raiseDeviceOfflineAlert(device, source, reason);
|
|
208
|
+
// Raise issue when the device goes offline if longer than the baseline (OPERATIONAL only)
|
|
209
|
+
await this.issueService.createDeviceOfflineIssue(device, source, reason);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async handleOnlineStatus(device, oldStatus, newStatus, source, auditBody, reason, currentTime) {
|
|
214
|
+
const isExistingStatusOnline = oldStatus?.online === true;
|
|
215
|
+
const isExistingStatusOffline = oldStatus?.online === false;
|
|
216
|
+
if (isExistingStatusOnline) {
|
|
217
|
+
// Existing status is Online
|
|
218
|
+
if (oldStatus?.liveStatus === "ONLINE") {
|
|
219
|
+
// liveStatus = Online → Do nothing
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
else if (oldStatus?.liveStatus === "OFFLINE") {
|
|
223
|
+
// liveStatus = Offline → Follow the step #2 (same as existing status is Offline)
|
|
224
|
+
newStatus.online = true;
|
|
225
|
+
newStatus.liveStatus = "ONLINE";
|
|
226
|
+
newStatus.lastUpdated = currentTime;
|
|
227
|
+
newStatus.error = {}; // Clear the error
|
|
228
|
+
await this.deviceRepository.setStatus(device.deviceId, newStatus);
|
|
229
|
+
await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
|
|
230
|
+
//TODO: ALERT NEEDED?
|
|
231
|
+
// Raise alert
|
|
232
|
+
await this.alertService.raiseDeviceOnlineAlert(device, source, reason);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else if (isExistingStatusOffline) {
|
|
236
|
+
// Existing status is Offline
|
|
237
|
+
newStatus.online = true;
|
|
238
|
+
newStatus.liveStatus = "ONLINE";
|
|
239
|
+
newStatus.lastUpdated = currentTime;
|
|
240
|
+
newStatus.error = undefined; // Clear the error
|
|
241
|
+
await this.deviceRepository.setStatus(device.deviceId, newStatus);
|
|
242
|
+
await this.eventHandler.onStatusChange(device.deviceId, newStatus, auditBody);
|
|
243
|
+
//TODO: ALERT NEEDED?
|
|
244
|
+
// Raise alert
|
|
245
|
+
await this.alertService.raiseDeviceOnlineAlert(device, source, reason);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async getDeviceBaseline(deviceId) {
|
|
249
|
+
// TODO: Implement device baseline retrieval
|
|
250
|
+
// This should return the baseline time in milliseconds for the specific device
|
|
251
|
+
// For now, returning a default value of 5 minutes (300000ms)
|
|
252
|
+
// In a real implementation, this would fetch from device configuration or settings
|
|
253
|
+
return 3600000; // 1 hour in milliseconds
|
|
254
|
+
}
|
|
255
|
+
async getBatteryLevel(deviceId) {
|
|
256
|
+
if (!deviceId) {
|
|
257
|
+
throw new Error("Device ID is required");
|
|
258
|
+
}
|
|
259
|
+
return await this.deviceRepository.getBatteryLevel(deviceId);
|
|
260
|
+
}
|
|
261
|
+
async setBatteryLevel(deviceId, batteryLevel, source, auditBody) {
|
|
262
|
+
if (!deviceId || batteryLevel === undefined || batteryLevel === null) {
|
|
263
|
+
throw new Error("Device ID and battery level are required");
|
|
264
|
+
}
|
|
265
|
+
// Get device information
|
|
266
|
+
const device = await this.getDevice(deviceId);
|
|
267
|
+
// Fetch the old battery level state
|
|
268
|
+
const oldBatteryLevel = device.state?.batteryPercentage;
|
|
269
|
+
// Check if battery level has changed
|
|
270
|
+
const isDifferent = !(0, lodash_1.isEqual)(oldBatteryLevel?.value, batteryLevel);
|
|
271
|
+
if (isDifferent) {
|
|
272
|
+
// Check if current time is greater than or equal to last updated time (8-hour logic)
|
|
273
|
+
const shouldUpdate = this.shouldUpdateBatteryLevel(oldBatteryLevel?.lastUpdated);
|
|
274
|
+
if (shouldUpdate) {
|
|
275
|
+
// Save the battery level in the device
|
|
276
|
+
await this.deviceRepository.setBatteryLevel(deviceId, batteryLevel);
|
|
277
|
+
await this.eventHandler.onBatteryLevelChange(deviceId, batteryLevel, auditBody);
|
|
278
|
+
// Get property threshold
|
|
279
|
+
const propertyThreshold = await this.getPropertyBatteryThreshold(device.propertyId);
|
|
280
|
+
// Check if battery level is below threshold
|
|
281
|
+
if (batteryLevel < propertyThreshold) {
|
|
282
|
+
// Raise alert
|
|
283
|
+
await this.alertService.raiseDeviceBatteryAlert(device, batteryLevel, propertyThreshold, source);
|
|
284
|
+
// Raise issue when the level is below threshold
|
|
285
|
+
await this.issueService.createDeviceBatteryIssue(device, batteryLevel, propertyThreshold, source);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
shouldUpdateBatteryLevel(lastUpdated) {
|
|
291
|
+
if (!lastUpdated) {
|
|
292
|
+
return true; // No previous update, so we should update
|
|
293
|
+
}
|
|
294
|
+
const lastUpdateTime = new Date(lastUpdated).getTime();
|
|
295
|
+
const currentTime = Date.now();
|
|
296
|
+
const eightHoursInMs = 8 * 60 * 60 * 1000; // 8 hours in milliseconds
|
|
297
|
+
// Return true if current time is greater than or equal to last updated time + 8 hours
|
|
298
|
+
return currentTime >= lastUpdateTime + eightHoursInMs;
|
|
299
|
+
}
|
|
300
|
+
async getPropertyBatteryThreshold(propertyId) {
|
|
301
|
+
// TODO: Implement property battery threshold retrieval
|
|
302
|
+
// This should return the battery threshold for the specific property
|
|
303
|
+
// For now, returning a default value of 20%
|
|
304
|
+
// In a real implementation, this would fetch from property configuration or settings
|
|
305
|
+
return 20; // 20% default threshold
|
|
306
|
+
}
|
|
307
|
+
async checkForDeviceMalfunctions(device, source, reason) {
|
|
308
|
+
// TODO: Implement device malfunction detection logic
|
|
309
|
+
// This should check for:
|
|
310
|
+
// - Lock jammed
|
|
311
|
+
// - Device not accepting codes
|
|
312
|
+
// - Other malfunction indicators
|
|
313
|
+
// For now, we'll check if the reason indicates a malfunction
|
|
314
|
+
const malfunctionIndicators = [
|
|
315
|
+
"jammed",
|
|
316
|
+
"not accepting codes",
|
|
317
|
+
"malfunction",
|
|
318
|
+
"error",
|
|
319
|
+
"failure",
|
|
320
|
+
];
|
|
321
|
+
const hasMalfunction = malfunctionIndicators.some((indicator) => reason?.toLowerCase().includes(indicator));
|
|
322
|
+
if (hasMalfunction) {
|
|
323
|
+
// Raise alert for device malfunction (READINESS + OPERATIONAL)
|
|
324
|
+
await this.alertService.raiseDeviceIssueAlert(device, "Device Malfunction Detected", source, reason);
|
|
325
|
+
// Raise issue for device malfunction (READINESS + OPERATIONAL)
|
|
326
|
+
await this.issueService.createDeviceMalfunctionIssue(device, "Device Malfunction", source, reason);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
async getMetaData(deviceId) {
|
|
330
|
+
if (!deviceId) {
|
|
331
|
+
throw new Error("Device ID is required");
|
|
332
|
+
}
|
|
333
|
+
return await this.deviceRepository.getMetaData(deviceId);
|
|
334
|
+
}
|
|
335
|
+
async setMetaData(deviceId, metaData, auditBody) {
|
|
336
|
+
if (!deviceId || !metaData) {
|
|
337
|
+
throw new Error("Device ID and meta data are required");
|
|
338
|
+
}
|
|
339
|
+
await this.eventHandler.onDeviceMetaChange(deviceId, metaData, auditBody);
|
|
340
|
+
return await this.deviceRepository.setMetaData(deviceId, metaData);
|
|
341
|
+
}
|
|
342
|
+
async getDevicesByZone(zoneId) {
|
|
343
|
+
if (!zoneId) {
|
|
344
|
+
throw new Error("Zone ID is required");
|
|
345
|
+
}
|
|
346
|
+
return await this.deviceRepository.getDevicesByZone(zoneId);
|
|
347
|
+
}
|
|
348
|
+
async getDevicesByAccessGroup(accessGroupId) {
|
|
349
|
+
if (!accessGroupId) {
|
|
350
|
+
throw new Error("Access Group ID is required");
|
|
351
|
+
}
|
|
352
|
+
return await this.deviceRepository.getDevicesByAccessGroup(accessGroupId);
|
|
353
|
+
}
|
|
354
|
+
async querySelect(query, fields) {
|
|
355
|
+
if (!query || Object.keys(query).length === 0) {
|
|
356
|
+
throw new Error("Query is required");
|
|
357
|
+
}
|
|
358
|
+
return await this.deviceRepository.querySelect(query, fields);
|
|
359
|
+
}
|
|
360
|
+
async queryCount(query) {
|
|
361
|
+
if (!query || Object.keys(query).length === 0) {
|
|
362
|
+
throw new Error("Query is required");
|
|
363
|
+
}
|
|
364
|
+
const count = await this.deviceRepository.queryCount(query);
|
|
365
|
+
return count;
|
|
366
|
+
}
|
|
367
|
+
async deleteDevices(query) {
|
|
368
|
+
if (!query || Object.keys(query).length === 0) {
|
|
369
|
+
throw new Error("Query is required");
|
|
370
|
+
}
|
|
371
|
+
return await this.deviceRepository.deleteDevices(query);
|
|
372
|
+
}
|
|
373
|
+
async queryDevices(query) {
|
|
374
|
+
if (!query || Object.keys(query).length === 0) {
|
|
375
|
+
throw new Error("Query is required");
|
|
376
|
+
}
|
|
377
|
+
const res = await this.deviceRepository.queryDevices(query);
|
|
378
|
+
return res;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
__setFunctionName(_classThis, "LocalDeviceService");
|
|
382
|
+
(() => {
|
|
383
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
384
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
385
|
+
LocalDeviceService = _classThis = _classDescriptor.value;
|
|
386
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
387
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
388
|
+
})();
|
|
389
|
+
return LocalDeviceService = _classThis;
|
|
390
|
+
})();
|
|
391
|
+
exports.LocalDeviceService = LocalDeviceService;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IHubCreateParams } from "../interfaces";
|
|
2
|
+
export declare class DeviceHubService {
|
|
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
|
+
deleteHub(hubId: string): Promise<any>;
|
|
10
|
+
deleteAllHubs(hubIds: string[]): Promise<any>;
|
|
11
|
+
}
|