UNPKG

dt-common-device

Version:

A secure and robust device management library for IoT applications

151 lines (150 loc) 7.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AlertServiceExample = void 0; const Service_1 = require("../constants/Service"); const AlertBuilder_1 = require("./AlertBuilder"); const alert_types_1 = require("./alert.types"); /** * Example usage of the updated AlertService with AlertBuilder integration * This file demonstrates various ways to use the AlertService with the new AlertBuilder */ class AlertServiceExample { constructor(alertService) { this.alertService = alertService; } /** * Example 1: Using the updated createAlert method with AlertBuilder */ async createAlertWithBuilder() { const alertBuilder = new AlertBuilder_1.AlertBuilder() .setCategory(alert_types_1.AlertCategory.OPERATIONS) .setPropertyId("prop123") .setTitle("Device Offline") .setDescription("Device has been offline for more than 5 minutes") .setEntityId("device456") .setEntityType(alert_types_1.EntityType.DEVICE) .setSeverity(alert_types_1.AlertSeverity.HIGH) .setCreatedBy("user789"); // Pass the AlertBuilder directly to createAlert const alert = await this.alertService.createAlert(alertBuilder); return alert; } /** * Example 2: Using convenience methods for specific alert types */ async createSpecificAlerts() { // Create a readiness alert const readinessAlert = await this.alertService.raiseReadinessAlert("prop123", "System Maintenance Required", "System maintenance is scheduled for tonight", "system456", alert_types_1.EntityType.PROPERTY, "admin"); // Create an operations alert const operationsAlert = await this.alertService.raiseOperationsAlert("prop123", "Device Temperature High", "Device temperature exceeds normal operating range", "device789", alert_types_1.EntityType.DEVICE, "monitor"); // Create a security alert const securityAlert = await this.alertService.raiseSecurityAlert("prop123", "Unauthorized Access Attempt", "Multiple failed login attempts detected", "user123", alert_types_1.EntityType.USER, "security-system"); // Create an energy alert 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"); return { readinessAlert, operationsAlert, securityAlert, energyAlert }; } /** * Example 3: Using device-specific alert methods */ async createDeviceAlerts() { // Create a device alert with default category and severity const deviceAlert1 = await this.alertService.raiseDeviceAlert("device123", "prop456", "Device Battery Low", "Device battery level is below 20%", undefined, // Use default category undefined, // Use default severity Service_1.Source.CLOUD_EVENT); // Create a device alert with custom category and severity 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, Service_1.Source.CLOUD_EVENT); return { deviceAlert1, deviceAlert2 }; } /** * Example 4: Using hub-specific alert methods */ async createHubAlerts() { // Create a hub alert with default settings const hubAlert1 = await this.alertService.raiseHubAlert("hub123", "prop456", "Hub Connection Lost", "Hub has lost connection to the network", undefined, // Use default category undefined, // Use default severity "network-monitor"); // Create a hub alert with custom settings 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"); return { hubAlert1, hubAlert2 }; } /** * Example 5: Using static factory methods with AlertBuilder */ async createAlertsWithStaticMethods() { // Create a device alert using static factory method const deviceAlert = AlertBuilder_1.AlertBuilder.createDeviceAlert("device123", "prop456") .setCategory(alert_types_1.AlertCategory.OPERATIONS) .setTitle("Device Offline") .setDescription("Device has been offline for more than 5 minutes") .setSeverity(alert_types_1.AlertSeverity.HIGH) .setCreatedBy("monitor"); const alert1 = await this.alertService.createAlert(deviceAlert); // Create a hub alert using static factory method const hubAlert = AlertBuilder_1.AlertBuilder.createHubAlert("hub789", "prop202") .setCategory(alert_types_1.AlertCategory.SECURITY) .setTitle("Hub Security Breach") .setDescription("Unauthorized access attempt detected on hub") .setSeverity(alert_types_1.AlertSeverity.CRITICAL) .setCreatedBy("security-system"); const alert2 = await this.alertService.createAlert(hubAlert); return { alert1, alert2 }; } /** * Example 6: Creating multiple alerts efficiently */ async createMultipleAlerts() { const alerts = []; // Create multiple device alerts efficiently const deviceIds = ["device1", "device2", "device3"]; const propertyId = "prop123"; for (const deviceId of deviceIds) { const alertBuilder = AlertBuilder_1.AlertBuilder.createDeviceAlert(deviceId, propertyId) .setCategory(alert_types_1.AlertCategory.OPERATIONS) .setTitle(`Device ${deviceId} Status`) .setDescription(`Status check for device ${deviceId}`) .setSeverity(alert_types_1.AlertSeverity.MEDIUM) .setCreatedBy("batch-processor"); const alert = await this.alertService.createAlert(alertBuilder); alerts.push(alert); } return alerts; } /** * Example 7: Creating alerts with snooze functionality */ async createSnoozedAlert() { const snoozeUntil = new Date(); snoozeUntil.setHours(snoozeUntil.getHours() + 2); // Snooze for 2 hours const alertBuilder = new AlertBuilder_1.AlertBuilder() .setCategory(alert_types_1.AlertCategory.OTHER) .setPropertyId("prop303") .setTitle("Scheduled Maintenance") .setDescription("System maintenance scheduled for tonight") .setEntityType(alert_types_1.EntityType.PROPERTY) .setSeverity(alert_types_1.AlertSeverity.INFO) .setCreatedBy("admin") .setSnoozeUntil(snoozeUntil); const alert = await this.alertService.createAlert(alertBuilder); return alert; } /** * Example 8: Backward compatibility - still works with CreateAlertData */ async createAlertWithLegacyData() { const alertData = { category: alert_types_1.AlertCategory.OPERATIONS, propertyId: "prop123", title: "Legacy Alert", description: "This alert was created using the old CreateAlertData format", entityId: "device456", entityType: alert_types_1.EntityType.DEVICE, severity: alert_types_1.AlertSeverity.MEDIUM, createdBy: "legacy-system", source: Service_1.Source.CLOUD_EVENT, }; // This still works with the updated createAlert method const alert = await this.alertService.createAlert(alertData); return alert; } } exports.AlertServiceExample = AlertServiceExample;