IoSignalTower.cs 30 KB

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