@@ -0,0 +1,69 @@ | |||||
using JTTools.JsonConvert; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Text.Json; | |||||
using Xunit; | |||||
namespace JTTools.Test | |||||
{ | |||||
public class ByteArrayHexTextJsonConverterTest | |||||
{ | |||||
[Fact] | |||||
public void Test1() | |||||
{ | |||||
var options = new JsonSerializerOptions(); | |||||
options.Converters.Add(new ByteArrayHexTextJsonConverter()); | |||||
string jsonSerialized = JsonSerializer.Serialize(new byte[] { 0xA1,0x2D,0xF6}, options); | |||||
Assert.Equal("\"A1 2D F6\"", jsonSerialized); | |||||
} | |||||
[Fact] | |||||
public void Test2() | |||||
{ | |||||
string json = "\"A1 2D,F6\""; | |||||
var options = new JsonSerializerOptions(); | |||||
options.Converters.Add(new ByteArrayHexTextJsonConverter()); | |||||
byte[] arr = JsonSerializer.Deserialize<byte[]>(json, options); | |||||
Assert.Equal(0xA1, arr[0]); | |||||
Assert.Equal(0x2D, arr[1]); | |||||
Assert.Equal(0xF6, arr[2]); | |||||
} | |||||
[Fact] | |||||
public void Test3() | |||||
{ | |||||
string json = "{\"Array1\":\"A1 2D,F6\"}"; | |||||
var options = new JsonSerializerOptions(); | |||||
options.Converters.Add(new ByteArrayHexTextJsonConverter()); | |||||
ClassWithProperty obj = JsonSerializer.Deserialize<ClassWithProperty>(json, options); | |||||
Assert.Equal(0xA1, obj.Array1[0]); | |||||
Assert.Equal(0x2D, obj.Array1[1]); | |||||
Assert.Equal(0xF6, obj.Array1[2]); | |||||
} | |||||
[Fact] | |||||
public void Test4() | |||||
{ | |||||
string json = "{\"Array1\":\"A1 2D,O6\"}"; | |||||
var options = new JsonSerializerOptions(); | |||||
options.Converters.Add(new ByteArrayHexTextJsonConverter()); | |||||
try | |||||
{ | |||||
ClassWithProperty obj = JsonSerializer.Deserialize<ClassWithProperty>(json, options); | |||||
} | |||||
catch (System.FormatException ex) | |||||
{ | |||||
Assert.Equal("Could not find any recognizable digits.", ex.Message); | |||||
} | |||||
} | |||||
private class ClassWithProperty | |||||
{ | |||||
public byte[] Array1 { get; set; } | |||||
} | |||||
} | |||||
} |
@@ -1,7 +1,7 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||
<PropertyGroup> | <PropertyGroup> | ||||
<TargetFramework>netcoreapp2.2</TargetFramework> | |||||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||||
<IsPackable>false</IsPackable> | <IsPackable>false</IsPackable> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29123.88 | |||||
MinimumVisualStudioVersion = 10.0.40219.1 | MinimumVisualStudioVersion = 10.0.40219.1 | ||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JTTools", "JTTools\JTTools.csproj", "{4F7C65A6-85D2-4F32-AC00-B43D2C296618}" | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JTTools", "JTTools\JTTools.csproj", "{4F7C65A6-85D2-4F32-AC00-B43D2C296618}" | ||||
EndProject | EndProject | ||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JTTools.Test", "JTTools.Test\JTTools.Test.csproj", "{4D721C46-A6A2-45F9-8934-CDEED97F3FC5}" | |||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JTTools.Test", "JTTools.Test\JTTools.Test.csproj", "{4D721C46-A6A2-45F9-8934-CDEED97F3FC5}" | |||||
EndProject | EndProject | ||||
Global | Global | ||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
@@ -4,7 +4,7 @@ using System.Linq; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using JTTools.Dtos; | using JTTools.Dtos; | ||||
using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||
using JT1078.Protocol; | |||||
//using JT1078.Protocol; | |||||
using JT808.Protocol; | using JT808.Protocol; | ||||
using JT809.Protocol; | using JT809.Protocol; | ||||
using JT809.Protocol.Exceptions; | using JT809.Protocol.Exceptions; | ||||
@@ -13,6 +13,8 @@ using JT808.Protocol.Exceptions; | |||||
using Microsoft.AspNetCore.Cors; | using Microsoft.AspNetCore.Cors; | ||||
using Newtonsoft.Json; | using Newtonsoft.Json; | ||||
using JT808.Protocol.Interfaces; | using JT808.Protocol.Interfaces; | ||||
using JT809.Protocol.Interfaces; | |||||
using Microsoft.AspNetCore.Authorization; | |||||
namespace JTTools.Controllers | namespace JTTools.Controllers | ||||
{ | { | ||||
@@ -93,26 +95,26 @@ namespace JTTools.Controllers | |||||
return jTResultDto; | return jTResultDto; | ||||
} | } | ||||
[Route("Parse1078")] | |||||
[HttpPost] | |||||
public ActionResult<JTResultDto> Parse1078([FromBody]JTRequestDto parameter) | |||||
{ | |||||
JTResultDto jTResultDto = new JTResultDto(); | |||||
try | |||||
{ | |||||
jTResultDto.Code = 200; | |||||
jTResultDto.Data = JT1078Serializer.Deserialize(parameter.HexData.ToHexBytes()); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
jTResultDto.Code = 500; | |||||
jTResultDto.Message = ex.Message; | |||||
} | |||||
return jTResultDto; | |||||
} | |||||
//[Route("Parse1078")] | |||||
//[HttpPost] | |||||
//public ActionResult<JTResultDto> Parse1078([FromBody]JTRequestDto parameter) | |||||
//{ | |||||
// JTResultDto jTResultDto = new JTResultDto(); | |||||
// try | |||||
// { | |||||
// jTResultDto.Code = 200; | |||||
// jTResultDto.Data = JT1078Serializer.Deserialize(parameter.HexData.ToHexBytes()); | |||||
// } | |||||
// catch (Exception ex) | |||||
// { | |||||
// jTResultDto.Code = 500; | |||||
// jTResultDto.Message = ex.Message; | |||||
// } | |||||
// return jTResultDto; | |||||
//} | |||||
} | } | ||||
class JT809Config : JT809.Protocol.Interfaces.GlobalConfigBase | |||||
class JT809Config :JT809GlobalConfigBase | |||||
{ | { | ||||
public JT809Config(string configId) | public JT809Config(string configId) | ||||
{ | { | ||||
@@ -1,7 +1,7 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk.Web"> | <Project Sdk="Microsoft.NET.Sdk.Web"> | ||||
<PropertyGroup> | <PropertyGroup> | ||||
<TargetFramework>netcoreapp2.2</TargetFramework> | |||||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
@@ -10,14 +10,15 @@ | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="JT1078" Version="1.0.0" /> | |||||
<PackageReference Include="JT808" Version="2.1.3" /> | |||||
<PackageReference Include="JT808.Protocol.Extensions.JT1078" Version="2.1.3" /> | |||||
<PackageReference Include="JT809" Version="2.1.1" /> | |||||
<PackageReference Include="JT809.Protocol.Extensions.JT1078" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.App" /> | |||||
<!--<PackageReference Include="JT1078" Version="1.0.0" />--> | |||||
<PackageReference Include="JT808" Version="2.1.7" /> | |||||
<PackageReference Include="JT808.Protocol.Extensions.JT1078" Version="2.1.7" /> | |||||
<PackageReference Include="JT809" Version="2.1.2" /> | |||||
<PackageReference Include="JT809.Protocol.Extensions.JT1078" Version="2.1.2" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> | <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> | ||||
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.2" /> | |||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | |||||
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
@@ -0,0 +1,37 @@ | |||||
using Microsoft.AspNetCore.Http; | |||||
using Microsoft.AspNetCore.Mvc.Formatters; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Text.Json; | |||||
using System.Text.Json.Serialization; | |||||
using System.Threading.Tasks; | |||||
namespace JTTools.JsonConvert | |||||
{ | |||||
/// <summary> | |||||
/// | |||||
/// ref:https://github.com/dotnet/corefx/blob/release/3.0/src/System.Text.Json/tests/Serialization/CustomConverterTests.Array.cs | |||||
/// </summary> | |||||
public class ByteArrayHexTextJsonConverter : JsonConverter<byte[]> | |||||
{ | |||||
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |||||
{ | |||||
//string hexJson = reader.get(); | |||||
var hexJson = reader.GetString(); | |||||
var list = new List<byte>(); | |||||
foreach (string str in hexJson.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries)) | |||||
{ | |||||
list.Add(Convert.ToByte(str, 16)); | |||||
} | |||||
return list.ToArray(); | |||||
} | |||||
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options) | |||||
{ | |||||
var hexString = string.Join(" ", (value).Select(p => p.ToString("X2"))); | |||||
writer.WriteStringValue(hexString); | |||||
} | |||||
} | |||||
} |
@@ -12,6 +12,8 @@ using System; | |||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
using NLog.Extensions.Logging; | using NLog.Extensions.Logging; | ||||
using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||
using Microsoft.Extensions.Hosting; | |||||
using JTTools.JsonConvert; | |||||
namespace JTTools | namespace JTTools | ||||
{ | { | ||||
@@ -19,54 +21,68 @@ namespace JTTools | |||||
{ | { | ||||
public static void Main(string[] args) | public static void Main(string[] args) | ||||
{ | { | ||||
WebHost.CreateDefaultBuilder(args) | |||||
.ConfigureAppConfiguration((hostingContext, config) => | |||||
Host.CreateDefaultBuilder(args) | |||||
.ConfigureWebHostDefaults(webBuilder => | |||||
{ | { | ||||
config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); | |||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | |||||
}) | |||||
.ConfigureKestrel(ksOptions=> | |||||
{ | |||||
ksOptions.ListenAnyIP(18888); | |||||
}) | |||||
.ConfigureLogging((context,logging) => { | |||||
//if (Environment.OSVersion.Platform == PlatformID.Unix) | |||||
//{ | |||||
// NLog.LogManager.LoadConfiguration("Configs/nlog.unix.config"); | |||||
//} | |||||
//else | |||||
//{ | |||||
// NLog.LogManager.LoadConfiguration("Configs/nlog.win.config"); | |||||
//} | |||||
//logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true }); | |||||
//logging.SetMinimumLevel(LogLevel.Trace); | |||||
webBuilder.ConfigureServices((hostingContext, services) => | |||||
{ | |||||
services.AddControllers() | |||||
//Microsoft.AspNetCore.Mvc.NewtonsoftJson | |||||
//.AddNewtonsoftJson(jsonOptions => | |||||
//{ | |||||
// jsonOptions.SerializerSettings.Converters.Add(new ByteArrayHexConverter()); | |||||
// jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver(); | |||||
// jsonOptions.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; | |||||
//}) | |||||
.AddJsonOptions(jsonOptions => | |||||
{ | |||||
jsonOptions.JsonSerializerOptions.Converters.Add(new ByteArrayHexTextJsonConverter()); | |||||
}); | |||||
services.AddCors(options => | |||||
options.AddPolicy("Domain", builder => | |||||
builder.WithOrigins(hostingContext.Configuration.GetSection("AllowedOrigins").Value.Split(",")) | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.AllowAnyOrigin())); | |||||
}) | |||||
.ConfigureKestrel(ksOptions => | |||||
{ | |||||
ksOptions.ListenAnyIP(18888); | |||||
}) | |||||
.ConfigureLogging((context, logging) => { | |||||
//if (Environment.OSVersion.Platform == PlatformID.Unix) | |||||
//{ | |||||
// NLog.LogManager.LoadConfiguration("Configs/nlog.unix.config"); | |||||
//} | |||||
//else | |||||
//{ | |||||
// NLog.LogManager.LoadConfiguration("Configs/nlog.win.config"); | |||||
//} | |||||
//logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true }); | |||||
//logging.SetMinimumLevel(LogLevel.Trace); | |||||
}) | |||||
.ConfigureAppConfiguration((hostingContext, config) => | |||||
{ | |||||
config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); | |||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | |||||
}) | |||||
.Configure(app => { | |||||
app.UseRouting(); | |||||
app.UseCors("Domain"); | |||||
app.UseEndpoints(endpoints => | |||||
{ | |||||
endpoints.MapControllers(); | |||||
}); | |||||
}); | |||||
}) | }) | ||||
.ConfigureServices((hostingContext, services) => | |||||
.ConfigureServices(services => | |||||
{ | { | ||||
services.AddMvc() | |||||
.AddJsonOptions(jsonOptions => | |||||
{ | |||||
jsonOptions.SerializerSettings.Converters.Add(new ByteArrayHexConverter()); | |||||
jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver(); | |||||
//jsonOptions.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; | |||||
}) | |||||
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | |||||
services.AddCors(options => | |||||
options.AddPolicy("Domain",builder => | |||||
builder.WithOrigins(hostingContext.Configuration.GetSection("AllowedOrigins").Value.Split(",")) | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.AllowAnyOrigin() | |||||
.AllowCredentials())); | |||||
services.AddJT808Configure() | services.AddJT808Configure() | ||||
.AddJT1078Configure(); | .AddJT1078Configure(); | ||||
services.AddJT809Configure() | services.AddJT809Configure() | ||||
.AddJT1078Configure(); | .AddJT1078Configure(); | ||||
}) | }) | ||||
.Configure(app => { | |||||
app.UseCors("Domain"); | |||||
app.UseMvc(); | |||||
}) | |||||
.Build() | .Build() | ||||
.Run(); | .Run(); | ||||
} | } | ||||