SignalTowerBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  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.WriteExeption(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. string pathFile = PathManager.GetCfgDir() + "_SignalTower.xml";
  186. if (!File.Exists(pathFile))
  187. {
  188. pathFile = PathManager.GetCfgDir() + "SignalTower.xml";
  189. }
  190. return CustomSignalTower(pathFile);
  191. //return CustomSignalTower(PathManager.GetCfgDir() + "SignalTower.xml");
  192. }
  193. public bool CustomSignalTower(string configPathFile)
  194. {
  195. try
  196. {
  197. if (!File.Exists(configPathFile))
  198. {
  199. MessageBox.Show($"Signal tower config file not exist, " + configPathFile, "Error",
  200. MessageBoxButton.OK, MessageBoxImage.Error);
  201. return false;
  202. }
  203. foreach (int light in Enum.GetValues(typeof(LightType)))
  204. {
  205. _lights[(LightType)light] = CreateLight((LightType)light);
  206. }
  207. STEvents config = CustomXmlSerializer.Deserialize<STEvents>(new FileInfo(configPathFile));
  208. List<LightType> configTypes = _lights.Keys.ToList();
  209. foreach (STEvent e in config.Events)
  210. {
  211. if (!_config.ContainsKey(e.Name))
  212. {
  213. _config[e.Name] = new Dictionary<LightType, TowerLightStatus>();
  214. }
  215. foreach (LightType lightType in _lights.Keys)
  216. {
  217. PropertyInfo[] ps = e.GetType().GetProperties();
  218. foreach (PropertyInfo p in ps)
  219. {
  220. if (p.Name.ToLower() == lightType.ToString().ToLower())
  221. {
  222. string value = (string)p.GetValue(e);
  223. if (!string.IsNullOrEmpty(value))
  224. {
  225. value = value.ToLower();
  226. if (value == "on")
  227. _config[e.Name][lightType] = TowerLightStatus.On;
  228. else if (value == "off")
  229. _config[e.Name][lightType] = TowerLightStatus.Off;
  230. if (value.Contains("blink")) //bink, blinking,
  231. _config[e.Name][lightType] = TowerLightStatus.Blinking;
  232. if (configTypes.Contains(lightType))
  233. configTypes.Remove(lightType);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. foreach (var light in configTypes)
  240. {
  241. _lights.Remove(light);
  242. }
  243. }
  244. catch (Exception e)
  245. {
  246. EV.PostWarningLog(Module, $"Signal tower config file invalid, {e.Message}");
  247. return false;
  248. }
  249. return true;
  250. }
  251. public void MonitorSignalTowerAuto()
  252. {
  253. Dictionary<LightType, TowerLightStatus> lightStateValue = new Dictionary<LightType, TowerLightStatus>();
  254. foreach (int light in Enum.GetValues(typeof(LightType)))
  255. {
  256. lightStateValue[(LightType)light] = TowerLightStatus.Off;
  257. }
  258. foreach (var trigCondition in _config)
  259. {
  260. var conditionValue = DATA.Poll(trigCondition.Key);
  261. if (conditionValue == null)
  262. continue;
  263. bool isTrig = (bool)conditionValue;
  264. if (isTrig)
  265. {
  266. foreach (LightType lightType in trigCondition.Value.Keys)
  267. {
  268. if (IsBuzzer(lightType) && _isBuzzerOff)
  269. {
  270. lightStateValue[lightType] = TowerLightStatus.Off;
  271. }
  272. else
  273. {
  274. lightStateValue[lightType] =
  275. MergeCondition(lightStateValue[lightType], trigCondition.Value[lightType]);
  276. }
  277. }
  278. }
  279. }
  280. foreach (var light in _lights)
  281. {
  282. if (light.Value != null)
  283. {
  284. light.Value.StateSetPoint = lightStateValue[light.Key];
  285. }
  286. }
  287. }
  288. private bool IsBuzzer(LightType type)
  289. {
  290. return type == LightType.Buzzer || type == LightType.Buzzer1 || type == LightType.Buzzer2 ||
  291. type == LightType.Buzzer3 ||
  292. type == LightType.Buzzer4 || type == LightType.Buzzer5;
  293. }
  294. private TowerLightStatus MergeCondition(TowerLightStatus newValue, TowerLightStatus oldValue)
  295. {
  296. if (newValue == TowerLightStatus.Blinking || oldValue == TowerLightStatus.Blinking)
  297. return TowerLightStatus.Blinking;
  298. if (newValue == TowerLightStatus.On || oldValue == TowerLightStatus.On)
  299. return TowerLightStatus.On;
  300. return TowerLightStatus.Off;
  301. }
  302. }
  303. }