using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using EEMSUIClient.Services; using GeneralData; using System.IO; using System.Text.Json; using System.Windows; namespace EEMSUIClient.ViewModels; public partial class MainWindowViewModel : ObservableObject { private readonly IClientService _clientService; private readonly string jsonFilePath = $"{Environment.CurrentDirectory}/DeviceInformation.json"; private bool _isInitialized = false; [ObservableProperty] private string _ipAddress = string.Empty; [ObservableProperty] private string _port = string.Empty; [ObservableProperty] private string _hubName = string.Empty; [ObservableProperty] [NotifyPropertyChangedFor(nameof(IsNotConnected))] private bool _isConnected; [ObservableProperty] private string[] _deviceModels = Enum.GetNames(typeof(DeviceModel)); [ObservableProperty] private string _deviceModel = string.Empty; [ObservableProperty] private int _deviceModelIndex = -1; [ObservableProperty] private string _deviceSubModel = string.Empty; [ObservableProperty] private string _deviceName = string.Empty; [ObservableProperty] private string _position = string.Empty; [ObservableProperty] private string _softwareVersion = string.Empty; [ObservableProperty] private string _dBConnectionString = string.Empty; [ObservableProperty] private string _guidStr = string.Empty; public MainWindowViewModel(IClientService clientService) { _clientService = clientService; _clientService.RequestFileReceived += OnRequestFileReceived; Initialize(); } public bool IsNotConnected => !IsConnected; [RelayCommand] private void Connect() { if (string.IsNullOrWhiteSpace(IpAddress) || string.IsNullOrWhiteSpace(Port) || string.IsNullOrWhiteSpace(HubName)) { MessageBox.Show("存在未被填写的项目!"); //log return; } if (!_clientService.Initialize(IpAddress, int.Parse(Port), HubName)) { MessageBox.Show("初始化失败!"); //log return; } IsConnected = true; } [RelayCommand] private void Register() { if (string.IsNullOrWhiteSpace(DeviceModel) || string.IsNullOrWhiteSpace(DeviceSubModel) || string.IsNullOrWhiteSpace(DeviceName) || string.IsNullOrWhiteSpace(Position) || string.IsNullOrWhiteSpace(SoftwareVersion) || string.IsNullOrWhiteSpace(DBConnectionString)) { MessageBox.Show("存在未被填写的项目!"); return; } try { _ = Enum.TryParse(DeviceModel, out var deviceModel); var deviceInfo = new Models.DeviceInfo { DeviceModel = deviceModel, DeviceSubModel = DeviceSubModel, DeviceName = DeviceName, Position = Position, Guid = _isInitialized ? Guid.Parse(GuidStr) : null, SoftwareVersion = SoftwareVersion, DBConnectionString = DBConnectionString }; var guid = _clientService.RegisterDevice(deviceInfo); if (guid == Guid.Empty) { MessageBox.Show("未能获取Guid,设备注册失败!"); return; } GuidStr = guid.ToString(); MessageBox.Show("设备注册成功。"); string jsonString = JsonSerializer.Serialize(deviceInfo); File.WriteAllText(jsonFilePath, jsonString); } catch (Exception ex) { MessageBox.Show($"遭遇异常:{ex.Message}"); } } private void Initialize() { if (!Path.Exists(jsonFilePath)) { return; } string jsonFromFile = File.ReadAllText(jsonFilePath); var deserializedDeviceInfo = JsonSerializer.Deserialize(jsonFromFile); if (deserializedDeviceInfo is null) { return; } DeviceModelIndex = (int)deserializedDeviceInfo.DeviceModel!; DeviceModel = deserializedDeviceInfo.DeviceModel.ToString() ?? string.Empty; DeviceSubModel = deserializedDeviceInfo.DeviceSubModel ?? string.Empty; DeviceName = deserializedDeviceInfo.DeviceName ?? string.Empty; Position = deserializedDeviceInfo.Position ?? string.Empty; SoftwareVersion = deserializedDeviceInfo.SoftwareVersion ?? string.Empty; GuidStr = deserializedDeviceInfo.Guid.ToString() ?? string.Empty; DBConnectionString = deserializedDeviceInfo.DBConnectionString ?? string.Empty; _isInitialized = true; } private void OnRequestFileReceived(object? sender, (Guid guid, ServiceBase.FileType fileType) e) { //log } }