InfoSendingService.cs 3.4 KB

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