|
|
|
|
@ -4,15 +4,13 @@ using Common.Shared.Application.DaHua; |
|
|
|
|
using Common.Shared.DomainService; |
|
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
using MongoDB.Bson; |
|
|
|
|
using Org.BouncyCastle.Crypto.Parameters; |
|
|
|
|
using Org.BouncyCastle.Security; |
|
|
|
|
using System; |
|
|
|
|
using NewLife.Remoting; |
|
|
|
|
using OfficeOpenXml.FormulaParsing.LexicalAnalysis; |
|
|
|
|
using Org.BouncyCastle.Ocsp; |
|
|
|
|
using System.Net.Http.Headers; |
|
|
|
|
using System.Net.Http.Json; |
|
|
|
|
using System.Security.Cryptography; |
|
|
|
|
using System.Text.Json; |
|
|
|
|
using System.Text.Json.Serialization; |
|
|
|
|
using WeiCloud.Core.BaseModels; |
|
|
|
|
|
|
|
|
|
namespace Alarm.DomainService.DahAlarm |
|
|
|
|
{ |
|
|
|
|
@ -59,9 +57,9 @@ namespace Alarm.DomainService.DahAlarm |
|
|
|
|
/// <summary> |
|
|
|
|
/// 开发测试的时候,忽略证书 |
|
|
|
|
/// </summary> |
|
|
|
|
private static readonly HttpClient _http = new HttpClient(new HttpClientHandler |
|
|
|
|
private static readonly HttpClient _http = new(new HttpClientHandler |
|
|
|
|
{ |
|
|
|
|
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator |
|
|
|
|
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
@ -206,11 +204,11 @@ namespace Alarm.DomainService.DahAlarm |
|
|
|
|
if (dto.Info.AlarmType == 4321) |
|
|
|
|
{ |
|
|
|
|
//拼接物联平台标准的mqtt消息格式 |
|
|
|
|
var payload = "[{\"taglabel\":\"" + dto.Info.DeviceCode + " + \".alart.\" + " + dto.Info.DeviceName + "\",\"value\":\"" + dto.Info.AlarmStat + "\",\"time\":\"" + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "\"}]"; |
|
|
|
|
var payload = "[{\"taglabel\":\"" + dto.Info.DeviceCode + ".alart." + dto.Info.DeviceName + "\",\"value\":\"" + dto.Info.AlarmStat + "\",\"time\":\"" + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "\"}]"; |
|
|
|
|
|
|
|
|
|
await _mqttClient.EnsureConnectedAsync(mqttHostIp, mqttHostPort, mqttUsername, mqttPassword, topicName, mqttClientId); |
|
|
|
|
|
|
|
|
|
await _mqttClientService.PublishAsync("/zrh/sun/alarm", payload); |
|
|
|
|
await _mqttClientService.PublishAsync(topicName, payload); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -229,9 +227,85 @@ namespace Alarm.DomainService.DahAlarm |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="name"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public async Task<DaHApiResult<object>> DeleteSubscribe(string name) |
|
|
|
|
public async Task<bool> DeleteEvent(string name) |
|
|
|
|
{ |
|
|
|
|
throw new NotImplementedException("该方法未实现"); |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
|
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-event/1.0.0/subscribe/mqinfo?name={name}"; |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Delete, url); |
|
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Authorization", token); |
|
|
|
|
|
|
|
|
|
using var resp = await _http.SendAsync(request); |
|
|
|
|
var body = await resp.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
|
|
if (!resp.IsSuccessStatusCode) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var result = JsonSerializer.Deserialize<DaHApiResult<object>>(body); |
|
|
|
|
if (result == null || !result.Success) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("实时流请求业务失败: {Body}", body); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
_logger.LogError(ex, "大华实时流请求出错"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 获取事件列表 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="name"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
/// <exception cref="NotImplementedException"></exception> |
|
|
|
|
public async Task<DaHApiResult<SubscriptionMapDto>> GetEventList(string name = "alarm") |
|
|
|
|
{ |
|
|
|
|
var clientId = _configuration["DahuaAuth:ClientId"]; |
|
|
|
|
var token = await _tokenProviderService.GetTokenAsync(clientId!); |
|
|
|
|
|
|
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-event/1.0.0/subscribe/subscribe-list?monitorType=url&category={name}"; |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url); |
|
|
|
|
|
|
|
|
|
request.Headers.TryAddWithoutValidation("Authorization", token); |
|
|
|
|
|
|
|
|
|
using var resp = await _http.SendAsync(request); |
|
|
|
|
var body = await resp.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
|
|
if (!resp.IsSuccessStatusCode) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); |
|
|
|
|
return new DaHApiResult<SubscriptionMapDto> { Success = false, Code = "1010", Msg = $"HTTP错误 {(int)resp.StatusCode}" }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var result = JsonSerializer.Deserialize<DaHApiResult<SubscriptionMapDto>>(body); |
|
|
|
|
if (result == null || !result.Success) |
|
|
|
|
{ |
|
|
|
|
_logger.LogWarning("实时流请求业务失败: {Body}", body); |
|
|
|
|
return new DaHApiResult<SubscriptionMapDto> { Success = false, Code = "1010", Msg = "实时流请求失败" }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
_logger.LogError(ex, "大华实时流请求出错"); |
|
|
|
|
return new DaHApiResult<SubscriptionMapDto> { Success = false, Code = "1010", Msg = "实时流请求失败" }; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|