From 1983eba9626f01ea70ec8dd54eeed3367c66f56b Mon Sep 17 00:00:00 2001 From: LiuXin Date: Wed, 27 Aug 2025 16:42:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DahAlarm/DahuaGeneralCtlService.cs | 17 ++++--- .../DahAlarm/IDahuaGeneralCtlService.cs | 2 +- .../Controllers/AlarmController.cs | 44 ++++++++++++++++++- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/DahuaGeneralCtlService.cs b/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/DahuaGeneralCtlService.cs index aedb2a1..f0a4e71 100644 --- a/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/DahuaGeneralCtlService.cs +++ b/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/DahuaGeneralCtlService.cs @@ -2,6 +2,7 @@ using Common.Shared.DomainService; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using MongoDB.Bson; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; @@ -98,16 +99,14 @@ namespace Alarm.DomainService.DahAlarm MonitorType = "url", Events = new List { - new EventConfig - { + new() { Category = "alarm", SubscribeAll = 1, DomainSubscribe = 2, Authorities = new List { - new Authority - { - Types = new List { "4321" } + new() { + Types = ["81"] } } } @@ -189,13 +188,13 @@ namespace Alarm.DomainService.DahAlarm /// /// /// - public async Task> HandleAsync(object dto2) + public async Task> HandleAsync(EventEnvelopeDto dto) { DaHApiResult result = new() { Code = "200", Msg = "接口调用成功", Data = true }; - _logger.LogWarning($"报警回调的数据{dto2}"); + _logger.LogWarning($"报警回调的数据{dto.ToJson()}"); try { - if (dto2 is null) + if (dto is null) { result.Code = "500"; result.Msg = "请求参数不能为空"; @@ -203,7 +202,7 @@ namespace Alarm.DomainService.DahAlarm _logger.LogWarning("大华报警事件订阅回调处理失败,参数不能为空"); return result; } - EventEnvelopeDto dto = dto2 as EventEnvelopeDto; + if (dto.Info is not null) { //这是大华的残卫报警类型 diff --git a/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/IDahuaGeneralCtlService.cs b/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/IDahuaGeneralCtlService.cs index e419693..d37e808 100644 --- a/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/IDahuaGeneralCtlService.cs +++ b/WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/IDahuaGeneralCtlService.cs @@ -25,6 +25,6 @@ namespace Alarm.DomainService.DahAlarm /// /// /// - Task> HandleAsync(object env); + Task> HandleAsync(EventEnvelopeDto env); } } \ No newline at end of file diff --git a/WeiCloud.Fusion/AlarmService/AlarmService.API/Controllers/AlarmController.cs b/WeiCloud.Fusion/AlarmService/AlarmService.API/Controllers/AlarmController.cs index 3c8222c..dbe2e65 100644 --- a/WeiCloud.Fusion/AlarmService/AlarmService.API/Controllers/AlarmController.cs +++ b/WeiCloud.Fusion/AlarmService/AlarmService.API/Controllers/AlarmController.cs @@ -1,6 +1,8 @@ using Alarm.DomainService.DahAlarm; using Common.Shared.Application.DaHua; using Microsoft.AspNetCore.Mvc; +using MongoDB.Bson; +using System.Text.Json; namespace AlarmService.API.Controllers { @@ -39,9 +41,47 @@ namespace AlarmService.API.Controllers [HttpPost] [Consumes("application/json")] [Produces("application/json")] - public async Task> DahuaAuthCallback([FromBody] object env) + public async Task DahuaAuthCallback([FromBody] JsonElement data) { - return await _generalCtlService.HandleAsync(env); + var rawJson = data.GetRawText(); + _logger.LogWarning($"收到了报警回调{data.ToJson()}"); + _logger.LogWarning($"收到了报警回调{rawJson}"); + EventEnvelopeDto dto = null; + try + { + dto = JsonSerializer.Deserialize(rawJson); + } + catch (Exception ex) + { + _logger.LogError(ex, "JSON反序列化失败"); + // 即使反序列化失败也要返回成功,避免平台反复重试 + } + + // 即使数据结构异常,也不应该让平台收到 HTTP 500 + try + { + if (dto != null) + { + await _generalCtlService.HandleAsync(dto); + } + else + { + _logger.LogWarning("接收到的DTO为 null,跳过业务处理"); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "处理回调时异常"); + } + + // 返回 Java 示例中的成功结构 + var result = new + { + code = "0", + message = "成功" + }; + + return new JsonResult(result); } ///