IoSignalTower.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. using Aitex.Core.Common.DeviceData;
  10. using Aitex.Core.RT.DataCenter;
  11. using Aitex.Core.RT.Device;
  12. using Aitex.Core.RT.Device.Unit;
  13. using Aitex.Core.RT.Event;
  14. using Aitex.Core.RT.IOCore;
  15. using Aitex.Core.RT.Log;
  16. using Aitex.Core.RT.OperationCenter;
  17. using Aitex.Core.Util;
  18. using MECF.Framework.Common.Device.Bases;
  19. namespace Aitex.Core.RT.Device.Unit
  20. {
  21. public enum LightType
  22. {
  23. Red,
  24. Yellow,
  25. Green,
  26. White,
  27. Blue,
  28. Buzzer,
  29. Buzzer1,
  30. Buzzer2,
  31. Buzzer3,
  32. Buzzer4,
  33. Buzzer5,
  34. }
  35. public enum LightState
  36. {
  37. Off,
  38. On,
  39. Blink,
  40. }
  41. public class STEvents
  42. {
  43. [XmlElement(ElementName = "STEvent")]
  44. public List<STEvent> Events;
  45. }
  46. public class STEvent
  47. {
  48. [XmlAttribute(AttributeName = "name")]
  49. public string Name { get; set; }
  50. [XmlAttribute]
  51. public string Red { get; set; }
  52. [XmlAttribute]
  53. public string Yellow { get; set; }
  54. [XmlAttribute]
  55. public string Green { get; set; }
  56. [XmlAttribute]
  57. public string Blue { get; set; }
  58. [XmlAttribute]
  59. public string Buzzer { get; set; }
  60. [XmlAttribute]
  61. public string White { get; set; }
  62. [XmlAttribute]
  63. public string Buzzer1 { get; set; }
  64. [XmlAttribute]
  65. public string Buzzer2 { get; set; }
  66. [XmlAttribute]
  67. public string Buzzer3 { get; set; }
  68. [XmlAttribute]
  69. public string Buzzer4 { get; set; }
  70. [XmlAttribute]
  71. public string Buzzer5 { get; set; }
  72. }
  73. public class IoSignalTower : BaseDevice, IDevice
  74. {
  75. //device
  76. private IoSignalLight _red = null;
  77. private IoSignalLight _yellow = null;
  78. private IoSignalLight _green = null;
  79. private IoSignalLight _blue = null;
  80. private IoSignalLight _white = null;
  81. private IoSignalLight _buzzer = null;
  82. private IoSignalLight _buzzer1 = null;
  83. private IoSignalLight _buzzer2 = null;
  84. private IoSignalLight _buzzer3 = null;
  85. private IoSignalLight _buzzer4 = null;
  86. private IoSignalLight _buzzer5 = null;
  87. public bool HostControl { get; set; }
  88. private bool _buzzerSwitchOff = false;
  89. private bool _buzzer1SwitchOff = false;
  90. private bool _buzzer2SwitchOff = false;
  91. private bool _buzzer3SwitchOff = false;
  92. private bool _buzzer4SwitchOff = false;
  93. private bool _buzzer5SwitchOff = false;
  94. public AITSignalTowerData DeviceData
  95. {
  96. get
  97. {
  98. AITSignalTowerData data = new AITSignalTowerData()
  99. {
  100. DeviceName = Name,
  101. DeviceSchematicId = DeviceID,
  102. DisplayName = Display,
  103. IsGreenLightOn = _green != null && _green.Value,
  104. IsRedLightOn = _red != null && _red.Value,
  105. IsYellowLightOn = _yellow != null && _yellow.Value,
  106. IsWhiteLightOn = _white != null && _white.Value,
  107. IsBlueLightOn = _blue != null && _blue.Value,
  108. IsBuzzerOn = _buzzer != null && _buzzer.Value,
  109. IsBuzzer1On = _buzzer1 != null && _buzzer1.Value,
  110. IsBuzzer2On = _buzzer2 != null && _buzzer2.Value,
  111. IsBuzzer3On = _buzzer3 != null && _buzzer3.Value,
  112. IsBuzzer4On = _buzzer4 != null && _buzzer4.Value,
  113. IsBuzzer5On = _buzzer5 != null && _buzzer5.Value,
  114. };
  115. return data;
  116. }
  117. }
  118. private object _locker = new object();
  119. private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =
  120. new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();
  121. private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();
  122. public IoSignalTower(string module, XmlElement node, string ioModule = "")
  123. {
  124. base.Module = node.GetAttribute("module");
  125. base.Name = node.GetAttribute("id");
  126. base.Display = node.GetAttribute("display");
  127. base.DeviceID = node.GetAttribute("schematicId");
  128. DOAccessor doRed = ParseDoNode("doRed", node, ioModule);
  129. if (doRed != null)
  130. {
  131. _red = new IoSignalLight(module, "SignalLightRed", "Red Light", "SignalLightRed", doRed);
  132. }
  133. DOAccessor doYellow = ParseDoNode("doYellow", node, ioModule);
  134. if (doYellow != null)
  135. {
  136. _yellow = new IoSignalLight(module, "SignalLightYellow", "Yellow Light", "SignalLightYellow", doYellow);
  137. }
  138. DOAccessor doGreen = ParseDoNode("doGreen", node, ioModule);
  139. if (doGreen != null)
  140. {
  141. _green = new IoSignalLight(module, "SignalLightGreen", "Green Light", "SignalLightGreen", doGreen);
  142. }
  143. DOAccessor doBlue = ParseDoNode("doBlue", node, ioModule);
  144. if (doBlue != null)
  145. {
  146. _blue = new IoSignalLight(module, "SignalLightBlue", "Blue Light", "SignalLightBlue", doBlue);
  147. }
  148. DOAccessor doWhite = ParseDoNode("doWhite", node, ioModule);
  149. if (doWhite != null)
  150. {
  151. _white = new IoSignalLight(module, "SignalLightWhite", "White Light", "SignalLightWhite", doWhite);
  152. }
  153. DOAccessor doBuzzer1 = ParseDoNode("doBuzzer1", node, ioModule);
  154. if (doBuzzer1 != null)
  155. {
  156. _buzzer1 = new IoSignalLight(module, "SignalLightBuzzer1", "Buzzer1 Light", "SignalLightBuzzer1", doBuzzer1);
  157. }
  158. DOAccessor doBuzzer2 = ParseDoNode("doBuzzer2", node, ioModule);
  159. if (doBuzzer2 != null)
  160. {
  161. _buzzer2 = new IoSignalLight(module, "SignalLightBuzzer2", "Buzzer2 Light", "SignalLightBuzzer2", doBuzzer2);
  162. }
  163. DOAccessor doBuzzer3 = ParseDoNode("doBuzzer3", node, ioModule);
  164. if (doBuzzer3 != null)
  165. {
  166. _buzzer3 = new IoSignalLight(module, "SignalLightBuzzer3", "Buzzer3 Light", "SignalLightBuzzer3", doBuzzer3);
  167. }
  168. DOAccessor doBuzzer4 = ParseDoNode("doBuzzer4", node, ioModule);
  169. if (doBuzzer4 != null)
  170. {
  171. _buzzer4 = new IoSignalLight(module, "SignalLightBuzzer4", "Buzzer4 Light", "SignalLightBuzzer4", doBuzzer4);
  172. }
  173. DOAccessor doBuzzer5 = ParseDoNode("doBuzzer5", node, ioModule);
  174. if (doBuzzer5 != null)
  175. {
  176. _buzzer5 = new IoSignalLight(module, "SignalLightBuzzer5", "Buzzer5 Light", "SignalLightBuzzer5", doBuzzer5);
  177. }
  178. IoSignalLight[] buzzers = new[] {_buzzer1, _buzzer2, _buzzer3, _buzzer4, _buzzer5};
  179. _buzzer = Array.Find(buzzers, x => x != null);
  180. }
  181. public bool Initialize()
  182. {
  183. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  184. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer1}", SwitchOffBuzzer);
  185. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer2}", SwitchOffBuzzer);
  186. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer3}", SwitchOffBuzzer);
  187. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer4}", SwitchOffBuzzer);
  188. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  189. return true;
  190. }
  191. public void SetLight(LightType lightType, LightState state, int blinkInterval = 500)
  192. {
  193. IoSignalLight light = null;
  194. switch (lightType)
  195. {
  196. case LightType.Red:
  197. light = _red;
  198. break;
  199. case LightType.Green:
  200. light = _green;
  201. break;
  202. case LightType.Yellow:
  203. light = _yellow;
  204. break;
  205. case LightType.White:
  206. light = _white;
  207. break;
  208. case LightType.Blue:
  209. light = _blue;
  210. break;
  211. case LightType.Buzzer:
  212. light = _buzzer;
  213. break;
  214. case LightType.Buzzer1:
  215. light = _buzzer1;
  216. break;
  217. case LightType.Buzzer2:
  218. light = _buzzer2;
  219. break;
  220. case LightType.Buzzer3:
  221. light = _buzzer3;
  222. break;
  223. case LightType.Buzzer4:
  224. light = _buzzer4;
  225. break;
  226. case LightType.Buzzer5:
  227. light = _buzzer5;
  228. break;
  229. }
  230. if (light == null)
  231. {
  232. LOG.Write($"Undefined light {lightType}");
  233. return;
  234. }
  235. switch (state)
  236. {
  237. case LightState.On:
  238. _cmdSetPoint[light] = TowerLightStatus.On;
  239. light.StateSetPoint = TowerLightStatus.On;
  240. break;
  241. case LightState.Off:
  242. _cmdSetPoint[light] = TowerLightStatus.Off;
  243. light.StateSetPoint = TowerLightStatus.Off;
  244. break;
  245. case LightState.Blink:
  246. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  247. light.Interval = blinkInterval;
  248. light.StateSetPoint = TowerLightStatus.Blinking;
  249. break;
  250. }
  251. }
  252. public bool CustomSignalTower(string configPathFile)
  253. {
  254. try
  255. {
  256. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  257. lock (_locker)
  258. {
  259. foreach (STEvent e in config.Events)
  260. {
  261. if (!_config.ContainsKey(e.Name))
  262. {
  263. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  264. }
  265. if (_red != null)
  266. _config[e.Name][_red] = ParseLight(e.Red);
  267. if (_yellow != null)
  268. _config[e.Name][_yellow] = ParseLight(e.Yellow);
  269. if (_green != null)
  270. _config[e.Name][_green] = ParseLight(e.Green);
  271. if (_blue != null)
  272. _config[e.Name][_blue] = ParseLight(e.Blue);
  273. if (_white != null)
  274. _config[e.Name][_white] = ParseLight(e.White);
  275. if (_buzzer != null)
  276. _config[e.Name][_buzzer] = ParseLight(e.Buzzer);
  277. if (_buzzer1 != null)
  278. _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1);
  279. if (_buzzer2 != null)
  280. _config[e.Name][_buzzer2] = ParseLight(e.Buzzer2);
  281. if (_buzzer3 != null)
  282. _config[e.Name][_buzzer3] = ParseLight(e.Buzzer3);
  283. if (_buzzer4 != null)
  284. _config[e.Name][_buzzer4] = ParseLight(e.Buzzer4);
  285. if (_buzzer5 != null)
  286. _config[e.Name][_buzzer5] = ParseLight(e.Buzzer5);
  287. }
  288. }
  289. }
  290. catch (Exception e)
  291. {
  292. EV.PostWarningLog(Module, $"Signal tower config file is invalid, {e.Message}");
  293. return false;
  294. }
  295. return true;
  296. }
  297. public void Monitor()
  298. {
  299. Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  300. if (_red != null)
  301. dicState[_red] = TowerLightStatus.Off;
  302. if (_yellow != null)
  303. dicState[_yellow] = TowerLightStatus.Off;
  304. if (_green != null)
  305. dicState[_green] = TowerLightStatus.Off;
  306. if (_blue != null)
  307. dicState[_blue] = TowerLightStatus.Off;
  308. if (_white != null)
  309. dicState[_white] = TowerLightStatus.Off;
  310. if (_buzzer != null)
  311. dicState[_buzzer] = TowerLightStatus.Off;
  312. if (_buzzer1 != null)
  313. dicState[_buzzer1] = TowerLightStatus.Off;
  314. if (_buzzer2 != null)
  315. dicState[_buzzer2] = TowerLightStatus.Off;
  316. if (_buzzer3 != null)
  317. dicState[_buzzer3] = TowerLightStatus.Off;
  318. if (_buzzer4 != null)
  319. dicState[_buzzer4] = TowerLightStatus.Off;
  320. if (_buzzer5 != null)
  321. dicState[_buzzer5] = TowerLightStatus.Off;
  322. foreach (var trigCondition in _config)
  323. {
  324. var conditionValue = DATA.Poll(trigCondition.Key);
  325. if (conditionValue == null)
  326. continue;
  327. bool isTrig = (bool)conditionValue;
  328. if (isTrig)
  329. {
  330. if (_red != null)
  331. {
  332. dicState[_red] = MergeCondition(dicState[_red], trigCondition.Value[_red]);
  333. }
  334. if (_yellow != null)
  335. {
  336. dicState[_yellow] = MergeCondition(dicState[_yellow], trigCondition.Value[_yellow]);
  337. }
  338. if (_green != null)
  339. {
  340. dicState[_green] = MergeCondition(dicState[_green], trigCondition.Value[_green]);
  341. }
  342. if (_white != null)
  343. {
  344. dicState[_white] = MergeCondition(dicState[_white], trigCondition.Value[_white]);
  345. }
  346. if (_blue != null)
  347. {
  348. dicState[_blue] = MergeCondition(dicState[_blue], trigCondition.Value[_blue]);
  349. }
  350. if (_buzzer != null)
  351. {
  352. dicState[_buzzer] = _buzzerSwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer], trigCondition.Value[_buzzer]);
  353. }
  354. if (_buzzer1 != null)
  355. {
  356. dicState[_buzzer1] = _buzzer1SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer1], trigCondition.Value[_buzzer1]);
  357. }
  358. if (_buzzer2 != null)
  359. {
  360. dicState[_buzzer2] = _buzzer2SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer2], trigCondition.Value[_buzzer2]);
  361. }
  362. if (_buzzer3 != null)
  363. {
  364. dicState[_buzzer3] = _buzzer3SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer3], trigCondition.Value[_buzzer3]);
  365. }
  366. if (_buzzer4 != null)
  367. {
  368. dicState[_buzzer4] = _buzzer4SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer4], trigCondition.Value[_buzzer4]);
  369. }
  370. if (_buzzer5 != null)
  371. {
  372. dicState[_buzzer5] = _buzzer5SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer5], trigCondition.Value[_buzzer5]);
  373. }
  374. }
  375. }
  376. if (_config.Count > 0 && !HostControl)
  377. {
  378. SetLight(_red, dicState);
  379. SetLight(_blue, dicState);
  380. SetLight(_yellow, dicState);
  381. SetLight(_green, dicState);
  382. SetLight(_white, dicState);
  383. SetLight(_buzzer, dicState);
  384. SetLight(_buzzer1, dicState);
  385. SetLight(_buzzer2, dicState);
  386. SetLight(_buzzer3, dicState);
  387. SetLight(_buzzer4, dicState);
  388. SetLight(_buzzer5, dicState);
  389. }
  390. MonitorLight(_red);
  391. MonitorLight(_blue);
  392. MonitorLight(_yellow);
  393. MonitorLight(_green);
  394. MonitorLight(_white);
  395. MonitorLight(_buzzer);
  396. MonitorLight(_buzzer1);
  397. MonitorLight(_buzzer2);
  398. MonitorLight(_buzzer3);
  399. MonitorLight(_buzzer4);
  400. MonitorLight(_buzzer5);
  401. }
  402. public void Reset()
  403. {
  404. ResetLight(_red );
  405. ResetLight(_blue );
  406. ResetLight(_yellow );
  407. ResetLight(_green );
  408. ResetLight(_white);
  409. ResetLight(_buzzer );
  410. _buzzerSwitchOff = false;
  411. _buzzer1SwitchOff = false;
  412. _buzzer2SwitchOff = false;
  413. _buzzer3SwitchOff = false;
  414. _buzzer4SwitchOff = false;
  415. _buzzer5SwitchOff = false;
  416. }
  417. public bool SwitchOffBuzzer(string cmd, object[] objs)
  418. {
  419. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer != null && _buzzer.StateSetPoint != TowerLightStatus.Off)
  420. {
  421. _buzzerSwitchOff = true;
  422. }
  423. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer1}"&&_buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)
  424. {
  425. _buzzer1SwitchOff = true;
  426. }
  427. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer2}" && _buzzer2 != null && _buzzer2.StateSetPoint != TowerLightStatus.Off)
  428. {
  429. _buzzer2SwitchOff = true;
  430. }
  431. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer3}" && _buzzer3 != null && _buzzer3.StateSetPoint != TowerLightStatus.Off)
  432. {
  433. _buzzer3SwitchOff = true;
  434. }
  435. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer4}" && _buzzer4 != null && _buzzer4.StateSetPoint != TowerLightStatus.Off)
  436. {
  437. _buzzer4SwitchOff = true;
  438. }
  439. return true;
  440. }
  441. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  442. {
  443. if (light != null)
  444. {
  445. light.StateSetPoint = state[light];
  446. }
  447. }
  448. private void ResetLight(IoSignalLight light )
  449. {
  450. if (light != null)
  451. {
  452. light.Reset();
  453. }
  454. }
  455. private void MonitorLight(IoSignalLight light)
  456. {
  457. if (light != null)
  458. {
  459. light.Monitor();
  460. }
  461. }
  462. public void Terminate()
  463. {
  464. }
  465. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  466. {
  467. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  468. return TowerLightStatus.Blinking;
  469. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  470. return TowerLightStatus.On;
  471. return TowerLightStatus.Off;
  472. }
  473. private TowerLightStatus ParseLight(string light)
  474. {
  475. if (string.IsNullOrEmpty(light))
  476. return TowerLightStatus.Off;
  477. TowerLightStatus result = TowerLightStatus.Unknown;
  478. light = light.Trim().ToLower();
  479. switch (light)
  480. {
  481. case "on":
  482. result = TowerLightStatus.On;
  483. break;
  484. case "off":
  485. result = TowerLightStatus.Off;
  486. break;
  487. case "blinking":
  488. result = TowerLightStatus.Blinking;
  489. break;
  490. default:
  491. LOG.Write("signal tower config file has invalid set, " + light);
  492. break;
  493. }
  494. return result;
  495. }
  496. }
  497. }