SenGao 2 тижнів тому
батько
коміт
796dc6f543

+ 3 - 3
CommunicationProtocols/AdsCommunicatorNet8/AdsCommunicator.cs

@@ -22,7 +22,7 @@ public class AdsCommunicator(ILog? log) : IDisposable
     private readonly ConcurrentDictionary<string, IAdsSymbol> _symbols = [];
 
 
-    public bool IsConnected { get { return _isConnected; } }
+    public bool IsConnected => _isConnected;
 
     public bool IsInitialized => _isInitialized;
 
@@ -37,6 +37,7 @@ public class AdsCommunicator(ILog? log) : IDisposable
         this._adsCsvInfo = [];
         this._connectionNotify = connectionNotify;
         _isInitialized = true;
+        disposedValue = false;
         return true;
     }
 
@@ -83,7 +84,6 @@ public class AdsCommunicator(ILog? log) : IDisposable
         this._port = port;
 
         log?.Info($"Connect success");
-        this._isConnected = true;
         return true;
     }
 
@@ -278,8 +278,8 @@ public class AdsCommunicator(ILog? log) : IDisposable
             {
                 if (this._adsClient is not null)
                 {
-                    this._adsClient.ConnectionStateChanged -= AdsClient_ConnectionStateChanged;
                     this._adsClient.Dispose();
+                    this._adsClient.ConnectionStateChanged -= AdsClient_ConnectionStateChanged;
                     this._adsClient = null;
                     _isInitialized = false;
                 }

+ 34 - 2
Tools/AlarmInfoServerSim/ViewModels/LogBarViewModel.cs

@@ -4,13 +4,15 @@ using System.Collections.ObjectModel;
 
 namespace AlarmInfoServerSim.ViewModels;
 
-public partial class LogBarViewModel : ObservableObject
+public partial class LogBarViewModel : ObservableObject, IDisposable
 {
     private readonly ILogService _logService;
 
+    private bool disposedValue;
+
     [ObservableProperty]
     private ObservableCollection<string> _logs;
-
+    
     public LogBarViewModel(ILogService logService) 
     {
         _logService= logService;
@@ -30,4 +32,34 @@ public partial class LogBarViewModel : ObservableObject
             Logs.Insert(0, e);
         });
     }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (!disposedValue)
+        {
+            if (disposing)
+            {
+                // TODO: dispose managed state (managed objects)
+                _logService.LogReceived -= OnLogReceived;
+            }
+
+            // 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
+    // ~LogBarViewModel()
+    // {
+    //     // 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);
+    }
 }

+ 43 - 11
Tools/AlarmInfoServerSim/ViewModels/StatusBarViewModel.cs

@@ -5,12 +5,14 @@ using System.Net;
 
 namespace AlarmInfoServerSim.ViewModels;
 
-public partial class StatusBarViewModel : ObservableObject
+public partial class StatusBarViewModel : ObservableObject ,IDisposable
 {
     private readonly ILogService _logService; 
     private readonly ISharedConfig _sharedConfig;
     private readonly IInfoSendingService _infoSendingService;
 
+    private bool disposedValue;
+
     [ObservableProperty]
     private List<IPAddress> _iPAddresses;
 
@@ -27,10 +29,10 @@ public partial class StatusBarViewModel : ObservableObject
     private string _buttonContent;
 
     [ObservableProperty]
-    private IPAddress _remoteIp;
+    private string _remoteIp;
 
     [ObservableProperty]
-    private int _remotePort;
+    private string _remotePort;
 
     [ObservableProperty]
     private string _status;
@@ -43,14 +45,13 @@ public partial class StatusBarViewModel : ObservableObject
         _infoSendingService.TcpServerStatusChanged += OnTcpServerStatusChanged;
         _infoSendingService.ConnectionChanged += OnConnectionChanged;
 
-
         _iPAddresses = _sharedConfig.IPAddresses;
-        _isSelectingEnable=true;
+        _isSelectingEnable = true;
         _selectedIP = IPAddress.None;
         _selectedPort = 50052;
         _buttonContent = "Open";
-        _remoteIp =IPAddress.None;
-        _remotePort = -1;
+        _remoteIp = "Unknown";
+        _remotePort = "Unknown";
         _status = "Disconnected";
     }
 
@@ -94,17 +95,48 @@ public partial class StatusBarViewModel : ObservableObject
             if (e.Item1)
             {
                 Status = "Connected";
-                RemoteIp = e.Item2.RemoteEndPoint.Address;
-                RemotePort = e.Item2.RemoteEndPoint.Port;
+                RemoteIp = e.Item2.RemoteEndPoint.Address.ToString();
+                RemotePort = e.Item2.RemoteEndPoint.Port.ToString();
                 _logService.Log($"Get a connection from {RemoteIp}:{RemotePort}");
             }
             else
             {
                 Status = "Disconnected";
-                RemoteIp = IPAddress.None;
-                RemotePort = -1;
+                RemoteIp = "Unknown";
+                RemotePort = "Unknown";
                 _logService.Log($"The connection lost");
             }
         });
     }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (!disposedValue)
+        {
+            if (disposing)
+            {
+                // TODO: dispose managed state (managed objects)
+                _infoSendingService.TcpServerStatusChanged-=OnTcpServerStatusChanged;
+                _infoSendingService.ConnectionChanged -= OnConnectionChanged;
+            }
+
+            // 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
+    // ~StatusBarViewModel()
+    // {
+    //     // 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);
+    }
 }

+ 34 - 3
Tools/AlarmInfoServerSim/ViewModels/WorkAreaViewModel.cs

@@ -10,13 +10,15 @@ using System.Text;
 
 namespace AlarmInfoServerSim.ViewModels;
 
-public partial class WorkAreaViewModel : ObservableObject
+public partial class WorkAreaViewModel : ObservableObject, IDisposable
 {
     private readonly ILogService _logService;
     private readonly IInfoSendingService _infoSendingService;
 
     private readonly Hardwares _hardwares;
 
+    private bool disposedValue;
+
     [ObservableProperty]
     private ConcurrentDictionary<byte, Mini8Data> _mini8;
 
@@ -51,8 +53,6 @@ public partial class WorkAreaViewModel : ObservableObject
     [NotifyCanExecuteChangedFor(nameof(SendInfoCommand))]
     private bool _hasConnection;
 
-
-
     public WorkAreaViewModel(ILogService logService, ISharedConfig sharedConfig ,IInfoSendingService infoSendingService)
     {
         if(sharedConfig.Hardwares is null)
@@ -128,4 +128,35 @@ public partial class WorkAreaViewModel : ObservableObject
             ReceivedTlvData.Insert(0, e.Item2.DateTime.ToString() + $"  {fileName}");
         });
     }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (!disposedValue)
+        {
+            if (disposing)
+            {
+                // TODO: dispose managed state (managed objects)
+                _infoSendingService.ConnectionChanged -= OnConnectionChanged;
+                _infoSendingService.DataReceived -= OnDataReceived;
+            }
+
+            // 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
+    // ~WorkAreaViewModel()
+    // {
+    //     // 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);
+    }
 }

+ 1 - 5
Tools/PLCIOPointTool/Services/PLCCommunicator.cs

@@ -7,6 +7,7 @@ namespace PLCIOPointTool.Services;
 public class PLCCommunicator : ICommunicator, ITcpConnectNority, IDisposable
 {
     private readonly ILog? _log;
+
     private readonly AdsCommunicator _adsCommunicator;
     private const int _retryCount = 10;
     private bool _isConnected;
@@ -40,14 +41,11 @@ public class PLCCommunicator : ICommunicator, ITcpConnectNority, IDisposable
             _log?.Error("Failed to open Tcp Client");
             return;
         }
-
-        _log?.Info("Communicator started");
     }
 
     public void Close()
     {
         _adsCommunicator.Dispose();
-        _log?.Info("Communicator Closed");
     }
 
     public void GetValue(string symbolName, ValueType valueType)
@@ -158,14 +156,12 @@ public class PLCCommunicator : ICommunicator, ITcpConnectNority, IDisposable
 
     public void Connect(string ip, int port)
     {
-        _log?.Info($"Connect to {ip}:{port}");
         _isConnected = true;
         ConnectionStatusChanged?.Invoke(null, true);
     }
 
     public void DisConnect(string ip, int port)
     {
-        _log?.Info($"Disconnect from {ip}:{port}");
         _isConnected = false;
         ConnectionStatusChanged?.Invoke(null, false);
     }

+ 3 - 3
Tools/PLCIOPointTool/ViewModels/StatusBarViewModel.cs

@@ -31,8 +31,8 @@ public partial class StatusBarViewModel : ObservableObject, IDisposable
 
     public StatusBarViewModel(ICommunicator communicator)
     {
-        _netID = string.Empty;
-        _port = 531;
+        _netID = "192.168.250.10.1.1";
+        _port = 851;
         _isInputEnable = true;
         _buttonContent = "Connect";
         _isNotInOperating = true;
@@ -53,7 +53,7 @@ public partial class StatusBarViewModel : ObservableObject, IDisposable
                 case "Connect":
                     _communicator.Open(NetID, Port);
                     break;
-                case "DisConnect":
+                case "Disconnect":
                     _communicator.Close();
                     break;
                 default:

+ 3 - 3
Tools/PLCIOPointTool/Views/StatusBar.xaml

@@ -19,17 +19,17 @@
             <ColumnDefinition Width="180"/>
         </Grid.ColumnDefinitions>
         <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center" VerticalAlignment="Center">
-            <Label>AMSID:</Label>
+            <Label>AmsNetId:</Label>
             <TextBox Text="{Binding NetID}" IsEnabled="{Binding IsInputEnable}" Width="130" Height="20"/>
         </StackPanel>
         <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center" VerticalAlignment="Center">
             <Label>Port:</Label>
             <TextBox Text="{Binding Port}" IsEnabled="{Binding IsInputEnable}" Width="80" Height="20"/>
         </StackPanel>
-        <Button Content="{Binding ButtonContent}" Command="{Binding ConnectOrDisconnectCommand}" Grid.Row="0" Grid.Column="2" Height="20" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
-        <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
+        <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
             <Label>Status:</Label>
             <Label Content="{Binding Status}"></Label>
         </StackPanel>
+        <Button Content="{Binding ButtonContent}" Command="{Binding ConnectOrDisconnectCommand}" Grid.Row="1" Grid.Column="1" Height="20" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
     </Grid>
 </UserControl>