SendInfoService.cs 2.8 KB

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