DioCnd.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using SKMC.Api.Core;
  2. using System;
  3. namespace SKMC.Api.Motion.Model
  4. {
  5. /// <summary>
  6. /// IO条件, 可作为触发动作的条件, 也可以作为告警状态的条件
  7. /// </summary>
  8. public class DioCnd
  9. {
  10. private static readonly MotionCacher motionCacher = ObjectFactory.Resolve<MotionCacher>();
  11. // 条件函数
  12. public Func<MotionIO, bool> CndExpress { get; set; }
  13. public MotionIO DataModel { get; set; }
  14. public static DioCnd Make(string code) => new DioCnd
  15. {
  16. Code = code
  17. };
  18. /// <summary>
  19. /// 创建一个DioCnd
  20. /// </summary>
  21. /// <param name="code"></param>
  22. /// <param name="cndExpress"></param>
  23. /// <returns></returns>
  24. public static DioCnd Make(string code, Func<MotionIO, bool> cndExpress) => new DioCnd
  25. {
  26. Code = code,
  27. CndExpress = cndExpress
  28. };
  29. /// <summary>
  30. /// 匹配一个基于MotionIO的条件函数
  31. /// </summary>
  32. /// <param name="code"></param>
  33. /// <param name="cndExpress"></param>
  34. /// <returns></returns>
  35. public static bool Match(string code, Func<MotionIO, bool> cndExpress) => cndExpress.Invoke(motionCacher.GetMotionDio(code));
  36. public bool Match() => CndExpress.Invoke(DataModel);
  37. public bool Match(Func<MotionIO, bool> cndExpress) => cndExpress.Invoke(DataModel);
  38. public void Reset()
  39. {
  40. if (DataModel != null) DataModel.Reset();
  41. }
  42. private string _code;
  43. public string Code
  44. {
  45. get { return _code; }
  46. set
  47. {
  48. _code = value;
  49. DataModel = motionCacher.GetMotionDio(_code);
  50. }
  51. }
  52. }
  53. }