瀏覽代碼

Optimize UI and add LogService

SenGao 3 周之前
父節點
當前提交
ae4dc83722

+ 1 - 0
Tools/AlarmInfoServerSim/App.xaml.cs

@@ -14,6 +14,7 @@ namespace AlarmInfoServerSim
         protected override void RegisterTypes(IContainerRegistry containerRegistry)
         {
             //Services
+            containerRegistry.RegisterSingleton<ILogService,LogService>();
             containerRegistry.RegisterSingleton<ISharedConfig, SharedConfig>();
             containerRegistry.RegisterSingleton<IInfoSendingService, InfoSendingService>();
         }

+ 15 - 0
Tools/AlarmInfoServerSim/Services/ILogService.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AlarmInfoServerSim.Services
+{
+    public interface ILogService
+    {
+        void Log(string message);
+
+        event EventHandler<string>? LogReceived;
+    }
+}

+ 11 - 9
Tools/AlarmInfoServerSim/Services/InfoSendingService.cs

@@ -1,4 +1,5 @@
 using RTCommunicatorBase;
+using System.Text;
 using TLVProtocal;
 using UniversalNetFrame451.IO;
 
@@ -6,18 +7,20 @@ namespace AlarmInfoServerSim.Services;
 
 public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
 {
+    private readonly ILogService _logService;
     private readonly ISharedConfig _sharedConfig;
     private readonly ITlvCommunicatorServer _server;
 
     private bool _isConnected;
     private bool disposedValue;
 
-    public InfoSendingService(ISharedConfig sharedConfig) 
+    public InfoSendingService(ILogService logService, ISharedConfig sharedConfig)
     {
-        _sharedConfig= sharedConfig;
+        _logService = logService;
+        _sharedConfig = sharedConfig;
 
         _server = TlvFactory.GetTlvServer();
-        if(!_server.Initialize(this))
+        if (!_server.Initialize(this))
         {
             throw new InvalidOperationException("Failed to initialize tcp server");
         }
@@ -33,13 +36,13 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
     {
         if(_sharedConfig.BasicInfo is null)
         {
-            //log
+            _logService.Log("Cannot find BasicInfo file! Please make sure it is existing!");
             return;
         }
 
         if (!_server.Open(_sharedConfig.BasicInfo.RTServerAddress, _sharedConfig.BasicInfo.RTServerPort))
         {
-            //log
+            _logService.Log($"Failed to start TCP server in {_sharedConfig.BasicInfo.RTServerAddress}:{_sharedConfig.BasicInfo.RTServerPort}");
         }
     }
 
@@ -47,13 +50,13 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
     {
         if(!_isConnected)
         {
-            //log
+            _logService.Log("Cannot send information due to disconnected status");
             return;
         }
 
         if(!_server.Send(tag, alarm))
         {
-            //log
+            _logService.Log("Cannot send information due to unknown reason");
         }
     }
 
@@ -73,14 +76,13 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
     {
         if (data.RawData is null || data.RawData.Length == 0)
         {
-            //Log
+            _logService.Log("Received RawData is null");
             return;
         }
 
         if(data.Tag==1)
         {
             DataReceived?.Invoke(null,(data.Tag, data));
-            //log
         }
     }
 

+ 24 - 0
Tools/AlarmInfoServerSim/Services/LogService.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AlarmInfoServerSim.Services
+{
+    public class LogService : ILogService
+    {
+        public LogService() 
+        {
+
+        }
+
+        public void Log(string message)
+        {
+            string logFormat = $"[{DateTime.Now.ToString()}] Content: {message} ";
+            LogReceived?.Invoke(null, logFormat);
+        }
+
+        public event EventHandler<string>? LogReceived;
+    }
+}

+ 25 - 3
Tools/AlarmInfoServerSim/ViewModels/LogBarViewModel.cs

@@ -1,11 +1,33 @@
-using CommunityToolkit.Mvvm.ComponentModel;
+using AlarmInfoServerSim.Services;
+using CommunityToolkit.Mvvm.ComponentModel;
+using System.Collections.ObjectModel;
 
 namespace AlarmInfoServerSim.ViewModels;
 
-public class LogBarViewModel : ObservableObject
+public partial class LogBarViewModel : ObservableObject
 {
-    public LogBarViewModel() 
+    private readonly ILogService _logService;
+
+    [ObservableProperty]
+    private ObservableCollection<string> _logs;
+
+    public LogBarViewModel(ILogService logService) 
     {
+        _logService= logService;
+        _logService.LogReceived += OnLogReceived;
 
+        _logs = [];
+    }
+
+    private void OnLogReceived(object? sender, string e)
+    {
+        App.Current.Dispatcher.BeginInvoke(() =>
+        {
+            if(Logs.Count>=50)
+            {
+                Logs.RemoveAt(Logs.Count - 1);
+            }
+            Logs.Insert(0, e);
+        });
     }
 }

+ 5 - 1
Tools/AlarmInfoServerSim/ViewModels/StatusBarViewModel.cs

@@ -5,6 +5,7 @@ namespace AlarmInfoServerSim.ViewModels;
 
 public partial class StatusBarViewModel : ObservableObject
 {
+    private readonly ILogService _logService; 
     private readonly ISharedConfig _sharedConfig;
     private readonly IInfoSendingService _infoSendingService;
 
@@ -17,8 +18,9 @@ public partial class StatusBarViewModel : ObservableObject
     [ObservableProperty]
     private string _remotePort;
 
-    public StatusBarViewModel(ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
+    public StatusBarViewModel(ILogService logService, ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
     {
+        _logService = logService;
         _sharedConfig = sharedConfig;
         _infoSendingService = sendInfoService;
         _infoSendingService.ConnectionChanged += OnConnectionChanged;
@@ -35,12 +37,14 @@ public partial class StatusBarViewModel : ObservableObject
             Status = "Connected";
             RemoteIp = e.Item2.RemoteEndPoint.Address.ToString();
             RemotePort = e.Item2.RemoteEndPoint.Port.ToString();
+            _logService.Log($"Get a connection from {RemoteIp}:{RemotePort}");
         }
         else
         {
             Status = "Disconnected";
             RemoteIp = "Unknown";
             RemotePort = "Unknown";
+            _logService.Log($"The connection lost");
         }
     }
 }

+ 17 - 5
Tools/AlarmInfoServerSim/ViewModels/WorkAreaViewModel.cs

@@ -6,11 +6,13 @@ using RealtimeData;
 using RTCommunicatorBase;
 using System.Collections.Concurrent;
 using System.Collections.ObjectModel;
+using System.Text;
 
 namespace AlarmInfoServerSim.ViewModels;
 
 public partial class WorkAreaViewModel : ObservableObject
 {
+    private readonly ILogService _logService;
     private readonly IInfoSendingService _infoSendingService;
 
     private readonly Hardwares _hardwares;
@@ -43,7 +45,7 @@ public partial class WorkAreaViewModel : ObservableObject
     private float _floor;
 
     [ObservableProperty]
-    private ObservableCollection<string> _receivingTime;
+    private ObservableCollection<string> _receivedTlvData;
 
     [ObservableProperty]
     [NotifyCanExecuteChangedFor(nameof(SendInfoCommand))]
@@ -51,7 +53,7 @@ public partial class WorkAreaViewModel : ObservableObject
 
 
 
-    public WorkAreaViewModel(ISharedConfig sharedConfig ,IInfoSendingService infoSendingService)
+    public WorkAreaViewModel(ILogService logService, ISharedConfig sharedConfig ,IInfoSendingService infoSendingService)
     {
         if(sharedConfig.Hardwares is null)
         {
@@ -63,8 +65,9 @@ public partial class WorkAreaViewModel : ObservableObject
         _mini8 = _hardwares.Mini8s;
         _mini8Channel = [];
         _alarmTypes = Enum.GetValues<AlarmType>();
-        _receivingTime = [];
+        _receivedTlvData = [];
 
+        _logService= logService;
         _infoSendingService = infoSendingService;
         _infoSendingService.ConnectionChanged += OnConnectionChanged;
         _infoSendingService.DataReceived += OnDataReceived;
@@ -87,7 +90,7 @@ public partial class WorkAreaViewModel : ObservableObject
     {
         if (SelectedMini8Index == -1 || SelectedChannelIndex == -1 || SelectedAlarmTypeIndex == -1)
         {
-            //log
+            _logService.Log("Failed to send information, please select every ComboBox");
             return;
         }
 
@@ -113,6 +116,15 @@ public partial class WorkAreaViewModel : ObservableObject
 
     private void OnDataReceived(object? sender, (ushort, TLVProtocal.TlvData) e)
     {
-        ReceivingTime.Add(e.Item2.DateTime.ToString());
+        App.Current.Dispatcher.Invoke(() =>
+        {
+            if (ReceivedTlvData.Count >= 50)
+            {
+                ReceivedTlvData.RemoveAt(ReceivedTlvData.Count - 1);
+            }
+
+            string fileName = Encoding.UTF8.GetString(e.Item2.RawData);
+            ReceivedTlvData.Insert(0, e.Item2.DateTime.ToString() + $" {fileName}");
+        });
     }
 }

+ 9 - 2
Tools/AlarmInfoServerSim/Views/LogBar.xaml

@@ -6,7 +6,14 @@
              xmlns:local="clr-namespace:AlarmInfoServerSim.Views"
              xmlns:prism="http://prismlibrary.com/"
              prism:ViewModelLocator.AutoWireViewModel="True"
-             Height="200" Width="600"
+             Height="230" Width="600"
              mc:Ignorable="d">
-    <Grid Background="#FFCECECE" Margin="5,5,5,0"/>
+    <Grid Background="#FFCECECE" Margin="5,5,5,0">
+        <Grid.RowDefinitions>
+            <RowDefinition Height="26"/>
+            <RowDefinition Height="*"/>
+        </Grid.RowDefinitions>
+        <Label Grid.Row="0">Log Information:</Label>
+        <ListView Grid.Row="1" ItemsSource="{Binding Logs}" Margin="3,2,3,2" />
+    </Grid>
 </UserControl>

+ 2 - 2
Tools/AlarmInfoServerSim/Views/MainWindow.xaml

@@ -6,13 +6,13 @@
         xmlns:local="clr-namespace:AlarmInfoServerSim.Views"
         xmlns:prism="http://prismlibrary.com/"
         prism:ViewModelLocator.AutoWireViewModel="True"
-        Title="Alarm Information Service" Height="600" Width="600" ResizeMode="CanMinimize"
+        Title="Alarm Information Service" Height="680" Width="620" ResizeMode="CanMinimize"
         mc:Ignorable="d">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="50"/>
             <RowDefinition Height="350"/>
-            <RowDefinition Height="200"/>
+            <RowDefinition Height="230"/>
         </Grid.RowDefinitions>
         <local:StatusBar Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
         <local:WorkArea Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>

+ 29 - 29
Tools/AlarmInfoServerSim/Views/WorkArea.xaml

@@ -13,36 +13,36 @@
             <ColumnDefinition Width="300" />
             <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>
-        <StackPanel Grid.Column="0" Orientation="Vertical" Margin="5,10,5,10">
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>Mini8:</Label>
-                <ComboBox Width="120" SelectedIndex="{Binding SelectedMini8Index}" ItemsSource="{Binding Mini8}" DisplayMemberPath="Value.Name"/>
+        <Grid Grid.Column="0">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="100"/>
+                <ColumnDefinition Width="*"/>
+            </Grid.ColumnDefinitions>
+            <StackPanel Grid.Column="0" Orientation="Vertical" Margin="5,10,0,10">
+                <Label HorizontalAlignment="Right">Mini8:</Label>
+                <Label HorizontalAlignment="Right">Channel:</Label>
+                <Label HorizontalAlignment="Right">Alarm Type:</Label>
+                <Label HorizontalAlignment="Right">PV:</Label>
+                <Label HorizontalAlignment="Right">Caps:</Label>
+                <Label HorizontalAlignment="Right">Floor:</Label>
             </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>Channel:</Label>
-                <ComboBox Width="120" SelectedIndex="{Binding SelectedChannelIndex}" ItemsSource="{Binding Mini8Channel}" DisplayMemberPath="Value.Name"/>
+            <StackPanel Grid.Column="1" Orientation="Vertical" Margin="0,10,5,10">
+                <ComboBox Width="120" SelectedIndex="{Binding SelectedMini8Index}" ItemsSource="{Binding Mini8}" DisplayMemberPath="Value.Name" HorizontalAlignment="Left" Margin="0,3,0,0"/>
+                <ComboBox Width="120" SelectedIndex="{Binding SelectedChannelIndex}" ItemsSource="{Binding Mini8Channel}" DisplayMemberPath="Value.Name" HorizontalAlignment="Left" Margin="0,5,0,0"/>
+                <ComboBox Width="120" SelectedIndex="{Binding SelectedAlarmTypeIndex}" ItemsSource="{Binding AlarmTypes}" HorizontalAlignment="Left" Margin="0,5,0,0"/>
+                <TextBox Width="80" Text="{Binding PV}" HorizontalAlignment="Left" Margin="0,6,0,0"></TextBox>
+                <TextBox Width="80" Text="{Binding Caps}" HorizontalAlignment="Left" Margin="0,6,0,0"></TextBox>
+                <TextBox Width="80" Text="{Binding Floor}" HorizontalAlignment="Left" Margin="0,8,0,0"></TextBox>
+                <Button Content="Send" Command="{Binding SendInfoCommand}" Margin="0,20,0,0" Width="120" HorizontalAlignment="Left"/>
             </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>Alarm Type:</Label>
-                <ComboBox Width="120" SelectedIndex="{Binding SelectedAlarmTypeIndex}" ItemsSource="{Binding AlarmTypes}"/>
-            </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>PV:</Label>
-                <TextBox Width="80" Text="{Binding PV}"></TextBox>
-            </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>Caps:</Label>
-                <TextBox Width="80" Text="{Binding Caps}"></TextBox>
-            </StackPanel>
-            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
-                <Label>Floor:</Label>
-                <TextBox Width="80" Text="{Binding Floor}"></TextBox>
-            </StackPanel>
-            <Button Content="Send" Command="{Binding SendInfoCommand}" Margin="20,20,20,20" Width="120" />
-        </StackPanel>
-        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="5,10,5,10">
-            <ListView MaxHeight="280" ItemsSource="{Binding ReceivingTime}" Margin="5,20,5,0"></ListView>
-        </StackPanel>
-
+        </Grid>
+        <Grid Grid.Column="1">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="30"/>
+                <RowDefinition Height="*"/>
+            </Grid.RowDefinitions>
+            <Label Grid.Row="0">Received Data:</Label>
+            <ListView Grid.Row="1" MaxHeight="310" ItemsSource="{Binding ReceivingTime}"/>
+        </Grid>
     </Grid>
 </UserControl>