2025-04-22 21:01:28 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
|
|
|
|
using JiShe.CollectBus.FreeRedis;
|
|
|
|
|
|
using JiShe.CollectBus.IotSystems.Protocols;
|
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using Volo.Abp;
|
2025-04-25 09:28:56 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-04-22 21:01:28 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Protocol.Contracts.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ProtocolService : IProtocolService, ISingletonDependency
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IFreeRedisProvider _freeRedisProvider;
|
2025-04-23 15:10:20 +08:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-04-25 09:28:56 +08:00
|
|
|
|
private readonly ILogger<ProtocolService> _logger;
|
2025-04-22 21:01:28 +08:00
|
|
|
|
|
2025-04-25 09:28:56 +08:00
|
|
|
|
public ProtocolService(IFreeRedisProvider freeRedisProvider, IServiceProvider serviceProvider, ILogger<ProtocolService> logger)
|
2025-04-22 21:01:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
_freeRedisProvider = freeRedisProvider;
|
2025-04-23 15:10:20 +08:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2025-04-25 09:28:56 +08:00
|
|
|
|
_logger= logger;
|
2025-04-22 21:01:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过仪器设备型号获取协议信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="deviceCode"></param>
|
|
|
|
|
|
/// <param name="isSpecial"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
|
|
|
|
public async Task<ProtocolInfo> FirstOrDefaultByDeviceAsync(string deviceCode, bool isSpecial = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var protocols = await _freeRedisProvider.Instance.HGetAllAsync<ProtocolInfo>(RedisConst.ProtocolKey);
|
|
|
|
|
|
var keyValuePair = protocols.FirstOrDefault(a => ContainsExactPartRegex(deviceCode, a.Value.RegularExpression));
|
|
|
|
|
|
if (!keyValuePair.Key.IsNullOrWhiteSpace() || keyValuePair.Value != null) return keyValuePair.Value;
|
|
|
|
|
|
if (isSpecial) throw new UserFriendlyException("The device protocol plugin does not exist!", ExceptionCode.NotFound);
|
|
|
|
|
|
var hasStandardProtocolPlugin = protocols.TryGetValue("StandardProtocolPlugin", out var protocolInfo);
|
|
|
|
|
|
if (!hasStandardProtocolPlugin) throw new UserFriendlyException("Standard protocol plugin does not exist!", ExceptionCode.NotFound);
|
|
|
|
|
|
return protocolInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-23 15:10:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取协议池服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="deviceCode"></param>
|
|
|
|
|
|
/// <param name="isSpecial"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<IProtocolPlugin?> GetProtocolServiceAsync(string deviceCode, bool isSpecial = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ProtocolInfo protocolInfo= await FirstOrDefaultByDeviceAsync(deviceCode, isSpecial);
|
|
|
|
|
|
if(protocolInfo==null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
return _serviceProvider.GetKeyedService<IProtocolPlugin>(protocolInfo.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-04-25 09:28:56 +08:00
|
|
|
|
_logger.LogError(ex, "获取协议失败");
|
2025-04-23 15:10:20 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-22 21:01:28 +08:00
|
|
|
|
private static bool ContainsExactPartRegex(string searchPattern, string fullString)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 构建正则表达式 - 匹配以逗号或开头为边界,以逗号或结尾为边界的部分
|
|
|
|
|
|
var pattern = $"(^|,)\\s*{Regex.Escape(searchPattern)}\\s*(,|$)";
|
|
|
|
|
|
return Regex.IsMatch(fullString, pattern, RegexOptions.IgnoreCase);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|