@@ -0,0 +1,27 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<PropertyGroup> | |||||
<OutputType>Exe</OutputType> | |||||
<TargetFramework>netcoreapp2.1</TargetFramework> | |||||
<LangVersion>7.3</LangVersion> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<ProjectReference Include="..\JT809.Protocol.Extensions.DependencyInjection\JT809.Protocol.Extensions.DependencyInjection.csproj" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<None Update="appsettings.json"> | |||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
</None> | |||||
</ItemGroup> | |||||
</Project> |
@@ -0,0 +1,27 @@ | |||||
using JT809.Protocol.Extensions.DependencyInjection.Options; | |||||
using Microsoft.Extensions.Configuration; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using Microsoft.Extensions.Hosting; | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
namespace JT809.Protocol.Extensions.DependencyInjection.Test | |||||
{ | |||||
class Program | |||||
{ | |||||
static async Task Main(string[] args) | |||||
{ | |||||
var serverHostBuilder = new HostBuilder() | |||||
.ConfigureAppConfiguration((hostingContext, config) => | |||||
{ | |||||
config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); | |||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | |||||
}) | |||||
.ConfigureServices((hostContext, services) => | |||||
{ | |||||
services.AddJT809Configure(hostContext.Configuration.GetSection("JT809Options")); | |||||
}); | |||||
await serverHostBuilder.RunConsoleAsync(); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,29 @@ | |||||
{ | |||||
"Logging": { | |||||
"IncludeScopes": false, | |||||
"Debug": { | |||||
"LogLevel": { | |||||
"Default": "Trace" | |||||
} | |||||
}, | |||||
"Console": { | |||||
"LogLevel": { | |||||
"Default": "Trace" | |||||
} | |||||
} | |||||
}, | |||||
"JT809Options": { | |||||
"HeaderOptions": { | |||||
"MsgGNSSCENTERID": 20181012, | |||||
"EncryptFlag": "None", | |||||
"EncryptKey": 9999, | |||||
"Version": "1.0.0" | |||||
}, | |||||
"EncryptOptions": { | |||||
"M1": 0, | |||||
"IA1": 0, | |||||
"IC1": 0, | |||||
"Key": 0 | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using Microsoft.Extensions.DependencyInjection.Extensions; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using JT809.Protocol.JT809Configs; | |||||
using Microsoft.Extensions.Configuration; | |||||
using JT809.Protocol.JT809Encrypt; | |||||
using JT809.Protocol.Extensions.DependencyInjection.Options; | |||||
using Microsoft.Extensions.Options; | |||||
namespace JT809.Protocol.Extensions.DependencyInjection | |||||
{ | |||||
public static class DependencyInjectionExtensions | |||||
{ | |||||
public static IServiceCollection AddJT809Configure(this IServiceCollection services, IConfiguration configuration) | |||||
{ | |||||
services.Configure<JT809Options>(configuration); | |||||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | |||||
JT809Options options = serviceProvider.GetRequiredService<IOptions<JT809Options>>().Value; | |||||
var versions = options.Version.Split(new string []{ "." }, StringSplitOptions.RemoveEmptyEntries); | |||||
if (versions.Length == 3) | |||||
{ | |||||
options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]), byte.Parse(versions[2])); | |||||
} | |||||
else if(versions.Length == 2) | |||||
{ | |||||
options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]),0); | |||||
} | |||||
else if (versions.Length == 1) | |||||
{ | |||||
options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), 0, 0); | |||||
} | |||||
JT809GlobalConfig.Instance.SetHeaderOptions(options.HeaderOptions); | |||||
JT809GlobalConfig.Instance.SetSkipCRCCode(options.SkipCRCCode); | |||||
if (options.HeaderOptions.EncryptFlag == JT809Header_Encrypt.Common) | |||||
{ | |||||
JT809GlobalConfig.Instance.SetEncrypt(new JT809EncryptImpl(options.EncryptOptions)); | |||||
} | |||||
try | |||||
{ | |||||
var msgSNDistributedImpl = services.BuildServiceProvider().GetRequiredService<IMsgSNDistributed>(); | |||||
JT809GlobalConfig.Instance.SetMsgSNDistributed(msgSNDistributedImpl); | |||||
} | |||||
catch | |||||
{ | |||||
} | |||||
return services; | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,15 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<PropertyGroup> | |||||
<TargetFramework>netstandard2.0</TargetFramework> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="JT809" Version="1.0.4" /> | |||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" /> | |||||
</ItemGroup> | |||||
</Project> |
@@ -0,0 +1,23 @@ | |||||
using JT809.Protocol.JT809Configs; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace JT809.Protocol.Extensions.DependencyInjection.Options | |||||
{ | |||||
public class JT809Options | |||||
{ | |||||
public JT809EncryptOptions EncryptOptions { get; set; } | |||||
public JT809HeaderOptions HeaderOptions { get; set; } | |||||
/// <summary> | |||||
/// 版本号: | |||||
/// 默认 1.0.0 | |||||
/// </summary> | |||||
public string Version { get; set; } | |||||
/// <summary> | |||||
/// 设置跳过校验码 | |||||
/// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用 | |||||
/// </summary> | |||||
public bool SkipCRCCode { get; set; } | |||||
} | |||||
} |