IoValve.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using System;
  2. using System.Diagnostics;
  3. using System.Xml;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.IOCore;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.Event;
  13. namespace Aitex.Core.RT.Device.Unit
  14. {
  15. public interface IValve
  16. {
  17. bool TurnValve(bool isOn, out string reason);
  18. }
  19. public class IoValve : BaseDevice, IDevice, IValve
  20. {
  21. public string GVName { get { return Name; } }
  22. public string BtnName { get; set; }
  23. public string GVDeviceID { get { return DeviceID; } }
  24. public bool GVIsDefaultOpen { get { return _isDefaultOpen; } }
  25. public AlarmEventItem InterlockAlarm;
  26. public AlarmEventItem InterlockWarning;
  27. public DOAccessor DOOpen => _doOpen;
  28. public DOAccessor DOClose => _doClose;
  29. private bool _setPoint;
  30. [Subscription(AITValveDataPropertyName.SetPoint)]
  31. public bool SetPoint //True:open| False:close
  32. {
  33. get
  34. {
  35. return _setPoint;
  36. //if (_doOpen == null)
  37. // return false;
  38. //return _isNc ? _doOpen.Value : !_doOpen.Value;
  39. }
  40. set
  41. {
  42. if (_doOpen != null)
  43. {
  44. _doOpen.SetValue(_isNc ? value : !value, out _, false);
  45. }
  46. if (_doClose != null)
  47. {
  48. _doClose.SetValue(_isNc ? !value : value, out _, false);
  49. }
  50. _setPoint = value;
  51. }
  52. }
  53. [Subscription(AITValveDataPropertyName.Status)]
  54. public bool Status //True:open | False:close
  55. {
  56. get
  57. {
  58. if (_diOpenSensor != null && _diCloseSensor != null)
  59. return _diOpenSensor.Value && !_diCloseSensor.Value;
  60. if (_diCloseSensor != null)
  61. return !_diCloseSensor.Value;
  62. if (_diOpen != null)
  63. return _isNc ? _diOpen.Value : !_diOpen.Value;
  64. if (_doOpen != null)
  65. return _isNc ? _doOpen.Value : !_doOpen.Value;
  66. if (_doClose != null)
  67. return _isNc ? !_doClose.Value : _doClose.Value;
  68. return false;
  69. }
  70. }
  71. private AITValveData DeviceData
  72. {
  73. get
  74. {
  75. AITValveData data = new AITValveData()
  76. {
  77. UniqueName = _uniqueName,
  78. DeviceName = GVName,
  79. DefaultValue = GVIsDefaultOpen,
  80. DeviceSchematicId = DeviceID,
  81. DisplayName = Display,
  82. Feedback = Status,
  83. SetPoint = SetPoint,
  84. ILKDiValue = ILKDiSensor == null ? true : ILKDiSensor.Value,
  85. VirtualFeedback = VirtualStatus,
  86. };
  87. return data;
  88. }
  89. }
  90. /// <summary>
  91. /// normal closed, 0 关闭,1打开
  92. /// </summary>
  93. public bool _isNc;
  94. /// <summary>
  95. /// default open
  96. /// </summary>
  97. public bool _isDefaultOpen;
  98. private DIAccessor _diOpenSensor;
  99. private DIAccessor _diCloseSensor;
  100. private DIAccessor _diOpen;
  101. private DOAccessor _doOpen;
  102. private DOAccessor _doClose;
  103. private DIAccessor ILKDiSensor;
  104. private bool _operation;
  105. R_TRIG eventTrigger = new R_TRIG();
  106. R_TRIG _mutexSignalTrigger = new R_TRIG();
  107. DeviceTimer _timer = new DeviceTimer();
  108. DeviceTimer _mutexSignalTimer = new DeviceTimer();
  109. private string _uniqueName;
  110. public bool VirtualStatus;
  111. private string _writeLog = "";
  112. private SCConfigItem _interlockNoneOrExist;
  113. private SCConfigItem _interlockNormaly0nOrOff;
  114. private SCConfigItem _interlockDelayTime;
  115. private float _interlockDelayTimeInMS;
  116. private Stopwatch _interlockDelayTimer = new Stopwatch();
  117. public IoValve(string module, XmlElement node, string ioModule = "")
  118. {
  119. var attrModule = node.GetAttribute("module");
  120. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  121. base.Name = node.GetAttribute("id");
  122. base.Display = node.GetAttribute("display");
  123. base.DeviceID = node.GetAttribute("schematicId");
  124. BtnName = node.GetAttribute("btnName");
  125. _diOpenSensor = ParseDiNode("diOpenSensor", node, ioModule);
  126. _diCloseSensor = ParseDiNode("diCloseSensor", node, ioModule);
  127. _doOpen = ParseDoNode("doOpen", node, ioModule);
  128. _diOpen = ParseDiNode("diOpen", node, ioModule);
  129. _doClose = ParseDoNode("doClose", node, ioModule);
  130. ILKDiSensor = ParseDiNode("ILKDi", node, ioModule);
  131. _uniqueName = $"{Module}.{Name}";
  132. _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
  133. _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
  134. SetPoint = _isDefaultOpen;
  135. }
  136. public bool Initialize()
  137. {
  138. DATA.Subscribe($"Device.{Module}.{GVName}", () => DeviceData);
  139. DATA.Subscribe($"{_uniqueName}.DeviceData", () => DeviceData);
  140. DATA.Subscribe($"{Module}.{Name}.SetPoint", () => SetPoint);
  141. DATA.Subscribe($"{Module}.{Name}.Feedback", () => Status);
  142. DATA.Subscribe($"{Module}.{Name}.VirtualStatus", () => VirtualStatus);
  143. if (!string.IsNullOrEmpty(BtnName))
  144. {
  145. //因为气路图上部分按钮采用的是IoValve的数据和操作,
  146. //因此为了兼容xml气路图和原来的版本 对于这些特殊的IoValve统一和按钮一致
  147. DATA.Subscribe($"{Module}.{Name}Btn", () => BtnName);
  148. DATA.Subscribe($"{Module}.{BtnName}Enable", () => Status);
  149. OP.Subscribe($"{Module}.Set{BtnName}Enable", InvokeOpenCloseValve);
  150. }
  151. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVTurnValve}", InvokeOpenCloseValve);
  152. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVVirtualTurnValve}", InvokeOpenCloseVirtualValve);
  153. DEVICE.Register(String.Format("{0}.{1}", Name, AITValveOperation.GVTurnValve), (out string reason, int time, object[] param) =>
  154. {
  155. bool bOn = Convert.ToBoolean((string)param[0]);
  156. bool ret = TurnValve(bOn, out reason);
  157. if (ret)
  158. {
  159. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  160. return true;
  161. }
  162. return false;
  163. });
  164. //for recipe
  165. DEVICE.Register(String.Format("{0}", Name), (out string reason, int time, object[] param) =>
  166. {
  167. bool bOn = Convert.ToBoolean((string)param[0]);
  168. bool ret = TurnValve(bOn, out reason);
  169. if (ret)
  170. {
  171. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  172. return true;
  173. }
  174. return false;
  175. });
  176. //for recipe
  177. OP.Subscribe($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  178. {
  179. reason = string.Empty;
  180. if (param[0].ToString() == "Continue")
  181. {
  182. EV.PostInfoLog(Module, $"Turn {Display} Continue");
  183. return true;
  184. }
  185. bool bOn = Convert.ToBoolean((string)param[0]);
  186. bool ret = TurnValve(bOn, out reason);
  187. if (!ret)
  188. {
  189. reason = string.Format("Valve {0}{1} failed", Name, bOn ? "Open" : "Close");
  190. return false;
  191. }
  192. return true;
  193. });
  194. _operation = _isNc ? false : true;
  195. var valveIndex = Name.Replace("ValveAV", "");
  196. _interlockNoneOrExist = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.NoneOrExist");
  197. _interlockNormaly0nOrOff = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.Normaly0nOrOff");
  198. _interlockDelayTime = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.DelayTime");
  199. return true;
  200. }
  201. public void Terminate()
  202. {
  203. string reason;
  204. TurnValve(_isDefaultOpen, out reason);
  205. }
  206. public bool InvokeOpenCloseValve(string method, object[] args)
  207. {
  208. string reason;
  209. bool op = Convert.ToBoolean(args[0]);
  210. string name = op ? "Open" : "Close";
  211. if (!TurnValve(op, out reason))
  212. {
  213. if (InterlockWarning != null)
  214. {
  215. InterlockWarning.Description = $"{reason}";
  216. InterlockWarning.Set();
  217. }
  218. else
  219. {
  220. EV.PostWarningLog(Module, $"Can not {name} valve {Module}.{Name}, {reason}");
  221. }
  222. return false;
  223. }
  224. EV.PostInfoLog(Module, $"{name} valve {Module}.{Name}");
  225. return true;
  226. }
  227. public bool InvokeOpenCloseVirtualValve(string method, object[] args)
  228. {
  229. bool op = Convert.ToBoolean(args[0]);
  230. VirtualStatus = op;
  231. return true;
  232. }
  233. public void Monitor()
  234. {
  235. if (!string.IsNullOrEmpty(_writeLog))
  236. {
  237. LOG.Write(_writeLog);
  238. _writeLog = "";
  239. }
  240. if (_interlockDelayTimer.IsRunning)
  241. {
  242. if (_interlockDelayTimer.ElapsedMilliseconds >= _interlockDelayTimeInMS)
  243. {
  244. _interlockDelayTimer.Stop();
  245. SetPoint = _setPoint;
  246. }
  247. else
  248. {
  249. return;
  250. }
  251. }
  252. try
  253. {
  254. if (_diOpenSensor != null && _diCloseSensor != null && _doOpen != null)
  255. {
  256. if (_diOpenSensor.Value != _diCloseSensor.Value)
  257. {
  258. _mutexSignalTimer.Start(2000);
  259. }
  260. _mutexSignalTrigger.CLK = _mutexSignalTimer.IsTimeout();
  261. if (_mutexSignalTrigger.Q)
  262. {
  263. EV.PostWarningLog(Module, $"Valve {Name} was abnormal,Reason:diOpenSensor's value is {_diOpenSensor.Value} and diCloseSensor's value is {_diCloseSensor.Value} too.");
  264. }
  265. }
  266. if (_timer.IsTimeout() && _doOpen != null)
  267. {
  268. _timer.Stop();
  269. if (Status != _operation)
  270. {
  271. if (_operation)
  272. {
  273. string reason;
  274. if (!_doOpen.Check(_isNc ? true : false, out reason))
  275. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Failed for interlock " + reason);
  276. else
  277. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", $":Valve {Name} keep closed ");
  278. }
  279. else
  280. {
  281. string reason;
  282. if (!_doOpen.Check(_isNc ? true : false, out reason))
  283. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Failed for interlock " + reason);
  284. else
  285. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", $":Valve {Name} keep open");
  286. }
  287. }
  288. _operation = SetPoint;
  289. }
  290. else if (_timer.IsIdle() && _doOpen != null)
  291. {
  292. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  293. if (eventTrigger.Q)
  294. {
  295. if (_operation)
  296. {
  297. string reason;
  298. if (!_doOpen.Check(_isNc ? true : false, out reason))
  299. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Close", reason));
  300. else
  301. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
  302. }
  303. else
  304. {
  305. string reason;
  306. if (!_doOpen.Check(_isNc ? true : false, out reason))
  307. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Open", reason));
  308. else
  309. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
  310. }
  311. _operation = SetPoint;
  312. }
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. LOG.Write(ex);
  318. }
  319. }
  320. public bool TurnValve(bool isOn, out string reason)
  321. {
  322. bool bValue = _isNc ? isOn : !isOn;
  323. if (_doOpen != null)
  324. {
  325. if (!_doOpen.Check(bValue, out reason))
  326. {
  327. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  328. return false;
  329. }
  330. }
  331. if (_doClose != null)
  332. {
  333. if (!_doClose.Check(!bValue, out reason))
  334. {
  335. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  336. return false;
  337. }
  338. }
  339. var stringOnOff = isOn ? "On" : "Off";
  340. //EV.PostInfoLog(Module, $"Turn {Display} {stringOnOff}");
  341. _writeLog = $"Turn {Display} {stringOnOff}";
  342. reason = "";
  343. if (_interlockNoneOrExist != null && _interlockDelayTime != null && _interlockNoneOrExist.BoolValue)
  344. {
  345. var timeParas = _interlockDelayTime.StringValue.Split(':');//00:00.0
  346. _interlockDelayTimeInMS = 0;
  347. if (timeParas.Length > 1)
  348. {
  349. float.TryParse(timeParas[0], out float min);
  350. float.TryParse(timeParas[1], out float sec);
  351. _interlockDelayTimeInMS = min * 60 * 1000 + sec * 1000;
  352. }
  353. if (_interlockDelayTimeInMS > 0)
  354. {
  355. _writeLog += $" delay time={_interlockDelayTimeInMS} ms";
  356. _interlockDelayTimer.Restart();
  357. _operation = isOn;
  358. _timer.Start(2000 + _interlockDelayTimeInMS); //2 seconds to monitor
  359. }
  360. else
  361. {
  362. SetPoint = isOn;
  363. _operation = isOn;
  364. _timer.Start(2000); //2 seconds to monitor
  365. }
  366. }
  367. else
  368. {
  369. SetPoint = isOn;
  370. _operation = isOn;
  371. _timer.Start(2000); //2 seconds to monitor
  372. }
  373. return true;
  374. }
  375. public void Reset()
  376. {
  377. eventTrigger.RST = true;
  378. }
  379. }
  380. }