PageStation2.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using NEIntelligentControl2.Models.BoostStation;
  2. using NEIntelligentControl2.Models.Datas;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Service.Station;
  5. using NEIntelligentControl2.Service.WebSocket;
  6. using SVGViewer.Models.Info;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Linq;
  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.Navigation;
  21. using System.Windows.Shapes;
  22. namespace NEIntelligentControl2.Pages.BoostStation
  23. {
  24. /// <summary>
  25. /// 升压站页面组态版
  26. /// </summary>
  27. public partial class PageStation2 : Page
  28. {
  29. private ShapeInfo shapeInfo;
  30. private bool _IsLoaded;
  31. /// <summary>
  32. /// 数据地址
  33. /// </summary>
  34. private string _Url = "";
  35. public ShapeInfo ShapeInfo { get => shapeInfo; internal set { shapeInfo = value; InitShape(); } }
  36. private Dictionary<string, List<TagInfo>> _TagInfos = new Dictionary<string, List<TagInfo>>();
  37. private WEBHelper _WEBHelper;
  38. private UrlManager _UrlManager;
  39. private StationManager _StationManager;
  40. public PageStation2(WEBHelper web, UrlManager um, StationManager sm)
  41. {
  42. InitializeComponent();
  43. _WEBHelper = web;
  44. _UrlManager = um;
  45. _StationManager = sm;
  46. }
  47. /// <summary>
  48. /// 初始化图形
  49. /// </summary>
  50. private void InitShape()
  51. {
  52. _svg.InitInfo(shapeInfo, _StationManager.GetCustomizeShapesDic(), false);
  53. _svg.Canvas.SwitchEditer(new BoostStationEventEditer());
  54. var taginfos = shapeInfo.GetAllTagInfos();
  55. _TagInfos = taginfos.GroupBy(v => v.Tag).ToDictionary(v => v.Key, v => v.ToList());
  56. var keys = string.Join(",", _TagInfos.Keys);
  57. _Url += $"{_UrlManager.DataServicePath}/ts/latest?keys={keys}";
  58. }
  59. private void Page_Loaded(object sender, RoutedEventArgs e)
  60. {
  61. _IsLoaded = true;
  62. Task.Factory.StartNew(RefreshData);
  63. }
  64. private async void RefreshData()
  65. {
  66. while (_IsLoaded)
  67. {
  68. try
  69. {
  70. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(_Url);
  71. if (vs != null && vs.Count > 0)
  72. {
  73. Dispatcher?.Invoke(() =>
  74. {
  75. foreach (var v in vs)
  76. {
  77. if (!_TagInfos.ContainsKey(v.Key)) continue;
  78. var tgs = _TagInfos[v.Key];
  79. foreach(var tg in tgs)
  80. {
  81. tg.Value = v.Value.Value;
  82. }
  83. }
  84. });
  85. }
  86. Console.WriteLine($"刷新:{shapeInfo.Id}");
  87. }
  88. catch { }
  89. await Task.Delay(2000);
  90. }
  91. }
  92. private void Page_Unloaded(object sender, RoutedEventArgs e)
  93. {
  94. _IsLoaded = false;
  95. }
  96. }
  97. }