MainViewModel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using FestoDebugger.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. namespace FestoDebugger.ViewModels
  15. {
  16. public class MainViewModel : ViewModelBase
  17. {
  18. #region 内部变量
  19. private string _ipAddress = "192.168.1.1";
  20. private string _port = "502";
  21. private string _connectionStatus = "未连接";
  22. private bool _isConnecting;
  23. private bool _isConnected;
  24. private ObservableCollection<SignalModuleData> _signalModuleDatas;
  25. #endregion
  26. #region 属性
  27. public string IpAddress
  28. {
  29. get => _ipAddress;
  30. set
  31. {
  32. if (_ipAddress != value)
  33. {
  34. _ipAddress = value;
  35. OnPropertyChanged(nameof(IpAddress));
  36. }
  37. }
  38. }
  39. public string Port
  40. {
  41. get => _port;
  42. set
  43. {
  44. if (_port != value)
  45. {
  46. _port = value;
  47. OnPropertyChanged(nameof(Port));
  48. }
  49. }
  50. }
  51. public string ConnectionStatus
  52. {
  53. get => _connectionStatus;
  54. set
  55. {
  56. if (_connectionStatus != value)
  57. {
  58. _connectionStatus = value;
  59. OnPropertyChanged(nameof(ConnectionStatus));
  60. }
  61. }
  62. }
  63. public ObservableCollection<SignalModuleData> SignalModuleDatas
  64. {
  65. get => _signalModuleDatas;
  66. set => SetProperty(ref _signalModuleDatas, value);
  67. }
  68. #endregion
  69. public bool IsNotConnecting => !_isConnecting;
  70. public string ConnectButtonText => _isConnected ? "断开连接" : "连接设备";
  71. public Brush StatusColor => _isConnected ? Brushes.Green : Brushes.Red;
  72. public ICommand ConnectCommand { get; private set; }
  73. public MainViewModel()
  74. {
  75. ConnectCommand = new RelayCommand(async () => await ToggleConnection());
  76. SignalModuleDatas = new ObservableCollection<SignalModuleData>();
  77. for (int i = 1; i < 11; i++)
  78. {
  79. SignalModuleData data = new SignalModuleData();
  80. data.ModuleName = $"V{i}";
  81. data.SignalAOn = true;
  82. data.SignalBOn = false;
  83. SignalModuleDatas.Add(data);
  84. }
  85. //LoadSignalConfig();
  86. }
  87. /// <summary>
  88. /// 加载配置文件,初始化数据
  89. /// </summary>
  90. private void LoadSignalConfig()
  91. {
  92. }
  93. private async Task ToggleConnection()
  94. {
  95. if (_isConnected)
  96. {
  97. Disconnect();
  98. return;
  99. }
  100. await Connect();
  101. }
  102. private async Task Connect()
  103. {
  104. if (!IPAddress.TryParse(IpAddress, out var ipAddress) || !int.TryParse(Port, out var port))
  105. {
  106. ConnectionStatus = "IP或端口格式错误";
  107. return;
  108. }
  109. _isConnecting = true;
  110. OnPropertyChanged(nameof(IsNotConnecting));
  111. ConnectionStatus = "连接中...";
  112. try
  113. {
  114. using var client = new TcpClient();
  115. var connectTask = client.ConnectAsync(ipAddress, port);
  116. if (await Task.WhenAny(connectTask, Task.Delay(5000)) == connectTask)
  117. {
  118. if (client.Connected)
  119. {
  120. _isConnected = true;
  121. ConnectionStatus = "已连接";
  122. }
  123. else
  124. {
  125. ConnectionStatus = "连接失败";
  126. }
  127. }
  128. else
  129. {
  130. ConnectionStatus = "连接超时";
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. ConnectionStatus = $"错误: {ex.Message}";
  136. }
  137. finally
  138. {
  139. _isConnecting = false;
  140. OnPropertyChanged(nameof(IsNotConnecting));
  141. OnPropertyChanged(nameof(ConnectButtonText));
  142. OnPropertyChanged(nameof(StatusColor));
  143. }
  144. }
  145. private void Disconnect()
  146. {
  147. _isConnected = false;
  148. ConnectionStatus = "已断开";
  149. OnPropertyChanged(nameof(ConnectButtonText));
  150. OnPropertyChanged(nameof(StatusColor));
  151. }
  152. }
  153. public class RelayCommand : ICommand
  154. {
  155. private readonly Func<Task> _execute;
  156. private readonly Func<bool> _canExecute;
  157. public event EventHandler CanExecuteChanged;
  158. public RelayCommand(Func<Task> execute, Func<bool> canExecute = null)
  159. {
  160. _execute = execute ?? throw new ArgumentNullException(nameof(execute));
  161. _canExecute = canExecute;
  162. }
  163. public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
  164. public async void Execute(object parameter) => await _execute();
  165. public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  166. }
  167. }