SignalTowerBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 bool IsCloseBuzzer { get; set; }
  100. public AITSignalTowerData DeviceData
  101. {
  102. get
  103. {
  104. AITSignalTowerData data = new AITSignalTowerData()
  105. {
  106. DeviceName = Name,
  107. DeviceSchematicId = DeviceID,
  108. DisplayName = Display,
  109. IsGreenLightOn = _lights.ContainsKey(LightType.Green) && _lights[LightType.Green] != null && _lights[LightType.Green].Value,
  110. IsRedLightOn = _lights.ContainsKey(LightType.Red) && _lights[LightType.Red] != null && _lights[LightType.Red].Value,
  111. IsYellowLightOn = _lights.ContainsKey(LightType.Yellow) && _lights[LightType.Yellow] != null && _lights[LightType.Yellow].Value,
  112. IsWhiteLightOn = _lights.ContainsKey(LightType.White) && _lights[LightType.White] != null && _lights[LightType.White].Value,
  113. IsBlueLightOn = _lights.ContainsKey(LightType.Blue) && _lights[LightType.Blue] != null && _lights[LightType.Blue].Value,
  114. IsBuzzerOn = _lights.ContainsKey(LightType.Buzzer) && _lights[LightType.Buzzer] != null && _lights[LightType.Buzzer].Value,
  115. IsBuzzer1On = _lights.ContainsKey(LightType.Buzzer1) && _lights[LightType.Buzzer1] != null && _lights[LightType.Buzzer1].Value,
  116. IsBuzzer2On = _lights.ContainsKey(LightType.Buzzer2) && _lights[LightType.Buzzer2] != null && _lights[LightType.Buzzer2].Value,
  117. IsBuzzer3On = _lights.ContainsKey(LightType.Buzzer3) && _lights[LightType.Buzzer3] != null && _lights[LightType.Buzzer3].Value,
  118. IsBuzzer4On = _lights.ContainsKey(LightType.Buzzer4) && _lights[LightType.Buzzer4] != null && _lights[LightType.Buzzer4].Value,
  119. IsBuzzer5On = _lights.ContainsKey(LightType.Buzzer5) && _lights[LightType.Buzzer5] != null && _lights[LightType.Buzzer5].Value,
  120. IsBuzzerOff = _isBuzzerOff
  121. };
  122. return data;
  123. }
  124. }
  125. private Dictionary<string, Dictionary<LightType, TowerLightStatus>> _config =
  126. new Dictionary<string, Dictionary<LightType, TowerLightStatus>>();
  127. private Dictionary<LightType, SignalLightBase> _lights = new Dictionary<LightType, SignalLightBase>();
  128. private bool _isBuzzerOff;
  129. public SignalTowerBase(string module, string name):base(module, name, name, name)
  130. {
  131. IsAutoSetLight = true;
  132. }
  133. public virtual bool Initialize()
  134. {
  135. OP.Subscribe($"System.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  136. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  137. return true;
  138. }
  139. private bool SwitchOffBuzzer(string arg1, object[] arg2)
  140. {
  141. _isBuzzerOff = !_isBuzzerOff;
  142. return true;
  143. }
  144. public void Monitor()
  145. {
  146. try
  147. {
  148. if (IsAutoSetLight)
  149. {
  150. MonitorSignalTowerAuto();
  151. }
  152. foreach (var signalLightBase in _lights)
  153. {
  154. if (signalLightBase.Value != null)
  155. {
  156. signalLightBase.Value.Monitor();
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. LOG.WriteExeption(ex);
  163. }
  164. }
  165. public void Terminate()
  166. {
  167. }
  168. public void Reset()
  169. {
  170. foreach (var signalLightBase in _lights)
  171. {
  172. if (signalLightBase.Value != null)
  173. {
  174. signalLightBase.Value.Reset();
  175. }
  176. }
  177. _isBuzzerOff = false;
  178. }
  179. public abstract SignalLightBase CreateLight(LightType type);
  180. public bool CustomSignalTower()
  181. {
  182. string pathFile = PathManager.GetCfgDir() + "_SignalTower.xml";
  183. if (!File.Exists(pathFile))
  184. {
  185. pathFile = PathManager.GetCfgDir() + "SignalTower.xml";
  186. }
  187. return CustomSignalTower(pathFile);
  188. // return CustomSignalTower(PathManager.GetCfgDir() + "SignalTower.xml");
  189. }
  190. public bool CustomSignalTower(string configPathFile)
  191. {
  192. try
  193. {
  194. if (!File.Exists(configPathFile))
  195. {
  196. MessageBox.Show($"Signal tower config file not exist, " + configPathFile, "Error",
  197. MessageBoxButton.OK, MessageBoxImage.Error);
  198. return false;
  199. }
  200. foreach (int light in Enum.GetValues(typeof(LightType)))
  201. {
  202. _lights[(LightType)light] = CreateLight((LightType)light);
  203. }
  204. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  205. List<LightType> configTypes = _lights.Keys.ToList();
  206. foreach (STEvent e in config.Events)
  207. {
  208. if (!_config.ContainsKey(e.Name))
  209. {
  210. _config[e.Name] = new Dictionary<LightType, TowerLightStatus>();
  211. }
  212. foreach (LightType lightType in _lights.Keys)
  213. {
  214. PropertyInfo[] ps = e.GetType().GetProperties();
  215. foreach (PropertyInfo p in ps)
  216. {
  217. if (p.Name.ToLower() == lightType.ToString().ToLower())
  218. {
  219. string value = (string)p.GetValue(e);
  220. if (!string.IsNullOrEmpty(value))
  221. {
  222. value = value.ToLower();
  223. if (value == "on")
  224. _config[e.Name][lightType] = TowerLightStatus.On;
  225. else if (value == "off")
  226. _config[e.Name][lightType] = TowerLightStatus.Off;
  227. if (value.Contains("blink")) //bink, blinking,
  228. _config[e.Name][lightType] = TowerLightStatus.Blinking;
  229. if (configTypes.Contains(lightType))
  230. configTypes.Remove(lightType);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. foreach (var light in configTypes)
  237. {
  238. _lights.Remove(light);
  239. }
  240. }
  241. catch (Exception e)
  242. {
  243. EV.PostWarningLog(Module, $"Signal tower config file invalid, {e.Message}");
  244. return false;
  245. }
  246. return true;
  247. }
  248. public void MonitorSignalTowerAuto()
  249. {
  250. Dictionary<LightType, TowerLightStatus> lightStateValue = new Dictionary<LightType, TowerLightStatus>();
  251. foreach (int light in Enum.GetValues(typeof(LightType)))
  252. {
  253. lightStateValue[(LightType)light] = TowerLightStatus.Off;
  254. }
  255. foreach (var trigCondition in _config)
  256. {
  257. var conditionValue = DATA.Poll(trigCondition.Key);
  258. if (conditionValue == null)
  259. continue;
  260. bool isTrig = (bool)conditionValue;
  261. if (isTrig)
  262. {
  263. foreach (LightType lightType in trigCondition.Value.Keys)
  264. {
  265. if (IsBuzzer(lightType) && _isBuzzerOff)
  266. {
  267. lightStateValue[lightType] = TowerLightStatus.Off;
  268. }
  269. else
  270. {
  271. lightStateValue[lightType] =
  272. MergeCondition(lightStateValue[lightType], trigCondition.Value[lightType]);
  273. }
  274. }
  275. }
  276. }
  277. foreach (var light in _lights)
  278. {
  279. if (light.Value != null)
  280. {
  281. light.Value.StateSetPoint = lightStateValue[light.Key];
  282. }
  283. }
  284. }
  285. private bool IsBuzzer(LightType type)
  286. {
  287. return type == LightType.Buzzer || type == LightType.Buzzer1 || type == LightType.Buzzer2 ||
  288. type == LightType.Buzzer3 ||
  289. type == LightType.Buzzer4 || type == LightType.Buzzer5;
  290. }
  291. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  292. {
  293. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  294. return TowerLightStatus.Blinking;
  295. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  296. return TowerLightStatus.On;
  297. return TowerLightStatus.Off;
  298. }
  299. }
  300. }