IoSignalTower.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 true;
  263. //return _buzzerAndRed = true;
  264. }
  265. public void Monitor()
  266. {
  267. if (_red != null)
  268. dicState[_red] = TowerLightStatus.Off;
  269. if (_yellow != null)
  270. dicState[_yellow] = TowerLightStatus.Off;
  271. if (_green != null)
  272. dicState[_green] = TowerLightStatus.Off;
  273. if (_blue != null)
  274. dicState[_blue] = TowerLightStatus.Off;
  275. if (_white != null)
  276. dicState[_white] = TowerLightStatus.Off;
  277. if (_buzzer1 != null)
  278. dicState[_buzzer1] = TowerLightStatus.Off;
  279. if (_buzzer2 != null)
  280. dicState[_buzzer2] = TowerLightStatus.Off;
  281. foreach (var trigCondition in _config)
  282. {
  283. var conditionValue = DATA.Poll(trigCondition.Key);
  284. if (conditionValue == null)
  285. continue;
  286. bool isTrig = (bool)conditionValue;
  287. if (isTrig)
  288. {
  289. if (_red != null)
  290. {
  291. dicState[_red] = MergeCondition(dicState[_red], trigCondition.Value[_red]);
  292. }
  293. if (_yellow != null)
  294. {
  295. dicState[_yellow] = MergeCondition(dicState[_yellow], trigCondition.Value[_yellow]);
  296. }
  297. if (_green != null)
  298. {
  299. dicState[_green] = MergeCondition(dicState[_green], trigCondition.Value[_green]);
  300. }
  301. if (_white != null)
  302. {
  303. dicState[_white] = MergeCondition(dicState[_white], trigCondition.Value[_white]);
  304. }
  305. if (_blue != null)
  306. {
  307. dicState[_blue] = MergeCondition(dicState[_blue], trigCondition.Value[_blue]);
  308. }
  309. if (_buzzer1 != null)
  310. {
  311. dicState[_buzzer1] = _buzzer1SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer1], trigCondition.Value[_buzzer1]);
  312. }
  313. if (_buzzer2 != null)
  314. {
  315. dicState[_buzzer2] = _buzzer2SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer2], trigCondition.Value[_buzzer2]);
  316. }
  317. }
  318. }
  319. if (_config.Count > 0 && !HostControl)
  320. {
  321. SetLight(_red, dicState);
  322. SetLight(_blue, dicState);
  323. SetLight(_yellow, dicState);
  324. SetLight(_green, dicState);
  325. SetLight(_white, dicState);
  326. SetLight(_buzzer1, dicState);
  327. SetLight(_buzzer2, dicState);
  328. }
  329. MonitorLight(_red);
  330. MonitorLight(_blue);
  331. MonitorLight(_yellow);
  332. MonitorLight(_green);
  333. MonitorLight(_white);
  334. MonitorLight(_buzzer1);
  335. MonitorLight(_buzzer2);
  336. //if (DATA.Poll(Module, StateData.PMState.ToString()) != null)
  337. // state = (PMState)DATA.Poll(Module, StateData.PMState.ToString());
  338. //List<IoValve> valves = DEVICE.GetDevice<IoValve>();
  339. //if (DEVICE.GetDevice<IoRf>() != null)
  340. //{
  341. // _Generator = DEVICE.GetDevice<IoRf>($"{Module}.{VenusDevice.Rf}");
  342. //}
  343. //else
  344. //if (DEVICE.GetDevice<AdTecGenerator>() != null)
  345. //{
  346. // _Generator = DEVICE.GetDevice<AdTecGenerator>($"{Module}.{VenusDevice.Rf}");
  347. //}
  348. //else
  349. //{
  350. // _Generator = null;
  351. //}
  352. ////pumping valve开,vent purge valve开,Gas valve开,RF开,只要有其中之一处于开的状态,就是绿灯
  353. //if (state == PMState.Error || _buzzerAndRed)
  354. //{
  355. // SetLight(LightType.GREEN, LightState.Off);
  356. // SetLight(LightType.RED, LightState.On);
  357. // SetLight(LightType.YELLOW, LightState.Off);
  358. // SetLight(LightType.BUZZER1, _buzzerSwitchOff ? LightState.Off : LightState.On);
  359. //}
  360. //else if ((_Generator != null && _Generator.IsPowerOn) || valves.Exists(v => v.Status))
  361. //{
  362. // SetLight(LightType.GREEN, LightState.On);
  363. // SetLight(LightType.RED, LightState.Off);
  364. // SetLight(LightType.YELLOW, LightState.Off);
  365. // SetLight(LightType.BUZZER1, LightState.Off);
  366. //}
  367. //else if (state == PMState.PreProcess || state == PMState.Processing || state == PMState.PostProcess)
  368. //{
  369. // SetLight(LightType.GREEN, LightState.On);
  370. // SetLight(LightType.RED, LightState.Off);
  371. // SetLight(LightType.YELLOW, LightState.Off);
  372. // SetLight(LightType.BUZZER1, LightState.Off);
  373. //}
  374. //else
  375. //{
  376. // SetLight(LightType.GREEN, LightState.Off);
  377. // SetLight(LightType.RED, LightState.Off);
  378. // SetLight(LightType.YELLOW, LightState.On);
  379. // SetLight(LightType.BUZZER1, LightState.Off);
  380. //}
  381. //
  382. //if (!_timerBuzzerBlinking.IsIdle() && !_timerBuzzerBlinking.IsTimeout())
  383. // SetLight(LightType.BUZZER1, LightState.Blink);
  384. //
  385. //MonitorLight(_red);
  386. //MonitorLight(_blue);
  387. //MonitorLight(_yellow);
  388. //MonitorLight(_green);
  389. //MonitorLight(_white);
  390. //MonitorLight(_buzzer1);
  391. //MonitorLight(_buzzer2);
  392. }
  393. public void Reset()
  394. {
  395. ResetLight(_red);
  396. ResetLight(_blue);
  397. ResetLight(_yellow);
  398. ResetLight(_green);
  399. ResetLight(_white);
  400. ResetLight(_buzzer1);
  401. ResetLight(_buzzer2);
  402. //_buzzerAndRed = false;
  403. //_buzzerSwitchOff = false;
  404. _buzzer1SwitchOff = false;
  405. _buzzer2SwitchOff = false;
  406. }
  407. public bool SwitchOffBuzzer(string cmd, object[] objs)
  408. {
  409. if (cmd == $"System.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)
  410. {
  411. //_buzzerSwitchOff = true;
  412. //_buzzer1SwitchOff = true;
  413. _buzzer2SwitchOff = true;
  414. }
  415. return true;
  416. }
  417. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  418. {
  419. if (light != null)
  420. {
  421. light.StateSetPoint = state[light];
  422. }
  423. }
  424. private void ResetLight(IoSignalLight light)
  425. {
  426. if (light != null)
  427. {
  428. light.Reset();
  429. }
  430. }
  431. private void MonitorLight(IoSignalLight light)
  432. {
  433. if (light != null)
  434. {
  435. light.Monitor();
  436. }
  437. }
  438. public void Terminate()
  439. {
  440. }
  441. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  442. {
  443. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  444. return TowerLightStatus.Blinking;
  445. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  446. return TowerLightStatus.On;
  447. return TowerLightStatus.Off;
  448. }
  449. private TowerLightStatus ParseLight(string light)
  450. {
  451. if (string.IsNullOrEmpty(light))
  452. return TowerLightStatus.Off;
  453. TowerLightStatus result = TowerLightStatus.Unknown;
  454. light = light.Trim().ToLower();
  455. switch (light)
  456. {
  457. case "on":
  458. result = TowerLightStatus.On;
  459. break;
  460. case "off":
  461. result = TowerLightStatus.Off;
  462. break;
  463. case "blinking":
  464. result = TowerLightStatus.Blinking;
  465. break;
  466. default:
  467. LOG.Write(eEvent.ERR_SIGNAL_TOWER_INVALID_CFG, ModuleName.System, light);
  468. break;
  469. }
  470. return result;
  471. }
  472. }
  473. }