SignalTowerBase.cs 13 KB

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