IoSignalTower.cs 14 KB

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