SignalTowerBase.cs 12 KB

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