| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- namespace SHJX.Service.Control.LiquidHandler
- {
- public class LiquidContent
- {
- private const string LIQUIDCONTENT_NAMESPACE = "SHJX.Service.Control.LiquidHandler.Content";
- private static readonly ILogger logger = LogFactory.BuildLogger(typeof(LiquidContent));
- private static readonly object obj_locker = new();
- private readonly ConcurrentDictionary<string, ILiquidContent> DicContent = new();
- public static LiquidContent StartNew
- {
- get
- {
- lock (obj_locker)
- {
- return new();
- }
- }
- }
- private LiquidContent() { }
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="liquidName">液体名称</param>
- /// <param name="handleType">处理方式</param>
- /// <param name="callBack"></param>
- /// <returns></returns>
- public bool Execute(string liquidName, string handleType, Action<object> callBack = null, params object[] args)
- {
- try
- {
- ILiquidContent content = ContentFactory(liquidName);
- bool response = handleType switch
- {
- TaskTypeName.WASH => content.WashFactory().Execute(),
- _ => content.NormalFactory().Execute(callBack, args),
- };
- logger.LogDebug($"液体{liquidName}以{handleType}方式下发{(response ? "成功" : "失败")}");
- return response;
- }
- catch (Exception ex)
- {
- logger.LogError(ex.ToString());
- return false;
- }
- }
- private ILiquidContent ContentFactory(string liquidName)
- {
- if (DicContent.TryGetValue(liquidName, out ILiquidContent value))
- {
- return value;
- }
- string fullName = CreatContentClassFullName(liquidName);
- ILiquidContent content = Activator.CreateInstance(Type.GetType(fullName), true) as ILiquidContent;
- DicContent.TryAdd(liquidName, content);
- return content;
- }
- private static string CreatContentClassFullName(string name)
- {
- return $"{LIQUIDCONTENT_NAMESPACE}.{name}Content";
- }
- }
- }
|