RTCommunicator_TLV.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using RTCommunicatorBase;
  2. using System.Collections.Concurrent;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using TLVProtocal;
  7. using UniversalNetFrame451;
  8. using UniversalNetFrame451.IO;
  9. namespace RTCommunicatorTLV;
  10. public class RTCommunicator_TLV : IRTMini8Sender, ITlvProvider
  11. {
  12. private IRTMini8Provider _provider;
  13. private ITlvCommunicatorClient _client;
  14. private readonly ConcurrentDictionary<byte, Timer> _DisconnectNotifyTimer = [];
  15. public bool Initialize(IRTMini8Provider provider)
  16. {
  17. if (provider is null)
  18. return false;
  19. if (this._provider is not null)
  20. return false;
  21. this._provider = provider;
  22. return true;
  23. }
  24. public bool StartService(string ip, int port)
  25. {
  26. if (_provider is null)
  27. return false;
  28. if (this._client is not null)
  29. return false;
  30. if (string.IsNullOrEmpty(ip) || port < 5000 || port > ushort.MaxValue)
  31. return false;
  32. ITlvCommunicatorClient client = TlvFactory.GetTlvClient();
  33. if (!client.Initialize(this, false, 500))
  34. return false;
  35. Task.Factory.StartNew(() =>
  36. {
  37. while (!client.Open(ip, (ushort)port))
  38. {
  39. Thread.Sleep(1000);
  40. }
  41. this._client = client;
  42. this._provider?.Connected(ip, port);
  43. });
  44. return true;
  45. }
  46. public bool CloseService()
  47. {
  48. this._client?.Dispose();
  49. this._client = null;
  50. this._provider = null;
  51. return true;
  52. }
  53. public bool SelectConfigFile(string name)
  54. {
  55. if (this._client is null)
  56. return false;
  57. if (string.IsNullOrEmpty(name))
  58. return false;
  59. byte[] bytes = Encoding.UTF8.GetBytes(name);
  60. TlvData data = new(1, bytes);
  61. return this._client?.SendNotify(data) == true;
  62. }
  63. void ITlvProvider.Received(TlvData data)
  64. {
  65. if (data.RawData is null || data.RawData.Length == 0)
  66. return;
  67. Task.Factory.StartNew(() =>
  68. {
  69. switch (data.Tag)
  70. {
  71. case Tags.CurrentTempConfigFile:
  72. string fileName = Encoding.UTF8.GetString(data.RawData);
  73. if (string.IsNullOrEmpty(fileName))
  74. return;
  75. this._provider?.CurrentTempConfigFile(fileName);
  76. break;
  77. case Tags.ChannelAlarmNotify:
  78. if (!StructConverter.TryGetStruct(data.RawData, out ST_ALARM? alarm) || alarm is null)
  79. return;
  80. this._provider?.ChannelAlarmNotify(alarm.Value);
  81. break;
  82. case Tags.Mini8ConnectNotify:
  83. {
  84. byte index = data.RawData[0];
  85. this._DisconnectNotifyTimer.TryRemove(index, out Timer timer);
  86. timer?.Dispose();
  87. this._provider?.Mini8ConnectNotify(index);
  88. break;
  89. }
  90. case Tags.Mini8DisconnectNotify:
  91. {
  92. byte index = data.RawData[0];
  93. if (!this._DisconnectNotifyTimer.ContainsKey(index))
  94. this._DisconnectNotifyTimer[index] = new(DisconnectNotifyTimerCallBack, index, 0, 1000);
  95. break;
  96. }
  97. default:
  98. break;
  99. }
  100. });
  101. }
  102. TlvData ITlvProvider.RequestReply(TlvData tlvData)
  103. {
  104. return tlvData;
  105. }
  106. void ITlvProvider.Connected(TcpConnection connection)
  107. {
  108. this._provider.Connected(connection.RemoteEndPoint.Address.ToString(), connection.RemoteEndPoint.Port);
  109. }
  110. void ITlvProvider.Disconnected(TcpConnection connection)
  111. {
  112. this._provider.DisConnected(connection.RemoteEndPoint.Address.ToString(), connection.RemoteEndPoint.Port);
  113. }
  114. private void DisconnectNotifyTimerCallBack(object state)
  115. {
  116. if (state is not byte index)
  117. return;
  118. this._provider?.Mini8DisconnectNotify(index);
  119. }
  120. }