@@ -0,0 +1,24 @@ | |||
using System; | |||
namespace JT808.DotNetty.Abstractions.Dtos | |||
{ | |||
public class JT808UdpSessionInfoDto | |||
{ | |||
/// <summary> | |||
/// 最后上线时间 | |||
/// </summary> | |||
public DateTime LastActiveTime { get; set; } | |||
/// <summary> | |||
/// 上线时间 | |||
/// </summary> | |||
public DateTime StartTime { get; set; } | |||
/// <summary> | |||
/// 终端手机号 | |||
/// </summary> | |||
public string TerminalPhoneNo { get; set; } | |||
/// <summary> | |||
/// 远程ip地址 | |||
/// </summary> | |||
public string RemoteAddressIP { get; set; } | |||
} | |||
} |
@@ -7,6 +7,6 @@ namespace JT808.DotNetty.Abstractions | |||
/// </summary> | |||
public interface IJT808SessionPublishing | |||
{ | |||
Task PublishAsync(string topicName, string key, string value); | |||
Task PublishAsync(string topicName, string value); | |||
} | |||
} |
@@ -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; | |||
} | |||
@@ -0,0 +1,25 @@ | |||
using JT808.DotNetty.Abstractions.Dtos; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace JT808.DotNetty.Core.Interfaces | |||
{ | |||
/// <summary> | |||
/// JT808 Udp会话服务 | |||
/// </summary> | |||
public interface IJT808UdpSessionService | |||
{ | |||
/// <summary> | |||
/// 获取会话集合 | |||
/// </summary> | |||
/// <returns></returns> | |||
JT808ResultDto<List<JT808UdpSessionInfoDto>> GetAll(); | |||
/// <summary> | |||
/// 通过设备终端号移除对应会话 | |||
/// </summary> | |||
/// <param name="terminalPhoneNo"></param> | |||
/// <returns></returns> | |||
JT808ResultDto<bool> RemoveByTerminalPhoneNo(string terminalPhoneNo); | |||
} | |||
} |
@@ -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<IJT808SourcePackageDispatcher, JT808SourcePackageDispatcherEmptyImpl>(); | |||
serviceDescriptors.TryAddSingleton<IJT808UnificationTcpSendService, JT808UnificationTcpSendService>(); | |||
serviceDescriptors.TryAddSingleton<IJT808UnificationUdpSendService, JT808UnificationUdpSendService>(); | |||
serviceDescriptors.TryAddSingleton<IJT808TcpSessionService, JT808TcpSessionService>(); | |||
serviceDescriptors.TryAddSingleton<IJT808UdpSessionService, JT808UdpSessionService>(); | |||
return serviceDescriptors; | |||
} | |||
} |
@@ -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<List<JT808UdpSessionInfoDto>> GetAll() | |||
{ | |||
JT808ResultDto<List<JT808UdpSessionInfoDto>> resultDto = new JT808ResultDto<List<JT808UdpSessionInfoDto>>(); | |||
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<bool> RemoveByTerminalPhoneNo(string terminalPhoneNo) | |||
{ | |||
JT808ResultDto<bool> resultDto = new JT808ResultDto<bool>(); | |||
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; | |||
} | |||
} | |||
} |
@@ -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; | |||
} | |||
@@ -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<JT808TcpSession> GetAll() | |||
@@ -10,6 +10,7 @@ namespace JT808.DotNetty.Core | |||
{ | |||
/// <summary> | |||
/// JT808 udp会话管理 | |||
/// todo:估计要轮询下 | |||
/// </summary> | |||
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 | |||
@@ -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; | |||
@@ -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 | |||
/// </summary> | |||
/// <param name="jT808UdpAtomicCounterService"></param> | |||
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 | |||
/// </summary> | |||
/// <param name="request"></param> | |||
/// <returns></returns> | |||
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 | |||
/// </summary> | |||
/// <param name="request"></param> | |||
/// <returns></returns> | |||
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); | |||
} | |||
/// <summary> | |||
/// 会话服务集合 | |||
/// </summary> | |||
/// <param name="request"></param> | |||
/// <returns></returns> | |||
public JT808HttpResponse GetUdpSessionAll(JT808HttpRequest request) | |||
{ | |||
var result = jT808UdpSessionService.GetAll(); | |||
return CreateJT808HttpResponse(result); | |||
} | |||
/// <summary> | |||
/// 会话服务-通过设备终端号移除对应会话 | |||
/// </summary> | |||
/// <param name="request"></param> | |||
/// <returns></returns> | |||
public JT808HttpResponse RemoveUdpSessionByTerminalPhoneNo(JT808HttpRequest request) | |||
{ | |||
if (string.IsNullOrEmpty(request.Json)) | |||
{ | |||
return EmptyHttpResponse(); | |||
} | |||
var result = jT808UdpSessionService.RemoveByTerminalPhoneNo(request.Json); | |||
return CreateJT808HttpResponse(result); | |||
} | |||
/// <summary> | |||
/// 添加转发过滤地址 | |||
/// </summary> | |||
@@ -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); | |||
} | |||
} | |||
} |