62 lines
2.0 KiB
C#
Raw Normal View History

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;
using JiShe.ServicePro.FreeRedisProvider;
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-24 17:48:20 +08:00
public abstract class ProtocolPlugin : IProtocolPlugin
{
//头部字节长度
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;
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>();
}
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()
{
if (Info == null)
{
throw new ArgumentNullException(nameof(Info));
}
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-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-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
}
}