2025-04-18 09:50:00 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
2025-04-16 09:54:21 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
2025-04-15 16:05:07 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Helpers;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Models;
|
2025-03-14 14:28:04 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
|
|
|
|
|
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
2025-03-19 14:31:04 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.MeterReadingRecords;
|
2025-04-15 18:58:38 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Attributes;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
2025-03-19 14:31:04 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
2025-03-24 21:55:22 +08:00
|
|
|
|
using JiShe.CollectBus.Repository.MeterReadingRecord;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-04-14 10:20:48 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-04-17 20:28:50 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Interface;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using JiShe.CollectBus.Kafka.Internal;
|
2025-04-22 09:40:09 +08:00
|
|
|
|
using JiShe.CollectBus.IoTDB.Provider;
|
2025-04-22 16:48:53 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Dto;
|
|
|
|
|
|
using System.Collections;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Subscribers
|
|
|
|
|
|
{
|
2025-04-18 09:50:00 +08:00
|
|
|
|
public class SubscriberAppService : CollectBusAppService, ISubscriberAppService, IKafkaSubscribe
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<SubscriberAppService> _logger;
|
|
|
|
|
|
private readonly ITcpService _tcpService;
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
private readonly IRepository<MessageReceivedLogin, Guid> _messageReceivedLoginEventRepository;
|
|
|
|
|
|
private readonly IRepository<MessageReceivedHeartbeat, Guid> _messageReceivedHeartbeatEventRepository;
|
2025-03-24 21:55:22 +08:00
|
|
|
|
private readonly IMeterReadingRecordRepository _meterReadingRecordsRepository;
|
2025-04-21 10:17:40 +08:00
|
|
|
|
private readonly IIoTDbProvider _dbProvider;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="SubscriberAppService"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
|
/// <param name="tcpService">The TCP service.</param>
|
|
|
|
|
|
/// <param name="serviceProvider">The service provider.</param>
|
|
|
|
|
|
/// <param name="messageReceivedLoginEventRepository">The message received login event repository.</param>
|
|
|
|
|
|
/// <param name="messageReceivedHeartbeatEventRepository">The message received heartbeat event repository.</param>
|
2025-03-19 14:31:04 +08:00
|
|
|
|
/// <param name="meterReadingRecordsRepository">The device repository.</param>
|
2025-03-24 21:55:22 +08:00
|
|
|
|
public SubscriberAppService(ILogger<SubscriberAppService> logger,
|
2025-04-21 09:54:34 +08:00
|
|
|
|
ITcpService tcpService,
|
|
|
|
|
|
IServiceProvider serviceProvider,
|
2025-03-24 21:55:22 +08:00
|
|
|
|
IRepository<MessageReceivedLogin, Guid> messageReceivedLoginEventRepository,
|
|
|
|
|
|
IRepository<MessageReceivedHeartbeat, Guid> messageReceivedHeartbeatEventRepository,
|
2025-04-22 10:05:38 +08:00
|
|
|
|
IIoTDbProvider dbProvider,
|
2025-04-10 17:06:53 +08:00
|
|
|
|
IMeterReadingRecordRepository meterReadingRecordsRepository)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_tcpService = tcpService;
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
|
_messageReceivedLoginEventRepository = messageReceivedLoginEventRepository;
|
|
|
|
|
|
_messageReceivedHeartbeatEventRepository = messageReceivedHeartbeatEventRepository;
|
2025-03-19 14:31:04 +08:00
|
|
|
|
_meterReadingRecordsRepository = meterReadingRecordsRepository;
|
2025-04-10 17:06:53 +08:00
|
|
|
|
_dbProvider = dbProvider;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-19 00:30:58 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberLoginIssuedEventName, EnableBatch = true)]
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//[CapSubscribe(ProtocolConst.SubscriberLoginIssuedEventName)]
|
2025-04-19 00:30:58 +08:00
|
|
|
|
public async Task<ISubscribeAck> LoginIssuedEvent(List<IssuedEventMessage> issuedEventMessages)
|
2025-04-22 09:34:59 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
bool isAck = false;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
foreach (var issuedEventMessage in issuedEventMessages)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
var loginEntity = await _messageReceivedLoginEventRepository.FirstOrDefaultAsync(a => a.MessageId == issuedEventMessage.MessageId);
|
|
|
|
|
|
if (loginEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
isAck=false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-04-22 09:34:59 +08:00
|
|
|
|
_logger.LogInformation($"集中器地址{issuedEventMessage.ClientId} 登录回复下发内容:{issuedEventMessage.Serialize()}");
|
2025-04-22 16:48:53 +08:00
|
|
|
|
|
2025-04-22 09:34:59 +08:00
|
|
|
|
loginEntity.AckTime = Clock.Now;
|
|
|
|
|
|
loginEntity.IsAck = true;
|
|
|
|
|
|
await _messageReceivedLoginEventRepository.UpdateAsync(loginEntity);
|
2025-04-22 16:48:53 +08:00
|
|
|
|
if (_tcpService.ClientExists(issuedEventMessage.ClientId))
|
|
|
|
|
|
await _tcpService.SendAsync(issuedEventMessage.ClientId, issuedEventMessage.Message);
|
|
|
|
|
|
isAck = true;
|
|
|
|
|
|
|
2025-04-19 00:30:58 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
// TODO:暂时ACK,等后续处理是否放到私信队列中
|
|
|
|
|
|
return isAck? SubscribeAck.Success(): SubscribeAck.Fail();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-19 00:30:58 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberHeartbeatIssuedEventName, EnableBatch = true)]
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//[CapSubscribe(ProtocolConst.SubscriberHeartbeatIssuedEventName)]
|
2025-04-19 00:30:58 +08:00
|
|
|
|
public async Task<ISubscribeAck> HeartbeatIssuedEvent(List<IssuedEventMessage> issuedEventMessages)
|
2025-04-08 23:15:46 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
bool isAck = false;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
foreach (var issuedEventMessage in issuedEventMessages)
|
2025-04-08 23:15:46 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
var heartbeatEntity = await _messageReceivedHeartbeatEventRepository.FirstOrDefaultAsync(a => a.MessageId == issuedEventMessage.MessageId);
|
|
|
|
|
|
if (heartbeatEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
isAck = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-04-22 09:34:59 +08:00
|
|
|
|
_logger.LogWarning($"集中器地址{issuedEventMessage.ClientId} 心跳回复下发内容:{issuedEventMessage.Serialize()}");
|
2025-04-22 16:48:53 +08:00
|
|
|
|
|
2025-04-22 09:34:59 +08:00
|
|
|
|
heartbeatEntity.AckTime = Clock.Now;
|
|
|
|
|
|
heartbeatEntity.IsAck = true;
|
|
|
|
|
|
await _messageReceivedHeartbeatEventRepository.UpdateAsync(heartbeatEntity);
|
2025-04-19 00:30:58 +08:00
|
|
|
|
//var device = await _deviceRepository.FindAsync(a => a.Number == issuedEventMessage.DeviceNo);
|
|
|
|
|
|
//if (device != null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// await _tcpService.SendAsync(device.ClientId, issuedEventMessage.Message);
|
|
|
|
|
|
//}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
if(_tcpService.ClientExists(issuedEventMessage.ClientId))
|
|
|
|
|
|
await _tcpService.SendAsync(issuedEventMessage.ClientId, issuedEventMessage.Message);
|
2025-04-19 00:30:58 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
// TODO:暂时ACK,等后续处理是否放到私信队列中
|
|
|
|
|
|
return isAck ? SubscribeAck.Success() : SubscribeAck.Fail();
|
2025-04-08 23:15:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-15 18:58:38 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberReceivedEventName)]
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//[CapSubscribe(ProtocolConst.SubscriberReceivedEventName)]
|
2025-04-15 18:58:38 +08:00
|
|
|
|
public async Task<ISubscribeAck> ReceivedEvent(MessageReceived receivedMessage)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-10 17:06:53 +08:00
|
|
|
|
var currentTime = Clock.Now;
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
|
|
|
|
if (protocolPlugin == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError("协议不存在!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-03-19 14:31:04 +08:00
|
|
|
|
//todo 会根据不同的协议进行解析,然后做业务处理
|
2025-04-22 09:34:59 +08:00
|
|
|
|
TB3761? tB3761 = protocolPlugin.Analysis3761(receivedMessage.MessageHexString);
|
2025-04-21 23:47:11 +08:00
|
|
|
|
if (tB3761 == null)
|
2025-03-24 21:55:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}");
|
2025-04-15 18:58:38 +08:00
|
|
|
|
return SubscribeAck.Success();
|
2025-03-24 21:55:22 +08:00
|
|
|
|
}
|
2025-04-21 23:47:11 +08:00
|
|
|
|
if (tB3761.DT == null || tB3761.AFN_FC == null)
|
2025-03-24 21:55:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}");
|
2025-04-15 18:58:38 +08:00
|
|
|
|
return SubscribeAck.Success();
|
2025-03-24 21:55:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-10 17:06:53 +08:00
|
|
|
|
//报文入库
|
|
|
|
|
|
var entity = new MeterReadingRecords()
|
2025-03-24 21:55:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
ReceivedMessageHexString = receivedMessage.MessageHexString,
|
2025-04-21 23:47:11 +08:00
|
|
|
|
AFN = (AFN)tB3761.AFN_FC.AFN,
|
|
|
|
|
|
Fn = tB3761.DT.Fn,
|
2025-03-24 21:55:22 +08:00
|
|
|
|
Pn = 0,
|
|
|
|
|
|
FocusAddress = "",
|
|
|
|
|
|
MeterAddress = "",
|
2025-04-10 17:06:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//如果没数据,则插入,有数据则更新
|
|
|
|
|
|
var updateEntity = await _meterReadingRecordsRepository.FirOrDefaultAsync(entity, currentTime);
|
|
|
|
|
|
if (updateEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _meterReadingRecordsRepository.InsertAsync(entity, currentTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-10 23:31:43 +08:00
|
|
|
|
//_dbProvider.InsertAsync();
|
2025-04-10 17:06:53 +08:00
|
|
|
|
//todo 查找是否有下发任务
|
2025-03-24 21:55:22 +08:00
|
|
|
|
|
2025-03-19 14:31:04 +08:00
|
|
|
|
//await _messageReceivedEventRepository.InsertAsync(receivedMessage);
|
2025-04-15 18:58:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
2025-04-15 18:58:38 +08:00
|
|
|
|
return SubscribeAck.Success();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-19 00:30:58 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberHeartbeatReceivedEventName, EnableBatch = true)]
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//[CapSubscribe(ProtocolConst.SubscriberHeartbeatReceivedEventName)]
|
2025-04-19 00:30:58 +08:00
|
|
|
|
public async Task<ISubscribeAck> ReceivedHeartbeatEvent(List<MessageReceivedHeartbeat> receivedHeartbeatMessages)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
//foreach (var receivedHeartbeatMessage in receivedHeartbeatMessages)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
|
|
|
|
// if (protocolPlugin == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _logger.LogError("协议不存在!");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// //await protocolPlugin.HeartbeatAsync(receivedHeartbeatMessage);
|
|
|
|
|
|
// await _messageReceivedHeartbeatEventRepository.InsertAsync(receivedHeartbeatMessage);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
await _messageReceivedHeartbeatEventRepository.InsertManyAsync(receivedHeartbeatMessages);
|
|
|
|
|
|
|
2025-04-15 18:58:38 +08:00
|
|
|
|
return SubscribeAck.Success();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-19 00:30:58 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberLoginReceivedEventName,EnableBatch =true)]
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//[CapSubscribe(ProtocolConst.SubscriberLoginReceivedEventName)]
|
2025-04-19 00:30:58 +08:00
|
|
|
|
public async Task<ISubscribeAck> ReceivedLoginEvent(List<MessageReceivedLogin> receivedLoginMessages)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
//foreach (var receivedLoginMessage in receivedLoginMessages)
|
|
|
|
|
|
//{
|
|
|
|
|
|
//var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
|
|
|
|
//if (protocolPlugin == null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// _logger.LogError("协议不存在!");
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// //await protocolPlugin.LoginAsync(receivedLoginMessage);
|
|
|
|
|
|
// await _messageReceivedLoginEventRepository.InsertAsync(receivedLoginMessage);
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
await _messageReceivedLoginEventRepository.InsertManyAsync(receivedLoginMessages);
|
|
|
|
|
|
return SubscribeAck.Success();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[KafkaSubscribe(ProtocolConst.SubscriberAFN01HReceivedEventNameTemp)]
|
|
|
|
|
|
public async Task<ISubscribeAck> ReceivedAFN00Event(MessageReceived receivedMessage)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
|
|
|
|
if (protocolPlugin == null)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
_logger.LogError("协议不存在!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
TB3761? tB3761 = protocolPlugin.Analysis3761(receivedMessage.MessageHexString);
|
|
|
|
|
|
if (tB3761 == null)
|
2025-04-19 00:30:58 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}");
|
|
|
|
|
|
return SubscribeAck.Success();
|
2025-04-19 00:30:58 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
if (tB3761.DT == null || tB3761.AFN_FC == null)
|
2025-04-19 00:30:58 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}");
|
|
|
|
|
|
return SubscribeAck.Success();
|
2025-04-19 00:30:58 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
//string serverName = $"AFN{tB3761.AFN_FC.AFN}_F{tB3761.DT.Fn}_Analysis";
|
|
|
|
|
|
//var analysisStrategy = _serviceProvider.GetKeyedService<IAnalysisStrategy>($"AFN0_F1_Analysis");
|
|
|
|
|
|
|
|
|
|
|
|
//var data = await analysisStrategy.ExecuteAsync<UnitDataDto<AFN0_F1_AnalysisDto>>(tB3761);
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
|
2025-04-15 18:58:38 +08:00
|
|
|
|
return SubscribeAck.Success();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
2025-04-22 16:48:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|