Browse Source

1.将配置迁移至抽象库中

2.增加网关应答后将应答数据推送到队列中
tags/pipeline-1.1.0
SmallChi(Koike) 4 years ago
parent
commit
89af3cd208
16 changed files with 102 additions and 52 deletions
  1. +6
    -2
      src/JT808.Gateway.Abstractions/Configurations/JT808Configuration.cs
  2. +15
    -0
      src/JT808.Gateway.Abstractions/IJT808MsgReplyLoggingConsumer.cs
  3. +17
    -0
      src/JT808.Gateway.Abstractions/IJT808MsgReplyLoggingProducer.cs
  4. +2
    -0
      src/JT808.Gateway.Abstractions/JT808.Gateway.Abstractions.csproj
  5. +53
    -10
      src/JT808.Gateway.Abstractions/JT808MessageHandler.cs
  6. +1
    -1
      src/JT808.Gateway.Tests/JT808.Gateway.NormalHosting/Program.cs
  7. +0
    -29
      src/JT808.Gateway.Tests/JT808.Gateway.Test/JT808HttpClientTest.cs
  8. +1
    -1
      src/JT808.Gateway/Authorization/JT808AuthorizationDefault.cs
  9. +0
    -2
      src/JT808.Gateway/JT808.Gateway.csproj
  10. +1
    -1
      src/JT808.Gateway/JT808GatewayExtensions.cs
  11. +1
    -1
      src/JT808.Gateway/JT808HttpServer.cs
  12. +1
    -1
      src/JT808.Gateway/JT808TcpServer.cs
  13. +1
    -1
      src/JT808.Gateway/JT808UdpServer.cs
  14. +1
    -1
      src/JT808.Gateway/Services/JT808MsgReplyHostedService.cs
  15. +1
    -1
      src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs
  16. +1
    -1
      src/JT808.Gateway/Services/JT808UdpReceiveTimeoutHostedService.cs

src/JT808.Gateway/Configurations/JT808Configuration.cs → src/JT808.Gateway.Abstractions/Configurations/JT808Configuration.cs View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;

namespace JT808.Gateway.Configurations
namespace JT808.Gateway.Abstractions.Configurations
{
public class JT808Configuration
{
@@ -19,7 +19,7 @@ namespace JT808.Gateway.Configurations
/// Tcp读超时
/// 默认10分钟检查一次
/// </summary>
public int TcpReaderIdleTimeSeconds { get; set; } = 60*10;
public int TcpReaderIdleTimeSeconds { get; set; } = 60 * 10;
/// <summary>
/// Tcp 60s检查一次
/// </summary>
@@ -32,5 +32,9 @@ namespace JT808.Gateway.Configurations
/// Udp 60s检查一次
/// </summary>
public int UdpReceiveTimeoutCheckTimeSeconds { get; set; } = 60;
/// <summary>
/// 网关不做消息业务处理,往队列发送
/// </summary>
public List<uint> FilterMsgIdHandlerForQueue { get; set; }
}
}

+ 15
- 0
src/JT808.Gateway.Abstractions/IJT808MsgReplyLoggingConsumer.cs View File

@@ -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();
}
}

+ 17
- 0
src/JT808.Gateway.Abstractions/IJT808MsgReplyLoggingProducer.cs View File

@@ -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);
}
}

+ 2
- 0
src/JT808.Gateway.Abstractions/JT808.Gateway.Abstractions.csproj View File

@@ -35,6 +35,8 @@
<ItemGroup>
<PackageReference Include="JT808" Version="2.2.12" />
<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>
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />


+ 53
- 10
src/JT808.Gateway.Abstractions/JT808MessageHandler.cs View File

@@ -5,6 +5,9 @@ using JT808.Protocol.MessageBody;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using JT808.Gateway.Abstractions.Configurations;

namespace JT808.Gateway.Abstractions
{
@@ -17,9 +20,19 @@ namespace JT808.Gateway.Abstractions

protected delegate byte[] MsgIdMethodDelegate(JT808HeaderPackage package, IJT808Session session);
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.MsgProducer = msgProducer;
this.JT808ConfigurationOptionsMonitor = jT808ConfigurationOptionsMonitor;
HandlerDict = new Dictionary<ushort, MsgIdMethodDelegate> {
{JT808MsgId.终端通用应答.ToUInt16Value(), Msg0x0001},
{JT808MsgId.终端鉴权.ToUInt16Value(), Msg0x0102},
@@ -39,9 +52,16 @@ namespace JT808.Gateway.Abstractions
{JT808MsgId.摄像头立即拍摄命令.ToUInt16Value(),Msg0x8801 },
{JT808MsgId.多媒体数据上传.ToUInt16Value(),Msg0x0801 },
{JT808MsgId.多媒体事件信息上传.ToUInt16Value(),Msg0x0800 },
{JT808MsgId.CAN总线数据上传.ToUInt16Value(),Msg0x0705 },
{JT808MsgId.CAN总线数据上传.ToUInt16Value(),Msg0x0705 },
};
}

public JT808MessageHandler(IOptionsMonitor<JT808Configuration> jT808ConfigurationOptionsMonitor
, IJT808Config jT808Config) : this(jT808ConfigurationOptionsMonitor, null, jT808Config)
{

}

/// <summary>
/// 消息处理
/// </summary>
@@ -52,15 +72,38 @@ namespace JT808.Gateway.Abstractions
{
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>
/// 终端通用应答
/// 平台无需回复
@@ -122,7 +165,7 @@ namespace JT808.Gateway.Abstractions
{
byte[] data = JT808Serializer.Serialize(JT808MsgId.查询服务器时间应答.Create(request.Header.TerminalPhoneNo, new JT808_0x8004()
{
Time=DateTime.Now
Time = DateTime.Now
}));
session.Send(data);
return data;


+ 1
- 1
src/JT808.Gateway.Tests/JT808.Gateway.NormalHosting/Program.cs View File

@@ -51,7 +51,7 @@ namespace JT808.Gateway.NormalHosting
////{
//// options.TcpPort = 808;
//// options.UdpPort = 808;
////})
////})
.AddGateway(hostContext.Configuration)
//.ReplaceNormalReplyMessageHandler<JT808NormalReplyMessageHandlerImpl>()
//.AddMsgLogging<JT808MsgLogging>()


+ 0
- 29
src/JT808.Gateway.Tests/JT808.Gateway.Test/JT808HttpClientTest.cs View File

@@ -50,34 +50,5 @@ namespace JT808.Gateway.Test
//123456789
//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
- 1
src/JT808.Gateway/Authorization/JT808AuthorizationDefault.cs View File

@@ -1,5 +1,5 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;


+ 0
- 2
src/JT808.Gateway/JT808.Gateway.csproj View File

@@ -25,8 +25,6 @@
</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" />
</ItemGroup>



+ 1
- 1
src/JT808.Gateway/JT808GatewayExtensions.cs View File

@@ -1,6 +1,6 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Authorization;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Handlers;
using JT808.Gateway.Internal;
using JT808.Gateway.Services;


+ 1
- 1
src/JT808.Gateway/JT808HttpServer.cs View File

@@ -1,5 +1,5 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Extensions;
using JT808.Gateway.Handlers;
using JT808.Gateway.Session;


+ 1
- 1
src/JT808.Gateway/JT808TcpServer.cs View File

@@ -6,7 +6,7 @@ using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using JT808.Gateway.Abstractions;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Session;
using JT808.Protocol;
using JT808.Protocol.Exceptions;


+ 1
- 1
src/JT808.Gateway/JT808UdpServer.cs View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JT808.Gateway.Abstractions;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Session;
using JT808.Protocol;
using JT808.Protocol.Exceptions;


+ 1
- 1
src/JT808.Gateway/Services/JT808MsgReplyHostedService.cs View File

@@ -1,5 +1,5 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Session;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;


+ 1
- 1
src/JT808.Gateway/Services/JT808TcpReceiveTimeoutHostedService.cs View File

@@ -1,4 +1,4 @@
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Session;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;


+ 1
- 1
src/JT808.Gateway/Services/JT808UdpReceiveTimeoutHostedService.cs View File

@@ -1,4 +1,4 @@
using JT808.Gateway.Configurations;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.Session;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;


Loading…
Cancel
Save