DeviceMangerViewModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Avalonia.Controls;
  2. using Shaker.Model;
  3. using ShakerApp.Tools;
  4. using SukiUI.Toasts;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics.CodeAnalysis;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Input;
  15. namespace ShakerApp.ViewModels
  16. {
  17. public class DeviceMangerViewModel : ViewModelBase<Shaker.Model.IModel>
  18. {
  19. public override bool CanResize => false;
  20. public override double Width => 960;
  21. public override double Height => 660;
  22. private object locker = new object();
  23. private DeviceMangerViewModel()
  24. {
  25. ButtonVisibily = false;
  26. Content = typeof(Views.DeviceMangerView);
  27. }
  28. static DeviceMangerViewModel()
  29. {
  30. }
  31. public static DeviceMangerViewModel Instance { get; } = new DeviceMangerViewModel();
  32. [AllowNull]
  33. private DeviceInfoViewModel? currentDevice;
  34. public ObservableCollection<IndexValueItemViewModel<DeviceInfoViewModel>> Devices { get; } = new ObservableCollection<IndexValueItemViewModel<DeviceInfoViewModel>>();
  35. public ICommand SearchDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(() =>
  36. {
  37. SearchEnabled = false;
  38. Task.Run(() => SearchDevice());
  39. });
  40. public bool SearchEnabled { get => searchEnabled; set => SetProperty(ref searchEnabled, value); }
  41. private CancellationTokenSource source = new CancellationTokenSource();
  42. private void WaitMsg(int timeout, CancellationTokenSource source)
  43. {
  44. Task.Run(async () =>
  45. {
  46. await Task.Delay(timeout, source.Token);
  47. source.Cancel();
  48. });
  49. }
  50. private async void SearchDevice()
  51. {
  52. int timeout = 10000;
  53. Tools.DispatherInovke.Inovke(() =>
  54. {
  55. Devices.Clear();
  56. CurrentDevice = null;
  57. });
  58. DateTime start = DateTime.Now;
  59. System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(Topic.DISCOVERYPORT - 1);
  60. source = new CancellationTokenSource();
  61. SendBroadcastMsg();
  62. WaitMsg(timeout, source);
  63. while (true)
  64. {
  65. try
  66. {
  67. var result = await udpClient.ReceiveAsync(source.Token);
  68. if (source.IsCancellationRequested) break;
  69. try
  70. {
  71. var deviceinfo = result.Buffer.GetValue<ResultDeviceModel>();
  72. if (deviceinfo != null && deviceinfo.Success)
  73. {
  74. Tools.DispatherInovke.Inovke(() =>
  75. {
  76. lock (locker)
  77. {
  78. Devices.Add(new IndexValueItemViewModel<DeviceInfoViewModel>(Devices.Count + 1, new DeviceInfoViewModel(deviceinfo.DeviceInfoModel)));
  79. }
  80. });
  81. }
  82. }
  83. catch
  84. {
  85. }
  86. if ((DateTime.Now - start).Microseconds >= timeout) break;
  87. }
  88. catch
  89. {
  90. break;
  91. }
  92. }
  93. udpClient.Close();
  94. Tools.DispatherInovke.Inovke(() =>
  95. {
  96. SearchEnabled = true;
  97. if (Devices.Count == 0)
  98. {
  99. ToastManager.CreateSimpleInfoToast()
  100. .WithTitle(App.Current?.FindResource("PromptTitle") + "")
  101. .WithContent(App.Current?.FindResource("DeviceNoFound") + "")
  102. .Queue();
  103. }
  104. });
  105. }
  106. [AllowNull]
  107. private bool searchEnabled = true;
  108. public ICommand ConnectDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(ConnectDevice);
  109. private void ConnectDevice()
  110. {
  111. if (CurrentDevice == null) return;
  112. CommunicationViewModel.Intance.Connect(CurrentDevice.IP, CurrentDevice.Port);
  113. if(CommunicationViewModel.Intance.LocalIsConnect)
  114. {
  115. CloseWindowAction?.Invoke();
  116. }
  117. }
  118. public ICommand DisConnectDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(DisConnectDevice);
  119. private void DisConnectDevice()
  120. {
  121. CommunicationViewModel.Intance.DisConnect();
  122. CurrentDevice = null;
  123. }
  124. public DeviceInfoViewModel? CurrentDevice
  125. {
  126. get => currentDevice;
  127. set => SetProperty(ref currentDevice, value);
  128. }
  129. private void SendBroadcastMsg()
  130. {
  131. Task.Run(()=>
  132. {
  133. System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
  134. Shaker.Model.SearchDeviceModel search = new Shaker.Model.SearchDeviceModel();
  135. udpClient.Send(search.GetBytes(), new System.Net.IPEndPoint(IPAddress.Broadcast, Topic.DISCOVERYPORT));
  136. udpClient.Close();
  137. });
  138. }
  139. }
  140. }