|
@@ -1,5 +1,4 @@
|
|
|
using RTCommunicatorBase;
|
|
|
-using System.Text;
|
|
|
using TLVProtocal;
|
|
|
using UniversalNetFrame451.IO;
|
|
|
|
|
@@ -12,6 +11,7 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
|
|
|
private readonly ITlvCommunicatorServer _server;
|
|
|
|
|
|
private bool _isConnected;
|
|
|
+ private bool _isTcpServerOpened;
|
|
|
private bool disposedValue;
|
|
|
|
|
|
public InfoSendingService(ILogService logService, ISharedConfig sharedConfig)
|
|
@@ -20,41 +20,53 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
|
|
|
_sharedConfig = sharedConfig;
|
|
|
|
|
|
_server = TlvFactory.GetTlvServer();
|
|
|
- if (!_server.Initialize(this))
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Failed to initialize tcp server");
|
|
|
- }
|
|
|
-
|
|
|
- Open();
|
|
|
}
|
|
|
|
|
|
+ public event EventHandler<bool>? TcpServerStatusChanged;
|
|
|
public event EventHandler<(bool, TcpConnection)>? ConnectionChanged;
|
|
|
-
|
|
|
public event EventHandler<(ushort, TlvData)>? DataReceived;
|
|
|
+
|
|
|
|
|
|
public void Open()
|
|
|
{
|
|
|
- if(_sharedConfig.BasicInfo is null)
|
|
|
+ if (_sharedConfig.SelectedIPAddress is null || _sharedConfig.SelectedPort is null)
|
|
|
{
|
|
|
- _logService.Log("Cannot find BasicInfo file! Please make sure it is existing!");
|
|
|
+ _logService.Log("Failed to open Tcp server because IP or Port is not correct");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (!_server.Open(_sharedConfig.BasicInfo.RTServerAddress, _sharedConfig.BasicInfo.RTServerPort))
|
|
|
+ if (!_server.Initialize(this))
|
|
|
{
|
|
|
- _logService.Log($"Failed to start TCP server in {_sharedConfig.BasicInfo.RTServerAddress}:{_sharedConfig.BasicInfo.RTServerPort}");
|
|
|
+ _logService.Log("Failed to initialize Tcp server");
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ if (!_server.Open(_sharedConfig.SelectedIPAddress.ToString(), (ushort)_sharedConfig.SelectedPort.Value))
|
|
|
+ {
|
|
|
+ _logService.Log($"Failed to start TCP server in {_sharedConfig.SelectedIPAddress.ToString()}:{_sharedConfig.SelectedPort.Value}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ _isTcpServerOpened = true;
|
|
|
+ TcpServerStatusChanged?.Invoke(null, _isTcpServerOpened);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Close()
|
|
|
+ {
|
|
|
+ _server.Dispose();
|
|
|
+ _isTcpServerOpened = false;
|
|
|
+ TcpServerStatusChanged?.Invoke(null, _isTcpServerOpened);
|
|
|
}
|
|
|
|
|
|
public void Send(byte tag, ST_ALARM alarm)
|
|
|
{
|
|
|
- if(!_isConnected)
|
|
|
+ if (!_isTcpServerOpened || !_isConnected)
|
|
|
{
|
|
|
- _logService.Log("Cannot send information due to disconnected status");
|
|
|
+ _logService.Log("Cannot send information due to deactivated Tcp server or disconnected status");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if(!_server.Send(tag, alarm))
|
|
|
+ if (!_server.Send(tag, alarm))
|
|
|
{
|
|
|
_logService.Log("Cannot send information due to unknown reason");
|
|
|
}
|
|
@@ -80,9 +92,9 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if(data.Tag==1)
|
|
|
+ if (data.Tag == 1)
|
|
|
{
|
|
|
- DataReceived?.Invoke(null,(data.Tag, data));
|
|
|
+ DataReceived?.Invoke(null, (data.Tag, data));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -99,6 +111,7 @@ public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
|
|
|
{
|
|
|
// TODO: dispose managed state (managed objects)
|
|
|
_server.Dispose();
|
|
|
+ _isTcpServerOpened = false;
|
|
|
}
|
|
|
|
|
|
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
|