SignalTowerBase.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. protected Dictionary<LightType, SignalLightBase> _lights = new Dictionary<LightType, SignalLightBase>();
  131. private bool _isBuzzerOff;
  132. public SignalTowerBase() : base()
  133. {
  134. IsAutoSetLight = true;
  135. }
  136. public SignalTowerBase(string module, string name) : base(module, name, name, name)
  137. {
  138. IsAutoSetLight = true;
  139. }
  140. public virtual bool Initialize()
  141. {
  142. //OP.Subscribe($"{Module}.{Name}.{AITSignalTowerOperation.SwitchOffBuzzer}", SwitchOffBuzzer);
  143. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  144. return true;
  145. }
  146. public void SwitchOffBuzzer(bool isOff)
  147. {
  148. if (isOff)
  149. IsAutoSetLight = true;
  150. _isBuzzerOff = isOff;
  151. }
  152. private bool SwitchOffBuzzer(string arg1, object[] arg2)
  153. {
  154. IsAutoSetLight = true;
  155. _isBuzzerOff = true;
  156. return true;
  157. }
  158. public void Monitor()
  159. {
  160. try
  161. {
  162. if (IsAutoSetLight)
  163. {
  164. MonitorSignalTowerAuto();
  165. }
  166. foreach (var signalLightBase in _lights)
  167. {
  168. if (signalLightBase.Value == null)
  169. continue;
  170. if (IsBuzzer(signalLightBase.Key) && !IsAutoSetLight)
  171. continue;
  172. signalLightBase.Value.Monitor();
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. LOG.Write(ex);
  178. }
  179. }
  180. public void Terminate()
  181. {
  182. }
  183. public void Reset()
  184. {
  185. foreach (var signalLightBase in _lights)
  186. {
  187. if (signalLightBase.Value != null)
  188. {
  189. signalLightBase.Value.Reset();
  190. }
  191. }
  192. _isBuzzerOff = false;
  193. }
  194. public abstract SignalLightBase CreateLight(LightType type);
  195. public bool CustomSignalTower()
  196. {
  197. string pathFile = PathManager.GetCfgDir() + "_SignalTower.xml";
  198. if (!File.Exists(pathFile))
  199. {
  200. pathFile = PathManager.GetCfgDir() + "SignalTower.xml";
  201. }
  202. return CustomSignalTower(pathFile);
  203. }
  204. public bool CustomSignalTower(string configPathFile)
  205. {
  206. try
  207. {
  208. if (!File.Exists(configPathFile))
  209. {
  210. MessageBox.Show($"Signal tower config file not exist, " + configPathFile, "Error",
  211. MessageBoxButton.OK, MessageBoxImage.Error);
  212. return false;
  213. }
  214. foreach (int light in Enum.GetValues(typeof(LightType)))
  215. {
  216. _lights[(LightType)light] = CreateLight((LightType)light);
  217. }
  218. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  219. List<LightType> configTypes = _lights.Keys.ToList();
  220. foreach (STEvent e in config.Events)
  221. {
  222. if (!_config.ContainsKey(e.Name))
  223. {
  224. _config[e.Name] = new Dictionary<LightType, TowerLightStatus>();
  225. }
  226. foreach (LightType lightType in _lights.Keys)
  227. {
  228. PropertyInfo[] ps = e.GetType().GetProperties();
  229. foreach (PropertyInfo p in ps)
  230. {
  231. if (p.Name.ToLower() == lightType.ToString().ToLower())
  232. {
  233. string value = (string)p.GetValue(e);
  234. if (!string.IsNullOrEmpty(value))
  235. {
  236. value = value.ToLower();
  237. if (value == "on")
  238. _config[e.Name][lightType] = TowerLightStatus.On;
  239. else if (value == "off")
  240. _config[e.Name][lightType] = TowerLightStatus.Off;
  241. if (value.Contains("blink")) //bink, blinking,
  242. _config[e.Name][lightType] = TowerLightStatus.Blinking;
  243. if (configTypes.Contains(lightType))
  244. configTypes.Remove(lightType);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. foreach (var light in configTypes)
  251. {
  252. _lights.Remove(light);
  253. }
  254. }
  255. catch (Exception e)
  256. {
  257. EV.PostWarningLog(Module, $"Signal tower config file invalid, {e.Message}");
  258. return false;
  259. }
  260. return true;
  261. }
  262. public void MonitorSignalTowerAuto()
  263. {
  264. Dictionary<LightType, TowerLightStatus> lightStateValue = new Dictionary<LightType, TowerLightStatus>();
  265. foreach (int light in Enum.GetValues(typeof(LightType)))
  266. {
  267. lightStateValue[(LightType)light] = TowerLightStatus.Off;
  268. }
  269. foreach (var trigCondition in _config)
  270. {
  271. var conditionValue = DATA.Poll(trigCondition.Key);
  272. if (conditionValue == null)
  273. continue;
  274. if (!(conditionValue is bool))
  275. continue;
  276. bool isTrig = (bool)conditionValue;
  277. if (isTrig)
  278. {
  279. foreach (LightType lightType in trigCondition.Value.Keys)
  280. {
  281. if (IsBuzzer(lightType) && _isBuzzerOff)
  282. {
  283. lightStateValue[lightType] = TowerLightStatus.Off;
  284. }
  285. else
  286. {
  287. lightStateValue[lightType] =
  288. MergeCondition(lightStateValue[lightType], trigCondition.Value[lightType]);
  289. }
  290. }
  291. //if (lightStateValue.ContainsKey(LightType.Red) && (lightStateValue[LightType.Red] == TowerLightStatus.Blinking || lightStateValue[LightType.Red] == TowerLightStatus.On))
  292. //{
  293. // if (lightStateValue.ContainsKey(LightType.Green))
  294. // {
  295. // lightStateValue[LightType.Green] = TowerLightStatus.Off;
  296. // }
  297. // if (lightStateValue.ContainsKey(LightType.Yellow))
  298. // {
  299. // lightStateValue[LightType.Yellow] = TowerLightStatus.Off;
  300. // }
  301. // if (lightStateValue.ContainsKey(LightType.Blue))
  302. // {
  303. // lightStateValue[LightType.Blue] = TowerLightStatus.Off;
  304. // }
  305. //}
  306. }
  307. }
  308. foreach (var light in _lights)
  309. {
  310. if (light.Value != null)
  311. {
  312. light.Value.StateSetPoint = lightStateValue[light.Key];
  313. }
  314. }
  315. }
  316. private bool IsBuzzer(LightType type)
  317. {
  318. return type == LightType.Buzzer || type == LightType.Buzzer1 || type == LightType.Buzzer2 ||
  319. type == LightType.Buzzer3 ||
  320. type == LightType.Buzzer4 || type == LightType.Buzzer5;
  321. }
  322. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  323. {
  324. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  325. return TowerLightStatus.Blinking;
  326. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  327. return TowerLightStatus.On;
  328. return TowerLightStatus.Off;
  329. }
  330. }
  331. }