IoSignalTower.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. DATA.Subscribe($"{Module}.{Name}.IsRedLightOn", () => _red != null && _red.Value);
  148. DATA.Subscribe($"{Module}.{Name}.IsYellowLightOn", () => _yellow != null && _yellow.Value);
  149. DATA.Subscribe($"{Module}.{Name}.IsGreenLightOn", () => _green != null && _green.Value);
  150. DATA.Subscribe($"{Module}.{Name}.IsBlueLightOn", () => _blue != null && _blue.Value);
  151. DATA.Subscribe($"{Module}.{Name}.IsBuzzerOn", () => (_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value));
  152. return true;
  153. }
  154. public void SetLight(LightType lightType, LightState state, int blinkInterval = 500)
  155. {
  156. IoSignalLight light = null;
  157. switch (lightType)
  158. {
  159. case LightType.RED:
  160. light = _red;
  161. break;
  162. case LightType.GREEN:
  163. light = _green;
  164. break;
  165. case LightType.YELLOW:
  166. light = _yellow;
  167. break;
  168. case LightType.WHITE:
  169. light = _white;
  170. break;
  171. case LightType.BLUE:
  172. light = _blue;
  173. break;
  174. case LightType.BUZZER1:
  175. light = _buzzer1;
  176. break;
  177. //case LightType.buzz:
  178. // light = _buzzer2;
  179. // break;
  180. }
  181. if (light == null)
  182. {
  183. LOG.Write(eEvent.ERR_SIGNAL_TOWER_UNDEFINE, ModuleName.System, light.ToString());
  184. return;
  185. }
  186. switch (state)
  187. {
  188. case LightState.On:
  189. _cmdSetPoint[light] = TowerLightStatus.On;
  190. light.StateSetPoint = TowerLightStatus.On;
  191. break;
  192. case LightState.Off:
  193. _cmdSetPoint[light] = TowerLightStatus.Off;
  194. light.StateSetPoint = TowerLightStatus.Off;
  195. break;
  196. case LightState.Blink:
  197. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  198. light.Interval = blinkInterval;
  199. light.StateSetPoint = TowerLightStatus.Blinking;
  200. break;
  201. }
  202. }
  203. public bool CustomSignalTower(string configPathFile)
  204. {
  205. try
  206. {
  207. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  208. lock (_locker)
  209. {
  210. foreach (STEvent e in config.Events)
  211. {
  212. if (!_config.ContainsKey(e.Name))
  213. {
  214. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  215. }
  216. if (_red != null)
  217. _config[e.Name][_red] = ParseLight(e.Red);
  218. if (_yellow != null)
  219. _config[e.Name][_yellow] = ParseLight(e.Yellow);
  220. if (_green != null)
  221. _config[e.Name][_green] = ParseLight(e.Green);
  222. if (_blue != null)
  223. _config[e.Name][_blue] = ParseLight(e.Blue);
  224. if (_white != null)
  225. _config[e.Name][_white] = ParseLight(e.White);
  226. if (_buzzer1 != null)
  227. _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1);
  228. }
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, e.Message);
  234. return false;
  235. }
  236. return true;
  237. }
  238. //响3声,通知工艺正常结束
  239. public void BuzzerBlinking(double time)
  240. {
  241. _timerBuzzerBlinking.Start(time);
  242. }
  243. public bool SwitchOnBuzzerAndRed(string cmd, object[] objs)
  244. {
  245. return _buzzerAndRed = true;
  246. }
  247. public void Monitor()
  248. {
  249. if (DATA.Poll(Module, StateData.PMState.ToString()) != null)
  250. state = (PMState)DATA.Poll(Module, StateData.PMState.ToString());
  251. List<IoValve> valves = DEVICE.GetDevice<IoValve>();
  252. //if (DEVICE.GetDevice<IoRf>() != null)
  253. //{
  254. // _Generator = DEVICE.GetDevice<IoRf>($"{Module}.{VenusDevice.Rf}");
  255. //}
  256. //else
  257. if (DEVICE.GetDevice<AdTecGenerator>() != null)
  258. {
  259. _Generator = DEVICE.GetDevice<AdTecGenerator>($"{Module}.{VenusDevice.Rf}");
  260. }
  261. else
  262. {
  263. _Generator = null;
  264. }
  265. //pumping valve开,vent purge valve开,Gas valve开,RF开,只要有其中之一处于开的状态,就是绿灯
  266. if (state == PMState.Error || _buzzerAndRed)
  267. {
  268. SetLight(LightType.GREEN, LightState.Off);
  269. SetLight(LightType.RED, LightState.On);
  270. SetLight(LightType.YELLOW, LightState.Off);
  271. SetLight(LightType.BUZZER1, _buzzerSwitchOff ? LightState.Off : LightState.On);
  272. }
  273. else if ((_Generator != null && _Generator.IsPowerOn) || valves.Exists(v => v.Status))
  274. {
  275. SetLight(LightType.GREEN, LightState.On);
  276. SetLight(LightType.RED, LightState.Off);
  277. SetLight(LightType.YELLOW, LightState.Off);
  278. SetLight(LightType.BUZZER1, LightState.Off);
  279. }
  280. else if (state == PMState.PreProcess || state == PMState.Processing || state == PMState.PostProcess)
  281. {
  282. SetLight(LightType.GREEN, LightState.On);
  283. SetLight(LightType.RED, LightState.Off);
  284. SetLight(LightType.YELLOW, LightState.Off);
  285. SetLight(LightType.BUZZER1, LightState.Off);
  286. }
  287. else
  288. {
  289. SetLight(LightType.GREEN, LightState.Off);
  290. SetLight(LightType.RED, LightState.Off);
  291. SetLight(LightType.YELLOW, LightState.On);
  292. SetLight(LightType.BUZZER1, LightState.Off);
  293. }
  294. if (!_timerBuzzerBlinking.IsIdle() && !_timerBuzzerBlinking.IsTimeout())
  295. SetLight(LightType.BUZZER1, LightState.Blink);
  296. MonitorLight(_red);
  297. MonitorLight(_blue);
  298. MonitorLight(_yellow);
  299. MonitorLight(_green);
  300. MonitorLight(_white);
  301. MonitorLight(_buzzer1);
  302. MonitorLight(_buzzer2);
  303. }
  304. public void Reset()
  305. {
  306. ResetLight(_red);
  307. ResetLight(_blue);
  308. ResetLight(_yellow);
  309. ResetLight(_green);
  310. ResetLight(_white);
  311. ResetLight(_buzzer1);
  312. ResetLight(_buzzer2);
  313. _buzzerAndRed = false;
  314. _buzzerSwitchOff = false;
  315. }
  316. public bool SwitchOffBuzzer(string cmd, object[] objs)
  317. {
  318. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)
  319. {
  320. _buzzerSwitchOff = true;
  321. }
  322. return true;
  323. }
  324. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  325. {
  326. if (light != null)
  327. {
  328. light.StateSetPoint = state[light];
  329. }
  330. }
  331. private void ResetLight(IoSignalLight light)
  332. {
  333. if (light != null)
  334. {
  335. light.Reset();
  336. }
  337. }
  338. private void MonitorLight(IoSignalLight light)
  339. {
  340. if (light != null)
  341. {
  342. light.Monitor();
  343. }
  344. }
  345. public void Terminate()
  346. {
  347. }
  348. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  349. {
  350. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  351. return TowerLightStatus.Blinking;
  352. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  353. return TowerLightStatus.On;
  354. return TowerLightStatus.Off;
  355. }
  356. private TowerLightStatus ParseLight(string light)
  357. {
  358. if (string.IsNullOrEmpty(light))
  359. return TowerLightStatus.Off;
  360. TowerLightStatus result = TowerLightStatus.Unknown;
  361. light = light.Trim().ToLower();
  362. switch (light)
  363. {
  364. case "on":
  365. result = TowerLightStatus.On;
  366. break;
  367. case "off":
  368. result = TowerLightStatus.Off;
  369. break;
  370. case "blinking":
  371. result = TowerLightStatus.Blinking;
  372. break;
  373. default:
  374. LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, light);
  375. break;
  376. }
  377. return result;
  378. }
  379. }
  380. }