IoSignalTower.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Device;
  10. using Aitex.Core.RT.Device.Unit;
  11. using Aitex.Core.RT.Event;
  12. using Aitex.Core.RT.IOCore;
  13. using Aitex.Core.RT.Log;
  14. using Aitex.Core.RT.OperationCenter;
  15. using Aitex.Core.Util;
  16. using MECF.Framework.Common.Device.Bases;
  17. using MECF.Framework.Common.Equipment;
  18. using Virgo_DCommon;
  19. namespace Virgo_DRT.Devices
  20. {
  21. public enum LightType
  22. {
  23. Red,
  24. Yellow,
  25. Green,
  26. White,
  27. Blue,
  28. Buzzer
  29. }
  30. public enum LightState
  31. {
  32. Off,
  33. On,
  34. Blink,
  35. }
  36. public class STEvents
  37. {
  38. [XmlElement(ElementName = "STEvent")]
  39. public List<STEvent> Events;
  40. }
  41. public class STEvent
  42. {
  43. [XmlAttribute(AttributeName = "name")]
  44. public string Name { get; set; }
  45. [XmlAttribute]
  46. public string Red { get; set; }
  47. [XmlAttribute]
  48. public string Yellow { get; set; }
  49. [XmlAttribute]
  50. public string Green { get; set; }
  51. [XmlAttribute]
  52. public string Blue { get; set; }
  53. [XmlAttribute]
  54. public string White { get; set; }
  55. [XmlAttribute]
  56. public string Buzzer { get; set; }
  57. }
  58. public class IoSignalTower : BaseDevice, IDevice
  59. {
  60. //device
  61. private IoSignalLight _red = null;
  62. private IoSignalLight _yellow = null;
  63. private IoSignalLight _green = null;
  64. private IoSignalLight _blue = null;
  65. private IoSignalLight _white = null;
  66. private IoSignalLight _buzzer = null;
  67. private PMState state;
  68. public bool HostControl { get; set; }
  69. private bool _buzzerSwitchOff = false;
  70. private bool _buzzerAndRed = false;
  71. private RfPowerBase _Generator;
  72. public DeviceTimer _timerBuzzerBlinking = new DeviceTimer();
  73. public AITSignalTowerData DeviceData
  74. {
  75. get
  76. {
  77. AITSignalTowerData data = new AITSignalTowerData()
  78. {
  79. DeviceName = Name,
  80. DeviceSchematicId = DeviceID,
  81. DisplayName = Display,
  82. IsGreenLightOn = _green != null && _green.Value,
  83. IsRedLightOn = _red != null && _red.Value,
  84. IsYellowLightOn = _yellow != null && _yellow.Value,
  85. IsWhiteLightOn = _white != null && _white.Value,
  86. IsBlueLightOn = _blue != null && _blue.Value,
  87. IsBuzzerOn = _buzzer != null && _buzzer.Value,
  88. };
  89. return data;
  90. }
  91. }
  92. private object _locker = new object();
  93. private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =
  94. new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();
  95. private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();
  96. public IoSignalTower(string module, XmlElement node, string ioModule = "")
  97. {
  98. base.Module = module;
  99. base.Name = node.GetAttribute("id");
  100. base.Display = node.GetAttribute("display");
  101. base.DeviceID = node.GetAttribute("schematicId");
  102. DOAccessor doRed = ParseDoNode("doRed", node, ioModule);
  103. if (doRed != null)
  104. {
  105. _red = new IoSignalLight(module, "SignalLightRed", "Red Light", "SignalLightRed", doRed);
  106. }
  107. DOAccessor doYellow = ParseDoNode("doYellow", node, ioModule);
  108. if (doYellow != null)
  109. {
  110. _yellow = new IoSignalLight(module, "SignalLightYellow", "Yellow Light", "SignalLightYellow", doYellow);
  111. }
  112. DOAccessor doGreen = ParseDoNode("doGreen", node, ioModule);
  113. if (doGreen != null)
  114. {
  115. _green = new IoSignalLight(module, "SignalLightGreen", "Green Light", "SignalLightGreen", doGreen);
  116. }
  117. DOAccessor doBlue = ParseDoNode("doBlue", node, ioModule);
  118. if (doBlue != null)
  119. {
  120. _blue = new IoSignalLight(module, "SignalLightBlue", "Blue Light", "SignalLightBlue", doBlue);
  121. }
  122. DOAccessor doWhite = ParseDoNode("doWhite", node, ioModule);
  123. if (doWhite != null)
  124. {
  125. _white = new IoSignalLight(module, "SignalLightWhite", "White Light", "SignalLightWhite", doWhite);
  126. }
  127. DOAccessor doBuzzer = ParseDoNode("doBuzzer", node, ioModule);
  128. if (doBuzzer != null)
  129. {
  130. _buzzer = new IoSignalLight(module, "SignalLightBuzzer", "Buzzer Light", "SignalLightBuzzer", doBuzzer);
  131. }
  132. }
  133. public bool Initialize()
  134. {
  135. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  136. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOnBuzzerAndRed}", SwitchOnBuzzerAndRed);
  137. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  138. return true;
  139. }
  140. public void SetLight(LightType lightType, LightState state, int blinkInterval = 500)
  141. {
  142. IoSignalLight light = null;
  143. switch (lightType)
  144. {
  145. case LightType.Red:
  146. light = _red;
  147. break;
  148. case LightType.Green:
  149. light = _green;
  150. break;
  151. case LightType.Yellow:
  152. light = _yellow;
  153. break;
  154. case LightType.White:
  155. light = _white;
  156. break;
  157. case LightType.Blue:
  158. light = _blue;
  159. break;
  160. case LightType.Buzzer:
  161. light = _buzzer;
  162. break;
  163. }
  164. if (light == null)
  165. {
  166. LOG.Write($"Undefined light {lightType}");
  167. return;
  168. }
  169. switch (state)
  170. {
  171. case LightState.On:
  172. _cmdSetPoint[light] = TowerLightStatus.On;
  173. light.StateSetPoint = TowerLightStatus.On;
  174. break;
  175. case LightState.Off:
  176. _cmdSetPoint[light] = TowerLightStatus.Off;
  177. light.StateSetPoint = TowerLightStatus.Off;
  178. break;
  179. case LightState.Blink:
  180. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  181. light.Interval = blinkInterval;
  182. light.StateSetPoint = TowerLightStatus.Blinking;
  183. break;
  184. }
  185. }
  186. public bool CustomSignalTower(string configPathFile)
  187. {
  188. try
  189. {
  190. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  191. lock (_locker)
  192. {
  193. foreach (STEvent e in config.Events)
  194. {
  195. if (!_config.ContainsKey(e.Name))
  196. {
  197. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  198. }
  199. if (_red != null)
  200. _config[e.Name][_red] = ParseLight(e.Red);
  201. if (_yellow != null)
  202. _config[e.Name][_yellow] = ParseLight(e.Yellow);
  203. if (_green != null)
  204. _config[e.Name][_green] = ParseLight(e.Green);
  205. if (_blue != null)
  206. _config[e.Name][_blue] = ParseLight(e.Blue);
  207. if (_white != null)
  208. _config[e.Name][_white] = ParseLight(e.White);
  209. if (_buzzer != null)
  210. _config[e.Name][_buzzer] = ParseLight(e.Buzzer);
  211. }
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. EV.PostWarningLog(Module, $"Signal tower config file is invalid, {e.Message}");
  217. return false;
  218. }
  219. return true;
  220. }
  221. //响3声,通知工艺正常结束
  222. public void BuzzerBlinking(double time)
  223. {
  224. _timerBuzzerBlinking.Start(time);
  225. }
  226. public bool SwitchOnBuzzerAndRed(string cmd, object[] objs)
  227. {
  228. return _buzzerAndRed = true;
  229. }
  230. public void Monitor()
  231. {
  232. if (DATA.Poll(Module, StateData.PMState.ToString()) != null)
  233. state = (PMState)DATA.Poll(Module, StateData.PMState.ToString());
  234. List<IoValve> valves = DEVICE.GetDevice<IoValve>();
  235. if (DEVICE.GetDevice<IoRf>() != null)
  236. {
  237. _Generator = DEVICE.GetDevice<IoRf>($"{Module}.{Virgo_DDevice.Rf}");
  238. }
  239. else if (DEVICE.GetDevice<AdTecGenerator>() != null)
  240. {
  241. _Generator = DEVICE.GetDevice<AdTecGenerator>($"{Module}.{Virgo_DDevice.Rf}");
  242. }
  243. else
  244. {
  245. _Generator = null;
  246. }
  247. //pumping valve开,vent purge valve开,Gas valve开,RF开,只要有其中之一处于开的状态,就是绿灯
  248. if (state == PMState.Error || _buzzerAndRed)
  249. {
  250. SetLight(LightType.Green, LightState.Off);
  251. SetLight(LightType.Red, LightState.On);
  252. SetLight(LightType.Yellow, LightState.Off);
  253. SetLight(LightType.Buzzer, _buzzerSwitchOff ? LightState.Off : LightState.On);
  254. }
  255. else if ((_Generator != null && _Generator.IsPowerOn) || valves.Exists(v => v.Status))
  256. {
  257. SetLight(LightType.Green, LightState.On);
  258. SetLight(LightType.Red, LightState.Off);
  259. SetLight(LightType.Yellow, LightState.Off);
  260. SetLight(LightType.Buzzer, LightState.Off);
  261. }
  262. //else if (state == PMState.PreProcess ||state == PMState.Processing ||state == PMState.PostProcess)
  263. //{
  264. // Green = TowerLightStatus.On;
  265. // Red = TowerLightStatus.Off;
  266. // Yellow = TowerLightStatus.Off;
  267. // Buzzer_Alarm = TowerLightStatus.Off;
  268. //}
  269. else
  270. {
  271. SetLight(LightType.Green, LightState.Off);
  272. SetLight(LightType.Red, LightState.Off);
  273. SetLight(LightType.Yellow, LightState.On);
  274. SetLight(LightType.Buzzer, LightState.Off);
  275. }
  276. if (!_timerBuzzerBlinking.IsIdle() && !_timerBuzzerBlinking.IsTimeout())
  277. SetLight(LightType.Buzzer, LightState.Blink);
  278. MonitorLight(_red);
  279. MonitorLight(_blue);
  280. MonitorLight(_yellow);
  281. MonitorLight(_green);
  282. MonitorLight(_white);
  283. MonitorLight(_buzzer);
  284. }
  285. public void Reset()
  286. {
  287. ResetLight(_red);
  288. ResetLight(_blue);
  289. ResetLight(_yellow);
  290. ResetLight(_green);
  291. ResetLight(_white);
  292. ResetLight(_buzzer);
  293. _buzzerAndRed = false;
  294. _buzzerSwitchOff = false;
  295. }
  296. public bool SwitchOffBuzzer(string cmd, object[] objs)
  297. {
  298. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer != null && _buzzer.StateSetPoint != TowerLightStatus.Off)
  299. {
  300. _buzzerSwitchOff = true;
  301. }
  302. return true;
  303. }
  304. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  305. {
  306. if (light != null)
  307. {
  308. light.StateSetPoint = state[light];
  309. }
  310. }
  311. private void ResetLight(IoSignalLight light)
  312. {
  313. if (light != null)
  314. {
  315. light.Reset();
  316. }
  317. }
  318. private void MonitorLight(IoSignalLight light)
  319. {
  320. if (light != null)
  321. {
  322. light.Monitor();
  323. }
  324. }
  325. public void Terminate()
  326. {
  327. }
  328. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  329. {
  330. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  331. return TowerLightStatus.Blinking;
  332. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  333. return TowerLightStatus.On;
  334. return TowerLightStatus.Off;
  335. }
  336. private TowerLightStatus ParseLight(string light)
  337. {
  338. if (string.IsNullOrEmpty(light))
  339. return TowerLightStatus.Off;
  340. TowerLightStatus result = TowerLightStatus.Unknown;
  341. light = light.Trim().ToLower();
  342. switch (light)
  343. {
  344. case "on":
  345. result = TowerLightStatus.On;
  346. break;
  347. case "off":
  348. result = TowerLightStatus.Off;
  349. break;
  350. case "blinking":
  351. result = TowerLightStatus.Blinking;
  352. break;
  353. default:
  354. LOG.Write("signal tower config file has invalid set, " + light);
  355. break;
  356. }
  357. return result;
  358. }
  359. }
  360. }