IoSignalTower.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 class IoSignalTower : BaseDevice, IDevice
  22. {
  23. //device
  24. private IoSignalLight _red = null;
  25. private IoSignalLight _yellow = null;
  26. private IoSignalLight _green = null;
  27. private IoSignalLight _blue = null;
  28. private IoSignalLight _white = null;
  29. private IoSignalLight _buzzer = null;
  30. private IoSignalLight _buzzer1 = null;
  31. private IoSignalLight _buzzer2 = null;
  32. private IoSignalLight _buzzer3 = null;
  33. private IoSignalLight _buzzer4 = null;
  34. private IoSignalLight _buzzer5 = null;
  35. public bool HostControl { get; set; }
  36. private bool _buzzerSwitchOff = false;
  37. private bool _buzzer1SwitchOff = false;
  38. private bool _buzzer2SwitchOff = false;
  39. private bool _buzzer3SwitchOff = false;
  40. private bool _buzzer4SwitchOff = false;
  41. private bool _buzzer5SwitchOff = false;
  42. public bool BuzzerSwithcOff => _buzzerSwitchOff;
  43. public virtual AITSignalTowerData DeviceData
  44. {
  45. get
  46. {
  47. AITSignalTowerData data = new AITSignalTowerData()
  48. {
  49. DeviceName = Name,
  50. DeviceSchematicId = DeviceID,
  51. DisplayName = Display,
  52. IsGreenLightOn = _green != null && _green.Value,
  53. IsRedLightOn = _red != null && _red.Value,
  54. IsYellowLightOn = _yellow != null && _yellow.Value,
  55. IsWhiteLightOn = _white != null && _white.Value,
  56. IsBlueLightOn = _blue != null && _blue.Value,
  57. IsBuzzerOn = _buzzer != null && _buzzer.Value,
  58. IsBuzzer1On = _buzzer1 != null && _buzzer1.Value,
  59. IsBuzzer2On = _buzzer2 != null && _buzzer2.Value,
  60. IsBuzzer3On = _buzzer3 != null && _buzzer3.Value,
  61. IsBuzzer4On = _buzzer4 != null && _buzzer4.Value,
  62. IsBuzzer5On = _buzzer5 != null && _buzzer5.Value,
  63. };
  64. return data;
  65. }
  66. }
  67. public Dictionary<IoSignalLight, TowerLightStatus> SignalTowerState
  68. {
  69. get { return dicState; }
  70. }
  71. public IoSignalLight Red
  72. {
  73. get { return _red; }
  74. }
  75. public IoSignalLight Yellow
  76. {
  77. get { return _yellow; }
  78. }
  79. public IoSignalLight Green
  80. {
  81. get { return _green; }
  82. }
  83. public IoSignalLight Blue
  84. {
  85. get { return _blue; }
  86. }
  87. public IoSignalLight White
  88. {
  89. get { return _white; }
  90. }
  91. public IoSignalLight Buzzer1
  92. {
  93. get { return _buzzer1; }
  94. }
  95. public IoSignalLight Buzzer2
  96. {
  97. get { return _buzzer2; }
  98. }
  99. public IoSignalLight Buzzer3
  100. {
  101. get { return _buzzer3; }
  102. }
  103. public IoSignalLight Buzzer4
  104. {
  105. get { return _buzzer4; }
  106. }
  107. public IoSignalLight Buzzer5
  108. {
  109. get { return _buzzer5; }
  110. }
  111. private object _locker = new object();
  112. private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =
  113. new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();
  114. private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();
  115. private Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  116. public IoSignalTower(string module, XmlElement node, string ioModule = "")
  117. {
  118. if (node == null)
  119. return;
  120. base.Module = node.GetAttribute("module");
  121. base.Name = node.GetAttribute("id");
  122. base.Display = node.GetAttribute("display");
  123. base.DeviceID = node.GetAttribute("schematicId");
  124. DOAccessor doRed = ParseDoNode("doRed", node, ioModule);
  125. if (doRed != null)
  126. {
  127. _red = new IoSignalLight(module, "SignalLightRed", "Red Light", "SignalLightRed", doRed);
  128. }
  129. DOAccessor doYellow = ParseDoNode("doYellow", node, ioModule);
  130. if (doYellow != null)
  131. {
  132. _yellow = new IoSignalLight(module, "SignalLightYellow", "Yellow Light", "SignalLightYellow", doYellow);
  133. }
  134. DOAccessor doGreen = ParseDoNode("doGreen", node, ioModule);
  135. if (doGreen != null)
  136. {
  137. _green = new IoSignalLight(module, "SignalLightGreen", "Green Light", "SignalLightGreen", doGreen);
  138. }
  139. DOAccessor doBlue = ParseDoNode("doBlue", node, ioModule);
  140. if (doBlue != null)
  141. {
  142. _blue = new IoSignalLight(module, "SignalLightBlue", "Blue Light", "SignalLightBlue", doBlue);
  143. }
  144. DOAccessor doWhite = ParseDoNode("doWhite", node, ioModule);
  145. if (doWhite != null)
  146. {
  147. _white = new IoSignalLight(module, "SignalLightWhite", "White Light", "SignalLightWhite", doWhite);
  148. }
  149. DOAccessor doBuzzer1 = ParseDoNode("doBuzzer1", node, ioModule);
  150. if (doBuzzer1 != null)
  151. {
  152. _buzzer1 = new IoSignalLight(module, "SignalLightBuzzer1", "Buzzer1 Light", "SignalLightBuzzer1", doBuzzer1);
  153. }
  154. DOAccessor doBuzzer2 = ParseDoNode("doBuzzer2", node, ioModule);
  155. if (doBuzzer2 != null)
  156. {
  157. _buzzer2 = new IoSignalLight(module, "SignalLightBuzzer2", "Buzzer2 Light", "SignalLightBuzzer2", doBuzzer2);
  158. }
  159. DOAccessor doBuzzer3 = ParseDoNode("doBuzzer3", node, ioModule);
  160. if (doBuzzer3 != null)
  161. {
  162. _buzzer3 = new IoSignalLight(module, "SignalLightBuzzer3", "Buzzer3 Light", "SignalLightBuzzer3", doBuzzer3);
  163. }
  164. DOAccessor doBuzzer4 = ParseDoNode("doBuzzer4", node, ioModule);
  165. if (doBuzzer4 != null)
  166. {
  167. _buzzer4 = new IoSignalLight(module, "SignalLightBuzzer4", "Buzzer4 Light", "SignalLightBuzzer4", doBuzzer4);
  168. }
  169. DOAccessor doBuzzer5 = ParseDoNode("doBuzzer5", node, ioModule);
  170. if (doBuzzer5 != null)
  171. {
  172. _buzzer5 = new IoSignalLight(module, "SignalLightBuzzer5", "Buzzer5 Light", "SignalLightBuzzer5", doBuzzer5);
  173. }
  174. IoSignalLight[] buzzers = new[] {_buzzer1, _buzzer2, _buzzer3, _buzzer4, _buzzer5};
  175. _buzzer = Array.Find(buzzers, x => x != null);
  176. }
  177. public virtual bool Initialize()
  178. {
  179. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  180. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer1}", SwitchOffBuzzer);
  181. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer2}", SwitchOffBuzzer);
  182. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer3}", SwitchOffBuzzer);
  183. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer4}", SwitchOffBuzzer);
  184. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  185. return true;
  186. }
  187. public void SetLight(LightType lightType, LightState state, int blinkInterval = 500)
  188. {
  189. IoSignalLight light = null;
  190. switch (lightType)
  191. {
  192. case LightType.Red:
  193. light = _red;
  194. break;
  195. case LightType.Green:
  196. light = _green;
  197. break;
  198. case LightType.Yellow:
  199. light = _yellow;
  200. break;
  201. case LightType.White:
  202. light = _white;
  203. break;
  204. case LightType.Blue:
  205. light = _blue;
  206. break;
  207. case LightType.Buzzer:
  208. light = _buzzer;
  209. break;
  210. case LightType.Buzzer1:
  211. light = _buzzer1;
  212. break;
  213. case LightType.Buzzer2:
  214. light = _buzzer2;
  215. break;
  216. case LightType.Buzzer3:
  217. light = _buzzer3;
  218. break;
  219. case LightType.Buzzer4:
  220. light = _buzzer4;
  221. break;
  222. case LightType.Buzzer5:
  223. light = _buzzer5;
  224. break;
  225. }
  226. if (light == null)
  227. {
  228. LOG.Write($"Undefined light {lightType}");
  229. return;
  230. }
  231. switch (state)
  232. {
  233. case LightState.On:
  234. _cmdSetPoint[light] = TowerLightStatus.On;
  235. light.StateSetPoint = TowerLightStatus.On;
  236. break;
  237. case LightState.Off:
  238. _cmdSetPoint[light] = TowerLightStatus.Off;
  239. light.StateSetPoint = TowerLightStatus.Off;
  240. break;
  241. case LightState.Blink:
  242. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  243. light.Interval = blinkInterval;
  244. light.StateSetPoint = TowerLightStatus.Blinking;
  245. break;
  246. }
  247. }
  248. public virtual bool CustomSignalTower(string configPathFile)
  249. {
  250. try
  251. {
  252. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  253. lock (_locker)
  254. {
  255. foreach (STEvent e in config.Events)
  256. {
  257. if (!_config.ContainsKey(e.Name))
  258. {
  259. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  260. }
  261. if (_red != null)
  262. _config[e.Name][_red] = ParseLight(e.Red);
  263. if (_yellow != null)
  264. _config[e.Name][_yellow] = ParseLight(e.Yellow);
  265. if (_green != null)
  266. _config[e.Name][_green] = ParseLight(e.Green);
  267. if (_blue != null)
  268. _config[e.Name][_blue] = ParseLight(e.Blue);
  269. if (_white != null)
  270. _config[e.Name][_white] = ParseLight(e.White);
  271. if (_buzzer != null)
  272. _config[e.Name][_buzzer] = ParseLight(e.Buzzer);
  273. if (_buzzer1 != null)
  274. _config[e.Name][_buzzer1] = ParseLight(e.Buzzer1);
  275. if (_buzzer2 != null)
  276. _config[e.Name][_buzzer2] = ParseLight(e.Buzzer2);
  277. if (_buzzer3 != null)
  278. _config[e.Name][_buzzer3] = ParseLight(e.Buzzer3);
  279. if (_buzzer4 != null)
  280. _config[e.Name][_buzzer4] = ParseLight(e.Buzzer4);
  281. if (_buzzer5 != null)
  282. _config[e.Name][_buzzer5] = ParseLight(e.Buzzer5);
  283. }
  284. }
  285. }
  286. catch (Exception e)
  287. {
  288. EV.PostWarningLog(Module, $"Signal tower config file is invalid, {e.Message}");
  289. return false;
  290. }
  291. return true;
  292. }
  293. public virtual void Monitor()
  294. {
  295. //Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  296. if (_red != null)
  297. dicState[_red] = TowerLightStatus.Off;
  298. if (_yellow != null)
  299. dicState[_yellow] = TowerLightStatus.Off;
  300. if (_green != null)
  301. dicState[_green] = TowerLightStatus.Off;
  302. if (_blue != null)
  303. dicState[_blue] = TowerLightStatus.Off;
  304. if (_white != null)
  305. dicState[_white] = TowerLightStatus.Off;
  306. if (_buzzer != null)
  307. dicState[_buzzer] = TowerLightStatus.Off;
  308. if (_buzzer1 != null)
  309. dicState[_buzzer1] = TowerLightStatus.Off;
  310. if (_buzzer2 != null)
  311. dicState[_buzzer2] = TowerLightStatus.Off;
  312. if (_buzzer3 != null)
  313. dicState[_buzzer3] = TowerLightStatus.Off;
  314. if (_buzzer4 != null)
  315. dicState[_buzzer4] = TowerLightStatus.Off;
  316. if (_buzzer5 != null)
  317. dicState[_buzzer5] = TowerLightStatus.Off;
  318. foreach (var trigCondition in _config)
  319. {
  320. var conditionValue = DATA.Poll(trigCondition.Key);
  321. if (conditionValue == null)
  322. continue;
  323. bool isTrig = (bool)conditionValue;
  324. if (isTrig)
  325. {
  326. if (_red != null)
  327. {
  328. dicState[_red] = MergeCondition(dicState[_red], trigCondition.Value[_red]);
  329. }
  330. if (_yellow != null)
  331. {
  332. dicState[_yellow] = MergeCondition(dicState[_yellow], trigCondition.Value[_yellow]);
  333. }
  334. if (_green != null)
  335. {
  336. dicState[_green] = MergeCondition(dicState[_green], trigCondition.Value[_green]);
  337. }
  338. if (_white != null)
  339. {
  340. dicState[_white] = MergeCondition(dicState[_white], trigCondition.Value[_white]);
  341. }
  342. if (_blue != null)
  343. {
  344. dicState[_blue] = MergeCondition(dicState[_blue], trigCondition.Value[_blue]);
  345. }
  346. if (_buzzer != null)
  347. {
  348. dicState[_buzzer] = _buzzerSwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer], trigCondition.Value[_buzzer]);
  349. }
  350. if (_buzzer1 != null)
  351. {
  352. dicState[_buzzer1] = _buzzer1SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer1], trigCondition.Value[_buzzer1]);
  353. }
  354. if (_buzzer2 != null)
  355. {
  356. dicState[_buzzer2] = _buzzer2SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer2], trigCondition.Value[_buzzer2]);
  357. }
  358. if (_buzzer3 != null)
  359. {
  360. dicState[_buzzer3] = _buzzer3SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer3], trigCondition.Value[_buzzer3]);
  361. }
  362. if (_buzzer4 != null)
  363. {
  364. dicState[_buzzer4] = _buzzer4SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer4], trigCondition.Value[_buzzer4]);
  365. }
  366. if (_buzzer5 != null)
  367. {
  368. dicState[_buzzer5] = _buzzer5SwitchOff ? TowerLightStatus.Off : MergeCondition(dicState[_buzzer5], trigCondition.Value[_buzzer5]);
  369. }
  370. }
  371. }
  372. if (_config.Count > 0 && !HostControl)
  373. {
  374. SetLight(_red, dicState);
  375. SetLight(_blue, dicState);
  376. SetLight(_yellow, dicState);
  377. SetLight(_green, dicState);
  378. SetLight(_white, dicState);
  379. SetLight(_buzzer, dicState);
  380. SetLight(_buzzer1, dicState);
  381. SetLight(_buzzer2, dicState);
  382. SetLight(_buzzer3, dicState);
  383. SetLight(_buzzer4, dicState);
  384. SetLight(_buzzer5, dicState);
  385. }
  386. MonitorLight(_red);
  387. MonitorLight(_blue);
  388. MonitorLight(_yellow);
  389. MonitorLight(_green);
  390. MonitorLight(_white);
  391. MonitorLight(_buzzer);
  392. MonitorLight(_buzzer1);
  393. MonitorLight(_buzzer2);
  394. MonitorLight(_buzzer3);
  395. MonitorLight(_buzzer4);
  396. MonitorLight(_buzzer5);
  397. }
  398. public virtual void Reset()
  399. {
  400. ResetLight(_red );
  401. ResetLight(_blue );
  402. ResetLight(_yellow );
  403. ResetLight(_green );
  404. ResetLight(_white);
  405. ResetLight(_buzzer );
  406. _buzzerSwitchOff = false;
  407. _buzzer1SwitchOff = false;
  408. _buzzer2SwitchOff = false;
  409. _buzzer3SwitchOff = false;
  410. _buzzer4SwitchOff = false;
  411. _buzzer5SwitchOff = false;
  412. }
  413. public virtual bool SwitchOffBuzzer(string cmd, object[] objs)
  414. {
  415. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer}" && _buzzer != null && _buzzer.StateSetPoint != TowerLightStatus.Off)
  416. {
  417. _buzzerSwitchOff = true;
  418. _buzzer1SwitchOff = true;
  419. }
  420. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer1}"&&_buzzer1 != null && _buzzer1.StateSetPoint != TowerLightStatus.Off)
  421. {
  422. _buzzer1SwitchOff = true;
  423. }
  424. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer2}" && _buzzer2 != null && _buzzer2.StateSetPoint != TowerLightStatus.Off)
  425. {
  426. _buzzer2SwitchOff = true;
  427. }
  428. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer3}" && _buzzer3 != null && _buzzer3.StateSetPoint != TowerLightStatus.Off)
  429. {
  430. _buzzer3SwitchOff = true;
  431. }
  432. if (cmd == $"{ Module}.{ Name}.{ AITSignalTowerOperation.SwitchOffBuzzer4}" && _buzzer4 != null && _buzzer4.StateSetPoint != TowerLightStatus.Off)
  433. {
  434. _buzzer4SwitchOff = true;
  435. }
  436. return true;
  437. }
  438. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  439. {
  440. if (light != null)
  441. {
  442. light.StateSetPoint = state[light];
  443. }
  444. }
  445. private void ResetLight(IoSignalLight light )
  446. {
  447. if (light != null)
  448. {
  449. light.Reset();
  450. }
  451. }
  452. private void MonitorLight(IoSignalLight light)
  453. {
  454. if (light != null)
  455. {
  456. light.Monitor();
  457. }
  458. }
  459. public void Terminate()
  460. {
  461. }
  462. protected TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  463. {
  464. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  465. return TowerLightStatus.Blinking;
  466. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  467. return TowerLightStatus.On;
  468. return TowerLightStatus.Off;
  469. }
  470. protected TowerLightStatus ParseLight(string light)
  471. {
  472. if (string.IsNullOrEmpty(light))
  473. return TowerLightStatus.Off;
  474. TowerLightStatus result = TowerLightStatus.Unknown;
  475. light = light.Trim().ToLower();
  476. switch (light)
  477. {
  478. case "on":
  479. result = TowerLightStatus.On;
  480. break;
  481. case "off":
  482. result = TowerLightStatus.Off;
  483. break;
  484. case "blinking":
  485. result = TowerLightStatus.Blinking;
  486. break;
  487. default:
  488. LOG.Write("signal tower config file has invalid set, " + light);
  489. break;
  490. }
  491. return result;
  492. }
  493. }
  494. public class IoLoadPortSignal : BaseDevice, IDevice
  495. {
  496. //device
  497. private IoSignalLight _lightCassette1 = null;
  498. private IoSignalLight _lightCassette2 = null;
  499. private IoSignalLight _lightCassette3 = null;
  500. private IoSignalLight _lightCassette4 = null;
  501. private IoSignalLight _lightCassettePlacement = null;
  502. private IoSignalLight _lightAlarm = null;
  503. public bool HostControl { get; set; }
  504. public virtual AITSignalTowerData DeviceData
  505. {
  506. get
  507. {
  508. AITSignalTowerData data = new AITSignalTowerData()
  509. {
  510. DeviceName = Name,
  511. DeviceSchematicId = DeviceID,
  512. DisplayName = Display,
  513. };
  514. return data;
  515. }
  516. }
  517. public Dictionary<IoSignalLight, TowerLightStatus> SignalTowerState
  518. {
  519. get { return dicState; }
  520. }
  521. public IoSignalLight LightCassette1
  522. {
  523. get { return _lightCassette1; }
  524. }
  525. public IoSignalLight LightCassette2
  526. {
  527. get { return _lightCassette2; }
  528. }
  529. public IoSignalLight LightCassette3
  530. {
  531. get { return _lightCassette3; }
  532. }
  533. public IoSignalLight LightCassette4
  534. {
  535. get { return _lightCassette4; }
  536. }
  537. public IoSignalLight LightCassettePlacement
  538. {
  539. get { return _lightCassettePlacement; }
  540. }
  541. public IoSignalLight LightAlarm
  542. {
  543. get { return _lightAlarm; }
  544. }
  545. private object _locker = new object();
  546. private Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>> _config =
  547. new Dictionary<string, Dictionary<IoSignalLight, TowerLightStatus>>();
  548. private Dictionary<IoSignalLight, TowerLightStatus> _cmdSetPoint = new Dictionary<IoSignalLight, TowerLightStatus>();
  549. private Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  550. public IoLoadPortSignal(string module, XmlElement node, string ioModule = "")
  551. {
  552. if (node == null)
  553. return;
  554. base.Module = node.GetAttribute("module");
  555. base.Name = node.GetAttribute("id");
  556. base.Display = node.GetAttribute("display");
  557. base.DeviceID = node.GetAttribute("schematicId");
  558. DOAccessor doCS1 = ParseDoNode("DO_LP1_CS1", node, ioModule);
  559. if (doCS1 != null)
  560. {
  561. _lightCassette1 = new IoSignalLight(module, "SignalLightCS1", "CS1 Light", "SignalLightCS1", doCS1);
  562. }
  563. DOAccessor doCS2 = ParseDoNode("DO_LP1_CS2", node, ioModule);
  564. if (doCS2 != null)
  565. {
  566. _lightCassette2 = new IoSignalLight(module, "SignalLightCS2", "CS2 Light", "SignalLightCS2", doCS2);
  567. }
  568. DOAccessor doCS3 = ParseDoNode("DO_LP1_CS3", node, ioModule);
  569. if (doCS3 != null)
  570. {
  571. _lightCassette3 = new IoSignalLight(module, "SignalLightCS3", "CS1 Light", "SignalLightCS3", doCS3);
  572. }
  573. DOAccessor doCS4 = ParseDoNode("DO_LP1_CS4", node, ioModule);
  574. if (doCS4 != null)
  575. {
  576. _lightCassette4 = new IoSignalLight(module, "SignalLightCS4", "CS4 Light", "SignalLightCS4", doCS4);
  577. }
  578. }
  579. public virtual bool Initialize()
  580. {
  581. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  582. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  583. return true;
  584. }
  585. public void SetLight(LoadPortLightType lightType, LightState state, int blinkInterval = 500)
  586. {
  587. IoSignalLight light = null;
  588. switch (lightType)
  589. {
  590. case LoadPortLightType.CassetteNO1:
  591. light = _lightCassette1;
  592. break;
  593. case LoadPortLightType.CassetteNO2:
  594. light = _lightCassette2;
  595. break;
  596. case LoadPortLightType.CassetteNO3:
  597. light = _lightCassette3;
  598. break;
  599. case LoadPortLightType.CassetteNO4:
  600. light = _lightCassette4; ;
  601. break;
  602. case LoadPortLightType.CasseettePlacement:
  603. light = _lightCassettePlacement; ;
  604. break;
  605. case LoadPortLightType.LoadPortAlarm:
  606. light = _lightAlarm; ;
  607. break;
  608. }
  609. if (light == null)
  610. {
  611. LOG.Write($"Undefined light {lightType}");
  612. return;
  613. }
  614. switch (state)
  615. {
  616. case LightState.On:
  617. _cmdSetPoint[light] = TowerLightStatus.On;
  618. light.StateSetPoint = TowerLightStatus.On;
  619. break;
  620. case LightState.Off:
  621. _cmdSetPoint[light] = TowerLightStatus.Off;
  622. light.StateSetPoint = TowerLightStatus.Off;
  623. break;
  624. case LightState.Blink:
  625. _cmdSetPoint[light] = TowerLightStatus.Blinking;
  626. light.Interval = blinkInterval;
  627. light.StateSetPoint = TowerLightStatus.Blinking;
  628. break;
  629. }
  630. }
  631. public virtual bool CustomSignalTower(string configPathFile)
  632. {
  633. try
  634. {
  635. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  636. lock (_locker)
  637. {
  638. foreach (STEvent e in config.Events)
  639. {
  640. if (!_config.ContainsKey(e.Name))
  641. {
  642. _config[e.Name] = new Dictionary<IoSignalLight, TowerLightStatus>();
  643. }
  644. }
  645. }
  646. }
  647. catch (Exception e)
  648. {
  649. EV.PostWarningLog(Module, $"Signal tower config file is invalid, {e.Message}");
  650. return false;
  651. }
  652. return true;
  653. }
  654. public virtual void Monitor()
  655. {
  656. //Dictionary<IoSignalLight, TowerLightStatus> dicState = new Dictionary<IoSignalLight, TowerLightStatus>();
  657. if (_lightCassette1 != null)
  658. dicState[_lightCassette1] = TowerLightStatus.Off;
  659. if (_lightCassette2 != null)
  660. dicState[_lightCassette2] = TowerLightStatus.Off;
  661. if (_lightCassette3 != null)
  662. dicState[_lightCassette3] = TowerLightStatus.Off;
  663. if (_lightCassette4 != null)
  664. dicState[_lightCassette4] = TowerLightStatus.Off;
  665. if (_lightCassettePlacement != null)
  666. dicState[_lightCassettePlacement] = TowerLightStatus.Off;
  667. if (_lightAlarm != null)
  668. dicState[_lightAlarm] = TowerLightStatus.Off;
  669. MonitorLight(_lightCassette1);
  670. MonitorLight(_lightCassette2);
  671. MonitorLight(_lightCassette3);
  672. MonitorLight(_lightCassette4);
  673. MonitorLight(_lightCassettePlacement);
  674. MonitorLight(_lightAlarm);
  675. }
  676. public virtual void Reset()
  677. {
  678. ResetLight(_lightCassette1);
  679. ResetLight(_lightCassette2);
  680. ResetLight(_lightCassette3);
  681. ResetLight(_lightCassette4);
  682. ResetLight(_lightCassettePlacement);
  683. ResetLight(_lightAlarm);
  684. }
  685. public virtual bool SwitchOffBuzzer(string cmd, object[] objs)
  686. {
  687. return true;
  688. }
  689. private void SetLight(IoSignalLight light, Dictionary<IoSignalLight, TowerLightStatus> state)
  690. {
  691. if (light != null)
  692. {
  693. light.StateSetPoint = state[light];
  694. }
  695. }
  696. private void ResetLight(IoSignalLight light)
  697. {
  698. if (light != null)
  699. {
  700. light.Reset();
  701. }
  702. }
  703. private void MonitorLight(IoSignalLight light)
  704. {
  705. if (light != null)
  706. {
  707. light.Monitor();
  708. }
  709. }
  710. public void Terminate()
  711. {
  712. }
  713. protected TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  714. {
  715. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  716. return TowerLightStatus.Blinking;
  717. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  718. return TowerLightStatus.On;
  719. return TowerLightStatus.Off;
  720. }
  721. protected TowerLightStatus ParseLight(string light)
  722. {
  723. if (string.IsNullOrEmpty(light))
  724. return TowerLightStatus.Off;
  725. TowerLightStatus result = TowerLightStatus.Unknown;
  726. light = light.Trim().ToLower();
  727. switch (light)
  728. {
  729. case "on":
  730. result = TowerLightStatus.On;
  731. break;
  732. case "off":
  733. result = TowerLightStatus.Off;
  734. break;
  735. case "blinking":
  736. result = TowerLightStatus.Blinking;
  737. break;
  738. default:
  739. LOG.Write("signal tower config file has invalid set, " + light);
  740. break;
  741. }
  742. return result;
  743. }
  744. }
  745. public enum LoadPortLightType
  746. {
  747. CassetteNO1,
  748. CassetteNO2,
  749. CassetteNO3,
  750. CassetteNO4,
  751. CasseettePlacement,
  752. LoadPortAlarm,
  753. }
  754. }