SignalTowerBase.cs 11 KB

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