IoSignalTower.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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.Common.Util;
  8. using Aitex.Core.Common.DeviceData;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Device;
  11. using Aitex.Core.RT.Device.Unit;
  12. using Aitex.Core.RT.Event;
  13. using Aitex.Core.RT.IOCore;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.OperationCenter;
  16. using Aitex.Core.Util;
  17. using MECF.Framework.Common.Device.Bases;
  18. using MECF.Framework.Common.Equipment;
  19. using Venus_Core;
  20. namespace Venus_RT.Devices
  21. {
  22. //public enum LightType
  23. //{
  24. // Red,
  25. // Yellow,
  26. // Green,
  27. // White,
  28. // Blue,
  29. // Buzzer1,
  30. // Buzzer2
  31. //}
  32. public enum LightState
  33. {
  34. Off,
  35. On,
  36. Blink,
  37. }
  38. public class STEvents
  39. {
  40. [XmlElement(ElementName = "STEvent")]
  41. public List<STEvent> Events;
  42. }
  43. public class STEvent
  44. {
  45. [XmlAttribute(AttributeName = "name")]
  46. public string Name { get; set; }
  47. [XmlAttribute]
  48. public string Red { get; set; }
  49. [XmlAttribute]
  50. public string Yellow { get; set; }
  51. [XmlAttribute]
  52. public string Green { get; set; }
  53. [XmlAttribute]
  54. public string Blue { get; set; }
  55. [XmlAttribute]
  56. public string White { get; set; }
  57. [XmlAttribute]
  58. public string Buzzer1 { get; set; }
  59. [XmlAttribute]
  60. public string Buzzer2 { get; set; }
  61. }
  62. public class IoSignalTower : BaseDevice, IDevice
  63. {
  64. //device
  65. private IoSignalLight _red = null;
  66. private IoSignalLight _yellow = null;
  67. private IoSignalLight _green = null;
  68. private IoSignalLight _blue = null;
  69. private IoSignalLight _white = null;
  70. private IoSignalLight _buzzer1 = null;
  71. private IoSignalLight _buzzer2 = null;
  72. private PMState state;
  73. public bool HostControl { get; set; }
  74. private bool _buzzerSwitchOff = false;
  75. private bool _buzzerAndRed = false;
  76. private bool _buzzer1SwitchOff = false;
  77. private bool _buzzer2SwitchOff = false;
  78. private RfPowerBase _Generator;
  79. public DeviceTimer _timerBuzzerBlinking = new DeviceTimer();
  80. public AITSignalTowerData DeviceData
  81. {
  82. get
  83. {
  84. AITSignalTowerData data = new AITSignalTowerData()
  85. {
  86. DeviceName = Name,
  87. DeviceSchematicId = DeviceID,
  88. DisplayName = Display,
  89. IsGreenLightOn = _green != null && _green.Value,
  90. IsRedLightOn = _red != null && _red.Value,
  91. IsYellowLightOn = _yellow != null && _yellow.Value,
  92. IsWhiteLightOn = _white != null && _white.Value,
  93. IsBlueLightOn = _blue != null && _blue.Value,
  94. IsBuzzerOn = (_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value),
  95. IsBuzzerOff = !((_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value)),
  96. };
  97. return data;
  98. }
  99. }
  100. private object _locker = new object();
  101. private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =
  102. new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();
  103. private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();
  104. private Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  105. public IoSignalTower(string module, XmlElement node, string ioModule = "")
  106. {
  107. base.Module = module;
  108. base.Name = node.GetAttribute("id");
  109. base.Display = node.GetAttribute("display");
  110. base.DeviceID = node.GetAttribute("schematicId");
  111. DOAccessor doRed = ParseDoNode("doRed", node, ioModule);
  112. if (doRed != null)
  113. {
  114. _red = new IoSignalLight(module, "SignalLightRed", "Red Light", "SignalLightRed", doRed);
  115. }
  116. DOAccessor doYellow = ParseDoNode("doYellow", node, ioModule);
  117. if (doYellow != null)
  118. {
  119. _yellow = new IoSignalLight(module, "SignalLightYellow", "Yellow Light", "SignalLightYellow", doYellow);
  120. }
  121. DOAccessor doGreen = ParseDoNode("doGreen", node, ioModule);
  122. if (doGreen != null)
  123. {
  124. _green = new IoSignalLight(module, "SignalLightGreen", "Green Light", "SignalLightGreen", doGreen);
  125. }
  126. DOAccessor doBlue = ParseDoNode("doBlue", node, ioModule);
  127. if (doBlue != null)
  128. {
  129. _blue = new IoSignalLight(module, "SignalLightBlue", "Blue Light", "SignalLightBlue", doBlue);
  130. }
  131. DOAccessor doWhite = ParseDoNode("doWhite", node, ioModule);
  132. if (doWhite != null)
  133. {
  134. _white = new IoSignalLight(module, "SignalLightWhite", "White Light", "SignalLightWhite", doWhite);
  135. }
  136. DOAccessor doBuzzer1 = ParseDoNode("doBuzzer1", node, ioModule);
  137. if (doBuzzer1 != null)
  138. {
  139. _buzzer1 = new IoSignalLight(module, "SignalLightBuzzer1", "Buzzer Light 1", "SignalLightBuzzer1", doBuzzer1);
  140. }
  141. DOAccessor doBuzzer2 = ParseDoNode("doBuzzer2", node, ioModule);
  142. if (doBuzzer2 != null)
  143. {
  144. _buzzer2 = new IoSignalLight(module, "SignalLightBuzzer2", "Buzzer Light 2", "SignalLightBuzzer2", doBuzzer2);
  145. }
  146. }
  147. public bool Initialize()
  148. {
  149. OP.Subscribe($"System.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  150. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOnBuzzerAndRed}", SwitchOnBuzzerAndRed);
  151. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  152. DATA.Subscribe($"{Module}.{Name}.IsRedLightOn", () => _red != null && _red.Value);
  153. DATA.Subscribe($"{Module}.{Name}.IsYellowLightOn", () => _yellow != null && _yellow.Value);
  154. DATA.Subscribe($"{Module}.{Name}.IsGreenLightOn", () => _green != null && _green.Value);
  155. DATA.Subscribe($"{Module}.{Name}.IsBlueLightOn", () => _blue != null && _blue.Value);
  156. DATA.Subscribe($"{Module}.{Name}.IsBuzzerOn", () => (_buzzer1 != null && _buzzer1.Value) || (_buzzer2 != null && _buzzer2.Value));
  157. CustomSignalTower();
  158. return true;
  159. }
  160. public void SetLight(LightType lightType, LightState state, int blinkInterval = 500)
  161. {
  162. IoSignalLight light = null;
  163. switch (lightType)
  164. {
  165. case LightType.RED:
  166. light = _red;
  167. break;
  168. case LightType.GREEN:
  169. light = _green;
  170. break;
  171. case LightType.YELLOW:
  172. light = _yellow;
  173. break;
  174. case LightType.WHITE:
  175. light = _white;
  176. break;
  177. case LightType.BLUE:
  178. light = _blue;
  179. break;
  180. case LightType.BUZZER1:
  181. light = _buzzer1;
  182. break;
  183. //case LightType.buzz:
  184. // light = _buzzer2;
  185. // break;
  186. }
  187. if (light == null)
  188. {
  189. LOG.Write(eEvent.ERR_SIGNAL_TOWER_UNDEFINE, ModuleName.System, light.ToString());
  190. return;
  191. }
  192. switch (state)
  193. {
  194. case LightState.On:
  195. _cmdSetPoint[light] = TowerLightStatus.On;
  196. light.StateSetPoint = TowerLightStatus.On;
  197. break;
  198. case LightState.Off:
  199. _cmdSetPoint[light] = TowerLightStatus.Off;
  200. light.StateSetPoint = TowerLightStatus.Off;
  201. break;
  202. case LightState.Blink:
  203. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  204. light.Interval = blinkInterval;
  205. light.StateSetPoint = TowerLightStatus.Blinking;
  206. break;
  207. }
  208. }
  209. public bool CustomSignalTower()
  210. {
  211. string pathFile = PathManager.GetCfgDir() + "_VenusSignalTower.xml";
  212. if (!File.Exists(pathFile))
  213. {
  214. pathFile = PathManager.GetCfgDir() + "VenusSignalTower.xml";
  215. }
  216. return CustomSignalTower(pathFile);
  217. }
  218. public bool CustomSignalTower(string configPathFile)
  219. {
  220. try
  221. {
  222. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  223. lock (_locker)
  224. {
  225. foreach (STEvent e in config.Events)
  226. {
  227. if (!_config.ContainsKey(e.Name))
  228. {
  229. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  230. }
  231. if (_red != null)
  232. _config[e.Name][_red] = ParseLight(e.Red);
  233. if (_yellow != null)
  234. _config[e.Name][_yellow] = ParseLight(e.Yellow);
  235. if (_green != null)
  236. _config[e.Name][_green] = ParseLight(e.Green);
  237. if (_blue != null)
  238. _config[e.Name][_blue] = ParseLight(e.Blue);
  239. if (_white != null)
  240. _config[e.Name][_white] = ParseLight(e.White);
  241. if (_buzzer1 != null)
  242. _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1);
  243. if (_buzzer2 != null)
  244. _config[e.Name][_buzzer2] = ParseLight(e.Buzzer2);
  245. }
  246. }
  247. }
  248. catch (Exception e)
  249. {
  250. LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, e.Message);
  251. return false;
  252. }
  253. return true;
  254. }
  255. //响3声,通知工艺正常结束
  256. public void BuzzerBlinking(double time)
  257. {
  258. _timerBuzzerBlinking.Start(time);
  259. }
  260. public bool SwitchOnBuzzerAndRed(string cmd, object[] objs)
  261. {
  262. return _buzzerAndRed = true;
  263. }
  264. public void Monitor()
  265. {
  266. if (_red != null)
  267. dicState[_red] = TowerLightStatus.Off;
  268. if (_yellow != null)
  269. dicState[_yellow] = TowerLightStatus.Off;
  270. if (_green != null)
  271. dicState[_green] = TowerLightStatus.Off;
  272. if (_blue != null)
  273. dicState[_blue] = TowerLightStatus.Off;
  274. if (_white != null)
  275. dicState[_white] = TowerLightStatus.Off;
  276. if (_buzzer1 != null)
  277. dicState[_buzzer1] = TowerLightStatus.Off;
  278. if (_buzzer2 != null)
  279. dicState[_buzzer2] = TowerLightStatus.Off;
  280. foreach (var trigCondition in _config)
  281. {
  282. var conditionValue = DATA.Poll(trigCondition.Key);
  283. if (conditionValue == null)
  284. continue;
  285. bool isTrig = (bool)conditionValue;
  286. if (isTrig)
  287. {
  288. if (_red != null)
  289. {
  290. dicState[_red] = MergeCondition(dicState[_red], trigCondition.Value[_red]);
  291. }
  292. if (_yellow != null)
  293. {
  294. dicState[_yellow] = MergeCondition(dicState[_yellow], trigCondition.Value[_yellow]);
  295. }
  296. if (_green != null)
  297. {
  298. dicState[_green] = MergeCondition(dicState[_green], trigCondition.Value[_green]);
  299. }
  300. if (_white != null)
  301. {
  302. dicState[_white] = MergeCondition(dicState[_white], trigCondition.Value[_white]);
  303. }
  304. if (_blue != null)
  305. {
  306. dicState[_blue] = MergeCondition(dicState[_blue], trigCondition.Value[_blue]);
  307. }
  308. if (_buzzer1 != null)
  309. {
  310. dicState[_buzzer1] = _buzzer1SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer1], trigCondition.Value[_buzzer1]);
  311. }
  312. if (_buzzer2 != null)
  313. {
  314. dicState[_buzzer2] = _buzzer2SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer2], trigCondition.Value[_buzzer2]);
  315. }
  316. }
  317. }
  318. if (_config.Count > 0 && !HostControl)
  319. {
  320. SetLight(_red, dicState);
  321. SetLight(_blue, dicState);
  322. SetLight(_yellow, dicState);
  323. SetLight(_green, dicState);
  324. SetLight(_white, dicState);
  325. SetLight(_buzzer1, dicState);
  326. SetLight(_buzzer2, dicState);
  327. }
  328. MonitorLight(_red);
  329. MonitorLight(_blue);
  330. MonitorLight(_yellow);
  331. MonitorLight(_green);
  332. MonitorLight(_white);
  333. MonitorLight(_buzzer1);
  334. MonitorLight(_buzzer2);
  335. //if (DATA.Poll(Module, StateData.PMState.ToString()) != null)
  336. // state = (PMState)DATA.Poll(Module, StateData.PMState.ToString());
  337. //List<IoValve> valves = DEVICE.GetDevice<IoValve>();
  338. //if (DEVICE.GetDevice<IoRf>() != null)
  339. //{
  340. // _Generator = DEVICE.GetDevice<IoRf>($"{Module}.{VenusDevice.Rf}");
  341. //}
  342. //else
  343. //if (DEVICE.GetDevice<AdTecGenerator>() != null)
  344. //{
  345. // _Generator = DEVICE.GetDevice<AdTecGenerator>($"{Module}.{VenusDevice.Rf}");
  346. //}
  347. //else
  348. //{
  349. // _Generator = null;
  350. //}
  351. ////pumping valve开,vent purge valve开,Gas valve开,RF开,只要有其中之一处于开的状态,就是绿灯
  352. //if (state == PMState.Error || _buzzerAndRed)
  353. //{
  354. // SetLight(LightType.GREEN, LightState.Off);
  355. // SetLight(LightType.RED, LightState.On);
  356. // SetLight(LightType.YELLOW, LightState.Off);
  357. // SetLight(LightType.BUZZER1, _buzzerSwitchOff ? LightState.Off : LightState.On);
  358. //}
  359. //else if ((_Generator != null && _Generator.IsPowerOn) || valves.Exists(v => v.Status))
  360. //{
  361. // SetLight(LightType.GREEN, LightState.On);
  362. // SetLight(LightType.RED, LightState.Off);
  363. // SetLight(LightType.YELLOW, LightState.Off);
  364. // SetLight(LightType.BUZZER1, LightState.Off);
  365. //}
  366. //else if (state == PMState.PreProcess || state == PMState.Processing || state == PMState.PostProcess)
  367. //{
  368. // SetLight(LightType.GREEN, LightState.On);
  369. // SetLight(LightType.RED, LightState.Off);
  370. // SetLight(LightType.YELLOW, LightState.Off);
  371. // SetLight(LightType.BUZZER1, LightState.Off);
  372. //}
  373. //else
  374. //{
  375. // SetLight(LightType.GREEN, LightState.Off);
  376. // SetLight(LightType.RED, LightState.Off);
  377. // SetLight(LightType.YELLOW, LightState.On);
  378. // SetLight(LightType.BUZZER1, LightState.Off);
  379. //}
  380. //
  381. //if (!_timerBuzzerBlinking.IsIdle() && !_timerBuzzerBlinking.IsTimeout())
  382. // SetLight(LightType.BUZZER1, LightState.Blink);
  383. //
  384. //MonitorLight(_red);
  385. //MonitorLight(_blue);
  386. //MonitorLight(_yellow);
  387. //MonitorLight(_green);
  388. //MonitorLight(_white);
  389. //MonitorLight(_buzzer1);
  390. //MonitorLight(_buzzer2);
  391. }
  392. public void Reset()
  393. {
  394. ResetLight(_red);
  395. ResetLight(_blue);
  396. ResetLight(_yellow);
  397. ResetLight(_green);
  398. ResetLight(_white);
  399. ResetLight(_buzzer1);
  400. ResetLight(_buzzer2);
  401. _buzzerAndRed = false;
  402. _buzzerSwitchOff = false;
  403. _buzzer1SwitchOff = false;
  404. _buzzer2SwitchOff = false;
  405. }
  406. public bool SwitchOffBuzzer(string cmd, object[] objs)
  407. {
  408. if (cmd == $"System.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)
  409. {
  410. _buzzerSwitchOff = true;
  411. _buzzer1SwitchOff = true;
  412. _buzzer2SwitchOff = true;
  413. }
  414. return true;
  415. }
  416. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  417. {
  418. if (light != null)
  419. {
  420. light.StateSetPoint = state[light];
  421. }
  422. }
  423. private void ResetLight(IoSignalLight light)
  424. {
  425. if (light != null)
  426. {
  427. light.Reset();
  428. }
  429. }
  430. private void MonitorLight(IoSignalLight light)
  431. {
  432. if (light != null)
  433. {
  434. light.Monitor();
  435. }
  436. }
  437. public void Terminate()
  438. {
  439. }
  440. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  441. {
  442. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  443. return TowerLightStatus.Blinking;
  444. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  445. return TowerLightStatus.On;
  446. return TowerLightStatus.Off;
  447. }
  448. private TowerLightStatus ParseLight(string light)
  449. {
  450. if (string.IsNullOrEmpty(light))
  451. return TowerLightStatus.Off;
  452. TowerLightStatus result = TowerLightStatus.Unknown;
  453. light = light.Trim().ToLower();
  454. switch (light)
  455. {
  456. case "on":
  457. result = TowerLightStatus.On;
  458. break;
  459. case "off":
  460. result = TowerLightStatus.Off;
  461. break;
  462. case "blinking":
  463. result = TowerLightStatus.Blinking;
  464. break;
  465. default:
  466. LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, light);
  467. break;
  468. }
  469. return result;
  470. }
  471. }
  472. }