189 lines
8.2 KiB
C#
189 lines
8.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using DeviceDetectorNET.Parser.Device;
|
||
using JiShe.CollectBus.Common.Consts;
|
||
using JiShe.CollectBus.Common.Enums;
|
||
using JiShe.CollectBus.IotSystems.Devices;
|
||
using JiShe.CollectBus.IotSystems.MessageIssueds;
|
||
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
||
using JiShe.CollectBus.IotSystems.MeterReadingRecords;
|
||
using JiShe.CollectBus.Kafka.Attributes;
|
||
using JiShe.CollectBus.Kafka.Internal;
|
||
using JiShe.CollectBus.Protocol.Contracts;
|
||
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
||
using JiShe.CollectBus.Repository.MeterReadingRecord;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Logging;
|
||
using TouchSocket.Sockets;
|
||
using Volo.Abp.Caching;
|
||
using Volo.Abp.Domain.Repositories;
|
||
|
||
namespace JiShe.CollectBus.Subscribers
|
||
{
|
||
/// <summary>
|
||
/// 定时抄读任务消息消费订阅
|
||
/// </summary>
|
||
[Route($"/worker/app/subscriber")]
|
||
public class WorkerSubscriberAppService : CollectBusAppService, IWorkerSubscriberAppService, IKafkaSubscribe
|
||
{
|
||
private readonly ILogger<WorkerSubscriberAppService> _logger;
|
||
private readonly ITcpService _tcpService;
|
||
private readonly IServiceProvider _serviceProvider;
|
||
private readonly IRepository<Device, Guid> _deviceRepository;
|
||
private readonly IMeterReadingRecordRepository _meterReadingRecordsRepository;
|
||
|
||
|
||
/// <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>
|
||
/// <param name="deviceRepository">The Device pepository.</param>
|
||
/// <param name="serviceProvider">The service provider.</param>
|
||
public WorkerSubscriberAppService(ILogger<WorkerSubscriberAppService> logger,
|
||
ITcpService tcpService,
|
||
IRepository<Device, Guid> deviceRepository,
|
||
IMeterReadingRecordRepository meterReadingRecordsRepository,
|
||
IServiceProvider serviceProvider)
|
||
{
|
||
_logger = logger;
|
||
_tcpService = tcpService;
|
||
_serviceProvider = serviceProvider;
|
||
_deviceRepository = deviceRepository;
|
||
_meterReadingRecordsRepository = meterReadingRecordsRepository;
|
||
}
|
||
|
||
|
||
#region 电表消息采集
|
||
|
||
/// <summary>
|
||
/// 一分钟定时抄读任务消息消费订阅
|
||
/// </summary>
|
||
/// <param name="receivedMessage"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[Route("ammeter/oneminute/issued-event")]
|
||
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerOneMinuteIssuedEventName)]
|
||
//[CapSubscribe(ProtocolConst.AmmeterSubscriberWorkerOneMinuteIssuedEventName)]
|
||
public async Task<ISubscribeAck> AmmeterScheduledMeterOneMinuteReadingIssuedEvent(ScheduledMeterReadingIssuedEventMessage receivedMessage)
|
||
{
|
||
_logger.LogInformation("1分钟采集电表数据下行消息消费队列开始处理");
|
||
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
||
if (protocolPlugin == null)
|
||
{
|
||
_logger.LogError("【1分钟采集电表数据下行消息消费队列开始处理】协议不存在!");
|
||
}
|
||
else
|
||
{
|
||
var device = await _deviceRepository.FirstOrDefaultAsync(a => a.Number == receivedMessage.FocusAddress);
|
||
if (device != null)
|
||
{
|
||
await _tcpService.SendAsync(device.ClientId, Convert.FromHexString(receivedMessage.MessageHexString));
|
||
|
||
}
|
||
}
|
||
return SubscribeAck.Success();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 5分钟采集电表数据下行消息消费订阅
|
||
/// </summary>
|
||
/// <param name="receivedMessage"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[Route("ammeter/fiveminute/issued-event")]
|
||
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerFiveMinuteIssuedEventName)]
|
||
//[CapSubscribe(ProtocolConst.AmmeterSubscriberWorkerFiveMinuteIssuedEventName)]
|
||
public async Task<ISubscribeAck> AmmeterScheduledMeterFiveMinuteReadingIssuedEvent(ScheduledMeterReadingIssuedEventMessage receivedMessage)
|
||
{
|
||
_logger.LogInformation("5分钟采集电表数据下行消息消费队列开始处理");
|
||
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
||
if (protocolPlugin == null)
|
||
{
|
||
_logger.LogError("【5分钟采集电表数据下行消息消费队列开始处理】协议不存在!");
|
||
}
|
||
else
|
||
{
|
||
var device = await _deviceRepository.FirstOrDefaultAsync(a => a.Number == receivedMessage.FocusAddress);
|
||
if (device != null)
|
||
{
|
||
await _tcpService.SendAsync(device.ClientId, Convert.FromHexString(receivedMessage.MessageHexString));
|
||
|
||
}
|
||
}
|
||
return SubscribeAck.Success();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 15分钟采集电表数据下行消息消费订阅
|
||
/// </summary>
|
||
/// <param name="receivedMessage"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[Route("ammeter/fifteenminute/issued-event")]
|
||
[KafkaSubscribe(ProtocolConst.AmmeterSubscriberWorkerFifteenMinuteIssuedEventName)]
|
||
//[CapSubscribe(ProtocolConst.AmmeterSubscriberWorkerFifteenMinuteIssuedEventName)]
|
||
public async Task<ISubscribeAck> AmmeterScheduledMeterFifteenMinuteReadingIssuedEvent(ScheduledMeterReadingIssuedEventMessage receivedMessage)
|
||
{
|
||
_logger.LogInformation("15分钟采集电表数据下行消息消费队列开始处理");
|
||
try
|
||
{
|
||
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
||
if (protocolPlugin == null)
|
||
{
|
||
_logger.LogError("【15分钟采集电表数据下行消息消费队列开始处理】协议不存在!");
|
||
}
|
||
else
|
||
{
|
||
var device = await _deviceRepository.FirstOrDefaultAsync(a => a.Number == receivedMessage.FocusAddress);
|
||
if (device != null)
|
||
{
|
||
await _tcpService.SendAsync(device.ClientId, Convert.FromHexString(receivedMessage.MessageHexString));
|
||
|
||
}
|
||
}
|
||
return SubscribeAck.Success();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
throw ex;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 水表消息采集
|
||
|
||
/// <summary>
|
||
/// 水表数据下行消息消费订阅
|
||
/// </summary>
|
||
/// <param name="receivedMessage"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[Route("watermeter/fifteenminute/issued-event")]
|
||
[KafkaSubscribe(ProtocolConst.WatermeterSubscriberWorkerAutoReadingIssuedEventName)]
|
||
//[CapSubscribe(ProtocolConst.WatermeterSubscriberWorkerAutoReadingIssuedEventName)]
|
||
public async Task<ISubscribeAck> WatermeterSubscriberWorkerAutoReadingIssuedEvent(ScheduledMeterReadingIssuedEventMessage receivedMessage)
|
||
{
|
||
_logger.LogInformation("15分钟采集水表数据下行消息消费队列开始处理");
|
||
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
||
if (protocolPlugin == null)
|
||
{
|
||
_logger.LogError("【15分钟采集水表数据下行消息消费队列开始处理】协议不存在!");
|
||
}
|
||
else
|
||
{
|
||
var device = await _deviceRepository.FindAsync(a => a.Number == receivedMessage.FocusAddress);
|
||
if (device != null)
|
||
{
|
||
await _tcpService.SendAsync(device.ClientId, Convert.FromHexString(receivedMessage.MessageHexString));
|
||
}
|
||
}
|
||
return SubscribeAck.Success();
|
||
}
|
||
#endregion
|
||
}
|
||
}
|