@@ -2,7 +2,7 @@ | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Text; | using System.Text; | ||||
namespace JT808.Gateway.Configurations | |||||
namespace JT808.Gateway.Abstractions.Configurations | |||||
{ | { | ||||
public class JT808Configuration | public class JT808Configuration | ||||
{ | { | ||||
@@ -19,7 +19,7 @@ namespace JT808.Gateway.Configurations | |||||
/// Tcp读超时 | /// Tcp读超时 | ||||
/// 默认10分钟检查一次 | /// 默认10分钟检查一次 | ||||
/// </summary> | /// </summary> | ||||
public int TcpReaderIdleTimeSeconds { get; set; } = 60*10; | |||||
public int TcpReaderIdleTimeSeconds { get; set; } = 60 * 10; | |||||
/// <summary> | /// <summary> | ||||
/// Tcp 60s检查一次 | /// Tcp 60s检查一次 | ||||
/// </summary> | /// </summary> | ||||
@@ -32,5 +32,9 @@ namespace JT808.Gateway.Configurations | |||||
/// Udp 60s检查一次 | /// Udp 60s检查一次 | ||||
/// </summary> | /// </summary> | ||||
public int UdpReceiveTimeoutCheckTimeSeconds { get; set; } = 60; | public int UdpReceiveTimeoutCheckTimeSeconds { get; set; } = 60; | ||||
/// <summary> | |||||
/// 网关不做消息业务处理,往队列发送 | |||||
/// </summary> | |||||
public List<uint> FilterMsgIdHandlerForQueue { get; set; } | |||||
} | } | ||||
} | } |
@@ -0,0 +1,15 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Threading; | |||||
namespace JT808.Gateway.Abstractions | |||||
{ | |||||
public interface IJT808MsgReplyLoggingConsumer : IJT808PubSub, IDisposable | |||||
{ | |||||
void OnMessage(Action<(string TerminalNo, byte[] Data)> callback); | |||||
CancellationTokenSource Cts { get; } | |||||
void Subscribe(); | |||||
void Unsubscribe(); | |||||
} | |||||
} |
@@ -0,0 +1,17 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace JT808.Gateway.Abstractions | |||||
{ | |||||
public interface IJT808MsgReplyLoggingProducer : IJT808PubSub, IDisposable | |||||
{ | |||||
/// <summary> | |||||
/// | |||||
/// </summary> | |||||
/// <param name="terminalNo">设备终端号</param> | |||||
/// <param name="data">808 hex data</param> | |||||
ValueTask ProduceAsync(string terminalNo, byte[] data); | |||||
} | |||||
} |
@@ -35,6 +35,8 @@ | |||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="JT808" Version="2.2.12" /> | <PackageReference Include="JT808" Version="2.2.12" /> | ||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.7" /> | <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.7" /> | ||||
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.7" /> | |||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.7" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="..\..\LICENSE" Pack="true" PackagePath="" /> | <None Include="..\..\LICENSE" Pack="true" PackagePath="" /> | ||||
@@ -5,6 +5,9 @@ using JT808.Protocol.MessageBody; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Text; | using System.Text; | ||||
using System.Threading.Tasks; | |||||
using Microsoft.Extensions.Options; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
namespace JT808.Gateway.Abstractions | namespace JT808.Gateway.Abstractions | ||||
{ | { | ||||
@@ -17,9 +20,19 @@ namespace JT808.Gateway.Abstractions | |||||
protected delegate byte[] MsgIdMethodDelegate(JT808HeaderPackage package, IJT808Session session); | protected delegate byte[] MsgIdMethodDelegate(JT808HeaderPackage package, IJT808Session session); | ||||
protected JT808Serializer JT808Serializer { get; } | protected JT808Serializer JT808Serializer { get; } | ||||
public JT808MessageHandler(IJT808Config jT808Config) | |||||
protected IJT808MsgProducer MsgProducer; | |||||
protected IOptionsMonitor<JT808Configuration> JT808ConfigurationOptionsMonitor; | |||||
public JT808MessageHandler( | |||||
IOptionsMonitor<JT808Configuration> jT808ConfigurationOptionsMonitor, | |||||
IJT808MsgProducer msgProducer, | |||||
IJT808Config jT808Config) | |||||
{ | { | ||||
this.JT808Serializer = jT808Config.GetSerializer(); | this.JT808Serializer = jT808Config.GetSerializer(); | ||||
this.MsgProducer = msgProducer; | |||||
this.JT808ConfigurationOptionsMonitor = jT808ConfigurationOptionsMonitor; | |||||
HandlerDict = new Dictionary<ushort, MsgIdMethodDelegate> { | HandlerDict = new Dictionary<ushort, MsgIdMethodDelegate> { | ||||
{JT808MsgId.终端通用应答.ToUInt16Value(), Msg0x0001}, | {JT808MsgId.终端通用应答.ToUInt16Value(), Msg0x0001}, | ||||
{JT808MsgId.终端鉴权.ToUInt16Value(), Msg0x0102}, | {JT808MsgId.终端鉴权.ToUInt16Value(), Msg0x0102}, | ||||
@@ -39,9 +52,16 @@ namespace JT808.Gateway.Abstractions | |||||
{JT808MsgId.摄像头立即拍摄命令.ToUInt16Value(),Msg0x8801 }, | {JT808MsgId.摄像头立即拍摄命令.ToUInt16Value(),Msg0x8801 }, | ||||
{JT808MsgId.多媒体数据上传.ToUInt16Value(),Msg0x0801 }, | {JT808MsgId.多媒体数据上传.ToUInt16Value(),Msg0x0801 }, | ||||
{JT808MsgId.多媒体事件信息上传.ToUInt16Value(),Msg0x0800 }, | {JT808MsgId.多媒体事件信息上传.ToUInt16Value(),Msg0x0800 }, | ||||
{JT808MsgId.CAN总线数据上传.ToUInt16Value(),Msg0x0705 }, | |||||
{JT808MsgId.CAN总线数据上传.ToUInt16Value(),Msg0x0705 }, | |||||
}; | }; | ||||
} | } | ||||
public JT808MessageHandler(IOptionsMonitor<JT808Configuration> jT808ConfigurationOptionsMonitor | |||||
, IJT808Config jT808Config) : this(jT808ConfigurationOptionsMonitor, null, jT808Config) | |||||
{ | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// 消息处理 | /// 消息处理 | ||||
/// </summary> | /// </summary> | ||||
@@ -52,15 +72,38 @@ namespace JT808.Gateway.Abstractions | |||||
{ | { | ||||
if (HandlerDict.TryGetValue(request.Header.MsgId, out var func)) | if (HandlerDict.TryGetValue(request.Header.MsgId, out var func)) | ||||
{ | { | ||||
return func(request, session); | |||||
if (JT808ConfigurationOptionsMonitor.CurrentValue.FilterMsgIdHandlerForQueue != null) | |||||
{ | |||||
// 网关不做消息业务处理,往队列发送 | |||||
if (JT808ConfigurationOptionsMonitor.CurrentValue.FilterMsgIdHandlerForQueue.Contains(request.Header.MsgId)) | |||||
{ | |||||
if (MsgProducer != null) | |||||
{ | |||||
MsgProducer.ProduceAsync(request.Header.TerminalPhoneNo, request.OriginalData.ToArray()); | |||||
} | |||||
return default; | |||||
} | |||||
else | |||||
{ | |||||
return func(request, session); | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
return func(request, session); | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
//处理不了的消息Id统一发队列 | |||||
if (MsgProducer != null) | |||||
{ | |||||
MsgProducer.ProduceAsync(request.Header.TerminalPhoneNo, request.OriginalData.ToArray()); | |||||
} | |||||
return default; | |||||
} | } | ||||
return default; | |||||
//else | |||||
//{ | |||||
// //处理不了的消息统一回复通用应答 | |||||
// return CommonReply(request, session); | |||||
//} | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 终端通用应答 | /// 终端通用应答 | ||||
/// 平台无需回复 | /// 平台无需回复 | ||||
@@ -122,7 +165,7 @@ namespace JT808.Gateway.Abstractions | |||||
{ | { | ||||
byte[] data = JT808Serializer.Serialize(JT808MsgId.查询服务器时间应答.Create(request.Header.TerminalPhoneNo, new JT808_0x8004() | byte[] data = JT808Serializer.Serialize(JT808MsgId.查询服务器时间应答.Create(request.Header.TerminalPhoneNo, new JT808_0x8004() | ||||
{ | { | ||||
Time=DateTime.Now | |||||
Time = DateTime.Now | |||||
})); | })); | ||||
session.Send(data); | session.Send(data); | ||||
return data; | return data; | ||||
@@ -51,7 +51,7 @@ namespace JT808.Gateway.NormalHosting | |||||
////{ | ////{ | ||||
//// options.TcpPort = 808; | //// options.TcpPort = 808; | ||||
//// options.UdpPort = 808; | //// options.UdpPort = 808; | ||||
////}) | |||||
////}) | |||||
.AddGateway(hostContext.Configuration) | .AddGateway(hostContext.Configuration) | ||||
//.ReplaceNormalReplyMessageHandler<JT808NormalReplyMessageHandlerImpl>() | //.ReplaceNormalReplyMessageHandler<JT808NormalReplyMessageHandlerImpl>() | ||||
//.AddMsgLogging<JT808MsgLogging>() | //.AddMsgLogging<JT808MsgLogging>() | ||||
@@ -50,34 +50,5 @@ namespace JT808.Gateway.Test | |||||
//123456789 | //123456789 | ||||
//http://127.0.0.1:828/jt808api/Udp/Session/QueryUdpSessionByTerminalPhoneNo | //http://127.0.0.1:828/jt808api/Udp/Session/QueryUdpSessionByTerminalPhoneNo | ||||
} | } | ||||
[Fact] | |||||
public void Test3() | |||||
{ | |||||
IServiceCollection serviceDescriptors = new ServiceCollection(); | |||||
serviceDescriptors.AddSingleton<A>(); | |||||
IServiceProvider aa = serviceDescriptors.BuildServiceProvider(); | |||||
IServiceCollection sc= aa.GetRequiredService<IServiceCollection>(); | |||||
sc.AddSingleton<B>(); | |||||
IServiceProvider aa1 = serviceDescriptors.BuildServiceProvider(); | |||||
B b = aa.GetRequiredService<B>(); | |||||
} | |||||
} | |||||
public class A | |||||
{ | |||||
} | |||||
public class B | |||||
{ | |||||
} | |||||
public class C | |||||
{ | |||||
} | } | ||||
} | } |
@@ -1,5 +1,5 @@ | |||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
@@ -25,8 +25,6 @@ | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.7" /> | |||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.7" /> | |||||
<PackageReference Include="System.IO.Pipelines" Version="4.7.2" /> | <PackageReference Include="System.IO.Pipelines" Version="4.7.2" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
@@ -1,6 +1,6 @@ | |||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Authorization; | using JT808.Gateway.Authorization; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Handlers; | using JT808.Gateway.Handlers; | ||||
using JT808.Gateway.Internal; | using JT808.Gateway.Internal; | ||||
using JT808.Gateway.Services; | using JT808.Gateway.Services; | ||||
@@ -1,5 +1,5 @@ | |||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Extensions; | using JT808.Gateway.Extensions; | ||||
using JT808.Gateway.Handlers; | using JT808.Gateway.Handlers; | ||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
@@ -6,7 +6,7 @@ using System.Net.Sockets; | |||||
using System.Threading; | using System.Threading; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
using JT808.Protocol; | using JT808.Protocol; | ||||
using JT808.Protocol.Exceptions; | using JT808.Protocol.Exceptions; | ||||
@@ -8,7 +8,7 @@ using System.Text; | |||||
using System.Threading; | using System.Threading; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
using JT808.Protocol; | using JT808.Protocol; | ||||
using JT808.Protocol.Exceptions; | using JT808.Protocol.Exceptions; | ||||
@@ -1,5 +1,5 @@ | |||||
using JT808.Gateway.Abstractions; | using JT808.Gateway.Abstractions; | ||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
using Microsoft.Extensions.Hosting; | using Microsoft.Extensions.Hosting; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
@@ -1,4 +1,4 @@ | |||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
using Microsoft.Extensions.Hosting; | using Microsoft.Extensions.Hosting; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
@@ -1,4 +1,4 @@ | |||||
using JT808.Gateway.Configurations; | |||||
using JT808.Gateway.Abstractions.Configurations; | |||||
using JT808.Gateway.Session; | using JT808.Gateway.Session; | ||||
using Microsoft.Extensions.Hosting; | using Microsoft.Extensions.Hosting; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||