152 lines
6.5 KiB
C#
152 lines
6.5 KiB
C#
using JiShe.CollectBus.Common.BuildSendDatas;
|
|
using JiShe.CollectBus.Common.Consts;
|
|
using JiShe.CollectBus.Common.Enums;
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
using JiShe.CollectBus.Common.Helpers;
|
|
using JiShe.CollectBus.Common.Models;
|
|
using JiShe.CollectBus.Enums;
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
|
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
|
using JiShe.CollectBus.IotSystems.Protocols;
|
|
using JiShe.CollectBus.Kafka.Producer;
|
|
using JiShe.CollectBus.Protocol.Contracts.SendData;
|
|
using JiShe.CollectBus.Protocol.SendData;
|
|
using Mapster;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using TouchSocket.Sockets;
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
namespace JiShe.CollectBus.Protocol
|
|
{
|
|
public class T1882018ProtocolPlugin : T37612012ProtocolPlugin
|
|
{
|
|
private readonly ILogger<T1882018ProtocolPlugin> _logger;
|
|
|
|
private readonly IProducerService _producerService;
|
|
|
|
private readonly IRepository<Device, Guid> _deviceRepository;
|
|
private readonly ITcpService _tcpService;
|
|
|
|
public readonly Dictionary<string, Telemetry1882018PacketBuilder.T1882018Delegate> T188ControlHandlers;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="T1882018ProtocolPlugin"/> class.
|
|
/// </summary>
|
|
/// <param name="serviceProvider">The service provider.</param>
|
|
public T1882018ProtocolPlugin(IServiceProvider serviceProvider, ILogger<T1882018ProtocolPlugin> logger, ITcpService tcpService) : base(serviceProvider, logger, tcpService)
|
|
{
|
|
_logger = logger;
|
|
//_logger = serviceProvider.GetRequiredService<ILogger<StandardProtocolPlugin>>();
|
|
_producerService = serviceProvider.GetRequiredService<IProducerService>();
|
|
_deviceRepository = serviceProvider.GetRequiredService<IRepository<Device, Guid>>();
|
|
_tcpService = tcpService;
|
|
T188ControlHandlers = Telemetry1882018PacketBuilder.T1882018ControlHandlers;
|
|
}
|
|
|
|
public sealed override ProtocolInfo Info => new(nameof(T1882018ProtocolPlugin), "376.1/188", "TCP", "376.1协议", "DTS1980");
|
|
|
|
public override async Task<T> AnalyzeAsync<T>(ITcpSessionClient client, string messageReceived, Action<T>? sendAction = null)
|
|
{
|
|
//TB3761? tB3761 = Analysis3761(messageReceived);
|
|
//if (tB3761 != null)
|
|
//{
|
|
// if (tB3761.AFN_FC?.AFN == (int)AFN.链路接口检测)
|
|
// {
|
|
// if (tB3761.A == null || tB3761.A.Code.IsNullOrWhiteSpace() || tB3761.A.A3?.D1_D7 == null || tB3761.SEQ?.PSEQ == null)
|
|
// {
|
|
// _logger.LogError($"解析AFN.链路接口检测报文失败,报文:{messageReceived},TB3761:{tB3761.Serialize()}");
|
|
// }
|
|
// else
|
|
// {
|
|
// if (tB3761.DT?.Fn == (int)FN.登录)
|
|
// {
|
|
// // 登录回复
|
|
// if (tB3761.SEQ.CON == (int)CON.需要对该帧进行确认)
|
|
// await LoginAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ);
|
|
// }
|
|
// else if (tB3761.DT?.Fn == (int)FN.心跳)
|
|
// {
|
|
// // 心跳回复
|
|
// //心跳帧有两种情况:
|
|
// //1. 集中器先有登录帧,再有心跳帧
|
|
// //2. 集中器没有登录帧,只有心跳帧
|
|
// await HeartbeatAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ);
|
|
// }
|
|
// }
|
|
|
|
// }
|
|
// await OnTcpNormalReceived(client, tB3761);
|
|
//}
|
|
//return (tB3761 as T)!;
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装报文
|
|
/// </summary>
|
|
/// <param name="request">报文构建参数</param>
|
|
/// <returns></returns>
|
|
public override async Task<ProtocolBuildResponse> BuildAsync(ProtocolBuildRequest request)
|
|
{
|
|
if (request == null)
|
|
{
|
|
throw new Exception($"{nameof(T1882018ProtocolPlugin)} 报文构建失败,参数为空");
|
|
}
|
|
var itemCodeArr = request.ItemCode.Split('_');
|
|
var aFNStr = itemCodeArr[0];
|
|
var aFN = (AFN)aFNStr.HexToDec();
|
|
var fn = int.Parse(itemCodeArr[1]);
|
|
Telemetry3761PacketResponse builderResponse = null;
|
|
|
|
List<string> dataUnit = new List<string>();
|
|
//数据转发场景 10H_F1_1CH
|
|
if (aFNStr == "10" && request.SubProtocolRequest != null && string.IsNullOrWhiteSpace(request.SubProtocolRequest.ItemCode) == false)
|
|
{
|
|
var t645PacketHandlerName = $"C{request.SubProtocolRequest.ItemCode}_Send";
|
|
Telemetry1882018PacketResponse t645PacketResponse = null;
|
|
|
|
if (T188ControlHandlers != null && T188ControlHandlers.TryGetValue(t645PacketHandlerName
|
|
, out var t645PacketHandler))
|
|
{
|
|
t645PacketResponse = t645PacketHandler(new Telemetry1882018PacketRequest()
|
|
{
|
|
MeterAddress = request.SubProtocolRequest.MeterAddress,
|
|
Password = request.SubProtocolRequest.Password,
|
|
ItemCode = request.SubProtocolRequest.ItemCode,
|
|
});
|
|
}
|
|
|
|
if (t645PacketResponse != null)
|
|
{
|
|
dataUnit = t645PacketResponse.Data;
|
|
}
|
|
}
|
|
|
|
string afnMethonCode = $"AFN{aFNStr}_Fn_Send";
|
|
if (base.T3761AFNHandlers != null && base.T3761AFNHandlers.TryGetValue(afnMethonCode
|
|
, out var handler))
|
|
{
|
|
builderResponse = handler(new Telemetry3761PacketRequest()
|
|
{
|
|
FocusAddress = request.FocusAddress,
|
|
Fn = fn,
|
|
Pn = request.Pn,
|
|
DataUnit = dataUnit,
|
|
});
|
|
}
|
|
|
|
if (builderResponse == null)
|
|
{
|
|
return new ProtocolBuildResponse();
|
|
}
|
|
|
|
var result = builderResponse.Adapt<ProtocolBuildResponse>();
|
|
result.IsSuccess = true;
|
|
|
|
return await Task.FromResult(result);
|
|
}
|
|
}
|
|
}
|