IoValve.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 GVDeviceID { get { return DeviceID; } }
  23. public bool GVIsDefaultOpen { get { return _isDefaultOpen; } }
  24. public AlarmEventItem InterlockAlarm;
  25. public AlarmEventItem InterlockWarning;
  26. public DOAccessor DOOpen => _doOpen;
  27. public DOAccessor DOClose => _doClose;
  28. private bool _setPoint;
  29. public bool IsILKOK = true;
  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. if (_doOpen.SetValue(_isNc ? value : !value, out _, false))
  45. {
  46. _setPoint = value;
  47. }
  48. }
  49. if (_doClose != null)
  50. {
  51. if (_doClose.SetValue(_isNc ? !value : value, out _, false))
  52. {
  53. _setPoint = value;
  54. }
  55. }
  56. }
  57. }
  58. [Subscription(AITValveDataPropertyName.Status)]
  59. public bool Status //True:open | False:close
  60. {
  61. get
  62. {
  63. if (_diOpenSensor != null && _diCloseSensor != null)
  64. return _diOpenSensor.Value && !_diCloseSensor.Value;
  65. if (_diCloseSensor != null)
  66. return !_diCloseSensor.Value;
  67. if (_diOpen != null)
  68. return _isNc ? _diOpen.Value : !_diOpen.Value;
  69. if (_doOpen != null)
  70. return _isNc ? _doOpen.Value : !_doOpen.Value;
  71. if (_doClose != null)
  72. return _isNc ? !_doClose.Value : _doClose.Value;
  73. return false;
  74. }
  75. }
  76. private AITValveData DeviceData
  77. {
  78. get
  79. {
  80. AITValveData data = new AITValveData()
  81. {
  82. UniqueName = _uniqueName,
  83. DeviceName = GVName,
  84. DefaultValue = GVIsDefaultOpen,
  85. DeviceSchematicId = DeviceID,
  86. DisplayName = Display,
  87. Feedback = Status,
  88. SetPoint = SetPoint,
  89. ILKDiValue = ILKDiSensor == null ? true : ILKDiSensor.Value,
  90. IsILKOK = this.IsILKOK,
  91. VirtualFeedback = VirtualStatus,
  92. };
  93. return data;
  94. }
  95. }
  96. /// <summary>
  97. /// normal closed, 0 关闭,1打开
  98. /// </summary>
  99. public bool _isNc;
  100. /// <summary>
  101. /// default open
  102. /// </summary>
  103. public bool _isDefaultOpen;
  104. private DIAccessor _diOpenSensor;
  105. private DIAccessor _diCloseSensor;
  106. private DIAccessor _diOpen;
  107. private DOAccessor _doOpen;
  108. private DOAccessor _doClose;
  109. private DIAccessor ILKDiSensor;
  110. private bool _operation;
  111. public bool IsILKNot { get; set; } = false;
  112. R_TRIG eventTrigger = new R_TRIG();
  113. R_TRIG _mutexSignalTrigger = new R_TRIG();
  114. DeviceTimer _timer = new DeviceTimer();
  115. DeviceTimer _mutexSignalTimer = new DeviceTimer();
  116. private string _uniqueName;
  117. public bool VirtualStatus;
  118. private string _writeLog = "";
  119. private SCConfigItem _interlockNoneOrExist;
  120. private SCConfigItem _interlockNormaly0nOrOff;
  121. private SCConfigItem _interlockDelayTime;
  122. private float _interlockDelayTimeInMS;
  123. private Stopwatch _interlockDelayTimer = new Stopwatch();
  124. public IoValve(string module, XmlElement node, string ioModule = "")
  125. {
  126. var attrModule = node.GetAttribute("module");
  127. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  128. base.Name = node.GetAttribute("id");
  129. base.Display = node.GetAttribute("display");
  130. base.DeviceID = node.GetAttribute("schematicId");
  131. _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
  132. _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
  133. _diOpenSensor = ParseDiNode("diOpenSensor", node, ioModule);
  134. _diCloseSensor = ParseDiNode("diCloseSensor", node, ioModule);
  135. _doOpen = ParseDoNode("doOpen", node, ioModule);
  136. _diOpen = ParseDiNode("diOpen", node, ioModule);
  137. _doClose = ParseDoNode("doClose", node, ioModule);
  138. ILKDiSensor = ParseDiNode("ILKDi", node, ioModule);
  139. var attIsILKNot = node.GetAttribute("IsILKNot");
  140. IsILKNot = string.IsNullOrEmpty(attIsILKNot) ? false : Convert.ToBoolean(attIsILKNot);
  141. _uniqueName = $"{Module}.{Name}";
  142. }
  143. public bool Initialize()
  144. {
  145. _operation = _isNc ? false : true;
  146. if (_isNc == false && _isDefaultOpen == false)
  147. {
  148. TurnValve(_isDefaultOpen, out string tempReason);
  149. }
  150. DATA.Subscribe($"Device.{Module}.{GVName}", () => DeviceData);
  151. DATA.Subscribe($"{_uniqueName}.DeviceData", () => DeviceData);
  152. DATA.Subscribe($"{Module}.{Name}.SetPoint", () => SetPoint);
  153. DATA.Subscribe($"{Module}.{Name}.Feedback", () => Status);
  154. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVTurnValve}", InvokeOpenCloseValve);
  155. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVVirtualTurnValve}", InvokeOpenCloseVirtualValve);
  156. DEVICE.Register(String.Format("{0}.{1}", Name, AITValveOperation.GVTurnValve), (out string reason, int time, object[] param) =>
  157. {
  158. bool bOn = Convert.ToBoolean((string)param[0]);
  159. bool ret = TurnValve(bOn, out reason);
  160. if (ret)
  161. {
  162. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  163. return true;
  164. }
  165. return false;
  166. });
  167. //for recipe
  168. DEVICE.Register(String.Format("{0}", Name), (out string reason, int time, object[] param) =>
  169. {
  170. bool bOn = Convert.ToBoolean((string)param[0]);
  171. bool ret = TurnValve(bOn, out reason);
  172. if (ret)
  173. {
  174. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  175. return true;
  176. }
  177. return false;
  178. });
  179. //for recipe
  180. OP.Subscribe($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  181. {
  182. reason = string.Empty;
  183. if (param[0].ToString() == "Continue")
  184. {
  185. EV.PostInfoLog(Module, $"Turn {Display} Continue");
  186. return true;
  187. }
  188. bool bOn = Convert.ToBoolean((string)param[0]);
  189. bool ret = TurnValve(bOn, out reason);
  190. if (!ret)
  191. {
  192. reason = string.Format("Valve {0}{1} failed", Name, bOn ? "Open" : "Close");
  193. return false;
  194. }
  195. return true;
  196. });
  197. var valveIndex = Name.Replace("ValveAV", "");
  198. _interlockNoneOrExist = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.NoneOrExist");
  199. _interlockNormaly0nOrOff = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.Normaly0nOrOff");
  200. _interlockDelayTime = SC.GetConfigItem($"PM1.RecipeEditParameter.InterLock.{valveIndex}.DelayTime");
  201. return true;
  202. }
  203. public void Terminate()
  204. {
  205. string reason;
  206. TurnValve(_isDefaultOpen, out reason);
  207. }
  208. public bool InvokeOpenCloseValve(string method, object[] args)
  209. {
  210. string reason;
  211. bool op = Convert.ToBoolean(args[0]);
  212. string name = op ? "Open" : "Close";
  213. if (!TurnValve(op, out reason))
  214. {
  215. if (InterlockWarning != null)
  216. {
  217. InterlockWarning.Description = $"{reason}";
  218. InterlockWarning.Set();
  219. }
  220. else
  221. {
  222. EV.PostWarningLog(Module, $"Can not {name} valve {Module}.{Name}, {reason}");
  223. }
  224. return false;
  225. }
  226. EV.PostInfoLog(Module, $"{name} valve {Module}.{Name}");
  227. return true;
  228. }
  229. public bool InvokeOpenCloseVirtualValve(string method, object[] args)
  230. {
  231. bool op = Convert.ToBoolean(args[0]);
  232. VirtualStatus = op;
  233. return true;
  234. }
  235. public void Monitor()
  236. {
  237. if (!string.IsNullOrEmpty(_writeLog))
  238. {
  239. LOG.Write(_writeLog);
  240. _writeLog = "";
  241. }
  242. if (_interlockDelayTimer.IsRunning)
  243. {
  244. if (_interlockDelayTimer.ElapsedMilliseconds >= _interlockDelayTimeInMS)
  245. {
  246. _interlockDelayTimer.Stop();
  247. bool bValue = _operation;
  248. if (_doOpen != null)
  249. {
  250. if (!_doOpen.Check(bValue, out var reason))
  251. {
  252. if (!_doOpen.SetValue(bValue, out reason, false))
  253. {
  254. EV.PostWarningLog("System", $"interlock set DO {Module} to {bValue}, {reason}");
  255. }
  256. // EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(_operation ? "Open" : "Close")}", ":Failed for interlock " + reason);
  257. _operation = SetPoint; //还原状态 防止定时检查状态再次写日志
  258. return;
  259. }
  260. }
  261. if (_doClose != null)
  262. {
  263. if (!_doClose.Check(bValue, out var reason))
  264. {
  265. if (!_doClose.SetValue(bValue, out reason, false))
  266. {
  267. EV.PostWarningLog("System", $"interlock set DO {Module} to {bValue}, {reason}");
  268. }
  269. //EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(_operation ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  270. _operation = SetPoint;//还原状态 防止定时检查状态再次写日志
  271. return;
  272. }
  273. }
  274. SetPoint = _isNc ? _operation : !_operation;
  275. }
  276. else
  277. {
  278. bool bValue = _operation;
  279. if (!_doOpen.Check(bValue, out var reason))
  280. {
  281. if (!_doOpen.SetValue(bValue, out reason, false))
  282. {
  283. EV.PostWarningLog("System", $"interlock set DO {Module} to {bValue}, {reason}");
  284. }
  285. }
  286. // EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Failed for interlock " + reason);
  287. else
  288. {
  289. if (!_doOpen.SetValue(bValue, out reason, false))
  290. {
  291. EV.PostWarningLog("System", $"interlock set DO {Module} to {bValue}, {reason}");
  292. }
  293. }
  294. //EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", $":Valve {Name} keep closed ");
  295. return;
  296. }
  297. }
  298. try
  299. {
  300. if (_diOpenSensor != null && _diCloseSensor != null && _doOpen != null)
  301. {
  302. if (_diOpenSensor.Value != _diCloseSensor.Value)
  303. {
  304. _mutexSignalTimer.Start(2000);
  305. }
  306. _mutexSignalTrigger.CLK = _mutexSignalTimer.IsTimeout();
  307. if (_mutexSignalTrigger.Q)
  308. {
  309. EV.PostWarningLog(Module, $"Valve {Name} was abnormal,Reason:diOpenSensor's value is {_diOpenSensor.Value} and diCloseSensor's value is {_diCloseSensor.Value} too.");
  310. }
  311. }
  312. if (_timer.IsTimeout() && _doOpen != null)
  313. {
  314. _timer.Stop();
  315. if (Status != _operation)
  316. {
  317. if (_operation)
  318. {
  319. string reason;
  320. if (!_doOpen.Check(_isNc ? true : false, out reason))
  321. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Failed for interlock " + reason);
  322. else
  323. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", $":Valve {Name} keep closed ");
  324. }
  325. else
  326. {
  327. string reason;
  328. if (!_doOpen.Check(_isNc ? true : false, out reason))
  329. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Failed for interlock " + reason);
  330. else
  331. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", $":Valve {Name} keep open");
  332. }
  333. }
  334. _operation = SetPoint;
  335. }
  336. else if (_timer.IsIdle() && _doOpen != null)
  337. {
  338. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  339. if (eventTrigger.Q)
  340. {
  341. if (_operation)
  342. {
  343. string reason;
  344. if (!_doOpen.Check(_isNc ? true : false, out reason))
  345. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Close", reason));
  346. else
  347. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
  348. }
  349. else
  350. {
  351. string reason;
  352. if (!_doOpen.Check(_isNc ? true : false, out reason))
  353. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Open", reason));
  354. else
  355. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
  356. }
  357. _operation = SetPoint;
  358. }
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. LOG.Write(ex);
  364. }
  365. if (ILKDiSensor == null)
  366. {
  367. IsILKOK = true;
  368. }
  369. else
  370. {
  371. if (!_isNc)
  372. {
  373. IsILKOK = true;
  374. return;
  375. }
  376. if (ILKDiSensor.Value)
  377. {
  378. IsILKOK = true;
  379. }
  380. else if (!ILKDiSensor.Value)
  381. {
  382. if (!IsILKNot)
  383. {
  384. IsILKOK = false;
  385. }
  386. }
  387. }
  388. }
  389. public bool TurnValve(bool isOn, out string reason)
  390. {
  391. bool bValue = _isNc ? isOn : !isOn;
  392. var stringOnOff = isOn ? "On" : "Off";
  393. //EV.PostInfoLog(Module, $"Turn {Display} {stringOnOff}");
  394. _writeLog = $"Turn {Display} {stringOnOff}";
  395. reason = "";
  396. if (_doOpen != null)
  397. {
  398. if (!_doOpen.Check(bValue, out reason))
  399. {
  400. if (!_isNc && !IsILKNot)
  401. {
  402. IsILKOK = false;
  403. }
  404. _doOpen.SetValue(bValue, out reason, false);
  405. //EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  406. return false;
  407. }
  408. }
  409. if (_doClose != null)
  410. {
  411. if (!_doClose.Check(!bValue, out reason))
  412. {
  413. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  414. return false;
  415. }
  416. }
  417. SetPoint = isOn;
  418. _operation = isOn;
  419. if (_interlockNoneOrExist != null && _interlockDelayTime != null && _interlockNoneOrExist.BoolValue)
  420. {
  421. var timeParas = _interlockDelayTime.StringValue.Split(':');//00:00.0
  422. _interlockDelayTimeInMS = 0;
  423. if (timeParas.Length > 1)
  424. {
  425. float.TryParse(timeParas[0], out float min);
  426. float.TryParse(timeParas[1], out float sec);
  427. _interlockDelayTimeInMS = min * 60 * 1000 + sec * 1000;
  428. }
  429. if (_interlockDelayTimeInMS > 0)
  430. {
  431. _writeLog += $" delay time={_interlockDelayTimeInMS} ms";
  432. _interlockDelayTimer.Restart();
  433. _operation = bValue;
  434. _timer.Start(2000 + _interlockDelayTimeInMS);
  435. }
  436. else
  437. {
  438. _timer.Start(2000); //2 seconds to monitor
  439. }
  440. }
  441. else
  442. {
  443. if (_doOpen != null)
  444. {
  445. if (!_doOpen.Check(bValue, out reason))
  446. {
  447. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  448. return false;
  449. }
  450. }
  451. if (_doClose != null)
  452. {
  453. if (!_doClose.Check(!bValue, out reason))
  454. {
  455. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, $"{(isOn ? "Open" : "Close")}", ":Failed for interlock " + reason); ;
  456. return false;
  457. }
  458. }
  459. SetPoint = isOn;
  460. _operation = isOn;
  461. _timer.Start(2000); //2 seconds to monitor
  462. }
  463. return true;
  464. }
  465. public void Reset()
  466. {
  467. eventTrigger.RST = true;
  468. }
  469. }
  470. }