SignalTowerBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.Serialization;
  7. using System.Windows;
  8. using System.Xml.Serialization;
  9. using Aitex.Common.Util;
  10. using Aitex.Core.Common.DeviceData;
  11. using Aitex.Core.RT.DataCenter;
  12. using Aitex.Core.RT.Device;
  13. using Aitex.Core.RT.Event;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.OperationCenter;
  16. using Aitex.Core.Util;
  17. namespace MECF.Framework.Common.Device.Bases
  18. {
  19. [Serializable]
  20. [DataContract]
  21. public enum TowerLightStatus
  22. {
  23. [EnumMember]
  24. Off,
  25. [EnumMember]
  26. On,
  27. [EnumMember]
  28. Blinking,
  29. [EnumMember]
  30. Unknown,
  31. }
  32. [Serializable]
  33. [DataContract]
  34. public enum LightState
  35. {
  36. [EnumMember]
  37. Off,
  38. [EnumMember]
  39. On,
  40. [EnumMember]
  41. Blink,
  42. [EnumMember]
  43. Unknown,
  44. }
  45. public enum LightType
  46. {
  47. Red,
  48. Yellow,
  49. Green,
  50. White,
  51. Blue,
  52. Buzzer,
  53. Buzzer1,
  54. Buzzer2,
  55. Buzzer3,
  56. Buzzer4,
  57. Buzzer5,
  58. }
  59. public class STEvents
  60. {
  61. [XmlElement(ElementName = "STEvent")]
  62. public List<STEvent> Events;
  63. }
  64. public class STEvent
  65. {
  66. [XmlAttribute(AttributeName = "name")]
  67. public string Name { get; set; }
  68. [XmlAttribute]
  69. public string Red { get; set; }
  70. [XmlAttribute]
  71. public string Yellow { get; set; }
  72. [XmlAttribute]
  73. public string Green { get; set; }
  74. [XmlAttribute]
  75. public string Blue { get; set; }
  76. [XmlAttribute]
  77. public string Buzzer { get; set; }
  78. [XmlAttribute]
  79. public string White { get; set; }
  80. [XmlAttribute]
  81. public string Buzzer1 { get; set; }
  82. [XmlAttribute]
  83. public string Buzzer2 { get; set; }
  84. [XmlAttribute]
  85. public string Buzzer3 { get; set; }
  86. [XmlAttribute]
  87. public string Buzzer4 { get; set; }
  88. [XmlAttribute]
  89. public string Buzzer5 { get; set; }
  90. }
  91. /*
  92. <STEvents>
  93. <STEvent name = "System.IsAlarm" Red="On" Yellow="Off" Green="Off" Blue="Off" White="" Buzzer="Off"/>
  94. </STEvents>
  95. */
  96. public abstract class SignalTowerBase : BaseDevice, IDevice
  97. {
  98. public bool IsAutoSetLight { get; set; }
  99. public AITSignalTowerData DeviceData
  100. {
  101. get
  102. {
  103. AITSignalTowerData data = new AITSignalTowerData()
  104. {
  105. DeviceName = Name,
  106. DeviceSchematicId = DeviceID,
  107. DisplayName = Display,
  108. IsGreenLightOn = _lights.ContainsKey(LightType.Green) && _lights[LightType.Green] != null && _lights[LightType.Green].Value,
  109. IsRedLightOn = _lights.ContainsKey(LightType.Red) && _lights[LightType.Red] != null && _lights[LightType.Red].Value,
  110. IsYellowLightOn = _lights.ContainsKey(LightType.Yellow) && _lights[LightType.Yellow] != null && _lights[LightType.Yellow].Value,
  111. IsWhiteLightOn = _lights.ContainsKey(LightType.White) && _lights[LightType.White] != null && _lights[LightType.White].Value,
  112. IsBlueLightOn = _lights.ContainsKey(LightType.Blue) && _lights[LightType.Blue] != null && _lights[LightType.Blue].Value,
  113. IsBuzzerOn = _lights.ContainsKey(LightType.Buzzer) && _lights[LightType.Buzzer] != null && _lights[LightType.Buzzer].Value,
  114. IsBuzzer1On = _lights.ContainsKey(LightType.Buzzer1) && _lights[LightType.Buzzer1] != null && _lights[LightType.Buzzer1].Value,
  115. IsBuzzer2On = _lights.ContainsKey(LightType.Buzzer2) && _lights[LightType.Buzzer2] != null && _lights[LightType.Buzzer2].Value,
  116. IsBuzzer3On = _lights.ContainsKey(LightType.Buzzer3) && _lights[LightType.Buzzer3] != null && _lights[LightType.Buzzer3].Value,
  117. IsBuzzer4On = _lights.ContainsKey(LightType.Buzzer4) && _lights[LightType.Buzzer4] != null && _lights[LightType.Buzzer4].Value,
  118. IsBuzzer5On = _lights.ContainsKey(LightType.Buzzer5) && _lights[LightType.Buzzer5] != null && _lights[LightType.Buzzer5].Value,
  119. };
  120. return data;
  121. }
  122. }
  123. private Dictionary<string, Dictionary<LightType, TowerLightStatus>> _config =
  124. new Dictionary<string, Dictionary<LightType, TowerLightStatus>>();
  125. private Dictionary<LightType, SignalLightBase> _lights = new Dictionary<LightType, SignalLightBase>();
  126. private bool _isBuzzerOff;
  127. public SignalTowerBase(string module, string name):base(module, name, name, name)
  128. {
  129. IsAutoSetLight = true;
  130. }
  131. public virtual bool Initialize()
  132. {
  133. OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  134. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  135. return true;
  136. }
  137. private bool SwitchOffBuzzer(string arg1, object[] arg2)
  138. {
  139. _isBuzzerOff = true;
  140. return true;
  141. }
  142. public void Monitor()
  143. {
  144. try
  145. {
  146. if (IsAutoSetLight)
  147. {
  148. MonitorSignalTowerAuto();
  149. }
  150. foreach (var signalLightBase in _lights)
  151. {
  152. if (signalLightBase.Value != null)
  153. {
  154. signalLightBase.Value.Monitor();
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. LOG.WriteExeption(ex);
  161. }
  162. }
  163. public void Terminate()
  164. {
  165. }
  166. public void Reset()
  167. {
  168. foreach (var signalLightBase in _lights)
  169. {
  170. if (signalLightBase.Value != null)
  171. {
  172. signalLightBase.Value.Reset();
  173. }
  174. }
  175. _isBuzzerOff = false;
  176. }
  177. public abstract SignalLightBase CreateLight(LightType type);
  178. public bool CustomSignalTower()
  179. {
  180. string pathFile = PathManager.GetCfgDir() + "_SignalTower.xml";
  181. if (!File.Exists(pathFile))
  182. {
  183. pathFile = PathManager.GetCfgDir() + "SignalTower.xml";
  184. }
  185. return CustomSignalTower(pathFile);
  186. // return CustomSignalTower(PathManager.GetCfgDir() + "SignalTower.xml");
  187. }
  188. public bool CustomSignalTower(string configPathFile)
  189. {
  190. try
  191. {
  192. if (!File.Exists(configPathFile))
  193. {
  194. MessageBox.Show($"Signal tower config file not exist, " + configPathFile, "Error",
  195. MessageBoxButton.OK, MessageBoxImage.Error);
  196. return false;
  197. }
  198. foreach (int light in Enum.GetValues(typeof(LightType)))
  199. {
  200. _lights[(LightType)light] = CreateLight((LightType)light);
  201. }
  202. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  203. List<LightType> configTypes = _lights.Keys.ToList();
  204. foreach (STEvent e in config.Events)
  205. {
  206. if (!_config.ContainsKey(e.Name))
  207. {
  208. _config[e.Name] = new Dictionary<LightType, TowerLightStatus>();
  209. }
  210. foreach (LightType lightType in _lights.Keys)
  211. {
  212. PropertyInfo[] ps = e.GetType().GetProperties();
  213. foreach (PropertyInfo p in ps)
  214. {
  215. if (p.Name.ToLower() == lightType.ToString().ToLower())
  216. {
  217. string value = (string)p.GetValue(e);
  218. if (!string.IsNullOrEmpty(value))
  219. {
  220. value = value.ToLower();
  221. if (value == "on")
  222. _config[e.Name][lightType] = TowerLightStatus.On;
  223. else if (value == "off")
  224. _config[e.Name][lightType] = TowerLightStatus.Off;
  225. if (value.Contains("blink")) //bink, blinking,
  226. _config[e.Name][lightType] = TowerLightStatus.Blinking;
  227. if (configTypes.Contains(lightType))
  228. configTypes.Remove(lightType);
  229. }
  230. }
  231. }
  232. }
  233. }
  234. foreach (var light in configTypes)
  235. {
  236. _lights.Remove(light);
  237. }
  238. }
  239. catch (Exception e)
  240. {
  241. EV.PostWarningLog(Module, $"Signal tower config file invalid, {e.Message}");
  242. return false;
  243. }
  244. return true;
  245. }
  246. public void MonitorSignalTowerAuto()
  247. {
  248. Dictionary<LightType, TowerLightStatus> lightStateValue = new Dictionary<LightType, TowerLightStatus>();
  249. foreach (int light in Enum.GetValues(typeof(LightType)))
  250. {
  251. lightStateValue[(LightType)light] = TowerLightStatus.Off;
  252. }
  253. foreach (var trigCondition in _config)
  254. {
  255. var conditionValue = DATA.Poll(trigCondition.Key);
  256. if (conditionValue == null)
  257. continue;
  258. bool isTrig = (bool)conditionValue;
  259. if (isTrig)
  260. {
  261. foreach (LightType lightType in trigCondition.Value.Keys)
  262. {
  263. if (IsBuzzer(lightType) && _isBuzzerOff)
  264. {
  265. lightStateValue[lightType] = TowerLightStatus.Off;
  266. }
  267. else
  268. {
  269. lightStateValue[lightType] =
  270. MergeCondition(lightStateValue[lightType], trigCondition.Value[lightType]);
  271. }
  272. }
  273. }
  274. }
  275. foreach (var light in _lights)
  276. {
  277. if (light.Value != null)
  278. {
  279. light.Value.StateSetPoint = lightStateValue[light.Key];
  280. }
  281. }
  282. }
  283. private bool IsBuzzer(LightType type)
  284. {
  285. return type == LightType.Buzzer || type == LightType.Buzzer1 || type == LightType.Buzzer2 ||
  286. type == LightType.Buzzer3 ||
  287. type == LightType.Buzzer4 || type == LightType.Buzzer5;
  288. }
  289. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  290. {
  291. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  292. return TowerLightStatus.Blinking;
  293. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  294. return TowerLightStatus.On;
  295. return TowerLightStatus.Off;
  296. }
  297. }
  298. }