2025-06-03 11:58:17 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Protocols;
|
2025-04-25 13:42:08 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Interfaces;
|
2025-04-27 09:16:37 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Models;
|
2025-06-03 11:58:17 +08:00
|
|
|
|
using JiShe.ServicePro.FreeRedisProvider;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
2025-04-27 09:16:37 +08:00
|
|
|
|
namespace JiShe.CollectBus.Protocol.Abstracts
|
2025-04-21 23:47:11 +08:00
|
|
|
|
{
|
2025-04-24 17:48:20 +08:00
|
|
|
|
public abstract class ProtocolPlugin : IProtocolPlugin
|
2025-04-21 23:47:11 +08:00
|
|
|
|
{
|
|
|
|
|
|
//头部字节长度
|
|
|
|
|
|
public const int hearderLen = 6;
|
|
|
|
|
|
|
|
|
|
|
|
public const int tPLen = 6;
|
|
|
|
|
|
|
|
|
|
|
|
public const string errorData = "EE";
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
2025-04-24 21:01:01 +08:00
|
|
|
|
private readonly IFreeRedisProvider _redisProvider;
|
|
|
|
|
|
|
2025-04-21 23:47:11 +08:00
|
|
|
|
public ProtocolPlugin(IServiceProvider serviceProvider, ILogger logger)
|
|
|
|
|
|
{
|
2025-04-24 17:48:20 +08:00
|
|
|
|
_logger = logger;
|
2025-04-24 21:01:01 +08:00
|
|
|
|
_redisProvider = serviceProvider.GetRequiredService<IFreeRedisProvider>();
|
2025-04-21 23:47:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public abstract ProtocolInfo Info { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public virtual async Task<ProtocolInfo> GetAsync() => await Task.FromResult(Info);
|
|
|
|
|
|
|
2025-04-22 21:06:56 +08:00
|
|
|
|
public virtual async Task LoadAsync()
|
2025-04-21 23:47:11 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (Info == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException(nameof(Info));
|
2025-05-26 11:20:32 +08:00
|
|
|
|
}
|
2025-04-21 23:47:11 +08:00
|
|
|
|
|
2025-04-24 21:01:01 +08:00
|
|
|
|
await _redisProvider.Instance.HDelAsync($"{RedisConst.ProtocolKey}", Info.Name);
|
|
|
|
|
|
await _redisProvider.Instance.HSetAsync($"{RedisConst.ProtocolKey}", Info.Name, Info);
|
2025-04-21 23:47:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-24 17:48:20 +08:00
|
|
|
|
public abstract Task<T> AnalyzeAsync<T>(ITcpSessionClient client, string messageReceived, Action<T>? receivedAction = null) where T : class;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
|
2025-04-24 17:48:20 +08:00
|
|
|
|
#region 下行命令构建
|
|
|
|
|
|
|
2025-04-23 17:49:13 +08:00
|
|
|
|
/// <summary>
|
2025-04-24 17:48:20 +08:00
|
|
|
|
/// 组装报文
|
2025-04-23 17:49:13 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2025-04-23 23:42:35 +08:00
|
|
|
|
/// <param name="entity">设备数据实体</param>
|
2025-04-24 17:48:20 +08:00
|
|
|
|
/// <param name="afnFnCode">映射读取执行方法的Code,例如10_1,表示 10H_F1_00000,10H_F1_00001,统一英文下划线分隔</param>
|
2025-04-23 17:49:13 +08:00
|
|
|
|
/// <returns></returns>
|
2025-04-25 08:33:13 +08:00
|
|
|
|
public abstract Task<ProtocolBuildResponse> BuildAsync(ProtocolBuildRequest request);
|
2025-04-24 17:48:20 +08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-04-23 17:49:13 +08:00
|
|
|
|
|
2025-04-21 23:47:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|