增加日志

pull/19/head
刘鑫 2 months ago
parent 47f03eafa8
commit 1983eba962
  1. 17
      WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/DahuaGeneralCtlService.cs
  2. 2
      WeiCloud.Fusion/AlarmService/Alarm.DomainService/DahAlarm/IDahuaGeneralCtlService.cs
  3. 44
      WeiCloud.Fusion/AlarmService/AlarmService.API/Controllers/AlarmController.cs

@ -2,6 +2,7 @@
using Common.Shared.DomainService; using Common.Shared.DomainService;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
@ -98,16 +99,14 @@ namespace Alarm.DomainService.DahAlarm
MonitorType = "url", MonitorType = "url",
Events = new List<EventConfig> Events = new List<EventConfig>
{ {
new EventConfig new() {
{
Category = "alarm", Category = "alarm",
SubscribeAll = 1, SubscribeAll = 1,
DomainSubscribe = 2, DomainSubscribe = 2,
Authorities = new List<Authority> Authorities = new List<Authority>
{ {
new Authority new() {
{ Types = ["81"]
Types = new List<string> { "4321" }
} }
} }
} }
@ -189,13 +188,13 @@ namespace Alarm.DomainService.DahAlarm
/// <param name="dto"></param> /// <param name="dto"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public async Task<DaHApiResult<bool>> HandleAsync(object dto2) public async Task<DaHApiResult<bool>> HandleAsync(EventEnvelopeDto dto)
{ {
DaHApiResult<bool> result = new() { Code = "200", Msg = "接口调用成功", Data = true }; DaHApiResult<bool> result = new() { Code = "200", Msg = "接口调用成功", Data = true };
_logger.LogWarning($"报警回调的数据{dto2}"); _logger.LogWarning($"报警回调的数据{dto.ToJson()}");
try try
{ {
if (dto2 is null) if (dto is null)
{ {
result.Code = "500"; result.Code = "500";
result.Msg = "请求参数不能为空"; result.Msg = "请求参数不能为空";
@ -203,7 +202,7 @@ namespace Alarm.DomainService.DahAlarm
_logger.LogWarning("大华报警事件订阅回调处理失败,参数不能为空"); _logger.LogWarning("大华报警事件订阅回调处理失败,参数不能为空");
return result; return result;
} }
EventEnvelopeDto dto = dto2 as EventEnvelopeDto;
if (dto.Info is not null) if (dto.Info is not null)
{ {
//这是大华的残卫报警类型 //这是大华的残卫报警类型

@ -25,6 +25,6 @@ namespace Alarm.DomainService.DahAlarm
/// <param name="env"></param> /// <param name="env"></param>
/// <param name="ct"></param> /// <param name="ct"></param>
/// <returns></returns> /// <returns></returns>
Task<DaHApiResult<bool>> HandleAsync(object env); Task<DaHApiResult<bool>> HandleAsync(EventEnvelopeDto env);
} }
} }

@ -1,6 +1,8 @@
using Alarm.DomainService.DahAlarm; using Alarm.DomainService.DahAlarm;
using Common.Shared.Application.DaHua; using Common.Shared.Application.DaHua;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using System.Text.Json;
namespace AlarmService.API.Controllers namespace AlarmService.API.Controllers
{ {
@ -39,9 +41,47 @@ namespace AlarmService.API.Controllers
[HttpPost] [HttpPost]
[Consumes("application/json")] [Consumes("application/json")]
[Produces("application/json")] [Produces("application/json")]
public async Task<DaHApiResult<bool>> DahuaAuthCallback([FromBody] object env) public async Task<IActionResult> 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<EventEnvelopeDto>(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);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save