diff --git a/src/JT808.DotNetty.Abstractions/Dtos/JT808UdpSessionInfoDto.cs b/src/JT808.DotNetty.Abstractions/Dtos/JT808UdpSessionInfoDto.cs new file mode 100644 index 0000000..a7d3f25 --- /dev/null +++ b/src/JT808.DotNetty.Abstractions/Dtos/JT808UdpSessionInfoDto.cs @@ -0,0 +1,24 @@ +using System; + +namespace JT808.DotNetty.Abstractions.Dtos +{ + public class JT808UdpSessionInfoDto + { + /// + /// 最后上线时间 + /// + public DateTime LastActiveTime { get; set; } + /// + /// 上线时间 + /// + public DateTime StartTime { get; set; } + /// + /// 终端手机号 + /// + public string TerminalPhoneNo { get; set; } + /// + /// 远程ip地址 + /// + public string RemoteAddressIP { get; set; } + } +} diff --git a/src/JT808.DotNetty.Abstractions/IJT808SessionPublishing.cs b/src/JT808.DotNetty.Abstractions/IJT808SessionPublishing.cs index 13968c6..3a8e56c 100644 --- a/src/JT808.DotNetty.Abstractions/IJT808SessionPublishing.cs +++ b/src/JT808.DotNetty.Abstractions/IJT808SessionPublishing.cs @@ -7,6 +7,6 @@ namespace JT808.DotNetty.Abstractions /// public interface IJT808SessionPublishing { - Task PublishAsync(string topicName, string key, string value); + Task PublishAsync(string topicName, string value); } } diff --git a/src/JT808.DotNetty.Core/Impls/JT808SessionPublishingEmptyImpl.cs b/src/JT808.DotNetty.Core/Impls/JT808SessionPublishingEmptyImpl.cs index 7c5d99b..af7aa6c 100644 --- a/src/JT808.DotNetty.Core/Impls/JT808SessionPublishingEmptyImpl.cs +++ b/src/JT808.DotNetty.Core/Impls/JT808SessionPublishingEmptyImpl.cs @@ -5,7 +5,7 @@ namespace JT808.DotNetty.Core { internal class JT808SessionPublishingEmptyImpl : IJT808SessionPublishing { - public Task PublishAsync(string topicName, string key, string value) + public Task PublishAsync(string topicName, string value) { return Task.CompletedTask; } diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs new file mode 100644 index 0000000..8e9052c --- /dev/null +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs @@ -0,0 +1,25 @@ +using JT808.DotNetty.Abstractions.Dtos; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.DotNetty.Core.Interfaces +{ + /// + /// JT808 Udp会话服务 + /// + public interface IJT808UdpSessionService + { + /// + /// 获取会话集合 + /// + /// + JT808ResultDto> GetAll(); + /// + /// 通过设备终端号移除对应会话 + /// + /// + /// + JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo); + } +} diff --git a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs index 5e20969..010ba24 100644 --- a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs @@ -2,6 +2,7 @@ using JT808.DotNetty.Core.Configurations; using JT808.DotNetty.Core.Impls; using JT808.DotNetty.Core.Interfaces; +using JT808.DotNetty.Core.Services; using JT808.DotNetty.Internal; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -21,6 +22,8 @@ namespace JT808.DotNetty.Core serviceDescriptors.TryAddSingleton(); serviceDescriptors.TryAddSingleton(); serviceDescriptors.TryAddSingleton(); + serviceDescriptors.TryAddSingleton(); + serviceDescriptors.TryAddSingleton(); return serviceDescriptors; } } diff --git a/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs b/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs new file mode 100644 index 0000000..975108c --- /dev/null +++ b/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using JT808.DotNetty.Abstractions.Dtos; +using JT808.DotNetty.Core.Interfaces; + +namespace JT808.DotNetty.Core.Services +{ + internal class JT808UdpSessionService : IJT808UdpSessionService + { + private readonly JT808UdpSessionManager jT808SessionManager; + + public JT808UdpSessionService( + JT808UdpSessionManager jT808SessionManager) + { + this.jT808SessionManager = jT808SessionManager; + } + + public JT808ResultDto> GetAll() + { + JT808ResultDto> resultDto = new JT808ResultDto>(); + try + { + resultDto.Data = jT808SessionManager.GetAll().Select(s => new JT808UdpSessionInfoDto + { + LastActiveTime = s.LastActiveTime, + StartTime = s.StartTime, + TerminalPhoneNo = s.TerminalPhoneNo, + RemoteAddressIP = s.Sender.ToString() + }).ToList(); + resultDto.Code = JT808ResultCode.Ok; + } + catch (Exception ex) + { + resultDto.Data = null; + resultDto.Code = JT808ResultCode.Error; + resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); + } + return resultDto; + } + + public JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo) + { + JT808ResultDto resultDto = new JT808ResultDto(); + try + { + var session = jT808SessionManager.RemoveSession(terminalPhoneNo); + if (session != null) + { + if(session.Channel.Open) + { + session.Channel.CloseAsync(); + } + } + resultDto.Code = JT808ResultCode.Ok; + resultDto.Data = true; + } + catch (AggregateException ex) + { + resultDto.Data = false; + resultDto.Code = 500; + resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); + } + catch (Exception ex) + { + resultDto.Data = false; + resultDto.Code = JT808ResultCode.Error; + resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); + } + return resultDto; + } + } +} diff --git a/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs b/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs index 293e950..82abe77 100644 --- a/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs +++ b/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs @@ -1,4 +1,5 @@ using DotNetty.Buffers; +using DotNetty.Transport.Channels.Sockets; using JT808.DotNetty.Abstractions.Dtos; using JT808.DotNetty.Core; using JT808.DotNetty.Core.Interfaces; @@ -25,7 +26,7 @@ namespace JT808.DotNetty.Internal { if (session.Channel.Open) { - session.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); + session.Channel.WriteAndFlushAsync(new DatagramPacket(Unpooled.WrappedBuffer(data), session.Sender)); resultDto.Code = JT808ResultCode.Ok; resultDto.Data = true; } diff --git a/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs index d1c9308..7c7cbcb 100644 --- a/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs +++ b/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs @@ -74,7 +74,7 @@ namespace JT808.DotNetty.Core //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 //todo: 有设备关联上来可以进行通知 例如:使用Redis发布订阅 - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline,null, appSession.TerminalPhoneNo); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, appSession.TerminalPhoneNo); } } @@ -100,7 +100,7 @@ namespace JT808.DotNetty.Core } string nos = string.Join(",", terminalPhoneNos); logger.LogInformation($">>>{terminalPhoneNo}-{nos} 1-n Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, nos); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); return jT808Session; } else @@ -108,7 +108,7 @@ namespace JT808.DotNetty.Core if (SessionIdDict.TryRemove(terminalPhoneNo, out JT808TcpSession jT808SessionRemove)) { logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, terminalPhoneNo); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline,terminalPhoneNo); return jT808SessionRemove; } else @@ -129,7 +129,7 @@ namespace JT808.DotNetty.Core } string nos = string.Join(",", terminalPhoneNos); logger.LogInformation($">>>{nos} Channel Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, nos); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline,nos); } public IEnumerable GetAll() diff --git a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs index 84415ec..850dbdf 100644 --- a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs +++ b/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs @@ -10,6 +10,7 @@ namespace JT808.DotNetty.Core { /// /// JT808 udp会话管理 + /// todo:估计要轮询下 /// public class JT808UdpSessionManager { @@ -63,7 +64,7 @@ namespace JT808.DotNetty.Core //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 //todo: 有设备关联上来可以进行通知 例如:使用Redis发布订阅 - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline,null, appSession.TerminalPhoneNo); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, appSession.TerminalPhoneNo); } } @@ -99,7 +100,7 @@ namespace JT808.DotNetty.Core } string nos = string.Join(",", terminalPhoneNos); logger.LogInformation($">>>{terminalPhoneNo}-{nos} 1-n Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, nos); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); return jT808Session; } else @@ -107,7 +108,7 @@ namespace JT808.DotNetty.Core if (SessionIdDict.TryRemove(terminalPhoneNo, out JT808UdpSession jT808SessionRemove)) { logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, terminalPhoneNo); + jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline,terminalPhoneNo); return jT808SessionRemove; } else diff --git a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs index b43117b..30e4ea2 100644 --- a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs @@ -1,6 +1,7 @@ using JT808.DotNetty.Codecs; using JT808.DotNetty.Core; using JT808.DotNetty.Core.Handlers; +using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Services; using JT808.DotNetty.Udp; using JT808.DotNetty.Udp.Handlers; diff --git a/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs b/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs index 952f3ee..a70eba9 100644 --- a/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs +++ b/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs @@ -24,6 +24,8 @@ namespace JT808.DotNetty.WebApi.Handlers private readonly IJT808TcpSessionService jT808TcpSessionService; + private readonly IJT808UdpSessionService jT808UdpSessionService; + private readonly IJT808UnificationTcpSendService jT808UnificationTcpSendService; private readonly IJT808UnificationUdpSendService jT808UnificationUdpSendService; @@ -51,10 +53,12 @@ namespace JT808.DotNetty.WebApi.Handlers /// /// public JT808MsgIdDefaultWebApiHandler( + IJT808UdpSessionService jT808UdpSessionService, IJT808UnificationUdpSendService jT808UnificationUdpSendService, JT808UdpAtomicCounterService jT808UdpAtomicCounterService ) { + this.jT808UdpSessionService = jT808UdpSessionService; this.jT808UnificationUdpSendService = jT808UnificationUdpSendService; this.jT808UdpAtomicCounterService = jT808UdpAtomicCounterService; InitUdpRoute(); @@ -69,11 +73,13 @@ namespace JT808.DotNetty.WebApi.Handlers IJT808UnificationTcpSendService jT808UnificationTcpSendService, IJT808UnificationUdpSendService jT808UnificationUdpSendService, IJT808TcpSessionService jT808TcpSessionService, + IJT808UdpSessionService jT808UdpSessionService, JT808TransmitAddressFilterService jT808TransmitAddressFilterService, JT808TcpAtomicCounterService jT808TcpAtomicCounterService, JT808UdpAtomicCounterService jT808UdpAtomicCounterService ) { + this.jT808UdpSessionService = jT808UdpSessionService; this.jT808UnificationTcpSendService = jT808UnificationTcpSendService; this.jT808UnificationUdpSendService = jT808UnificationUdpSendService; this.jT808TcpSessionService = jT808TcpSessionService; @@ -89,7 +95,7 @@ namespace JT808.DotNetty.WebApi.Handlers /// /// /// - public JT808HttpResponse GetSessionAll(JT808HttpRequest request) + public JT808HttpResponse GetTcpSessionAll(JT808HttpRequest request) { var result = jT808TcpSessionService.GetAll(); return CreateJT808HttpResponse(result); @@ -100,7 +106,7 @@ namespace JT808.DotNetty.WebApi.Handlers /// /// /// - public JT808HttpResponse RemoveByTerminalPhoneNo(JT808HttpRequest request) + public JT808HttpResponse RemoveTcpSessionByTerminalPhoneNo(JT808HttpRequest request) { if (string.IsNullOrEmpty(request.Json)) { @@ -110,6 +116,32 @@ namespace JT808.DotNetty.WebApi.Handlers return CreateJT808HttpResponse(result); } + /// + /// 会话服务集合 + /// + /// + /// + public JT808HttpResponse GetUdpSessionAll(JT808HttpRequest request) + { + var result = jT808UdpSessionService.GetAll(); + return CreateJT808HttpResponse(result); + } + + /// + /// 会话服务-通过设备终端号移除对应会话 + /// + /// + /// + public JT808HttpResponse RemoveUdpSessionByTerminalPhoneNo(JT808HttpRequest request) + { + if (string.IsNullOrEmpty(request.Json)) + { + return EmptyHttpResponse(); + } + var result = jT808UdpSessionService.RemoveByTerminalPhoneNo(request.Json); + return CreateJT808HttpResponse(result); + } + /// /// 添加转发过滤地址 /// @@ -222,8 +254,8 @@ namespace JT808.DotNetty.WebApi.Handlers CreateRoute($"{transmitPrefix}/Remove", RemoveTransmitAddress); CreateRoute($"{transmitPrefix}/GetAll", GetTransmitAll); CreateRoute($"GetTcpAtomicCounter", GetTcpAtomicCounter); - CreateRoute($"{sessionRoutePrefix}/GetAll", GetSessionAll); - CreateRoute($"{sessionRoutePrefix}/RemoveByTerminalPhoneNo", RemoveByTerminalPhoneNo); + CreateRoute($"{sessionRoutePrefix}/Tcp/GetAll", GetTcpSessionAll); + CreateRoute($"{sessionRoutePrefix}/Tcp/RemoveByTerminalPhoneNo", RemoveTcpSessionByTerminalPhoneNo); CreateRoute($"UnificationTcpSend", UnificationTcpSend); } @@ -231,6 +263,8 @@ namespace JT808.DotNetty.WebApi.Handlers { CreateRoute($"GetUdpAtomicCounter", GetUdpAtomicCounter); CreateRoute($"UnificationUdpSend", UnificationUdpSend); + CreateRoute($"{sessionRoutePrefix}/Udp/GetAll", GetUdpSessionAll); + CreateRoute($"{sessionRoutePrefix}/Udp/RemoveByTerminalPhoneNo", RemoveUdpSessionByTerminalPhoneNo); } } }