129 lines
6.1 KiB
C#
129 lines
6.1 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using DotNetCore.CAP;
|
|
using JiShe.CollectBus.Common.Enums;
|
|
using JiShe.CollectBus.Common.Models;
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
|
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
|
using JiShe.CollectBus.Protocol.Contracts;
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using TouchSocket.Sockets;
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
namespace JiShe.CollectBus.Subscribers
|
|
{
|
|
public class SubscriberAppService : CollectBusAppService, ISubscriberAppService,ICapSubscribe
|
|
{
|
|
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;
|
|
private readonly IRepository<MessageReceived, Guid> _messageReceivedEventRepository;
|
|
private readonly IRepository<Device, Guid> _deviceRepository;
|
|
|
|
/// <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>
|
|
/// <param name="messageReceivedEventRepository">The message received event repository.</param>
|
|
/// <param name="deviceRepository">The device repository.</param>
|
|
public SubscriberAppService(ILogger<SubscriberAppService> logger,
|
|
ITcpService tcpService, IServiceProvider serviceProvider,
|
|
IRepository<MessageReceivedLogin, Guid> messageReceivedLoginEventRepository,
|
|
IRepository<MessageReceivedHeartbeat, Guid> messageReceivedHeartbeatEventRepository,
|
|
IRepository<MessageReceived, Guid> messageReceivedEventRepository,
|
|
IRepository<Device, Guid> deviceRepository)
|
|
{
|
|
_logger = logger;
|
|
_tcpService = tcpService;
|
|
_serviceProvider = serviceProvider;
|
|
_messageReceivedLoginEventRepository = messageReceivedLoginEventRepository;
|
|
_messageReceivedHeartbeatEventRepository = messageReceivedHeartbeatEventRepository;
|
|
_messageReceivedEventRepository = messageReceivedEventRepository;
|
|
_deviceRepository = deviceRepository;
|
|
}
|
|
|
|
[CapSubscribe(ProtocolConst.SubscriberIssuedEventName)]
|
|
public async Task IssuedEvent(IssuedEventMessage issuedEventMessage)
|
|
{
|
|
switch (issuedEventMessage.Type)
|
|
{
|
|
case IssuedEventType.Heartbeat:
|
|
_logger.LogInformation($"IssuedEvent:{issuedEventMessage.MessageId}");
|
|
var heartbeatEntity = await _messageReceivedHeartbeatEventRepository.GetAsync(a => a.MessageId == issuedEventMessage.MessageId);
|
|
heartbeatEntity.AckTime = Clock.Now;
|
|
heartbeatEntity.IsAck = true;
|
|
await _messageReceivedHeartbeatEventRepository.UpdateAsync(heartbeatEntity);
|
|
break;
|
|
case IssuedEventType.Login:
|
|
var loginEntity = await _messageReceivedLoginEventRepository.GetAsync(a => a.MessageId == issuedEventMessage.MessageId);
|
|
loginEntity.AckTime = Clock.Now;
|
|
loginEntity.IsAck = true;
|
|
await _messageReceivedLoginEventRepository.UpdateAsync(loginEntity);
|
|
break;
|
|
case IssuedEventType.Data:
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
var device = await _deviceRepository.FindAsync(a => a.Number == issuedEventMessage.DeviceNo);
|
|
if (device!=null)
|
|
{
|
|
await _tcpService.SendAsync(device.ClientId, issuedEventMessage.Message);
|
|
}
|
|
}
|
|
|
|
[CapSubscribe(ProtocolConst.SubscriberReceivedEventName)]
|
|
public async Task ReceivedEvent(MessageReceived receivedMessage)
|
|
{
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
if (protocolPlugin == null)
|
|
{
|
|
_logger.LogError("协议不存在!");
|
|
}
|
|
else
|
|
{
|
|
await protocolPlugin.AnalyzeAsync(receivedMessage);
|
|
await _messageReceivedEventRepository.InsertAsync(receivedMessage);
|
|
}
|
|
}
|
|
|
|
[CapSubscribe(ProtocolConst.SubscriberReceivedHeartbeatEventName)]
|
|
public async Task ReceivedHeartbeatEvent(MessageReceivedHeartbeat receivedHeartbeatMessage)
|
|
{
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
if (protocolPlugin == null)
|
|
{
|
|
_logger.LogError("协议不存在!");
|
|
}
|
|
else
|
|
{
|
|
await protocolPlugin.HeartbeatAsync(receivedHeartbeatMessage);
|
|
await _messageReceivedHeartbeatEventRepository.InsertAsync(receivedHeartbeatMessage);
|
|
}
|
|
}
|
|
|
|
[CapSubscribe(ProtocolConst.SubscriberReceivedLoginEventName)]
|
|
public async Task ReceivedLoginEvent(MessageReceivedLogin receivedLoginMessage)
|
|
{
|
|
var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>("StandardProtocolPlugin");
|
|
if (protocolPlugin == null)
|
|
{
|
|
_logger.LogError("协议不存在!");
|
|
}
|
|
else
|
|
{
|
|
await protocolPlugin.LoginAsync(receivedLoginMessage);
|
|
await _messageReceivedLoginEventRepository.InsertAsync(receivedLoginMessage);
|
|
}
|
|
}
|
|
}
|
|
}
|