ParameterComparisonWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using NEIntelligentControl2.Models.Datas;
  2. using NEIntelligentControl2.Models.Windturbine;
  3. using NEIntelligentControl2.Service.Windturbine;
  4. using NEIntelligentControl2.Views.Windturbine;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Dynamic;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Shapes;
  21. namespace NEIntelligentControl2.Windows
  22. {
  23. /// <summary>
  24. /// 参数对比页面
  25. /// </summary>
  26. public partial class ParameterComparisonWindow : Window
  27. {
  28. public List<WindturbineInfo> WindturbineInfos { get; set; }
  29. private List<ParameterInfo> _ParameterInfos;
  30. private InfoManager _InfoManager;
  31. private List<CheckBox> _TitleCheckBoxs;
  32. private List<ExpandoObject> _Datas;
  33. private List<string> _Names;
  34. private Dictionary<string, List<ParameterInfo>> _WindturbineParameterInfos;
  35. private Dictionary<string, List<ComparisonTagInfo>> _TagInfos;
  36. private Dictionary<string, List<ComparisonTagInfo>> _NameTagInfos;
  37. private List<ComparisonTagInfo> _ComparisonTagInfos;
  38. private bool _IsActive;
  39. public ParameterComparisonWindow(InfoManager im)
  40. {
  41. InitializeComponent();
  42. _InfoManager = im;
  43. }
  44. /// <summary>
  45. /// 初始化信息
  46. /// </summary>
  47. private void InitInfos()
  48. {
  49. if (WindturbineInfos == null) return;
  50. var value = WindturbineInfos;
  51. _ParameterInfos = new List<ParameterInfo>();
  52. _TitleCheckBoxs = new List<CheckBox>();
  53. _Names = new List<string>();
  54. if (value == null || value.Count <= 0) return;
  55. HashSet<string> keys = new HashSet<string>();
  56. foreach (var v in value)
  57. {
  58. var id = $"{v.StationId}{v.ModelId}";
  59. if (keys.Contains(id)) continue;
  60. keys.Add(id);
  61. var pis = _InfoManager.GetParameterInfos(v.StationId, v.ModelId);
  62. foreach (var pi in pis)
  63. {
  64. if (_Names.Contains(pi.Name)) continue;
  65. _Names.Add(pi.Name);
  66. CheckBox cb = new CheckBox() { Content = pi.Name, Foreground = new SolidColorBrush(Colors.WhiteSmoke), Margin = new Thickness(3), FontSize = 14, IsChecked = true, Tag = pi.Name };
  67. System.Windows.Shell.WindowChrome.SetIsHitTestVisibleInChrome(cb, true);
  68. cb.Click += CheckBox_Click;
  69. _TitleCheckBoxs.Add(cb);
  70. _WPTitle.Children.Add(cb);
  71. }
  72. }
  73. InitWindturbine();
  74. }
  75. private void InitWindturbine()
  76. {
  77. _WindturbineParameterInfos = new Dictionary<string, List<ParameterInfo>>();
  78. _TagInfos = new Dictionary<string, List<ComparisonTagInfo>>();
  79. _NameTagInfos = new Dictionary<string, List<ComparisonTagInfo>>();
  80. _ComparisonTagInfos = new List<ComparisonTagInfo>();
  81. _Datas = new List<ExpandoObject>();
  82. foreach (var v in WindturbineInfos)
  83. {
  84. dynamic item = new ExpandoObject();
  85. item.Name = v.Name;
  86. item.WindturbineId = v.WindturbineId;
  87. ComparisonInfo ci = new ComparisonInfo() { Name = v.Name };
  88. item.ComparisonInfo = ci;
  89. IDictionary<string, object> itemc = item as IDictionary<string, object>;
  90. var pis = _InfoManager.GetParameterInfos(v.StationId, v.ModelId);
  91. _WindturbineParameterInfos.Add(v.WindturbineId, pis);
  92. Dictionary<string, int> indexs = new Dictionary<string, int>();
  93. foreach (var pi in pis)
  94. {
  95. int index = _Names.IndexOf(pi.Name);
  96. if (index < 0) continue;
  97. var vname = $"Val_{index}";
  98. if (!itemc.ContainsKey(vname))
  99. {
  100. var cif = new ComparisonTagInfo() { Code = pi.Code, Name = pi.Name };
  101. _ComparisonTagInfos.Add(cif);
  102. if (!_TagInfos.ContainsKey(v.WindturbineId))
  103. {
  104. _TagInfos.Add(v.WindturbineId, new List<ComparisonTagInfo>());
  105. }
  106. if (!_NameTagInfos.ContainsKey(pi.Name))
  107. {
  108. _NameTagInfos.Add(pi.Name, new List<ComparisonTagInfo>());
  109. }
  110. _TagInfos[v.WindturbineId].Add(cif);
  111. _NameTagInfos[pi.Name].Add(cif);
  112. itemc.Add(vname, cif);
  113. indexs.Add(pi.Code, index);
  114. }
  115. }
  116. ci.Infos = indexs;
  117. _Datas.Add(item);
  118. }
  119. var dgis = FindResource("DataGridItemStyle") as DataTemplate;
  120. int ind = 0;
  121. foreach (var v in _Names)
  122. {
  123. //var dg = new DataGridTemplateColumn() { Header = v, CellTemplate = dgis, MinWidth = 100 };
  124. var dg = new DataGridTemplateColumn() { MinWidth = 100, IsReadOnly = true };
  125. dg.Header = new ComparisonHeader() { Header = v, OnClick = HeaderClick };
  126. Binding binding = new Binding()
  127. {
  128. Path = new PropertyPath($"Val_{ind}")
  129. };
  130. DataTemplate dt = new DataTemplate();
  131. var item = new FrameworkElementFactory(typeof(ComparisonTag));
  132. item.SetBinding(ComparisonTag.InfoProperty, binding);
  133. dt.VisualTree = item;
  134. dg.CellTemplate = dt;
  135. _DGMain.Columns.Add(dg);
  136. ++ind;
  137. }
  138. _DGMain.ItemsSource = _Datas;
  139. }
  140. private void HeaderClick(string obj)
  141. {
  142. var vs = _ComparisonTagInfos.Where(i => i.Name == obj).ToList();
  143. foreach(var v in vs)
  144. {
  145. v.IsShowProgress = !v.IsShowProgress;
  146. }
  147. }
  148. public static void ShowWindow(List<Models.Windturbine.WindturbineInfo> ws)
  149. {
  150. ParameterComparisonWindow pcw = App.ServiceProvider.GetService(typeof(ParameterComparisonWindow)) as ParameterComparisonWindow;
  151. pcw.WindturbineInfos = ws;
  152. pcw.Owner = App.Current.MainWindow;
  153. pcw.ShowDialog();
  154. }
  155. private void Button_Click(object sender, RoutedEventArgs e)
  156. {
  157. switch (((Control)sender).Tag)
  158. {
  159. case "close":
  160. this.Close();
  161. break;
  162. }
  163. }
  164. private void CheckBox_Click(object sender, RoutedEventArgs e)
  165. {
  166. var tag = ((Control)sender).Tag as string;
  167. if (tag == "all")
  168. {
  169. var isChecked = (sender as CheckBox).IsChecked;
  170. foreach (var v in _TitleCheckBoxs)
  171. {
  172. v.IsChecked = isChecked;
  173. }
  174. }
  175. var ls = _TitleCheckBoxs.Where(c => c.IsChecked == true).Select(c => c.Tag as string).ToList();
  176. foreach (var v in _DGMain.Columns)
  177. {
  178. string s = v.Header as string;
  179. if (s == null)
  180. {
  181. s = (v.Header as ComparisonHeader)?.Header;
  182. }
  183. if (s == "风机名称" || ls.Contains(s))
  184. {
  185. v.Visibility = Visibility.Visible;
  186. }
  187. else
  188. {
  189. v.Visibility = Visibility.Collapsed;
  190. }
  191. }
  192. }
  193. private void Window_Loaded(object sender, RoutedEventArgs e)
  194. {
  195. InitInfos();
  196. _IsActive = true;
  197. Task.Run(RefreshData);
  198. }
  199. private async void RefreshData()
  200. {
  201. while (_IsActive)
  202. {
  203. try
  204. {
  205. foreach (var v in _WindturbineParameterInfos)
  206. {
  207. var ucs = v.Value.Select(x => x.Code).ToArray();
  208. var vv = _InfoManager.GetPointDatas(v.Key, ucs);
  209. UpdateData(v.Key, vv); ;
  210. }
  211. InitMaxValue();
  212. }
  213. catch { }
  214. await Task.Delay(1500);
  215. }
  216. }
  217. private void InitMaxValue()
  218. {
  219. foreach(var v in _NameTagInfos)
  220. {
  221. var max = v.Value.Max(i => i.Value);
  222. foreach(var vv in v.Value)
  223. {
  224. vv.MaxValue = max;
  225. }
  226. }
  227. }
  228. private void UpdateData(string key, Dictionary<string, TsData> vv)
  229. {
  230. if (!_TagInfos.ContainsKey(key)) return;
  231. var vs = _TagInfos[key];
  232. foreach (var v in vs)
  233. {
  234. if (vv.ContainsKey(v.Code))
  235. {
  236. v.Value = Math.Round(vv[v.Code].Value, 2);
  237. }
  238. }
  239. }
  240. private void Window_Closing(object sender, CancelEventArgs e)
  241. {
  242. _IsActive = false;
  243. }
  244. }
  245. class ComparisonInfo
  246. {
  247. public string Name { get; set; }
  248. public Dictionary<string, int> Infos { get; set; }
  249. }
  250. public class ComparisonTagInfo : INotifyPropertyChanged
  251. {
  252. private double value;
  253. public double Value { get => value; set { this.value = value; Progress = Value / MaxValue; OnPropertyChanged(); } }
  254. private double maxValue;
  255. public double MaxValue { get => maxValue; set { maxValue = value; Progress = Value / MaxValue; OnPropertyChanged(); } }
  256. private bool isShowProgress;
  257. public bool IsShowProgress { get=> isShowProgress; set { isShowProgress = value; OnPropertyChanged(); } }
  258. public string Code { get; set; }
  259. private double progress;
  260. public double Progress { get => progress; set { progress = value; OnPropertyChanged(); } }
  261. public string Name { get; set; }
  262. public event PropertyChangedEventHandler PropertyChanged;
  263. protected void OnPropertyChanged([CallerMemberName] string name = null)
  264. {
  265. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  266. }
  267. }
  268. }