InfoSendingService.cs 2.9 KB

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