using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Haoliang.Core.Services; using Haoliang.Models.System; using Haoliang.Models.Device; using Haoliang.Models.Common; namespace Haoliang.Api.Controllers { [ApiController] [Route("api/v1/[controller]")] public class AlarmController : ControllerBase { private readonly IAlarmService _alarmService; private readonly IAlarmRuleService _alarmRuleService; private readonly IAlarmNotificationService _notificationService; private readonly ILoggingService _loggingService; public AlarmController( IAlarmService alarmService, IAlarmRuleService alarmRuleService, IAlarmNotificationService notificationService, ILoggingService loggingService) { _alarmService = alarmService; _alarmRuleService = alarmRuleService; _notificationService = notificationService; _loggingService = loggingService; } [HttpGet] public async Task GetAllAlarms([FromQuery] AlarmType? type = null, [FromQuery] AlarmStatus? status = null) { try { IEnumerable alarms; if (type.HasValue) { alarms = await _alarmService.GetAlarmsByTypeAsync(type.Value); } else if (status.HasValue) { alarms = await _alarmService.GetActiveAlarmsAsync(); } else { alarms = await _alarmService.GetAllAlarmsAsync(); } return Ok(ApiResponse.Ok(alarms)); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to get alarms", ex); return StatusCode(500, ApiResponse.Error("Failed to get alarms")); } } [HttpGet("{id}")] public async Task GetAlarm(int id) { try { var alarm = await _alarmService.GetAlarmByIdAsync(id); if (alarm == null) { return NotFound(ApiResponse.NotFound("Alarm not found")); } return Ok(ApiResponse.Ok(alarm)); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to get alarm {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to get alarm")); } } [HttpPost] public async Task CreateAlarm([FromBody] Alarm alarm) { try { var createdAlarm = await _alarmService.CreateAlarmAsync(alarm); return Ok(ApiResponse.Ok(createdAlarm, "Alarm created successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to create alarm", ex); return StatusCode(500, ApiResponse.Error("Failed to create alarm")); } } [HttpPut("{id}")] public async Task UpdateAlarm(int id, [FromBody] Alarm alarm) { try { alarm.Id = id; var updatedAlarm = await _alarmService.UpdateAlarmAsync(id, alarm); if (updatedAlarm == null) { return NotFound(ApiResponse.NotFound("Alarm not found")); } return Ok(ApiResponse.Ok(updatedAlarm, "Alarm updated successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to update alarm {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to update alarm")); } } [HttpDelete("{id}")] public async Task DeleteAlarm(int id) { try { var result = await _alarmService.DeleteAlarmAsync(id); if (!result) { return NotFound(ApiResponse.NotFound("Alarm not found")); } return Ok(ApiResponse.Ok(null, "Alarm deleted successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to delete alarm {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to delete alarm")); } } [HttpPost("{id}/resolve")] public async Task ResolveAlarm(int id, [FromBody] ResolveAlarmRequest request) { try { var result = await _alarmService.ResolveAlarmAsync(id, request.ResolutionNote); if (!result) { return NotFound(ApiResponse.NotFound("Alarm not found")); } return Ok(ApiResponse.Ok(null, "Alarm resolved successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to resolve alarm {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to resolve alarm")); } } [HttpPost("{id}/acknowledge")] public async Task AcknowledgeAlarm(int id, [FromBody] AcknowledgeAlarmRequest request) { try { var result = await _alarmService.AcknowledgeAlarmAsync(id, request.AcknowledgeNote); if (!result) { return NotFound(ApiResponse.NotFound("Alarm not found")); } return Ok(ApiResponse.Ok(null, "Alarm acknowledged successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to acknowledge alarm {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to acknowledge alarm")); } } [HttpGet("device/{deviceId}")] public async Task GetDeviceAlarms(int deviceId, [FromQuery] int days = 7) { try { var alarms = await _alarmService.GetDeviceAlarmsAsync(deviceId, days); return Ok(ApiResponse.Ok(alarms)); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to get alarms for device {deviceId}", ex); return StatusCode(500, ApiResponse.Error("Failed to get device alarms")); } } [HttpGet("critical")] public async Task GetCriticalAlarms() { try { var alarms = await _alarmService.GetCriticalAlarmsAsync(); return Ok(ApiResponse.Ok(alarms)); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to get critical alarms", ex); return StatusCode(500, ApiResponse.Error("Failed to get critical alarms")); } } [HttpGet("statistics")] public async Task GetAlarmStatistics([FromQuery] DateTime date) { try { if (date == default) date = DateTime.Today; var statistics = await _alarmService.GetAlarmStatisticsAsync(date); return Ok(ApiResponse.Ok(statistics)); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to get alarm statistics", ex); return StatusCode(500, ApiResponse.Error("Failed to get alarm statistics")); } } [HttpGet("date-range")] public async Task GetAlarmsByDateRange([FromQuery] DateTime startDate, [FromQuery] DateTime endDate) { try { var alarms = await _alarmService.GetAlarmsByDateRangeAsync(startDate, endDate); return Ok(ApiResponse.Ok(alarms)); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to get alarms by date range", ex); return StatusCode(500, ApiResponse.Error("Failed to get alarms by date range")); } } [HttpPost("test-notification")] public async Task TestNotification([FromBody] TestNotificationRequest request) { try { var notification = new AlarmNotification { AlarmId = -1, NotificationType = request.NotificationType.ToString(), Subject = "Test notification", Content = "Test notification", IsSent = true, SendTime = DateTime.Now, Recipient = request.Recipient }; await _notificationService.SendAlarmNotificationAsync(notification); return Ok(ApiResponse.Ok(null, "Test notification sent successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to send test notification", ex); return StatusCode(500, ApiResponse.Error("Failed to send test notification")); } } [HttpGet("rules")] public async Task GetAlarmRules() { try { var rules = await _alarmRuleService.GetAllAlarmRulesAsync(); return Ok(ApiResponse.Ok(rules)); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to get alarm rules", ex); return StatusCode(500, ApiResponse.Error("Failed to get alarm rules")); } } [HttpPost("rules")] public async Task CreateAlarmRule([FromBody] AlarmRule rule) { try { var createdRule = await _alarmRuleService.CreateAlarmRuleAsync(rule); return Ok(ApiResponse.Ok(createdRule, "Alarm rule created successfully")); } catch (Exception ex) { await _loggingService.LogErrorAsync("Failed to create alarm rule", ex); return StatusCode(500, ApiResponse.Error("Failed to create alarm rule")); } } [HttpPost("rules/{id}/test")] public async Task TestAlarmRule(int id) { try { await _alarmRuleService.TestAlarmRuleAsync(id); return Ok(ApiResponse.Ok(null, "Alarm rule test completed")); } catch (Exception ex) { await _loggingService.LogErrorAsync($"Failed to test alarm rule {id}", ex); return StatusCode(500, ApiResponse.Error("Failed to test alarm rule")); } } } public class ResolveAlarmRequest { public string ResolutionNote { get; set; } } public class AcknowledgeAlarmRequest { public string AcknowledgeNote { get; set; } } public class TestNotificationRequest { public int DeviceId { get; set; } public NotificationType NotificationType { get; set; } public string Recipient { get; set; } } }