InfoSendingService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using RTCommunicatorBase;
  2. using System.Text;
  3. using TLVProtocal;
  4. using UniversalNetFrame451.IO;
  5. namespace AlarmInfoServerSim.Services
  6. {
  7. public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
  8. {
  9. private readonly ISharedConfig _sharedConfig;
  10. private readonly ITlvCommunicatorServer _server;
  11. private bool _isConnected = false;
  12. private bool disposedValue;
  13. public InfoSendingService(ISharedConfig sharedConfig)
  14. {
  15. _sharedConfig= sharedConfig;
  16. _server = TlvFactory.GetTlvServer();
  17. _server.Initialize(this);
  18. Open();
  19. }
  20. public event EventHandler<bool>? ConnectionStatusChanged;
  21. public event EventHandler<TcpConnection>? TcpConnectionChanged;
  22. public event EventHandler<(ushort, TlvData)>? DataReceived;
  23. public bool Open()
  24. {
  25. if (_sharedConfig.BasicInfo is not null)
  26. {
  27. return _server.Open(_sharedConfig.BasicInfo.RTServerAddress, _sharedConfig.BasicInfo.RTServerPort);
  28. }
  29. return false;
  30. }
  31. public bool Send(byte tag, ST_ALARM alarm)
  32. {
  33. if (_isConnected)
  34. {
  35. return _server.Send(tag, alarm);
  36. }
  37. return false;
  38. }
  39. public void Connected(TcpConnection connection)
  40. {
  41. _isConnected = true;
  42. ConnectionStatusChanged?.Invoke(null, _isConnected);
  43. TcpConnectionChanged?.Invoke(null, connection);
  44. }
  45. public void Disconnected(TcpConnection connection)
  46. {
  47. _isConnected = false;
  48. ConnectionStatusChanged?.Invoke(null, _isConnected);
  49. TcpConnectionChanged?.Invoke(null, connection);
  50. }
  51. public void Received(TlvData data)
  52. {
  53. if (data.RawData is null || data.RawData.Length == 0)
  54. {
  55. //Log
  56. return;
  57. }
  58. if(data.Tag==1)
  59. {
  60. DataReceived?.Invoke(null,(data.Tag, data));
  61. }
  62. }
  63. public TlvData RequestReply(TlvData tlvData)
  64. {
  65. throw new NotImplementedException();
  66. }
  67. protected virtual void Dispose(bool disposing)
  68. {
  69. if (!disposedValue)
  70. {
  71. if (disposing)
  72. {
  73. // TODO: 释放托管状态(托管对象)
  74. _server.Dispose();
  75. }
  76. // TODO: 释放未托管的资源(未托管的对象)并重写终结器
  77. // TODO: 将大型字段设置为 null
  78. disposedValue = true;
  79. }
  80. }
  81. // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
  82. // ~SendInfoService()
  83. // {
  84. // // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  85. // Dispose(disposing: false);
  86. // }
  87. public void Dispose()
  88. {
  89. // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  90. Dispose(disposing: true);
  91. GC.SuppressFinalize(this);
  92. }
  93. }
  94. }