LiquidContent.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. namespace SHJX.Service.Control.LiquidHandler
  2. {
  3. public class LiquidContent
  4. {
  5. private const string LIQUIDCONTENT_NAMESPACE = "SHJX.Service.Control.LiquidHandler.Content";
  6. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(LiquidContent));
  7. private static readonly object obj_locker = new();
  8. private readonly ConcurrentDictionary<string, ILiquidContent> DicContent = new();
  9. public static LiquidContent StartNew
  10. {
  11. get
  12. {
  13. lock (obj_locker)
  14. {
  15. return new();
  16. }
  17. }
  18. }
  19. private LiquidContent() { }
  20. /// <summary>
  21. /// 执行
  22. /// </summary>
  23. /// <param name="liquidName">液体名称</param>
  24. /// <param name="handleType">处理方式</param>
  25. /// <param name="callBack"></param>
  26. /// <returns></returns>
  27. public bool Execute(string liquidName, string handleType, Action<object> callBack = null, params object[] args)
  28. {
  29. try
  30. {
  31. ILiquidContent content = ContentFactory(liquidName);
  32. bool response = handleType switch
  33. {
  34. TaskTypeName.WASH => content.WashFactory().Execute(),
  35. _ => content.NormalFactory().Execute(callBack, args),
  36. };
  37. logger.LogDebug($"液体{liquidName}以{handleType}方式下发{(response ? "成功" : "失败")}");
  38. return response;
  39. }
  40. catch (Exception ex)
  41. {
  42. logger.LogError(ex.ToString());
  43. return false;
  44. }
  45. }
  46. private ILiquidContent ContentFactory(string liquidName)
  47. {
  48. if (DicContent.TryGetValue(liquidName, out ILiquidContent value))
  49. {
  50. return value;
  51. }
  52. string fullName = CreatContentClassFullName(liquidName);
  53. ILiquidContent content = Activator.CreateInstance(Type.GetType(fullName), true) as ILiquidContent;
  54. DicContent.TryAdd(liquidName, content);
  55. return content;
  56. }
  57. private static string CreatContentClassFullName(string name)
  58. {
  59. return $"{LIQUIDCONTENT_NAMESPACE}.{name}Content";
  60. }
  61. }
  62. }