2024-12-19 16:07:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-03-14 14:28:04 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
2025-03-19 14:31:04 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using MassTransit;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Consumers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Batch 一次最多 100 个,最多 10 个并发批次
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ReceivedConsumer: IConsumer<Batch<MessageReceived>>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<ReceivedConsumer> _logger;
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
private readonly IRepository<MessageReceived, Guid> _messageReceivedEventRepository;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MessageReceivedConsumer
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
|
/// <param name="messageReceivedEventRepository"></param>
|
|
|
|
|
|
public ReceivedConsumer(ILogger<ReceivedConsumer> logger,
|
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
|
IRepository<MessageReceived, Guid> messageReceivedEventRepository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
|
_messageReceivedEventRepository = messageReceivedEventRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Consume(ConsumeContext<Batch<MessageReceived>> context)
|
|
|
|
|
|
{
|
|
|
|
|
|
const string protocolType = "Standard";
|
|
|
|
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
|
|
|
|
|
|
if (protocolPlugin == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError("协议不存在!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var list = new List<MessageReceived>();
|
|
|
|
|
|
foreach (var contextItem in context.Message)
|
|
|
|
|
|
{
|
2025-03-19 14:31:04 +08:00
|
|
|
|
await protocolPlugin.AnalyzeAsync<TB3761FN>(contextItem.Message);
|
2024-12-19 16:07:07 +08:00
|
|
|
|
list.Add(contextItem.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
await _messageReceivedEventRepository.InsertManyAsync(list);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|