| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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>(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<Models.DeviceInfo>(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
- }
- }
|