InterlockAction.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Design.Serialization;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Aitex.Core.RT.IOCore
  8. {
  9. /*
  10. *
  11. <Action do="DO_MFC1_valve__" value="true" tip="" tip.zh-CN="" tip.en-US="">
  12. <Limit di="DI_Chamber_door_sw" value="true" tip="" tip.zh-CN="" tip.en-US="" />
  13. </Action>
  14. *
  15. */
  16. class InterlockAction
  17. {
  18. private List<InterlockLimit> _limits = new List<InterlockLimit>();
  19. private DOAccessor _do;
  20. private bool _actionValue;
  21. private string _actionstringValue;
  22. private string _tip;
  23. private Dictionary<string, string> _cultureTip = new Dictionary<string, string>();
  24. private string _actionName;
  25. public string ActionName => _do != null ? _do.Name : _actionName;
  26. public string Condition { get; set; }//KEEP,DELAY
  27. public float ConditionTime { get; set; }//和Condition配合使用
  28. public DOAccessor ActionDo => _do;
  29. public bool IsReverse => _isReverse;
  30. private bool _isReverse;
  31. public bool IsAuto => _isAuto;
  32. private bool _isAuto;
  33. public bool ActionValue => _actionValue;
  34. public List<InterlockLimit> Limits => _limits;
  35. public string Tip => _tip;
  36. public InterlockAction(DOAccessor doItem, bool value, string tip, Dictionary<string, string> cultureTip, List<InterlockLimit> limits, bool isReverse)
  37. {
  38. _do = doItem;
  39. _actionValue = value;
  40. _tip = tip;
  41. _cultureTip = cultureTip;
  42. _limits = limits;
  43. _isReverse = isReverse;
  44. }
  45. public InterlockAction(DOAccessor doItem, string value, string tip, Dictionary<string, string> cultureTip, List<InterlockLimit> limits)
  46. {
  47. _do = doItem;
  48. _actionstringValue = value;
  49. _tip = tip;
  50. _cultureTip = cultureTip;
  51. _limits = limits;
  52. }
  53. public InterlockAction(DOAccessor doItem, bool value, string tip, string condition, float conditionTime, List<InterlockLimit> limits, bool auto = true)
  54. {
  55. _do = doItem;
  56. _actionValue = value;
  57. _tip = tip;
  58. _limits = limits;
  59. _isAuto = auto;
  60. Condition = condition;
  61. ConditionTime = conditionTime;
  62. }
  63. public InterlockAction(string actionName, bool value, string tip, string condition, float conditionTime, List<InterlockLimit> limits, bool auto)
  64. {
  65. _actionName = actionName;
  66. _actionValue = value;
  67. _tip = tip;
  68. _limits = limits;
  69. _isAuto = auto;
  70. Condition = condition;
  71. ConditionTime = conditionTime;
  72. }
  73. public bool IsSame(string doName, string value)
  74. {
  75. return (doName == ActionName) && (_actionstringValue == value);
  76. }
  77. public bool IsSame(string doName, bool value)
  78. {
  79. return (doName == ActionName) && (_actionValue == value);
  80. }
  81. public bool Reverse(out string reason)
  82. {
  83. reason = string.Empty;
  84. if (_do.Value != _actionValue)
  85. return false;
  86. if (_do.SetValue(!_actionValue, out reason))
  87. {
  88. reason = string.Format("Force setting DO-{0}({1}) = [{2}]", _do.IoTableIndex, _do.Name, (!_actionValue) ? "ON" : "OFF");
  89. }
  90. return true;
  91. }
  92. public bool CanDo(out string reason)
  93. {
  94. reason = string.Empty;
  95. bool result = true;
  96. bool cando;
  97. foreach (var interlockLimit in _limits)
  98. {
  99. string info;
  100. cando = interlockLimit.CanDo(out info);
  101. if (!cando)
  102. {
  103. if (reason.Length > 0)
  104. reason += "\n";
  105. else
  106. {
  107. reason = string.Format("Interlock triggered, DO-{0}({1}) can not be [{2}], {3} \n", _do.IoTableIndex, _do.Name, _actionValue ? "ON" : "OFF",
  108. _tip);
  109. }
  110. reason += info;
  111. }
  112. switch (interlockLimit.Condition.ToUpper())
  113. {
  114. case "AND":
  115. result = result && cando;
  116. break;
  117. case "OR":
  118. result = result || cando;
  119. break;
  120. }
  121. }
  122. return result;
  123. }
  124. }
  125. }