141 lines
6.0 KiB
C#
Raw Normal View History

using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.Enums;
2024-10-29 16:28:14 +08:00
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Common.Models;
2025-03-14 14:28:04 +08:00
using JiShe.CollectBus.IotSystems.Protocols;
2025-04-27 09:16:37 +08:00
using JiShe.CollectBus.Protocol.Models;
using JiShe.CollectBus.Protocol.T37612012;
using JiShe.CollectBus.Protocol.T37612012.SendData;
using JiShe.CollectBus.Protocol.T6452007.SendData;
2025-04-24 17:48:20 +08:00
using Mapster;
using Microsoft.Extensions.Logging;
using TouchSocket.Sockets;
2024-09-30 17:10:43 +08:00
2025-04-27 09:16:37 +08:00
namespace JiShe.CollectBus.Protocol.T6452007
{
2025-04-25 17:18:59 +08:00
/// <summary>
/// T6452007协议插件
/// </summary>
public class T6452007ProtocolPlugin : T37612012ProtocolPlugin
2024-09-30 17:10:43 +08:00
{
2025-04-25 12:01:15 +08:00
private readonly ILogger<T6452007ProtocolPlugin> _logger;
2025-04-25 17:18:59 +08:00
2025-04-25 12:01:15 +08:00
public readonly Dictionary<string, Telemetry6452007PacketBuilder.T6452007Delegate> T645ControlHandlers;
2025-04-24 17:48:20 +08:00
2024-12-19 16:07:07 +08:00
/// <summary>
2025-04-25 12:01:15 +08:00
/// Initializes a new instance of the <see cref="T6452007ProtocolPlugin"/> class.
2024-12-19 16:07:07 +08:00
/// </summary>
2025-02-24 13:47:12 +08:00
/// <param name="serviceProvider">The service provider.</param>
2025-04-25 17:18:59 +08:00
public T6452007ProtocolPlugin(IServiceProvider serviceProvider, ILogger<T6452007ProtocolPlugin> logger, ITcpService tcpService) : base(serviceProvider, logger, tcpService)
2024-09-30 17:53:14 +08:00
{
_logger = logger;
2025-04-25 12:01:15 +08:00
T645ControlHandlers = Telemetry6452007PacketBuilder.T645ControlHandlers;
2024-09-30 17:10:43 +08:00
}
2025-04-25 15:21:46 +08:00
public sealed override ProtocolInfo Info => new(nameof(T6452007ProtocolPlugin), "376.1/645-2007", "TCP", "376.1/645-2007协议", "DTS1980");
2024-10-21 13:30:53 +08:00
public override async Task<T> AnalyzeAsync<T>(ITcpSessionClient client, string messageReceived, Action<T>? sendAction = null)
2024-10-29 16:28:14 +08:00
{
2025-04-25 14:23:06 +08:00
//TODO:645解析报文
//TB3761? tB3761 = Analysis3761(messageReceived);
//if (tB3761 != null)
2025-04-25 09:28:56 +08:00
//{
2025-04-25 14:23:06 +08:00
// 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;
2025-04-25 09:28:56 +08:00
}
2025-04-23 23:42:35 +08:00
/// <summary>
2025-04-24 17:48:20 +08:00
/// 组装报文
2025-04-23 23:42:35 +08:00
/// </summary>
2025-04-24 23:39:39 +08:00
/// <param name="request">报文构建参数</param>
2025-04-23 23:42:35 +08:00
/// <returns></returns>
2025-04-24 23:39:39 +08:00
public override async Task<ProtocolBuildResponse> BuildAsync(ProtocolBuildRequest request)
2025-04-23 23:42:35 +08:00
{
2025-04-24 23:39:39 +08:00
if (request == null)
{
2025-04-25 17:18:59 +08:00
_logger.LogError($"{nameof(ProtocolBuildResponse)} 报文构建失败,参数为空");
return new ProtocolBuildResponse();
2025-04-24 23:39:39 +08:00
}
2025-04-24 17:48:20 +08:00
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
if (request.ItemCode == T37612012PacketItemCodeConst.AFN10HFN01H && request.SubProtocolRequest != null && string.IsNullOrWhiteSpace(request.SubProtocolRequest.ItemCode) == false)
2025-04-24 17:48:20 +08:00
{
2025-04-24 23:39:39 +08:00
var t645PacketHandlerName = $"C{request.SubProtocolRequest.ItemCode}_Send";
2025-04-25 12:01:15 +08:00
Telemetry6452007PacketResponse t645PacketResponse = null;
2025-04-24 17:48:20 +08:00
2025-04-24 23:39:39 +08:00
if (T645ControlHandlers != null && T645ControlHandlers.TryGetValue(t645PacketHandlerName
, out var t645PacketHandler))
2025-04-24 17:48:20 +08:00
{
2025-04-25 12:01:15 +08:00
t645PacketResponse = t645PacketHandler(new Telemetry6452007PacketRequest()
2025-04-24 17:48:20 +08:00
{
2025-04-24 23:39:39 +08:00
MeterAddress = request.SubProtocolRequest.MeterAddress,
Password = request.SubProtocolRequest.Password,
ItemCode = request.SubProtocolRequest.ItemCode,
2025-04-24 17:48:20 +08:00
});
}
if (t645PacketResponse != null)
{
dataUnit = t645PacketResponse.Data;
}
}
string afnMethonCode = $"AFN{aFNStr}_Fn_Send";
2025-04-25 17:18:59 +08:00
if (base.T3761AFNHandlers != null && base.T3761AFNHandlers.TryGetValue(afnMethonCode
2025-04-24 17:48:20 +08:00
, out var handler))
{
builderResponse = handler(new Telemetry3761PacketRequest()
{
FocusAddress = request.FocusAddress,
Fn = fn,
Pn = request.Pn,
DataUnit = dataUnit,
});
}
if (builderResponse == null)
{
2025-04-24 23:39:39 +08:00
return new ProtocolBuildResponse();
2025-04-24 17:48:20 +08:00
}
2025-04-24 23:39:39 +08:00
var result = builderResponse.Adapt<ProtocolBuildResponse>();
2025-04-24 17:48:20 +08:00
result.IsSuccess = true;
2025-04-24 17:48:20 +08:00
return await Task.FromResult(result);
2025-04-27 17:27:04 +08:00
}
2024-09-30 17:10:43 +08:00
}
}