You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

116 lines
3.8 KiB

  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using DotNetty.Transport.Channels;
  7. using Microsoft.Extensions.Options;
  8. using System.Net;
  9. using JT1078.Gateway.Metadata;
  10. namespace JT1078.Gateway.Session
  11. {
  12. /// <summary>
  13. /// JT1078 udp会话管理
  14. /// 估计要轮询下
  15. /// </summary>
  16. public class JT1078UdpSessionManager
  17. {
  18. private readonly ILogger<JT1078UdpSessionManager> logger;
  19. public JT1078UdpSessionManager(
  20. ILoggerFactory loggerFactory)
  21. {
  22. logger = loggerFactory.CreateLogger<JT1078UdpSessionManager>();
  23. }
  24. private ConcurrentDictionary<string, JT1078UdpSession> SessionIdDict = new ConcurrentDictionary<string, JT1078UdpSession>(StringComparer.OrdinalIgnoreCase);
  25. public int SessionCount
  26. {
  27. get
  28. {
  29. return SessionIdDict.Count;
  30. }
  31. }
  32. public JT1078UdpSession GetSession(string terminalPhoneNo)
  33. {
  34. if (string.IsNullOrEmpty(terminalPhoneNo))
  35. return default;
  36. if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT1078UdpSession targetSession))
  37. {
  38. return targetSession;
  39. }
  40. else
  41. {
  42. return default;
  43. }
  44. }
  45. public void TryAdd(IChannel channel,EndPoint sender,string terminalPhoneNo)
  46. {
  47. //1.先判断是否在缓存里面
  48. if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT1078UdpSession UdpSession))
  49. {
  50. UdpSession.LastActiveTime=DateTime.Now;
  51. UdpSession.Sender = sender;
  52. UdpSession.Channel = channel;
  53. SessionIdDict.TryUpdate(terminalPhoneNo, UdpSession, UdpSession);
  54. }
  55. else
  56. {
  57. SessionIdDict.TryAdd(terminalPhoneNo, new JT1078UdpSession(channel, sender, terminalPhoneNo));
  58. }
  59. }
  60. public void Heartbeat(string terminalPhoneNo)
  61. {
  62. if (string.IsNullOrEmpty(terminalPhoneNo)) return;
  63. if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT1078UdpSession oldSession))
  64. {
  65. oldSession.LastActiveTime = DateTime.Now;
  66. SessionIdDict.TryUpdate(terminalPhoneNo, oldSession, oldSession);
  67. }
  68. }
  69. public JT1078UdpSession RemoveSession(string terminalPhoneNo)
  70. {
  71. //设备离线可以进行通知
  72. //使用Redis 发布订阅
  73. if (string.IsNullOrEmpty(terminalPhoneNo)) return default;
  74. if (SessionIdDict.TryRemove(terminalPhoneNo, out JT1078UdpSession SessionRemove))
  75. {
  76. logger.LogInformation($">>>{terminalPhoneNo} Session Remove.");
  77. return SessionRemove;
  78. }
  79. else
  80. {
  81. return default;
  82. }
  83. }
  84. public void RemoveSessionByChannel(IChannel channel)
  85. {
  86. //设备离线可以进行通知
  87. //使用Redis 发布订阅
  88. var terminalPhoneNos = SessionIdDict.Where(w => w.Value.Channel.Id == channel.Id).Select(s => s.Key).ToList();
  89. if (terminalPhoneNos.Count > 0)
  90. {
  91. foreach (var key in terminalPhoneNos)
  92. {
  93. SessionIdDict.TryRemove(key, out JT1078UdpSession SessionRemove);
  94. }
  95. string nos = string.Join(",", terminalPhoneNos);
  96. logger.LogInformation($">>>{nos} Channel Remove.");
  97. }
  98. }
  99. public IEnumerable<JT1078UdpSession> GetAll()
  100. {
  101. return SessionIdDict.Select(s => s.Value).ToList();
  102. }
  103. }
  104. }