InterlockLimit.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.SCCore;
  5. using Aitex.Core.Util;
  6. using DocumentFormat.OpenXml.Spreadsheet;
  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. public abstract class InterlockLimit
  17. {
  18. public string Name
  19. {
  20. get { return _name; }
  21. }
  22. public abstract bool CurrentValue { get; }
  23. public abstract string LimitReason { get; }
  24. public bool LimitValue
  25. {
  26. get { return _limitValue; }
  27. }
  28. public string Tip
  29. {
  30. get
  31. {
  32. return _tip;
  33. }
  34. }
  35. private string _name;
  36. private bool _limitValue;
  37. private string _tip;
  38. private Dictionary<string, string> _cultureTip = new Dictionary<string, string>();
  39. R_TRIG _trigger = new R_TRIG();
  40. public InterlockLimit(string name, bool value, string tip, Dictionary<string, string> cultureTip)
  41. {
  42. _name = name;
  43. _limitValue = value;
  44. _tip = tip;
  45. _cultureTip = cultureTip;
  46. }
  47. public bool IsSame(string name, bool value)
  48. {
  49. return (name == _name) && (_limitValue == value);
  50. }
  51. public bool IsSame(InterlockLimit limit)
  52. {
  53. return (limit.Name == _name) && (_limitValue == limit.LimitValue);
  54. }
  55. public bool IsTriggered()
  56. {
  57. _trigger.CLK = CurrentValue != _limitValue;
  58. return _trigger.Q;
  59. }
  60. public bool CanDo(out string reason)
  61. {
  62. reason = string.Empty;
  63. if (CurrentValue == _limitValue)
  64. return true;
  65. reason = LimitReason;
  66. return false;
  67. }
  68. }
  69. internal class DiLimit:InterlockLimit
  70. {
  71. private DIAccessor _di;
  72. public DiLimit(DIAccessor diItem, bool value, string tip, Dictionary<string, string> cultureTip)
  73. :base(diItem.Name, value, tip, cultureTip)
  74. {
  75. _di = diItem;
  76. }
  77. public override bool CurrentValue
  78. {
  79. get { return _di.Value; }
  80. }
  81. public override string LimitReason
  82. {
  83. get
  84. {
  85. return string.Format("DI-{0}({1}) = [{2}],{3}", _di.IoTableIndex, _di.Name, _di.Value ? "ON" : "OFF", Tip);
  86. }
  87. }
  88. }
  89. internal class DoLimit : InterlockLimit
  90. {
  91. private DOAccessor _do;
  92. public DoLimit(DOAccessor doItem, bool value, string tip, Dictionary<string, string> cultureTip)
  93. : base(doItem.Name, value, tip, cultureTip)
  94. {
  95. _do = doItem;
  96. }
  97. public override bool CurrentValue
  98. {
  99. get { return _do.Value; }
  100. }
  101. public override string LimitReason
  102. {
  103. get
  104. {
  105. return string.Format("DO-{0}({1}) = [{2}],{3}", _do.IoTableIndex, _do.Name, _do.Value ? "ON" : "OFF", Tip);
  106. }
  107. }
  108. }
  109. internal class AoLimit : InterlockLimit
  110. {
  111. private AOAccessor _ao;
  112. private float _limitFloatValue;
  113. //private string _operator;//>,>=,<,<=,=
  114. private string _operator;//H:>=,L:<
  115. private string _module = "";
  116. public AoLimit(AOAccessor aoItem, string value)
  117. : base(aoItem.Name, true, "", null)
  118. {
  119. _ao = aoItem;
  120. if (value.Contains("H"))
  121. {
  122. _operator = "H";
  123. }
  124. else if (value.Contains("L"))
  125. {
  126. _operator = "L";
  127. }
  128. float.TryParse(value.Replace(_operator, ""), out _limitFloatValue);
  129. var paras = aoItem.Name.Split('.');
  130. if (paras != null && paras.Length > 1)
  131. _module = paras[0];
  132. }
  133. public override string LimitReason
  134. {
  135. get
  136. {
  137. return string.Format("AO-{0}({1}) = [{2}],{3}", _ao.IoTableIndex, _ao.Name, (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ao.FloatValue : _ao.Value), Tip);
  138. }
  139. }
  140. public override bool CurrentValue
  141. {
  142. get
  143. {
  144. switch (_operator)
  145. {
  146. case "H"://>=
  147. return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ao.FloatValue : _ao.Value) >= _limitFloatValue;
  148. case "L"://<
  149. return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ao.FloatValue : _ao.Value) < _limitFloatValue;
  150. default://=
  151. return Math.Abs((SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ao.FloatValue : _ao.Value) - _limitFloatValue) < 0.000001;
  152. }
  153. }
  154. }
  155. }
  156. internal class AiLimit : InterlockLimit
  157. {
  158. private AIAccessor _ai;
  159. private float _limitFloatValue;
  160. //private string _operator;//>,>=,<,<=,=
  161. private string _operator;//H:>=,L:<
  162. private string _module = "";
  163. public AiLimit(AIAccessor aiItem, string value)
  164. : base(aiItem.Name, true, "", null)
  165. {
  166. _ai = aiItem;
  167. if (value.Contains("H"))
  168. {
  169. _operator = "H";
  170. }
  171. else if (value.Contains("L"))
  172. {
  173. _operator = "L";
  174. }
  175. float.TryParse(value.Replace(_operator, ""), out _limitFloatValue);
  176. var paras = aiItem.Name.Split('.');
  177. if (paras != null && paras.Length > 1)
  178. _module = paras[0];
  179. }
  180. public override string LimitReason
  181. {
  182. get
  183. {
  184. return string.Format("AO-{0}({1}) = [{2}],{3}", _ai.IoTableIndex, _ai.Name, (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value), Tip);
  185. }
  186. }
  187. public override bool CurrentValue
  188. {
  189. get
  190. {
  191. //switch (_operator)
  192. //{
  193. // case ">=":
  194. // return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) >= _limitFloatValue;
  195. // case "<=":
  196. // return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) <= _limitFloatValue;
  197. // case ">":
  198. // return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) > _limitFloatValue;
  199. // case "<":
  200. // return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) < _limitFloatValue;
  201. // default://=
  202. // return Math.Abs((SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) - _limitFloatValue) < 0.000001;
  203. //}
  204. switch (_operator)
  205. {
  206. case "H"://>=
  207. return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) >= _limitFloatValue;
  208. case "L"://<
  209. return (SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) < _limitFloatValue;
  210. default://=
  211. return Math.Abs((SC.ContainsItem($"{_module}.IsAIAOFloatType") && SC.GetValue<bool>($"{_module}.IsAIAOFloatType") ? _ai.FloatValue : _ai.Value) - _limitFloatValue) < 0.000001;
  212. }
  213. }
  214. }
  215. }
  216. internal class DataPollLimit : InterlockLimit
  217. {
  218. private string _operator;//H:>=,L:<,B
  219. private float _limitFloatValue;
  220. private bool _limitBoolValue;
  221. public DataPollLimit(string name, string limitValue)
  222. : base(name, true, "", null)
  223. {
  224. if (limitValue.Contains("H"))
  225. {
  226. _operator = "H";
  227. float.TryParse(limitValue.Replace(_operator, ""), out _limitFloatValue);
  228. }
  229. else if (limitValue.Contains("L"))
  230. {
  231. _operator = "L";
  232. float.TryParse(limitValue.Replace(_operator, ""), out _limitFloatValue);
  233. }
  234. else if (limitValue.Contains("B"))
  235. {
  236. _operator = "B";
  237. bool.TryParse(limitValue.Replace(_operator, ""), out _limitBoolValue);
  238. }
  239. }
  240. public override bool CurrentValue
  241. {
  242. get
  243. {
  244. switch (_operator)
  245. {
  246. case "H"://>=
  247. float.TryParse(DATA.Poll(Name).ToString(), out float valueH);
  248. return valueH >= _limitFloatValue;
  249. case "L"://<
  250. float.TryParse(DATA.Poll(Name).ToString(), out float valueL);
  251. return valueL < _limitFloatValue;
  252. case "B"://<
  253. bool.TryParse(DATA.Poll(Name).ToString(), out bool valueB);
  254. return valueB && _limitBoolValue;
  255. default://=
  256. return false;
  257. }
  258. }
  259. }
  260. public override string LimitReason { get; }
  261. }
  262. public class CustomLimitBase : InterlockLimit
  263. {
  264. public CustomLimitBase(string name, bool limitValue, string tip, Dictionary<string, string> cultureTip) : base(name, limitValue, tip, cultureTip)
  265. {
  266. }
  267. public override bool CurrentValue { get; }
  268. public override string LimitReason { get; }
  269. }
  270. }