瀏覽代碼

EEMSUIClient:
Feature:
1. Add two new buttons to give Recipe and Config folder path.

SenGao 7 小時之前
父節點
當前提交
73b0f37861
共有 2 個文件被更改,包括 116 次插入59 次删除
  1. 76 45
      EEMSUIClient/ViewModels/MainWindowViewModel.cs
  2. 40 14
      EEMSUIClient/Views/MainWindow.xaml

+ 76 - 45
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -1,8 +1,10 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
+using EEMSClientCore;
 using EEMSUIClient.Models;
 using EEMSUIClient.Services;
 using GeneralData;
+using Microsoft.Win32;
 using System.IO;
 using System.Text.Json;
 using System.Windows;
@@ -13,8 +15,9 @@ public partial class MainWindowViewModel : ObservableObject
 {
     private readonly IClientService _clientService;
 
-    private readonly string _ipAddressFilePath = $"{Environment.CurrentDirectory}/IpAddressInformation.json";
-    private readonly string _deviceInfoFilePath = $"{Environment.CurrentDirectory}/DeviceInformation.json";
+    private readonly string _settingsFolder = $"{Environment.CurrentDirectory}/Settings";
+    private readonly string _ipAddressFileName = "IpAddressInformation.json";
+    private readonly string _deviceInfoFileName = "DeviceInformation.json";
     private bool _isInitialized = false;
 
     [ObservableProperty]
@@ -31,6 +34,12 @@ public partial class MainWindowViewModel : ObservableObject
     private bool _isConnected;
 
     [ObservableProperty]
+    private string _selectedRecipeDict=string.Empty;
+
+    [ObservableProperty]
+    private string _selectedConfigDict = string.Empty;
+
+    [ObservableProperty]
     private string[] _deviceModels = Enum.GetNames(typeof(DeviceModel));
 
     [ObservableProperty]
@@ -60,13 +69,43 @@ public partial class MainWindowViewModel : ObservableObject
     public MainWindowViewModel(IClientService clientService)
     {
         _clientService = clientService;
-        InitializeIpAddress();
-        InitializeDeviceInfo();
+
+        Initialize();
     }
 
     public bool IsNotConnected => !IsConnected;
 
     [RelayCommand]
+    private void SelectRecipe()
+    {
+        var dialog = new OpenFolderDialog()
+        {
+            Title = "Select Folder"
+        };
+        if (dialog.ShowDialog() == true)
+        {
+            SelectedRecipeDict = dialog.FolderName;
+            var service = (IClientBaseProvider)_clientService;
+            service.RecipePath = SelectedRecipeDict;
+        }
+    }
+
+    [RelayCommand]
+    private void SelectConfig()
+    {
+        var dialog = new OpenFolderDialog()
+        {
+            Title = "Select Folder"
+        };
+        if (dialog.ShowDialog() == true)
+        {
+            SelectedConfigDict = dialog.FolderName;
+            var service = (IClientBaseProvider)_clientService;
+            service.ConfigPath = SelectedConfigDict;
+        }
+    }
+
+    [RelayCommand]
     private void Connect()
     {
         if (string.IsNullOrWhiteSpace(IpAddress) || string.IsNullOrWhiteSpace(Port) || string.IsNullOrWhiteSpace(HubName))
@@ -91,8 +130,9 @@ public partial class MainWindowViewModel : ObservableObject
             Port=int.Parse(Port),
             HubName=HubName,
         };
+
         string jsonString = JsonSerializer.Serialize(addressInfo);
-        File.WriteAllText(_ipAddressFilePath, jsonString);
+        File.WriteAllText(Path.Combine(_settingsFolder, _ipAddressFileName), jsonString);
     }
 
     [RelayCommand]
@@ -134,7 +174,7 @@ public partial class MainWindowViewModel : ObservableObject
             MessageBox.Show("设备注册成功。");
 
             var jsonString = JsonSerializer.Serialize(deviceInfo);
-            File.WriteAllText(_deviceInfoFilePath, jsonString);
+            File.WriteAllText(Path.Combine(_settingsFolder, _deviceInfoFileName), jsonString);
         }
         catch (Exception ex)
         {
@@ -142,53 +182,44 @@ public partial class MainWindowViewModel : ObservableObject
         }
     }
 
-    private void InitializeIpAddress()
+    private void Initialize()
     {
-        if (!Path.Exists(_ipAddressFilePath))
-        {
-            return;
-        }
-
-        string jsonFromFile = File.ReadAllText(_ipAddressFilePath);
-        var deserializedAddressInfo = JsonSerializer.Deserialize<AddressInfo>(jsonFromFile);
-        if (deserializedAddressInfo is null)
+        if (!Directory.Exists(_settingsFolder))
         {
-            return;
+            Directory.CreateDirectory(_settingsFolder);
         }
 
-        IpAddress = deserializedAddressInfo.Ip;
-        Port = deserializedAddressInfo.Port.ToString();
-        HubName = deserializedAddressInfo.HubName;
-    }
-
-    private void InitializeDeviceInfo()
-    {
-        if (!Path.Exists(_deviceInfoFilePath))
+        var ipAddressFilePath = Path.Combine(_settingsFolder, _ipAddressFileName);
+        if (Path.Exists(ipAddressFilePath))
         {
-            return;
+            string jsonFromFile = File.ReadAllText(ipAddressFilePath);
+            var deserializedAddressInfo = JsonSerializer.Deserialize<AddressInfo>(jsonFromFile);
+            if (deserializedAddressInfo is not null)
+            {
+                IpAddress = deserializedAddressInfo.Ip;
+                Port = deserializedAddressInfo.Port.ToString();
+                HubName = deserializedAddressInfo.HubName;
+            }
         }
 
-        string jsonFromFile = File.ReadAllText(_deviceInfoFilePath);
-        var deserializedDeviceInfo = JsonSerializer.Deserialize<Models.DeviceInfo>(jsonFromFile);
-        if (deserializedDeviceInfo is null)
+        var deviceInfoFilePath = Path.Combine(_settingsFolder, _deviceInfoFileName);
+        if (Path.Exists(deviceInfoFilePath))
         {
-            return;
+            string jsonFromFile = File.ReadAllText(deviceInfoFilePath);
+            var deserializedDeviceInfo = JsonSerializer.Deserialize<DeviceInfo>(jsonFromFile);
+            if (deserializedDeviceInfo is not null)
+            {
+                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;
+            }
         }
-
-        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
     }
 }

+ 40 - 14
EEMSUIClient/Views/MainWindow.xaml

@@ -11,19 +11,48 @@
         WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="SingleBorderWindow">
     <Grid Margin="5,5,5,5" >
         <Grid.RowDefinitions>
-            <RowDefinition Height="0.5*"/>
-            <RowDefinition Height="3*"/>
             <RowDefinition Height="1*"/>
+            <RowDefinition Height="3*"/>
         </Grid.RowDefinitions>
-        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="3,3,3,3" IsEnabled="{Binding IsNotConnected}" Background="#FFCECECE">
-            <Label Content="IP:" Height="30" Margin="20,0,0,0"/>
-            <TextBox Text="{Binding IpAddress}" Width="150" Height="30"/>
-            <Label Content="Port:" Height="30" Margin="20,0,0,0"/>
-            <TextBox Text="{Binding Port}" Width="150" Height="30"/>
-            <Label Content="HubName:" Height="30" Margin="20,0,0,0"/>
-            <TextBox Text="{Binding HubName}" Width="150" Height="30"/>
-            <Button Content="Connect" Command="{Binding ConnectCommand}" Margin="40,0,0,0" Height="30" Width="60"/>
-        </StackPanel>
+        <Grid Grid.Row="0" IsEnabled="{Binding IsNotConnected}" Background="#FFCECECE">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="1*"/>
+                <ColumnDefinition Width="1*"/>
+                <ColumnDefinition Width="1*"/>
+                <ColumnDefinition Width="1*"/>
+            </Grid.ColumnDefinitions>
+            <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Center" VerticalAlignment="Center">
+                <Label Content="IP:" Height="30"/>
+                <TextBox Text="{Binding IpAddress}" Width="150" Height="30"/>
+            </StackPanel>
+            <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Center" VerticalAlignment="Center">
+                <Label Content="Port:" Height="30"/>
+                <TextBox Text="{Binding Port}" Width="150" Height="30"/>
+            </StackPanel>
+            <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Center" VerticalAlignment="Center">
+                <Label Content="HubName:" Height="30"/>
+                <TextBox Text="{Binding HubName}" Width="100" Height="30"/>
+            </StackPanel>
+            <StackPanel Grid.Row="0" Grid.Column="3" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Center" VerticalAlignment="Center">
+                <Button Content="Connect" Command="{Binding ConnectCommand}" Height="30" Width="60"/>
+            </StackPanel>
+            <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Right" VerticalAlignment="Center">
+                <Button Content="Recipe" Command="{Binding SelectRecipeCommand}" Height="30" Width="60"/>
+            </StackPanel>
+            <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Left" VerticalAlignment="Center">
+                <TextBox Text="{Binding SelectedRecipeDict}" IsReadOnly="True" Height="30" Width="160"/>
+            </StackPanel>
+            <StackPanel Grid.Row="1" Grid.Column="2" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Right" VerticalAlignment="Center">
+                <Button Content="Config" Command="{Binding SelectConfigCommand}" Height="30" Width="60"/>
+            </StackPanel>
+            <StackPanel Grid.Row="1" Grid.Column="3" Orientation="Horizontal" Margin="3,3,3,3" HorizontalAlignment="Center" VerticalAlignment="Center">
+                <TextBox Text="{Binding SelectedConfigDict}" IsReadOnly="True" Height="30" Width="160"/>
+            </StackPanel>
+        </Grid>
         <Grid Grid.Row="1" Margin="3,5,3,5" Background="#FFCECECE">
             <Grid.RowDefinitions>
                 <RowDefinition Height="1*"/>
@@ -55,8 +84,5 @@
             <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding GuidStr}" Height="30"/>
             <Button Grid.Row="6" Grid.Column="2" Content="Register" Command="{Binding RegisterCommand}" Height="30" Width="100"/>
         </Grid>
-        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="3,3,3,3" Background="#FFCECECE">
-            
-        </StackPanel>
     </Grid>
 </Window>