@ -18,7 +18,7 @@ namespace Alarm.DomainService.DahAlarm
private readonly IMqttClientService _ mqttClientService ;
private readonly IMqttClientService _ mqttClientService ;
private readonly ITokenProviderService _ tokenProviderService ;
private readonly ITokenProviderService _ tokenProviderService ;
private readonly HttpClient _ http ;
// private readonly HttpClient _http ;
private string mqttHostIp ;
private string mqttHostIp ;
private int mqttHostPort ;
private int mqttHostPort ;
@ -50,16 +50,16 @@ namespace Alarm.DomainService.DahAlarm
topicName = _ configuration [ "SubscribeMQTT:TopicName" ] ! ;
topicName = _ configuration [ "SubscribeMQTT:TopicName" ] ! ;
_ mqttClientService = mqttClientService ;
_ mqttClientService = mqttClientService ;
_ tokenProviderService = tokenProviderService ;
_ tokenProviderService = tokenProviderService ;
_ http = http ;
// _http = http ;
}
}
///// <summary>
/// <summary>
///// 开发测试的时候,忽略证书
/// 开发测试的时候,忽略证书
///// </summary>
/// </summary>
//private static readonly HttpClient _http = new(new HttpClientHandler
private static readonly HttpClient _ http = new ( new HttpClientHandler
// {
{
// ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator ,
ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator ,
//}) ;
} ) ;
/// <summary>
/// <summary>
/// 新增报警事件订阅
/// 新增报警事件订阅
@ -189,12 +189,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 ( EventEnvelopeDto dto )
public async Task < DaHApiResult < bool > > HandleAsync ( object dto2 )
{
{
DaHApiResult < bool > result = new ( ) { Code = "200" , Msg = "接口调用成功" , Data = true } ;
DaHApiResult < bool > result = new ( ) { Code = "200" , Msg = "接口调用成功" , Data = true } ;
_l ogger . LogWarning ( $"报警回调的数据{dto2}" ) ;
try
try
{
{
if ( dto is null )
if ( dto2 is null )
{
{
result . Code = "500" ;
result . Code = "500" ;
result . Msg = "请求参数不能为空" ;
result . Msg = "请求参数不能为空" ;
@ -202,7 +203,7 @@ namespace Alarm.DomainService.DahAlarm
_l ogger . LogWarning ( "大华报警事件订阅回调处理失败,参数不能为空" ) ;
_l ogger . LogWarning ( "大华报警事件订阅回调处理失败,参数不能为空" ) ;
return result ;
return result ;
}
}
EventEnvelopeDto dto = dto2 as EventEnvelopeDto ;
if ( dto . Info is not null )
if ( dto . Info is not null )
{
{
//这是大华的残卫报警类型
//这是大华的残卫报警类型
@ -210,7 +211,7 @@ namespace Alarm.DomainService.DahAlarm
{
{
//拼接物联平台标准的mqtt消息格式
//拼接物联平台标准的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 ( ) + "\"}]" ;
//var payload = "[{\"taglabel\":\"残卫测试报警按钮.alarmStat\",\"value\":\"" + dto.Info.AlarmStat + "\",\"time\":\"" + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "\"}]";
// var payload = "[{\"taglabel\":\"残卫测试报警按钮.alarmStat\",\"value\":\"" + dto.Info.AlarmStat + "\",\"time\":\"" + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "\"}]";
await _ mqttClient . EnsureConnectedAsync ( mqttHostIp , mqttHostPort , mqttUsername , mqttPassword , topicName , mqttClientId ) ;
await _ mqttClient . EnsureConnectedAsync ( mqttHostIp , mqttHostPort , mqttUsername , mqttPassword , topicName , mqttClientId ) ;
await _ mqttClientService . PublishAsync ( topicName , payload ) ;
await _ mqttClientService . PublishAsync ( topicName , payload ) ;
@ -246,7 +247,7 @@ namespace Alarm.DomainService.DahAlarm
try
try
{
{
var request = new HttpRequestMessage ( HttpMethod . Delete , url ) ;
var request = new HttpRequestMessage ( HttpMethod . Delete , url ) ;
request . Headers . Authorization = new AuthenticationHeaderValue ( "Authorization" , token ) ;
request . Headers . TryAddWithoutValidation ( "Authorization" , token ) ;
using var resp = await _ http . SendAsync ( request ) ;
using var resp = await _ http . SendAsync ( request ) ;
var body = await resp . Content . ReadAsStringAsync ( ) ;
var body = await resp . Content . ReadAsStringAsync ( ) ;
@ -279,18 +280,17 @@ namespace Alarm.DomainService.DahAlarm
/// <param name="name"></param>
/// <param name="name"></param>
/// <returns></returns>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="NotImplementedException"></exception>
public async Task < DaHApiResult < SubscriptionMap Dto > > GetEventList ( string name = "alarm" )
public async Task < DaHApiResult < SubscribeData Dto > > GetEventList ( string name = "alarm" )
{
{
var clientId = _ configuration [ "DahuaAuth:ClientId" ] ;
var clientId = _ configuration [ "DahuaAuth:ClientId" ] ;
var token = await _ tokenProviderService . GetTokenAsync ( clientId ! ) ;
var token = await _ tokenProviderService . GetTokenAsync ( clientId ! ) ;
if ( ! _ tokenProviderService . IsTokenValid ( token ) )
if ( ! _ tokenProviderService . IsTokenValid ( token ) )
{
{
_l ogger . LogWarning ( "获取事件列表:token无效" ) ;
_l ogger . LogWarning ( "获取事件列表:token无效" ) ;
return new DaHApiResult < SubscriptionMap Dto > { Success = false , Code = "1009" , Msg = "token无效" } ;
return new DaHApiResult < SubscribeData Dto > { Success = false , Code = "1009" , Msg = "token无效" } ;
}
}
var url = $"https://{_configuration[" DahuaAuth : Host "]}/evo-apigw/evo-brm/1.0.0/device/1000021" ;
// var url = $"http://demo.weienergy.cn:15230/open-api/token/v1/oauth/token?client_id=69591850&client_secret=WHcpAryKFc28suzL&grant_type=client_credentials";
//var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-event/1.0.0/subscribe/subscribe-list?monitorType=url&category={name}";
var url = $"https://{_configuration[" DahuaAuth : Host "]}/evo-apigw/evo-event/1.0.0/subscribe/subscribe-list?monitorType=url&category={name}" ;
try
try
{
{
var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
@ -303,14 +303,14 @@ namespace Alarm.DomainService.DahAlarm
if ( ! resp . IsSuccessStatusCode )
if ( ! resp . IsSuccessStatusCode )
{
{
_l ogger . LogWarning ( "实时流请求 HTTP 失败: {Status}, Body: {Body}" , ( int ) resp . StatusCode , body ) ;
_l ogger . LogWarning ( "实时流请求 HTTP 失败: {Status}, Body: {Body}" , ( int ) resp . StatusCode , body ) ;
return new DaHApiResult < SubscriptionMap Dto > { Success = false , Code = "1010" , Msg = $"HTTP错误 {(int)resp.StatusCode}" } ;
return new DaHApiResult < SubscribeData Dto > { Success = false , Code = "1010" , Msg = $"HTTP错误 {(int)resp.StatusCode}" } ;
}
}
var result = JsonSerializer . Deserialize < DaHApiResult < SubscriptionMap Dto > > ( body ) ;
var result = JsonSerializer . Deserialize < DaHApiResult < SubscribeData Dto > > ( body ) ;
if ( result = = null | | ! result . Success )
if ( result = = null | | ! result . Success )
{
{
_l ogger . LogWarning ( "实时流请求业务失败: {Body}" , body ) ;
_l ogger . LogWarning ( "实时流请求业务失败: {Body}" , body ) ;
return new DaHApiResult < SubscriptionMap Dto > { Success = false , Code = "1010" , Msg = "实时流请求失败" } ;
return new DaHApiResult < SubscribeData Dto > { Success = false , Code = "1010" , Msg = "实时流请求失败" } ;
}
}
return result ;
return result ;
@ -318,7 +318,7 @@ namespace Alarm.DomainService.DahAlarm
catch ( Exception ex )
catch ( Exception ex )
{
{
_l ogger . LogError ( ex , "大华实时流请求出错" ) ;
_l ogger . LogError ( ex , "大华实时流请求出错" ) ;
return new DaHApiResult < SubscriptionMap Dto > { Success = false , Code = "1010" , Msg = "实时流请求失败" } ;
return new DaHApiResult < SubscribeData Dto > { Success = false , Code = "1010" , Msg = "实时流请求失败" } ;
}
}
}
}