InfoSendingService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using RTCommunicatorBase;
  2. using TLVProtocal;
  3. using UniversalNetFrame451.IO;
  4. namespace AlarmInfoServerSim.Services;
  5. public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
  6. {
  7. private readonly ILogService _logService;
  8. private readonly ISharedConfig _sharedConfig;
  9. private readonly ITlvCommunicatorServer _server;
  10. private bool _isConnected;
  11. private bool _isTcpServerOpened;
  12. private bool disposedValue;
  13. public InfoSendingService(ILogService logService, ISharedConfig sharedConfig)
  14. {
  15. _logService = logService;
  16. _sharedConfig = sharedConfig;
  17. _server = TlvFactory.GetTlvServer();
  18. }
  19. public event EventHandler<bool>? TcpServerStatusChanged;
  20. public event EventHandler<(bool, TcpConnection)>? ConnectionChanged;
  21. public event EventHandler<(ushort, TlvData)>? DataReceived;
  22. public void Open()
  23. {
  24. if (_sharedConfig.SelectedIPAddress is null || _sharedConfig.SelectedPort is null)
  25. {
  26. _logService.Log("Failed to open Tcp server because IP or Port is not correct");
  27. return;
  28. }
  29. if (!_server.Initialize(this))
  30. {
  31. _logService.Log("Failed to initialize Tcp server");
  32. return;
  33. }
  34. if (!_server.Open(_sharedConfig.SelectedIPAddress.ToString(), (ushort)_sharedConfig.SelectedPort.Value))
  35. {
  36. _logService.Log($"Failed to start TCP server in {_sharedConfig.SelectedIPAddress.ToString()}:{_sharedConfig.SelectedPort.Value}");
  37. return;
  38. }
  39. _isTcpServerOpened = true;
  40. TcpServerStatusChanged?.Invoke(null, _isTcpServerOpened);
  41. }
  42. public void Close()
  43. {
  44. _server.Dispose();
  45. _isTcpServerOpened = false;
  46. TcpServerStatusChanged?.Invoke(null, _isTcpServerOpened);
  47. }
  48. public void Send(byte tag, ST_ALARM alarm)
  49. {
  50. if (!_isTcpServerOpened || !_isConnected)
  51. {
  52. _logService.Log("Cannot send information due to deactivated Tcp server or disconnected status");
  53. return;
  54. }
  55. if (!_server.Send(tag, alarm))
  56. {
  57. _logService.Log("Cannot send information due to unknown reason");
  58. }
  59. }
  60. public void Connected(TcpConnection connection)
  61. {
  62. _isConnected = true;
  63. ConnectionChanged?.Invoke(null, (_isConnected, connection));
  64. }
  65. public void Disconnected(TcpConnection connection)
  66. {
  67. _isConnected = false;
  68. ConnectionChanged?.Invoke(null, (_isConnected, connection));
  69. }
  70. public void Received(TlvData data)
  71. {
  72. if (data.RawData is null || data.RawData.Length == 0)
  73. {
  74. _logService.Log("Received RawData is null");
  75. return;
  76. }
  77. if (data.Tag == 1)
  78. {
  79. DataReceived?.Invoke(null, (data.Tag, data));
  80. }
  81. }
  82. public TlvData RequestReply(TlvData tlvData)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. protected virtual void Dispose(bool disposing)
  87. {
  88. if (!disposedValue)
  89. {
  90. if (disposing)
  91. {
  92. // TODO: dispose managed state (managed objects)
  93. _server.Dispose();
  94. _isTcpServerOpened = false;
  95. }
  96. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  97. // TODO: set large fields to null
  98. disposedValue = true;
  99. }
  100. }
  101. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  102. // ~InfoSendingService()
  103. // {
  104. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  105. // Dispose(disposing: false);
  106. // }
  107. public void Dispose()
  108. {
  109. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  110. Dispose(disposing: true);
  111. GC.SuppressFinalize(this);
  112. }
  113. }