SignalTowerConfigViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Aitex.Core.RT.Log;
  2. using MECF.Framework.Common.CommonData;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.UI.Client.ClientBase;
  5. using SciChart.Core.Extensions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Xml;
  11. namespace MECF.Framework.UI.Client.CenterViews.Configs.SignalTowerConfig
  12. {
  13. public class SignalTowerConfigViewModel : ModuleUiViewModelBase, ISupportMultipleSystem
  14. {
  15. public class SignalTowerItem : NotifiableItem
  16. {
  17. private string _name;
  18. public string Name
  19. {
  20. get { return _name; }
  21. set { _name = value; InvokePropertyChanged(nameof(Name)); }
  22. }
  23. private bool _isRed;
  24. public bool IsRed
  25. {
  26. get { return _isRed; }
  27. set
  28. {
  29. _isRed = value;
  30. if (value) IsRedBlinking = false; InvokePropertyChanged(nameof(IsRed));
  31. }
  32. }
  33. private bool _isRedBlinking;
  34. public bool IsRedBlinking
  35. {
  36. get { return _isRedBlinking; }
  37. set
  38. {
  39. _isRedBlinking = value;
  40. if (value) IsRed = false; InvokePropertyChanged(nameof(IsRedBlinking));
  41. }
  42. }
  43. private bool _isGreen;
  44. public bool IsGreen
  45. {
  46. get { return _isGreen; }
  47. set
  48. {
  49. _isGreen = value;
  50. if (value) IsGreenBlinking = false; InvokePropertyChanged(nameof(IsGreen));
  51. }
  52. }
  53. private bool _isGreenBlinking;
  54. public bool IsGreenBlinking
  55. {
  56. get { return _isGreenBlinking; }
  57. set
  58. {
  59. _isGreenBlinking = value;
  60. if (value) IsGreen = false; InvokePropertyChanged(nameof(IsGreenBlinking));
  61. }
  62. }
  63. private bool _isBlue;
  64. public bool IsBlue
  65. {
  66. get { return _isBlue; }
  67. set
  68. {
  69. _isBlue = value;
  70. if (value) IsBlueBlinking = false; InvokePropertyChanged(nameof(IsBlue));
  71. }
  72. }
  73. private bool _isBlueBlinking;
  74. public bool IsBlueBlinking
  75. {
  76. get { return _isBlueBlinking; }
  77. set
  78. {
  79. _isBlueBlinking = value;
  80. if (value) IsBlue = false; InvokePropertyChanged(nameof(IsBlueBlinking));
  81. }
  82. }
  83. private bool _isYellow;
  84. public bool IsYellow
  85. {
  86. get { return _isYellow; }
  87. set
  88. {
  89. _isYellow = value;
  90. if (value) IsYellowBlinking = false; InvokePropertyChanged(nameof(IsYellow));
  91. }
  92. }
  93. private bool _isYellowBlinking;
  94. public bool IsYellowBlinking
  95. {
  96. get { return _isYellowBlinking; }
  97. set
  98. {
  99. _isYellowBlinking = value;
  100. if (value) IsYellow = false; InvokePropertyChanged(nameof(IsYellowBlinking));
  101. }
  102. }
  103. private bool _isBuzzer;
  104. public bool IsBuzzer
  105. {
  106. get { return _isBuzzer; }
  107. set
  108. {
  109. _isBuzzer = value;
  110. if (value) IsBuzzerBlinking = false; InvokePropertyChanged(nameof(IsBuzzer));
  111. }
  112. }
  113. private bool _isBuzzerBlinking;
  114. public bool IsBuzzerBlinking
  115. {
  116. get { return _isBuzzerBlinking; }
  117. set
  118. {
  119. _isBuzzerBlinking = value;
  120. if (value) IsBuzzer = false; InvokePropertyChanged(nameof(IsBuzzerBlinking));
  121. }
  122. }
  123. }
  124. #region Properties
  125. public ObservableCollection<SignalTowerItem> SignalTowerData { get; set; }
  126. private string _contentDataGroup;
  127. private XmlDocument _xmlSignalTower;
  128. private List<string> _lstItems = new List<string>();
  129. #endregion
  130. #region Functions
  131. public SignalTowerConfigViewModel()
  132. {
  133. this.DisplayName = "SignalTower Config ";
  134. SignalTowerData = new ObservableCollection<SignalTowerItem>();
  135. }
  136. protected override void OnInitialize()
  137. {
  138. base.OnInitialize();
  139. }
  140. protected override void OnActivate()
  141. {
  142. base.OnActivate();
  143. UpdateData();
  144. }
  145. private void UpdateData()
  146. {
  147. if (_xmlSignalTower == null)
  148. {
  149. string contentSignal = QueryDataClient.Instance.Service.GetTypedConfigContent("SignalTower", "");
  150. try
  151. {
  152. _xmlSignalTower = new XmlDocument();
  153. _xmlSignalTower.LoadXml(contentSignal);
  154. var items = _xmlSignalTower.SelectNodes($"STEvents/STEvent");
  155. foreach (var item in items)
  156. {
  157. XmlElement element = item as XmlElement;
  158. SignalTowerItem stItem = new SignalTowerItem();
  159. stItem.Name = element.GetAttribute("name");
  160. stItem.IsRed = string.Compare(element.GetAttribute("Red"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  161. stItem.IsRedBlinking = string.Compare(element.GetAttribute("Red"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  162. stItem.IsYellow = string.Compare(element.GetAttribute("Yellow"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  163. stItem.IsYellowBlinking = string.Compare(element.GetAttribute("Yellow"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  164. stItem.IsGreen = string.Compare(element.GetAttribute("Green"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  165. stItem.IsGreenBlinking = string.Compare(element.GetAttribute("Green"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  166. stItem.IsBlue = string.Compare(element.GetAttribute("Blue"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  167. stItem.IsBlueBlinking = string.Compare(element.GetAttribute("Blue"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  168. stItem.IsBuzzer = string.Compare(element.GetAttribute("Buzzer"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  169. stItem.IsBuzzerBlinking = string.Compare(element.GetAttribute("Buzzer"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  170. SignalTowerData.Add(stItem);
  171. stItem.InvokePropertyChanged();
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. LOG.Write(ex);
  177. }
  178. }
  179. string content = QueryDataClient.Instance.Service.GetTypedConfigContent("DataGroup", "");
  180. if (_contentDataGroup == content)
  181. return;
  182. try
  183. {
  184. XmlDocument xmlContent = new XmlDocument();
  185. xmlContent.LoadXml(content);
  186. _contentDataGroup = content;
  187. var groups = xmlContent.SelectNodes($"DataGroupConfig/DataGroup[@name='SignalTower']/DataItem");
  188. List<string> names = new List<string>();
  189. foreach (var item in groups)
  190. {
  191. XmlElement element = item as XmlElement;
  192. string name = element.GetAttribute("name");
  193. names.Add(name);
  194. if (SignalTowerData.FirstOrDefault(x => x.Name == name) == null)
  195. {
  196. SignalTowerData.Add(new SignalTowerItem() { Name = name, });
  197. }
  198. }
  199. SignalTowerData.RemoveWhere(x => !names.Contains(x.Name));
  200. }
  201. catch (Exception ex)
  202. {
  203. LOG.Write(ex);
  204. }
  205. }
  206. public void SaveSelection()
  207. {
  208. var nodeGroup = _xmlSignalTower.SelectSingleNode($"STEvents");
  209. var nodeItem = _xmlSignalTower.SelectNodes($"STEvents/STEvent");
  210. foreach (var nodeGroupChildNode in nodeItem)
  211. {
  212. var node = nodeGroupChildNode as XmlElement;
  213. nodeGroup.RemoveChild(node);
  214. }
  215. foreach (var item in SignalTowerData)
  216. {
  217. var node = _xmlSignalTower.CreateElement("STEvent");
  218. node.SetAttribute("name", item.Name);
  219. node.SetAttribute("Red", item.IsRed ? "on" : item.IsRedBlinking ? "blinking" : "off");
  220. node.SetAttribute("Yellow", item.IsYellow ? "on" : item.IsYellowBlinking ? "blinking" : "off");
  221. node.SetAttribute("Blue", item.IsBlue ? "on" : item.IsBlueBlinking ? "blinking" : "off");
  222. node.SetAttribute("Green", item.IsGreen ? "on" : item.IsGreenBlinking ? "blinking" : "off");
  223. node.SetAttribute("Buzzer", item.IsBuzzer ? "on" : item.IsBuzzerBlinking ? "blinking" : "off");
  224. nodeGroup.AppendChild(node);
  225. }
  226. QueryDataClient.Instance.Service.SetTypedConfigContent("SignalTower", "", _xmlSignalTower.InnerXml);
  227. }
  228. public void CancelSelection()
  229. {
  230. try
  231. {
  232. var items = _xmlSignalTower.SelectNodes($"STEvents/STEvent");
  233. foreach (var item in items)
  234. {
  235. XmlElement element = item as XmlElement;
  236. SignalTowerItem stItem = SignalTowerData.FirstOrDefault(x => x.Name == element.GetAttribute("name"));
  237. if (stItem == null)
  238. continue;
  239. stItem.IsRed = string.Compare(element.GetAttribute("Red"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  240. stItem.IsRedBlinking = string.Compare(element.GetAttribute("Red"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  241. stItem.IsYellow = string.Compare(element.GetAttribute("Yellow"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  242. stItem.IsYellowBlinking = string.Compare(element.GetAttribute("Yellow"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  243. stItem.IsGreen = string.Compare(element.GetAttribute("Green"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  244. stItem.IsGreenBlinking = string.Compare(element.GetAttribute("Green"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  245. stItem.IsBlue = string.Compare(element.GetAttribute("Blue"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  246. stItem.IsBlueBlinking = string.Compare(element.GetAttribute("Blue"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  247. stItem.IsBuzzer = string.Compare(element.GetAttribute("Buzzer"), "on", StringComparison.CurrentCultureIgnoreCase) == 0;
  248. stItem.IsBuzzerBlinking = string.Compare(element.GetAttribute("Buzzer"), "blinking", StringComparison.CurrentCultureIgnoreCase) == 0;
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. LOG.Write(ex);
  254. }
  255. }
  256. #endregion
  257. }
  258. }