浏览代码

Update code for registering the device information.

SenGao 11 小时之前
父节点
当前提交
e481a0dfc8

+ 0 - 15
EEMSMain.sln

@@ -33,10 +33,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj",
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Analyzer", "Analyzer", "{62F6EEA5-0B5B-44BC-88B8-8F875BF95424}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeplerAnalizer", "Analizer\KeplerAnalizer\KeplerAnalizer.csproj", "{2735D064-47D5-4905-87D0-D10619806546}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProximaAnalizer", "Analizer\ProximaAnalizer\ProximaAnalizer.csproj", "{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8}"
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DashBoard", "Module\DashBoard\DashBoard.csproj", "{C3B8BA88-50CB-48C9-954E-811B68280DE8}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataBase", "DataBase", "{664E8B6F-4E97-4592-B7CA-F608871592CD}"
@@ -132,14 +128,6 @@ Global
 		{7332403B-C2B6-404E-A196-AD6D3FDF207F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{7332403B-C2B6-404E-A196-AD6D3FDF207F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{7332403B-C2B6-404E-A196-AD6D3FDF207F}.Release|Any CPU.Build.0 = Release|Any CPU
-		{2735D064-47D5-4905-87D0-D10619806546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{2735D064-47D5-4905-87D0-D10619806546}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{2735D064-47D5-4905-87D0-D10619806546}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{2735D064-47D5-4905-87D0-D10619806546}.Release|Any CPU.Build.0 = Release|Any CPU
-		{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8}.Release|Any CPU.Build.0 = Release|Any CPU
 		{C3B8BA88-50CB-48C9-954E-811B68280DE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{C3B8BA88-50CB-48C9-954E-811B68280DE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{C3B8BA88-50CB-48C9-954E-811B68280DE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -216,8 +204,6 @@ Global
 		{4165E2E5-632E-4A7D-82E1-A1F256517B70} = {F81EF7E9-27B9-4DE0-95C9-CD1E7B58BA89}
 		{46863C36-9D19-40E7-8A0B-03EAB0944F1A} = {F81EF7E9-27B9-4DE0-95C9-CD1E7B58BA89}
 		{A7B0EC60-758A-3F1B-07BD-E6BDC72911F8} = {B8FCC141-1383-4797-AAD9-17F53945876E}
-		{2735D064-47D5-4905-87D0-D10619806546} = {62F6EEA5-0B5B-44BC-88B8-8F875BF95424}
-		{F7F2BC09-A8FD-4F79-82B1-BC578D50C9D8} = {62F6EEA5-0B5B-44BC-88B8-8F875BF95424}
 		{C3B8BA88-50CB-48C9-954E-811B68280DE8} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
 		{E01EF058-A268-B0AE-ACE3-A67E6C7C1981} = {664E8B6F-4E97-4592-B7CA-F608871592CD}
 		{EA1B249C-B1A9-30E4-3598-68B82121C8AF} = {664E8B6F-4E97-4592-B7CA-F608871592CD}
@@ -228,7 +214,6 @@ Global
 		{3F13236F-F673-42A2-B928-61A5F4B4C3AD} = {9EF4E4C0-6304-4339-8367-3BFD5E816464}
 		{8E249AE0-505B-4481-8C83-42C71561370E} = {9EF4E4C0-6304-4339-8367-3BFD5E816464}
 		{E1F62B2E-BC79-4A21-9458-B5CA3641BD08} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
-		{0B0BF6F8-6BFC-4294-A9C9-014075A09392} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
 		{0C79BB69-475D-40CC-AC3A-583AFD8F7527} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
 		{4D7874BD-0AFD-1BDF-FC6D-74C2F1EE9448} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
 	EndGlobalSection

+ 2 - 0
EEMSUIClient/App.xaml.cs

@@ -18,6 +18,7 @@ public partial class App : PrismApplication
     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
         containerRegistry.RegisterSingleton<ITrayControl, TrayController>();
+        containerRegistry.RegisterSingleton<IClientService, ClientService>();
     }
 
     protected override void OnInitialized()
@@ -45,6 +46,7 @@ public partial class App : PrismApplication
 
     private void OnExit(object sender, ExitEventArgs e)
     {
+        Container.Resolve<IClientService>().Dispose();
         Container.Resolve<ITrayControl>().Dispose();
     }
 }

+ 6 - 0
EEMSUIClient/EEMSUIClient.csproj

@@ -15,6 +15,12 @@
   </ItemGroup>
 
   <ItemGroup>
+    <ProjectReference Include="..\Data\GeneralData\GeneralData.csproj" />
+    <ProjectReference Include="..\Server\EEMSClientCore\EEMSClientCore.csproj" />
+    <ProjectReference Include="..\Server\ServiceBase\ServiceBase.csproj" />
+  </ItemGroup>
+
+  <ItemGroup>
     <Resource Include="Logo.ico" />
   </ItemGroup>
 </Project>

+ 32 - 0
EEMSUIClient/Models/DeviceInfo.cs

@@ -0,0 +1,32 @@
+using GeneralData;
+
+namespace EEMSUIClient.Models;
+
+public class DeviceInfo
+{
+    public DeviceModel DeviceModel { get; set; }
+    public string? DeviceSubModel { get; set; }
+    public string? DeviceName { get; set; }
+    public string? Position { get; set; }
+    public string? SoftwareVersion { get; set; }
+    public Guid? Guid { get; set; }
+    public string? IP { get; set; }
+    public ushort Port { get; set; }
+    public string? DBConnectionString { get; set; }
+
+    public Device.DeviceInfo Convert()
+    {
+        return new Device.DeviceInfo()
+        {
+            DeviceModel = this.DeviceModel,
+            DeviceSubModel = this.DeviceSubModel,
+            DeviceName = this.DeviceName,
+            Position = this.Position,
+            SoftwareVersion = this.SoftwareVersion,
+            Guid = this.Guid,
+            IP = this.IP,
+            Port = this.Port,
+            DBConnectionString = this.DBConnectionString
+        };
+    }
+}

+ 86 - 0
EEMSUIClient/Services/ClientService.cs

@@ -0,0 +1,86 @@
+using EEMSClientCore;
+using ServiceBase;
+
+namespace EEMSUIClient.Services
+{
+    public class ClientService : IClientService, IClientProvider
+    {
+        private readonly IClientCaller _clientCaller;
+        private readonly HubBase _hubBase;
+
+        private bool disposedValue;
+
+        public ClientService()
+        {
+            _clientCaller = new ClientCaller();
+            _hubBase = new HubBase();
+        }
+
+        public event EventHandler<(Guid guid, FileType fileType)>? RequestFileReceived;
+
+        public bool Initialize(string ip, int port, string hub)
+        {
+            if (string.IsNullOrWhiteSpace(ip) || port < 0 || string.IsNullOrWhiteSpace(hub))
+            {
+                //log
+                return false;
+            }
+
+            if (!_hubBase.Initialize(this))
+            {
+                //log
+                return false;
+            }
+
+            if (!_hubBase.Open(ip, port, hub))
+            {
+                //log
+                return false;
+            }
+
+            return true;
+        }
+
+        public Guid RegisterDevice(Models.DeviceInfo deviceInfo)
+        {
+            return _clientCaller.RegisterDevice(deviceInfo.Convert()).Result;
+        }
+
+        public Task<bool> RequestFile(Guid guid, FileType fileType)
+        {
+            //log
+            RequestFileReceived?.Invoke(null, (guid, fileType));
+            return Task.FromResult(true);
+        }
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposedValue)
+            {
+                if (disposing)
+                {
+                    // TODO: dispose managed state (managed objects)
+                    _hubBase?.Dispose();
+                }
+
+                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
+                // TODO: set large fields to null
+                disposedValue = true;
+            }
+        }
+
+        // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
+        // ~ClientService()
+        // {
+        //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+        //     Dispose(disposing: false);
+        // }
+
+        public void Dispose()
+        {
+            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+            Dispose(disposing: true);
+            GC.SuppressFinalize(this);
+        }
+    }
+}

+ 14 - 0
EEMSUIClient/Services/IClientService.cs

@@ -0,0 +1,14 @@
+using Device;
+using ServiceBase;
+
+namespace EEMSUIClient.Services
+{
+    public interface IClientService : IDisposable
+    {
+        bool Initialize(string ip, int port, string hub);
+
+        Guid RegisterDevice(Models.DeviceInfo deviceInfo);
+
+        event EventHandler<(Guid guid, FileType fileType)>? RequestFileReceived;
+    }
+}

+ 173 - 1
EEMSUIClient/ViewModels/MainWindowViewModel.cs

@@ -1,11 +1,183 @@
 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
 {
-    public MainWindowViewModel()
+    private readonly IClientService _clientService;
+
+    private readonly string jsonFilePath= $"{Environment.CurrentDirectory}/DeviceInformation.json";
+
+    [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 guid = _clientService.RegisterDevice(new Models.DeviceInfo
+            {
+                DeviceModel = deviceModel,
+                DeviceSubModel = DeviceSubModel,
+                DeviceName = DeviceName,
+                Position = Position,
+                SoftwareVersion = SoftwareVersion,
+                DBConnectionString = DBConnectionString
+            });
+            if (guid == Guid.Empty)
+            {
+                MessageBox.Show("未能获取Guid,设备注册失败!");
+                return;
+            }
+
+            GuidStr = guid.ToString();
+            MessageBox.Show("设备注册成功。");
+        }
+        catch (Exception ex)
+        {
+            MessageBox.Show($"遭遇异常:{ex.Message}");
+        }
+    }
+    [RelayCommand]
+    private void Save()
+    {
+        if(string.IsNullOrWhiteSpace(GuidStr) || !Guid.TryParse(GuidStr,out var guidStr))
+        {
+            MessageBox.Show("Guid错误!");
+            return;
+        }
+
+        try
+        {
+            _ = Enum.TryParse<DeviceModel>(DeviceModel, out var deviceModel);
+            var deviceInfo = new Models.DeviceInfo
+            {
+                DeviceModel = deviceModel,
+                DeviceSubModel = DeviceSubModel,
+                DeviceName = DeviceName,
+                Position = Position,
+                SoftwareVersion = SoftwareVersion,
+                Guid = guidStr,
+                DBConnectionString = DBConnectionString
+            };
+            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;
+    }
+
+    private void OnRequestFileReceived(object? sender, (Guid guid, ServiceBase.FileType fileType) e)
+    {
+        //log
     }
 }

+ 50 - 2
EEMSUIClient/Views/MainWindow.xaml

@@ -9,7 +9,55 @@
         mc:Ignorable="d"
         Title="MainWindow" Height="450" Width="800"
         WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowStyle="SingleBorderWindow">
-    <Grid>
-        
+    <Grid Margin="5,5,5,5" >
+        <Grid.RowDefinitions>
+            <RowDefinition Height="0.5*"/>
+            <RowDefinition Height="3*"/>
+            <RowDefinition Height="1*"/>
+        </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="1" Margin="3,5,3,5" Background="#FFCECECE">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+                <RowDefinition Height="1*"/>
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="0.8*"/>
+                <ColumnDefinition Width="1*"/>
+                <ColumnDefinition Width="1*"/>
+            </Grid.ColumnDefinitions>
+            <Label Grid.Row="0" Grid.Column="0" Content="Device Model:" HorizontalAlignment="Right" Height="30"/>
+            <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding DeviceModels}" SelectedItem="{Binding DeviceModel}" SelectedIndex="{Binding DeviceModelIndex}" Height="30"/>
+            <Label Grid.Row="1" Grid.Column="0" Content="Device SubModel:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding DeviceSubModel}" Height="30"/>
+            <Label Grid.Row="2" Grid.Column="0" Content="Device Name:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding DeviceName}" Height="30"/>
+            <Label Grid.Row="3" Grid.Column="0" Content="Position:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Position}" Height="30"/>
+            <Label Grid.Row="4" Grid.Column="0" Content="Software Version:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding SoftwareVersion}" Height="30"/>
+            <Label Grid.Row="5" Grid.Column="0" Content="DB Connection String:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding DBConnectionString}" Height="30"/>
+            <Label Grid.Row="6" Grid.Column="0" Content="Guid:" HorizontalAlignment="Right" Height="30"/>
+            <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding GuidStr}" Height="30"/>
+            <Button Grid.Row="5" Grid.Column="2" Content="Register" Command="{Binding RegisterCommand}" Height="30" Width="100"/>
+            <Button Grid.Row="6" Grid.Column="2" Content="Save" Command="{Binding SaveCommand}" Height="30" Width="100"/>
+        </Grid>
+        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="3,3,3,3" Background="#FFCECECE">
+            
+        </StackPanel>
     </Grid>
 </Window>