2025-04-23 16:17:29 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
|
|
|
|
|
using JiShe.CollectBus.IoTDB.Interface;
|
2025-03-20 16:40:27 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.MeterReadingRecords;
|
2025-04-15 18:58:38 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Attributes;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Internal;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-04-23 16:17:29 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Subscribers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 定时抄读任务消息消费订阅
|
|
|
|
|
|
/// </summary>
|
2025-04-18 09:50:00 +08:00
|
|
|
|
public class WorkerSubscriberAppService : CollectBusAppService, IWorkerSubscriberAppService, IKafkaSubscribe
|
2025-03-12 09:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<WorkerSubscriberAppService> _logger;
|
|
|
|
|
|
private readonly ITcpService _tcpService;
|
2025-04-23 16:17:29 +08:00
|
|
|
|
private readonly IIoTDbProvider _dbProvider;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="WorkerSubscriberAppService"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
|
/// <param name="tcpService">The TCP service.</param>
|
2025-04-23 16:17:29 +08:00
|
|
|
|
/// <param name="dbProvider">IoTDB数据驱动</param>
|
|
|
|
|
|
public WorkerSubscriberAppService(ILogger<WorkerSubscriberAppService> logger,
|
2025-03-13 10:51:16 +08:00
|
|
|
|
ITcpService tcpService,
|
2025-04-23 16:17:29 +08:00
|
|
|
|
IIoTDbProvider dbProvider)
|
2025-03-12 09:58:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_tcpService = tcpService;
|
2025-04-23 16:17:29 +08:00
|
|
|
|
_dbProvider = dbProvider;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-13 10:51:16 +08:00
|
|
|
|
|
2025-04-08 17:44:42 +08:00
|
|
|
|
#region 电表消息采集
|
2025-03-20 16:40:27 +08:00
|
|
|
|
|
2025-03-13 10:51:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 一分钟定时抄读任务消息消费订阅
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-15 18:58:38 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerOneMinuteIssuedEventName)]
|
2025-04-23 16:17:29 +08:00
|
|
|
|
public async Task<ISubscribeAck> AmmeterScheduledMeterOneMinuteReadingIssuedEvent(MeterReadingTelemetryPacketInfo receivedMessage)
|
2025-03-13 10:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("1分钟采集电表数据下行消息消费队列开始处理");
|
2025-04-23 16:17:29 +08:00
|
|
|
|
return await SendMessagesAsync(receivedMessage);
|
2025-03-13 10:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 5分钟采集电表数据下行消息消费订阅
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-15 18:58:38 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerFiveMinuteIssuedEventName)]
|
2025-04-23 16:17:29 +08:00
|
|
|
|
public async Task<ISubscribeAck> AmmeterScheduledMeterFiveMinuteReadingIssuedEvent(MeterReadingTelemetryPacketInfo receivedMessage)
|
2025-03-13 10:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("5分钟采集电表数据下行消息消费队列开始处理");
|
2025-04-23 16:17:29 +08:00
|
|
|
|
return await SendMessagesAsync(receivedMessage);
|
2025-03-13 10:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 15分钟采集电表数据下行消息消费订阅
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-15 18:58:38 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerFifteenMinuteIssuedEventName)]
|
2025-04-23 16:17:29 +08:00
|
|
|
|
public async Task<ISubscribeAck> AmmeterScheduledMeterFifteenMinuteReadingIssuedEvent(MeterReadingTelemetryPacketInfo receivedMessage)
|
2025-03-13 10:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("15分钟采集电表数据下行消息消费队列开始处理");
|
2025-04-23 16:17:29 +08:00
|
|
|
|
return await SendMessagesAsync(receivedMessage);
|
|
|
|
|
|
}
|
2025-03-21 11:48:31 +08:00
|
|
|
|
|
2025-04-23 16:17:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 电表自动阀控下行消息消费订阅
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerAutoValveControlIssuedEventName)]
|
|
|
|
|
|
public async Task<ISubscribeAck> AmmeterScheduledAutoValveControl(MeterReadingTelemetryPacketInfo receivedMessage)
|
|
|
|
|
|
{
|
2025-04-24 23:39:39 +08:00
|
|
|
|
//todo 如果是时段自动阀控,需要检查当前的时间,如果时间在自动阀控时间段内,则发送自动阀控报文,否则不发送,尤其是消息队列阻塞或者延时过长的时候。以免造成生产事故。
|
2025-04-23 16:17:29 +08:00
|
|
|
|
_logger.LogInformation("电表自动阀控下行消息消费队列开始处理");
|
|
|
|
|
|
return await SendMessagesAsync(receivedMessage);
|
2025-03-13 10:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 水表消息采集
|
2025-04-23 16:17:29 +08:00
|
|
|
|
|
2025-03-12 09:58:37 +08:00
|
|
|
|
/// <summary>
|
2025-04-14 16:41:41 +08:00
|
|
|
|
/// 水表数据下行消息消费订阅
|
2025-03-12 09:58:37 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-15 18:58:38 +08:00
|
|
|
|
[KafkaSubscribe(ProtocolConst.WatermeterSubscriberWorkerAutoReadingIssuedEventName)]
|
2025-04-23 16:17:29 +08:00
|
|
|
|
public async Task<ISubscribeAck> WatermeterSubscriberWorkerAutoReadingIssuedEvent(MeterReadingTelemetryPacketInfo receivedMessage)
|
2025-03-12 09:58:37 +08:00
|
|
|
|
{
|
2025-04-23 16:17:29 +08:00
|
|
|
|
return await SendMessagesAsync(receivedMessage);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备报文发送
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="receivedMessage">消息记录</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private async Task<ISubscribeAck> SendMessagesAsync(MeterReadingTelemetryPacketInfo receivedMessage)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2025-03-12 09:58:37 +08:00
|
|
|
|
{
|
2025-04-23 16:17:29 +08:00
|
|
|
|
var checkResult = _tcpService.ClientExists(receivedMessage.FocusAddress);
|
|
|
|
|
|
if (checkResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _tcpService.SendAsync(receivedMessage.FocusAddress, Convert.FromHexString(receivedMessage.IssuedMessageHexString));
|
|
|
|
|
|
|
|
|
|
|
|
receivedMessage.IsSend = true;
|
2025-04-27 15:44:54 +08:00
|
|
|
|
receivedMessage.SendNum += 1;
|
|
|
|
|
|
receivedMessage.NextSendTime = DateTime.Now.AddMinutes(5);
|
2025-04-23 16:17:29 +08:00
|
|
|
|
await _dbProvider.InsertAsync(receivedMessage);
|
|
|
|
|
|
|
|
|
|
|
|
return SubscribeAck.Success();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2025-03-13 10:51:16 +08:00
|
|
|
|
{
|
2025-04-23 16:17:29 +08:00
|
|
|
|
return SubscribeAck.Fail();
|
2025-03-13 10:51:16 +08:00
|
|
|
|
}
|
2025-03-12 09:58:37 +08:00
|
|
|
|
}
|
2025-04-23 16:17:29 +08:00
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-03-12 09:58:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|