123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using Avalonia.Controls;
- using Shaker.Model;
- using ShakerApp.Tools;
- using SukiUI.Toasts;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace ShakerApp.ViewModels
- {
- public class DeviceMangerViewModel : ViewModelBase<Shaker.Model.IModel>
- {
- public override bool CanResize => false;
- public override double Width => 960;
- public override double Height => 660;
- private object locker = new object();
- private DeviceMangerViewModel()
- {
- ButtonVisibily = false;
- Content = typeof(Views.DeviceMangerView);
- }
- static DeviceMangerViewModel()
- {
- }
- public static DeviceMangerViewModel Instance { get; } = new DeviceMangerViewModel();
- [AllowNull]
- private DeviceInfoViewModel? currentDevice;
- public ObservableCollection<IndexValueItemViewModel<DeviceInfoViewModel>> Devices { get; } = new ObservableCollection<IndexValueItemViewModel<DeviceInfoViewModel>>();
- public ICommand SearchDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(() =>
- {
- SearchEnabled = false;
- Task.Run(() => SearchDevice());
- });
- public bool SearchEnabled { get => searchEnabled; set => SetProperty(ref searchEnabled, value); }
- private CancellationTokenSource source = new CancellationTokenSource();
- private void WaitMsg(int timeout, CancellationTokenSource source)
- {
- Task.Run(async () =>
- {
- await Task.Delay(timeout, source.Token);
- source.Cancel();
- });
- }
- private async void SearchDevice()
- {
- int timeout = 10000;
- Tools.DispatherInovke.Inovke(() =>
- {
- Devices.Clear();
- CurrentDevice = null;
- });
- DateTime start = DateTime.Now;
- System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(Topic.DISCOVERYPORT - 1);
- source = new CancellationTokenSource();
- SendBroadcastMsg();
- WaitMsg(timeout, source);
- while (true)
- {
- try
- {
- var result = await udpClient.ReceiveAsync(source.Token);
- if (source.IsCancellationRequested) break;
- try
- {
- var deviceinfo = result.Buffer.GetValue<ResultDeviceModel>();
- if (deviceinfo != null && deviceinfo.Success)
- {
- Tools.DispatherInovke.Inovke(() =>
- {
- lock (locker)
- {
- Devices.Add(new IndexValueItemViewModel<DeviceInfoViewModel>(Devices.Count + 1, new DeviceInfoViewModel(deviceinfo.DeviceInfoModel)));
- }
- });
- }
- }
- catch
- {
- }
- if ((DateTime.Now - start).Microseconds >= timeout) break;
- }
- catch
- {
- break;
- }
- }
- udpClient.Close();
- Tools.DispatherInovke.Inovke(() =>
- {
- SearchEnabled = true;
- if (Devices.Count == 0)
- {
- ToastManager.CreateSimpleInfoToast()
- .WithTitle(App.Current?.FindResource("PromptTitle") + "")
- .WithContent(App.Current?.FindResource("DeviceNoFound") + "")
- .Queue();
- }
- });
- }
- [AllowNull]
- private bool searchEnabled = true;
- public ICommand ConnectDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(ConnectDevice);
- private void ConnectDevice()
- {
- if (CurrentDevice == null) return;
- CommunicationViewModel.Intance.Connect(CurrentDevice.IP, CurrentDevice.Port);
- if(CommunicationViewModel.Intance.LocalIsConnect)
- {
- CloseWindowAction?.Invoke();
- }
- }
- public ICommand DisConnectDeviceCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(DisConnectDevice);
- private void DisConnectDevice()
- {
- CommunicationViewModel.Intance.DisConnect();
- CurrentDevice = null;
- }
- public DeviceInfoViewModel? CurrentDevice
- {
- get => currentDevice;
- set => SetProperty(ref currentDevice, value);
- }
- private void SendBroadcastMsg()
- {
- Task.Run(()=>
- {
- System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
- Shaker.Model.SearchDeviceModel search = new Shaker.Model.SearchDeviceModel();
- udpClient.Send(search.GetBytes(), new System.Net.IPEndPoint(IPAddress.Broadcast, Topic.DISCOVERYPORT));
- udpClient.Close();
- });
- }
- }
- }
|