ThumbnailView.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using NEIntelligentControl2.Models.AGC;
  2. using NEIntelligentControl2.Models.Datas;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Service.WebSocket;
  5. using SVGViewer.Models.Info;
  6. using SVGViewer.Views;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Security.Policy;
  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.Animation;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Views.BoostStation
  24. {
  25. /// <summary>
  26. /// 升压站缩略图视图
  27. /// </summary>
  28. public partial class ThumbnailView : UserControl
  29. {
  30. public static readonly DependencyProperty IsAlarmProperty = DependencyProperty.Register("IsAlarm", typeof(bool), typeof(ThumbnailView), new PropertyMetadata(IsAlarmChanged));
  31. public bool IsAlarm { get => (bool)GetValue(IsAlarmProperty); set => SetValue(IsAlarmProperty, value); }
  32. public Action<string> DoubleClicked { get; set; }
  33. private bool _IsLoaded;
  34. private ShapeInfo shapeInfo;
  35. private Dictionary<string, ShapeInfo> customizeShapes;
  36. private WEBHelper _WEBHelper;
  37. private UrlManager _UrlManager;
  38. /// <summary>
  39. /// 数据地址
  40. /// </summary>
  41. private string _Url = "";
  42. private Dictionary<string, List<SVGViewer.Models.Info.TagInfo>> _TagInfos = new Dictionary<string, List<SVGViewer.Models.Info.TagInfo>>();
  43. public ThumbnailView(ShapeInfo v, Dictionary<string, ShapeInfo> customizeShapes)
  44. {
  45. InitializeComponent();
  46. shapeInfo = v;
  47. this.customizeShapes = customizeShapes;
  48. _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;
  49. _UrlManager = App.ServiceProvider.GetService(typeof(UrlManager)) as UrlManager;
  50. Init();
  51. }
  52. private void Init()
  53. {
  54. var sv = new SVGCanvas(shapeInfo, customizeShapes, false);
  55. _vb.Child = sv;
  56. _TBTitle.Text = shapeInfo.Name;
  57. var taginfos = shapeInfo.GetAllTagInfos();
  58. _TagInfos = taginfos.GroupBy(v => v.Tag).ToDictionary(v => v.Key, v => v.ToList());
  59. var keys = string.Join(",", _TagInfos.Keys);
  60. _Url += $"{_UrlManager.DataServicePath}/ts/latest?keys={keys}";
  61. }
  62. private static void IsAlarmChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  63. {
  64. if (e.NewValue == e.OldValue) return;
  65. var tv = d as ThumbnailView;
  66. if ((bool)e.NewValue)
  67. {
  68. SolidColorBrush sb = new SolidColorBrush();
  69. ColorAnimation ca = new ColorAnimation();
  70. ca.From = Color.FromRgb(0xFF, 0x71, 0x71);
  71. ca.To = Color.FromRgb(0xDB, 0x33, 0x33);
  72. ca.AutoReverse = true;
  73. ca.RepeatBehavior = RepeatBehavior.Forever;
  74. ca.Duration = new Duration(TimeSpan.FromSeconds(0.5));
  75. sb.BeginAnimation(SolidColorBrush.ColorProperty, ca);
  76. tv._bd.Background = sb;
  77. }
  78. else
  79. {
  80. tv._bd.Background = new SolidColorBrush(Color.FromRgb(0x2B, 0x77, 0xDC));
  81. }
  82. }
  83. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  84. {
  85. _IsLoaded = true;
  86. Task.Factory.StartNew(RefreshData);
  87. }
  88. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  89. {
  90. _IsLoaded = false;
  91. }
  92. private async void RefreshData()
  93. {
  94. while (_IsLoaded)
  95. {
  96. try
  97. {
  98. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(_Url);
  99. if (vs != null && vs.Count > 0)
  100. {
  101. Dispatcher?.Invoke(() =>
  102. {
  103. foreach (var v in vs)
  104. {
  105. if (!_TagInfos.ContainsKey(v.Key)) continue;
  106. var tgs = _TagInfos[v.Key];
  107. foreach (var tg in tgs)
  108. {
  109. tg.Value = v.Value.Value;
  110. }
  111. }
  112. });
  113. }
  114. Console.WriteLine($"刷新:{shapeInfo.Id}");
  115. }
  116. catch { }
  117. await Task.Delay(2000);
  118. }
  119. }
  120. private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  121. {
  122. if (e.ClickCount < 2 || DoubleClicked == null) return;
  123. DoubleClicked.Invoke(shapeInfo.Id);
  124. }
  125. }
  126. }