123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using FestoDebugger.Models;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace FestoDebugger.ViewModels
- {
- public class MainViewModel : ViewModelBase
- {
- #region 内部变量
- private string _ipAddress = "192.168.1.1";
- private string _port = "502";
- private string _connectionStatus = "未连接";
-
- private bool _isConnecting;
-
- private bool _isConnected;
- private ObservableCollection<SignalModuleData> _signalModuleDatas;
- #endregion
- #region 属性
- public string IpAddress
- {
- get => _ipAddress;
- set
- {
- if (_ipAddress != value)
- {
- _ipAddress = value;
- OnPropertyChanged(nameof(IpAddress));
- }
- }
- }
- public string Port
- {
- get => _port;
- set
- {
- if (_port != value)
- {
- _port = value;
- OnPropertyChanged(nameof(Port));
- }
- }
- }
- public string ConnectionStatus
- {
- get => _connectionStatus;
- set
- {
- if (_connectionStatus != value)
- {
- _connectionStatus = value;
- OnPropertyChanged(nameof(ConnectionStatus));
- }
- }
- }
- public ObservableCollection<SignalModuleData> SignalModuleDatas
- {
- get => _signalModuleDatas;
- set => SetProperty(ref _signalModuleDatas, value);
- }
- #endregion
- public bool IsNotConnecting => !_isConnecting;
- public string ConnectButtonText => _isConnected ? "断开连接" : "连接设备";
- public Brush StatusColor => _isConnected ? Brushes.Green : Brushes.Red;
- public ICommand ConnectCommand { get; private set; }
- public MainViewModel()
- {
- ConnectCommand = new RelayCommand(async () => await ToggleConnection());
- SignalModuleDatas = new ObservableCollection<SignalModuleData>();
- for (int i = 1; i < 11; i++)
- {
- SignalModuleData data = new SignalModuleData();
- data.ModuleName = $"V{i}";
- data.SignalAOn = true;
- data.SignalBOn = false;
- SignalModuleDatas.Add(data);
- }
- //LoadSignalConfig();
- }
- /// <summary>
- /// 加载配置文件,初始化数据
- /// </summary>
- private void LoadSignalConfig()
- {
- }
- private async Task ToggleConnection()
- {
- if (_isConnected)
- {
- Disconnect();
- return;
- }
- await Connect();
- }
- private async Task Connect()
- {
- if (!IPAddress.TryParse(IpAddress, out var ipAddress) || !int.TryParse(Port, out var port))
- {
- ConnectionStatus = "IP或端口格式错误";
- return;
- }
- _isConnecting = true;
- OnPropertyChanged(nameof(IsNotConnecting));
- ConnectionStatus = "连接中...";
- try
- {
- using var client = new TcpClient();
- var connectTask = client.ConnectAsync(ipAddress, port);
- if (await Task.WhenAny(connectTask, Task.Delay(5000)) == connectTask)
- {
- if (client.Connected)
- {
- _isConnected = true;
- ConnectionStatus = "已连接";
- }
- else
- {
- ConnectionStatus = "连接失败";
- }
- }
- else
- {
- ConnectionStatus = "连接超时";
- }
- }
- catch (Exception ex)
- {
- ConnectionStatus = $"错误: {ex.Message}";
- }
- finally
- {
- _isConnecting = false;
- OnPropertyChanged(nameof(IsNotConnecting));
- OnPropertyChanged(nameof(ConnectButtonText));
- OnPropertyChanged(nameof(StatusColor));
- }
- }
- private void Disconnect()
- {
- _isConnected = false;
- ConnectionStatus = "已断开";
- OnPropertyChanged(nameof(ConnectButtonText));
- OnPropertyChanged(nameof(StatusColor));
- }
- }
- public class RelayCommand : ICommand
- {
- private readonly Func<Task> _execute;
- private readonly Func<bool> _canExecute;
- public event EventHandler CanExecuteChanged;
- public RelayCommand(Func<Task> execute, Func<bool> canExecute = null)
- {
- _execute = execute ?? throw new ArgumentNullException(nameof(execute));
- _canExecute = canExecute;
- }
- public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
- public async void Execute(object parameter) => await _execute();
- public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- }
|