PageStation.xaml.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.BoostStation;
  4. using NEIntelligentControl2.Models.Datas;
  5. using NEIntelligentControl2.Models.Messages;
  6. using NEIntelligentControl2.Service.User;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Pages.BoostStation
  24. {
  25. /// <summary>
  26. /// 升压站页面
  27. /// </summary>
  28. public partial class PageStation : Page
  29. {
  30. public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(int), typeof(PageStation));
  31. public static readonly DependencyProperty DataTagProperty = DependencyProperty.Register("DataTag", typeof(string), typeof(PageStation));
  32. public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register("Coordinate", typeof(Point), typeof(PageStation));
  33. public static readonly DependencyProperty IsEditModelProperty = DependencyProperty.Register("IsEditModel", typeof(bool), typeof(PageStation));
  34. public static readonly DependencyProperty IsNegateProperty = DependencyProperty.Register("IsNegate", typeof(bool), typeof(PageStation));
  35. /// <summary>
  36. /// 控件方向
  37. /// </summary>
  38. public int Angle { get => (int)GetValue(AngleProperty); set => SetValue(AngleProperty, value); }
  39. /// <summary>
  40. /// 数据标签
  41. /// </summary>
  42. public string DataTag { get => GetValue(DataTagProperty) as string; set => SetValue(DataTagProperty, value); }
  43. /// <summary>
  44. /// 控件坐标
  45. /// </summary>
  46. public Point Coordinate { get => (Point)GetValue(CoordinateProperty); set => SetValue(CoordinateProperty, value); }
  47. /// <summary>
  48. /// 是否取反
  49. /// </summary>
  50. public bool IsNegate { get => (bool)GetValue(IsNegateProperty); set => SetValue(IsNegateProperty, value); }
  51. private Control _CurrentControl;
  52. private Control _TempleteControl;
  53. private bool _IsSelectAction;
  54. public BoostStationInfo StationInfo { get; set; }
  55. /// <summary>
  56. /// 保存动作
  57. /// </summary>
  58. public Action<BoostStationInfo> SaveAction { get; set; }
  59. /// <summary>
  60. /// 是否被编辑
  61. /// </summary>
  62. private bool IsEdited;
  63. /// <summary>
  64. /// 是否继续刷新数据
  65. /// </summary>
  66. private bool _IsRefreshData;
  67. /// <summary>
  68. /// 所有数据点
  69. /// </summary>
  70. private List<IBoostStationPoint> _BoostStationPoints;
  71. /// <summary>
  72. /// 数据地址
  73. /// </summary>
  74. private string _Url = "";
  75. /// <summary>
  76. /// 是否是编辑模式
  77. /// </summary>
  78. public bool IsEditModel { get => (bool)GetValue(IsEditModelProperty); set => SetValue(IsEditModelProperty, value); }
  79. /// <summary>
  80. /// 服务地址
  81. /// </summary>
  82. private string _InfoUrl = "";
  83. /// <summary>
  84. /// 单击时间
  85. /// </summary>
  86. private DateTime _ClickTime;
  87. private string _BackgroundOld;
  88. private static readonly object locker = new object();
  89. private UserManager _UserManager;
  90. private WEBHelper _WEBHelper;
  91. public PageStation(UserManager um, WEBHelper web)
  92. {
  93. InitializeComponent();
  94. _UserManager = um;
  95. _WEBHelper = web;
  96. try
  97. {
  98. #if (DEBUG)
  99. _InfoUrl = ConfigurationManager.AppSettings["ServicePathDebug"];
  100. #else
  101. _InfoUrl = ConfigurationManager.AppSettings["ServicePath"];
  102. #endif
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine("读取配置文件[ServicePath]出错!", ex);
  107. }
  108. }
  109. private void Init()
  110. {
  111. _BoostStationPoints = new List<IBoostStationPoint>();
  112. try
  113. {
  114. if (!string.IsNullOrWhiteSpace(StationInfo.Background))
  115. {
  116. BitmapImage image = GetBackgroundImage(StationInfo.Background);
  117. _Canvas.Background = new ImageBrush(image) { Stretch = Stretch.Uniform };
  118. }
  119. }
  120. catch { }
  121. _Canvas.Children.Clear();
  122. foreach (var v in StationInfo.BoostStationPointInfos)
  123. {
  124. try
  125. {
  126. var obj = Activator.CreateInstance(Type.GetType($"NEIntelligentControl2.Views.BoostStation.{v.Type}")) as IBoostStationPoint;
  127. if (obj == null)
  128. {
  129. continue;
  130. }
  131. _BoostStationPoints.Add(obj);
  132. obj.DataTag = v.DataTag;
  133. obj.Proportion = v.Proportion;
  134. obj.Info = v.Info;
  135. obj.IsNegate = v.IsNegate;
  136. var ct = obj as Control;
  137. Canvas.SetLeft(ct, v.Coordinate.X);
  138. Canvas.SetTop(ct, v.Coordinate.Y);
  139. ct.RenderTransform = GetTransform(v);
  140. _Canvas.Children.Add(ct);
  141. }
  142. catch { }
  143. }
  144. var keys = string.Join(",", _BoostStationPoints.Where(b => !string.IsNullOrWhiteSpace(b.DataTag)).Select(b => b.DataTag).ToArray());
  145. try
  146. {
  147. #if (DEBUG)
  148. var ul = ConfigurationManager.AppSettings["DataServicePathDebug"];
  149. #else
  150. var ul = ConfigurationManager.AppSettings["DataServicePath"];
  151. #endif
  152. _Url = $"{ul}/ts/latest?keys={keys}";
  153. }
  154. catch (Exception ex)
  155. {
  156. Console.WriteLine("读取配置文件[DataServicePath]出错!", ex);
  157. }
  158. }
  159. private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  160. {
  161. _CurrentControl?.ReleaseMouseCapture();
  162. if (e.OriginalSource != sender) return;
  163. ClosePopup();
  164. }
  165. private void ClosePopup(bool isSelect = false)
  166. {
  167. if (!isSelect)
  168. {
  169. AllNotSelect();
  170. }
  171. if (!_Popup.IsOpen)
  172. {
  173. return;
  174. }
  175. _Popup.IsOpen = false;
  176. var sp = _CurrentControl as IBoostStationPoint;
  177. if (sp == null) return;
  178. sp.DataTag = this.DataTag;
  179. sp.IsNegate = this.IsNegate;
  180. Canvas.SetLeft(_CurrentControl, Coordinate.X);
  181. Canvas.SetTop(_CurrentControl, Coordinate.Y);
  182. _CurrentControl = null;
  183. IsEdited = true;
  184. }
  185. private void AllNotSelect()
  186. {
  187. var vvs = GetSelectedItems();
  188. foreach (var v in vvs)
  189. {
  190. var issp = v as IBoostStationPoint;
  191. if (issp != null)
  192. {
  193. issp.IsSelected = false;
  194. }
  195. }
  196. }
  197. private Transform GetTransform()
  198. {
  199. RotateTransform rt = new RotateTransform();
  200. if (_TempleteControl != null)
  201. {
  202. RotateTransform ort = _TempleteControl.RenderTransform as RotateTransform;
  203. rt.Angle = ort.Angle;
  204. rt.CenterX = ort.CenterX;
  205. rt.CenterY = ort.CenterY;
  206. }
  207. return rt;
  208. }
  209. private Transform GetTransform(Control obj)
  210. {
  211. RotateTransform rt = new RotateTransform();
  212. rt.Angle = this.Angle;
  213. rt.CenterX = obj.ActualWidth / 2;
  214. rt.CenterY = obj.ActualHeight / 2;
  215. return rt;
  216. }
  217. private Transform GetTransform(BoostStationPointInfo v)
  218. {
  219. RotateTransform rt = new RotateTransform();
  220. rt.Angle = v.Angle;
  221. rt.CenterX = v.CenterCoordinate.X;
  222. rt.CenterY = v.CenterCoordinate.Y;
  223. return rt;
  224. }
  225. /// <summary>
  226. /// 小控件被单击
  227. /// </summary>
  228. private void PointClicked(Control c)
  229. {
  230. if ((c as IBoostStationPoint).IsTemplete)
  231. {
  232. TempleteClicked();
  233. }
  234. else
  235. {
  236. SelectClicked(c);
  237. }
  238. }
  239. private void SelectClicked(Control c)
  240. {
  241. _ClickTime = DateTime.Now;
  242. if (_Popup.IsOpen)
  243. {
  244. ClosePopup(true);
  245. }
  246. _CurrentControl = c;
  247. _CurrentControl.CaptureMouse();
  248. Coordinate = new Point(Canvas.GetLeft(c), Canvas.GetTop(c));
  249. DataTag = (c as IBoostStationPoint).DataTag;
  250. IsNegate = (c as IBoostStationPoint).IsNegate;
  251. _Popup.IsOpen = true;
  252. _LBISelect.Focus();
  253. }
  254. private void TempleteClicked()
  255. {
  256. var obj = Activator.CreateInstance(_TempleteControl.GetType()) as Control;
  257. if (obj == null) return;
  258. (obj as IBoostStationPoint).Clicked = PointClicked;
  259. (obj as IBoostStationPoint).IsEditModel = true;
  260. (obj as IBoostStationPoint).Proportion = (_TempleteControl as IBoostStationPoint).Proportion;
  261. Canvas.SetLeft(obj, Canvas.GetLeft(_TempleteControl));
  262. Canvas.SetTop(obj, Canvas.GetTop(_TempleteControl));
  263. obj.RenderTransform = GetTransform();
  264. _Canvas.Children.Add(obj);
  265. IsEdited = true;
  266. }
  267. private void Canvas_MouseMove(object sender, MouseEventArgs e)
  268. {
  269. if (_IsSelectAction)
  270. {
  271. if (e.LeftButton != MouseButtonState.Pressed || e.OriginalSource == _Canvas || (DateTime.Now - _ClickTime).TotalMilliseconds < 300) return;
  272. MovePoint(_CurrentControl, e);
  273. _Popup.IsOpen = false;
  274. }
  275. else if (_TempleteControl != null)
  276. {
  277. MovePoint(_TempleteControl, e);
  278. }
  279. }
  280. private void _Canvas_MouseLeave(object sender, MouseEventArgs e)
  281. {
  282. if (_TempleteControl == null) return;
  283. _TempleteControl.Visibility = Visibility.Collapsed;
  284. if (_Popup.IsOpen)
  285. {
  286. _Popup.IsOpen = false;
  287. }
  288. }
  289. private void _Canvas_MouseEnter(object sender, MouseEventArgs e)
  290. {
  291. if (_TempleteControl == null) return;
  292. _TempleteControl.Visibility = Visibility.Visible;
  293. }
  294. private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
  295. {
  296. string action = ((Control)sender).Tag as string;
  297. _IsSelectAction = action == "Select";
  298. if (_TempleteControl != null)
  299. {
  300. _Canvas.Children.Remove(_TempleteControl);
  301. }
  302. if (_IsSelectAction)
  303. {
  304. _CurrentControl = null;
  305. _TempleteControl?.ReleaseMouseCapture();
  306. _TempleteControl = null;
  307. }
  308. else
  309. {
  310. _TempleteControl?.CaptureMouse();
  311. }
  312. var ct = Type.GetType($"NEIntelligentControl2.Views.BoostStation.{action}");
  313. if (ct == null) return;
  314. var obj = Activator.CreateInstance(ct);
  315. if (!(obj is IBoostStationPoint)) return;
  316. (obj as IBoostStationPoint).IsTemplete = true;
  317. (obj as IBoostStationPoint).IsEditModel = true;
  318. (obj as IBoostStationPoint).Clicked = PointClicked;
  319. _TempleteControl = obj as Control;
  320. _TempleteControl.Loaded += PointLoaded;
  321. _Canvas.Children.Add(obj as Control);
  322. }
  323. private void PointLoaded(object sender, RoutedEventArgs e)
  324. {
  325. Control c = sender as Control;
  326. if (c == null) return;
  327. c.RenderTransform = GetTransform(c);
  328. }
  329. private void MovePoint(FrameworkElement c, MouseEventArgs e)
  330. {
  331. if (c == null) return;
  332. Point p = e.GetPosition(_Canvas);
  333. Canvas.SetTop(c, p.Y - c.ActualHeight / 2);
  334. Canvas.SetLeft(c, p.X - c.ActualWidth / 2);
  335. }
  336. private void Button_Click(object sender, RoutedEventArgs e)
  337. {
  338. switch (((Control)sender).Tag)
  339. {
  340. case "angle":// 角度
  341. AngleChange();
  342. break;
  343. case "background":// 选择背景图片
  344. ChoseBackground();
  345. break;
  346. case "delete":// 删除
  347. DeletePoint();
  348. break;
  349. case "save":// 保存
  350. Save();
  351. break;
  352. case "close":// 放弃保存
  353. Close();
  354. break;
  355. case "narrow":// 缩小控件
  356. Narrow();
  357. break;
  358. case "enlarge":// 放大控件
  359. Enlarge();
  360. break;
  361. default: return;
  362. }
  363. }
  364. /// <summary>
  365. /// 放大控件
  366. /// </summary>
  367. private void Enlarge()
  368. {
  369. if (_CurrentControl != null)
  370. {
  371. (_CurrentControl as IBoostStationPoint).Zoom(1);
  372. }
  373. if (_TempleteControl != null)
  374. {
  375. (_TempleteControl as IBoostStationPoint).Zoom(1);
  376. }
  377. }
  378. /// <summary>
  379. /// 缩小控件
  380. /// </summary>
  381. private void Narrow()
  382. {
  383. if (_CurrentControl != null)
  384. {
  385. (_CurrentControl as IBoostStationPoint).Zoom(-1);
  386. }
  387. if (_TempleteControl != null)
  388. {
  389. (_TempleteControl as IBoostStationPoint).Zoom(-1);
  390. }
  391. }
  392. private void Close()
  393. {
  394. var v = MessageBox.Show("放弃保存?", "提示", MessageBoxButton.YesNo);
  395. if (v != MessageBoxResult.Yes) return;
  396. if (StationInfo.Background != _BackgroundOld)
  397. {
  398. StationInfo.Background = _BackgroundOld;
  399. BitmapImage image = GetBackgroundImage(StationInfo.Background);
  400. _Canvas.Background = new ImageBrush(image) { Stretch = Stretch.Uniform };
  401. }
  402. ToNormalModel();
  403. }
  404. /// <summary>
  405. /// 回到正常模式
  406. /// </summary>
  407. private void ToNormalModel()
  408. {
  409. _ToolPane.Visibility = Visibility.Collapsed;
  410. IsEdited = false;
  411. IsEditModel = false;
  412. _Popup.IsOpen = false;
  413. lock (locker)
  414. {
  415. Init();
  416. }
  417. }
  418. /// <summary>
  419. /// 保存
  420. /// </summary>
  421. private void Save()
  422. {
  423. try
  424. {
  425. if (!IsEdited)
  426. {
  427. ToNormalModel();
  428. return;
  429. }
  430. if (_TempleteControl == null)
  431. {
  432. _Canvas.Children.Remove(_TempleteControl);
  433. _TempleteControl = null;
  434. }
  435. StationInfo.BoostStationPointInfos = GetBoostStationPointInfo();
  436. StationInfo.UpdatedDate = DateTime.Now.Ticks;
  437. SaveAction?.Invoke(StationInfo);
  438. ToNormalModel();
  439. }
  440. catch (Exception e) { Console.WriteLine(e.ToString()); }
  441. }
  442. /// <summary>
  443. /// 获取所有控件信息
  444. /// </summary>
  445. /// <returns></returns>
  446. private List<BoostStationPointInfo> GetBoostStationPointInfo()
  447. {
  448. List<BoostStationPointInfo> bspis = new List<BoostStationPointInfo>();
  449. foreach (var v in _Canvas.Children)
  450. {
  451. if (!(v is IBoostStationPoint))
  452. {
  453. continue;
  454. }
  455. var point = v as IBoostStationPoint;
  456. var ct = v as Control;
  457. BoostStationPointInfo bspi = new BoostStationPointInfo();
  458. bspi.Type = v.GetType().Name;
  459. bspi.DataTag = point.DataTag;
  460. bspi.Proportion = point.Proportion;
  461. bspi.Info = point.Info;
  462. bspi.IsNegate = point.IsNegate;
  463. RotateTransform rotate = ct.RenderTransform as RotateTransform;
  464. if (rotate != null)
  465. {
  466. bspi.CenterCoordinate = new Point(rotate.CenterX, rotate.CenterY);
  467. bspi.Angle = (int)rotate.Angle;
  468. }
  469. Point pt = new Point(Canvas.GetLeft(ct), Canvas.GetTop(ct));
  470. bspi.Coordinate = pt;
  471. bspis.Add(bspi);
  472. }
  473. return bspis;
  474. }
  475. /// <summary>
  476. /// 更改角度
  477. /// </summary>
  478. private void AngleChange()
  479. {
  480. Angle = Angle >= 270 ? 0 : Angle + 90;
  481. if (_CurrentControl != null)
  482. {
  483. _CurrentControl.RenderTransform = GetTransform(_CurrentControl);
  484. }
  485. if (_TempleteControl != null)
  486. {
  487. var tr = _TempleteControl.RenderTransform as RotateTransform;
  488. tr.Angle = this.Angle;
  489. _TempleteControl.RenderTransform = tr;
  490. }
  491. var vs = GetSelectedItems();
  492. foreach (var v in vs)
  493. {
  494. v.RenderTransform = GetTransform(v);
  495. }
  496. IsEdited = true;
  497. }
  498. /// <summary>
  499. /// 删除标签点
  500. /// </summary>
  501. private void DeletePoint()
  502. {
  503. List<Control> list = GetSelectedItems();
  504. if (list.Count <= 0) return;
  505. foreach (var v in list)
  506. {
  507. _Canvas.Children.Remove(v);
  508. }
  509. IsEdited = true;
  510. }
  511. private List<Control> GetSelectedItems()
  512. {
  513. List<Control> ls = new List<Control>();
  514. foreach (var v in _Canvas.Children)
  515. {
  516. var c = v as IBoostStationPoint;
  517. if (c == null || !c.IsSelected) continue;
  518. ls.Add(c as Control);
  519. }
  520. return ls;
  521. }
  522. private void ChoseBackground()
  523. {
  524. var dl = new OpenFileDialog();
  525. dl.Filter = "图片(.png)|*.png";
  526. dl.Title = "选择背景图片";
  527. var b = dl.ShowDialog();
  528. if (b == null || b == false) return;
  529. try
  530. {
  531. _BackgroundOld = StationInfo.Background;
  532. StationInfo.Background = GetBackgroundByFile(dl.FileName);
  533. BitmapImage image = GetBackgroundImage(StationInfo.Background);
  534. _Canvas.Background = new ImageBrush(image) { Stretch = Stretch.Uniform };
  535. IsEdited = true;
  536. }
  537. catch (Exception ex)
  538. {
  539. Console.WriteLine(ex.Message);
  540. }
  541. }
  542. /// <summary>
  543. /// 从文件中获取背景图片文字
  544. /// </summary>
  545. /// <exception cref="NotImplementedException"></exception>
  546. private string GetBackgroundByFile(string url)
  547. {
  548. var bs = File.ReadAllBytes(url);
  549. return Convert.ToBase64String(bs);
  550. }
  551. private BitmapImage GetBackgroundImage(string v)
  552. {
  553. if (string.IsNullOrWhiteSpace(v)) return null;
  554. BitmapImage bt = new BitmapImage();
  555. bt.BeginInit();
  556. bt.CacheOption = BitmapCacheOption.OnLoad;
  557. var bs = Convert.FromBase64String(v);
  558. using (Stream stream = new MemoryStream(bs))
  559. {
  560. bt.StreamSource = stream;
  561. bt.EndInit();
  562. bt.Freeze();
  563. }
  564. return bt;
  565. }
  566. private void MenuItem_Click(object sender, RoutedEventArgs e)
  567. {
  568. switch (((Control)sender).Tag)
  569. {
  570. case "edit":// 编辑
  571. EditModel();
  572. break;
  573. }
  574. }
  575. /// <summary>
  576. /// 开启编辑模式
  577. /// </summary>
  578. private void EditModel()
  579. {
  580. if (!_UserManager.IsLogined || _UserManager.User.Level != Models.User.UserLevel.Administrator)
  581. {
  582. MessageBox.Show("请登录管理员账号进入编辑模式!", "提示:", MessageBoxButton.OK, MessageBoxImage.Warning);
  583. return;
  584. }
  585. _BackgroundOld = StationInfo.Background;
  586. IsEditModel = true;
  587. _ToolPane.Visibility = Visibility.Visible;
  588. foreach (var v in _Canvas.Children)
  589. {
  590. var bsp = v as IBoostStationPoint;
  591. if (bsp == null) continue;
  592. bsp.Clicked = PointClicked;
  593. bsp.IsEditModel = true;
  594. }
  595. }
  596. private void Page_Loaded(object sender, RoutedEventArgs e)
  597. {
  598. Init();
  599. this._IsRefreshData = true;
  600. Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
  601. Task.Factory.StartNew(CheckUpdate, TaskCreationOptions.LongRunning);
  602. }
  603. /// <summary>
  604. /// 检查更新
  605. /// </summary>
  606. private async void CheckUpdate()
  607. {
  608. while (_IsRefreshData)
  609. {
  610. try
  611. {
  612. var dtstr = _WEBHelper.HttpGetString($"{_InfoUrl}/api/station/boost_station/update_time/{StationInfo.Id}");
  613. long.TryParse(dtstr, out long dt);
  614. if (dt > 0 && dt != StationInfo.UpdatedDate)
  615. {
  616. var info = _WEBHelper.HttpGetJSON<BoostStationInfo>($"{_InfoUrl}/api/station/boost_station/{StationInfo.Id}");
  617. Dispatcher.Invoke(() =>
  618. {
  619. lock (locker)
  620. {
  621. StationInfo = info;
  622. Init();
  623. }
  624. });
  625. }
  626. }
  627. catch { }
  628. await Task.Delay(10000);
  629. }
  630. }
  631. private async void RefreshData()
  632. {
  633. while (_IsRefreshData)
  634. {
  635. try
  636. {
  637. var vs = _WEBHelper.HttpGetJSON<Dictionary<string, TsData>>(_Url);
  638. if (vs != null && vs.Count > 0)
  639. {
  640. Dispatcher?.Invoke(() =>
  641. {
  642. lock (locker)
  643. {
  644. foreach (var v in _BoostStationPoints)
  645. {
  646. if (string.IsNullOrWhiteSpace(v.DataTag))
  647. {
  648. continue;
  649. }
  650. if (vs.ContainsKey(v.DataTag))
  651. {
  652. v.Value = vs[v.DataTag].Value;
  653. }
  654. }
  655. }
  656. });
  657. }
  658. Console.WriteLine($"刷新:{StationInfo.Id}");
  659. }
  660. catch { }
  661. await Task.Delay(2000);
  662. }
  663. }
  664. private void Page_Unloaded(object sender, RoutedEventArgs e)
  665. {
  666. this._IsRefreshData = false;
  667. if (IsEdited)
  668. {
  669. var v = MessageBox.Show("有未保存的改动,是否保存?", "提示:", MessageBoxButton.YesNo);
  670. if (v == MessageBoxResult.Yes)
  671. {
  672. Save();
  673. }
  674. }
  675. if (IsEditModel)
  676. {
  677. ToNormalModel();
  678. }
  679. }
  680. private void _Canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  681. {
  682. if (_Popup.IsOpen)
  683. {
  684. ClosePopup();
  685. }
  686. _LBTools.SelectedIndex = 0;
  687. }
  688. private void Direction_Click(object sender, RoutedEventArgs e)
  689. {
  690. if (_CurrentControl == null) return;
  691. int dtop = 0;
  692. int dleft = 0;
  693. switch (((Control)sender).Tag)
  694. {
  695. case "left":
  696. dleft = -1;
  697. break;
  698. case "right":
  699. dleft = 1;
  700. break;
  701. case "up":
  702. dtop = -1;
  703. break;
  704. case "down":
  705. dtop = 1;
  706. break;
  707. default: return;
  708. }
  709. double top = Canvas.GetTop(_CurrentControl) + dtop;
  710. double left = Canvas.GetLeft(_CurrentControl) + dleft;
  711. Coordinate = new Point(left, top);
  712. Canvas.SetTop(_CurrentControl, top);
  713. Canvas.SetLeft(_CurrentControl, left);
  714. }
  715. }
  716. }