123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using RTCommunicatorBase;
- using TLVProtocal;
- using UniversalNetFrame451.IO;
- 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 _isTcpServerOpened;
- private bool disposedValue;
- public InfoSendingService(ILogService logService, ISharedConfig sharedConfig)
- {
- _logService = logService;
- _sharedConfig = sharedConfig;
- _server = TlvFactory.GetTlvServer();
- }
- public event EventHandler<bool>? TcpServerStatusChanged;
- public event EventHandler<(bool, TcpConnection)>? ConnectionChanged;
- public event EventHandler<(ushort, TlvData)>? DataReceived;
-
- public void Open()
- {
- if (_sharedConfig.SelectedIPAddress is null || _sharedConfig.SelectedPort is null)
- {
- _logService.Log("Failed to open Tcp server because IP or Port is not correct");
- return;
- }
- if (!_server.Initialize(this))
- {
- _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 (!_isTcpServerOpened || !_isConnected)
- {
- _logService.Log("Cannot send information due to deactivated Tcp server or disconnected status");
- return;
- }
- if (!_server.Send(tag, alarm))
- {
- _logService.Log("Cannot send information due to unknown reason");
- }
- }
- public void Connected(TcpConnection connection)
- {
- _isConnected = true;
- ConnectionChanged?.Invoke(null, (_isConnected, connection));
- }
- public void Disconnected(TcpConnection connection)
- {
- _isConnected = false;
- ConnectionChanged?.Invoke(null, (_isConnected, connection));
- }
- public void Received(TlvData data)
- {
- if (data.RawData is null || data.RawData.Length == 0)
- {
- _logService.Log("Received RawData is null");
- return;
- }
- if (data.Tag == 1)
- {
- DataReceived?.Invoke(null, (data.Tag, data));
- }
- }
- public TlvData RequestReply(TlvData tlvData)
- {
- throw new NotImplementedException();
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- // TODO: dispose managed state (managed objects)
- _server.Dispose();
- _isTcpServerOpened = false;
- }
- // 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
- // ~InfoSendingService()
- // {
- // // 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);
- }
- }
|