From feee710f0180e1ff4adf6497fea25ad258decd06 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Fri, 9 Aug 2019 00:26:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=202.=E6=96=B0=E5=A2=9E=E8=A7=A3=E6=9E=90=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=203.=E5=AE=8C=E5=96=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 +- src/JTTools.sln | 25 +++ src/JTTools/Controllers/JTToolsController.cs | 103 +++++++++++ src/JTTools/Dtos/JTRequestDto.cs | 14 ++ src/JTTools/Dtos/JTResultDto.cs | 14 ++ src/JTTools/JTTools.csproj | 29 +++ .../JsonConvert/ByteArrayHexConverter.cs | 29 +++ src/JTTools/Program.cs | 72 ++++++++ src/JTTools/appsettings.Development.json | 9 + src/JTTools/appsettings.json | 8 + src/ui/index.html | 166 ++++++++++++++++++ 11 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 src/JTTools.sln create mode 100644 src/JTTools/Controllers/JTToolsController.cs create mode 100644 src/JTTools/Dtos/JTRequestDto.cs create mode 100644 src/JTTools/Dtos/JTResultDto.cs create mode 100644 src/JTTools/JTTools.csproj create mode 100644 src/JTTools/JsonConvert/ByteArrayHexConverter.cs create mode 100644 src/JTTools/Program.cs create mode 100644 src/JTTools/appsettings.Development.json create mode 100644 src/JTTools/appsettings.json create mode 100644 src/ui/index.html diff --git a/README.md b/README.md index b706fca..eeb32c5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ -# JTTools +# JTTools + JT808、JT809、JT1078、JTNE解析工具 + +[在线解析工具](http://jttools.smallchi.cn) + +使用nodejs的PM2托管应用程序 + +``` 1 +pm2 start "dotnet JTTools.dll ASPNETCORE_ENVIRONMENT=Production" -n "JTTools.18888" -o "/home/Logs/JTTools/out.log" -e "/home/Logs/JTTools/error.log" +``` diff --git a/src/JTTools.sln b/src/JTTools.sln new file mode 100644 index 0000000..595b53e --- /dev/null +++ b/src/JTTools.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29123.88 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JTTools", "JTTools\JTTools.csproj", "{4F7C65A6-85D2-4F32-AC00-B43D2C296618}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4F7C65A6-85D2-4F32-AC00-B43D2C296618}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F7C65A6-85D2-4F32-AC00-B43D2C296618}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F7C65A6-85D2-4F32-AC00-B43D2C296618}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F7C65A6-85D2-4F32-AC00-B43D2C296618}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3C3239D7-6A1E-4D5B-ABB5-851786B72AED} + EndGlobalSection +EndGlobal diff --git a/src/JTTools/Controllers/JTToolsController.cs b/src/JTTools/Controllers/JTToolsController.cs new file mode 100644 index 0000000..aa3931a --- /dev/null +++ b/src/JTTools/Controllers/JTToolsController.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using JTTools.Dtos; +using Microsoft.AspNetCore.Mvc; +using JT1078.Protocol; +using JT808.Protocol; +using JT809.Protocol; +using JT809.Protocol.Exceptions; +using JT808.Protocol.Extensions.JT1078; +using JT809.Protocol.Extensions.JT1078; +using JT808.Protocol.Interfaces; +using JT808.Protocol.Extensions; +using JT808.Protocol.Exceptions; +using Microsoft.AspNetCore.Cors; + +namespace JTTools.Controllers +{ + [Route("api/JTTools")] + [ApiController] + [EnableCors("Domain")] + public class JTToolsController : ControllerBase + { + private readonly IJT809Config jT809Config; + private readonly IJT808Config jT808Config; + private readonly JT808Serializer jT808Serializer; + private readonly JT809Serializer jT809Serializer; + public JTToolsController( + IJT809Config jT809Config, + IJT808Config jT808Config) + { + this.jT809Config = jT809Config; + this.jT808Config = jT808Config; + jT808Serializer = jT808Config.GetSerializer(); + jT809Serializer = jT809Config.GetSerializer(); + } + + [Route("Parse808")] + [HttpPost] + public ActionResult Parse808([FromBody]JTRequestDto parameter) + { + JTResultDto jTResultDto = new JTResultDto(); + try + { + jTResultDto.Code = 200; + jTResultDto.Data = jT808Serializer.Deserialize(parameter.HexData.ToHexBytes()); + } + catch(JT808Exception ex) + { + jTResultDto.Code = 500; + jTResultDto.Message = $"{ex.ErrorCode}-{ex.Message}"; + } + catch (Exception ex) + { + jTResultDto.Code = 500; + jTResultDto.Message = ex.Message; + } + return jTResultDto; + } + + [Route("Parse809")] + [HttpPost] + public ActionResult Parse809([FromBody]JTRequestDto parameter) + { + JTResultDto jTResultDto = new JTResultDto(); + try + { + jTResultDto.Code = 200; + jTResultDto.Data = jT809Serializer.Deserialize(parameter.HexData.ToHexBytes()); + } + catch (JT809Exception ex) + { + jTResultDto.Code = 500; + jTResultDto.Message = $"{ex.ErrorCode}-{ex.Message}"; + } + catch (Exception ex) + { + jTResultDto.Code = 500; + jTResultDto.Message = ex.Message; + } + return jTResultDto; + } + + [Route("Parse1078")] + [HttpPost] + public ActionResult 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; + } + } +} diff --git a/src/JTTools/Dtos/JTRequestDto.cs b/src/JTTools/Dtos/JTRequestDto.cs new file mode 100644 index 0000000..963c837 --- /dev/null +++ b/src/JTTools/Dtos/JTRequestDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace JTTools.Dtos +{ + public class JTRequestDto + { + public string HexData { get; set; } + public bool Skip { get; set; } + public bool Custom { get; set; } + } +} diff --git a/src/JTTools/Dtos/JTResultDto.cs b/src/JTTools/Dtos/JTResultDto.cs new file mode 100644 index 0000000..c3308a4 --- /dev/null +++ b/src/JTTools/Dtos/JTResultDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace JTTools.Dtos +{ + public class JTResultDto + { + public int Code { get; set; } + public string Message { get; set; } + public object Data { get; set; } + } +} diff --git a/src/JTTools/JTTools.csproj b/src/JTTools/JTTools.csproj new file mode 100644 index 0000000..4828c4f --- /dev/null +++ b/src/JTTools/JTTools.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp2.2 + InProcess + + + + + + + + + + + + + + + + + + + + Always + + + + diff --git a/src/JTTools/JsonConvert/ByteArrayHexConverter.cs b/src/JTTools/JsonConvert/ByteArrayHexConverter.cs new file mode 100644 index 0000000..27624d6 --- /dev/null +++ b/src/JTTools/JsonConvert/ByteArrayHexConverter.cs @@ -0,0 +1,29 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JTTools +{ + public class ByteArrayHexConverter : JsonConverter + { + public override bool CanConvert(Type objectType) => objectType == typeof(byte[]); + + public override bool CanRead => false; + public override bool CanWrite => true; + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotImplementedException(); + + private readonly string _separator; + + public ByteArrayHexConverter(string separator = " ") => _separator = separator; + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + var hexString = string.Join(_separator, ((byte[])value).Select(p => p.ToString("X2"))); + writer.WriteValue(hexString); + } + } +} diff --git a/src/JTTools/Program.cs b/src/JTTools/Program.cs new file mode 100644 index 0000000..f98f348 --- /dev/null +++ b/src/JTTools/Program.cs @@ -0,0 +1,72 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using JT808.Protocol; +using JT809.Protocol; +using JT808.Protocol.Extensions.JT1078; +using JT809.Protocol.Extensions.JT1078; +using Newtonsoft.Json.Serialization; +using System; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using Microsoft.Extensions.Configuration; + +namespace JTTools +{ + public class Program + { + public static void Main(string[] args) + { + WebHost.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((hostingContext, config) => + { + 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); + }) + .ConfigureServices(services => + { + services.AddMvc() + .AddJsonOptions(jsonOptions => + { + jsonOptions.SerializerSettings.Converters.Add(new ByteArrayHexConverter()); + jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver(); + }) + .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddCors(options => + options.AddPolicy("Domain",builder => builder.WithOrigins("http://jttools.smallchi.cn") + .AllowAnyMethod() + .AllowAnyHeader() + .AllowAnyOrigin() + .AllowCredentials())); + services.AddJT808Configure() + .AddJT1078Configure(); + services.AddJT809Configure() + .AddJT1078Configure(); + }) + .Configure(app => { + app.UseCors("Domain"); + app.UseMvc(); + }) + .Build() + .Run(); + } + } +} diff --git a/src/JTTools/appsettings.Development.json b/src/JTTools/appsettings.Development.json new file mode 100644 index 0000000..e203e94 --- /dev/null +++ b/src/JTTools/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/src/JTTools/appsettings.json b/src/JTTools/appsettings.json new file mode 100644 index 0000000..a1ff5a8 --- /dev/null +++ b/src/JTTools/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug" //Warning + } + }, + "AllowedHosts": "*" +} diff --git a/src/ui/index.html b/src/ui/index.html new file mode 100644 index 0000000..b887ae0 --- /dev/null +++ b/src/ui/index.html @@ -0,0 +1,166 @@ + + + + + JTTools解析工具 + + + + + + + + + + + +
+ + +
+
+ +
+
+ Primary +
+
+ +
+
+
+ +
+
+ +
+
+ Primary2 +
+
+ +
+
+
+ +
+
+ +
+
+ Primary3 +
+
+ +
+
+
+
+
+ + +