You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

109 lines
4.7 KiB

  1. using Microsoft.AspNetCore;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using JT808.Protocol;
  7. using JT808.Protocol.Interfaces;
  8. using JT809.Protocol;
  9. using JT808.Protocol.Extensions.JT1078;
  10. using JT808.Protocol.Extensions.JTActiveSafety;
  11. using JT809.Protocol.Extensions.JT1078;
  12. using Newtonsoft.Json.Serialization;
  13. using System;
  14. using Microsoft.Extensions.Logging;
  15. using NLog.Extensions.Logging;
  16. using Microsoft.Extensions.Configuration;
  17. using Microsoft.Extensions.Hosting;
  18. using JTTools.JsonConvert;
  19. namespace JTTools
  20. {
  21. public class Program
  22. {
  23. public static void Main(string[] args)
  24. {
  25. Host.CreateDefaultBuilder(args)
  26. .ConfigureWebHostDefaults(webBuilder =>
  27. {
  28. webBuilder.ConfigureServices((hostingContext, services) =>
  29. {
  30. services.AddControllers()
  31. //Microsoft.AspNetCore.Mvc.NewtonsoftJson
  32. .AddNewtonsoftJson(jsonOptions =>
  33. {
  34. jsonOptions.SerializerSettings.Converters.Add(new ByteArrayHexConverter());
  35. jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver();
  36. //jsonOptions.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
  37. })
  38. //.AddJsonOptions(jsonOptions =>
  39. //{
  40. // jsonOptions.JsonSerializerOptions.MaxDepth = 64;
  41. // jsonOptions.JsonSerializerOptions.Converters.Add(new ByteArrayHexTextJsonConverter());
  42. //})
  43. ;
  44. services.AddCors(options =>
  45. options.AddPolicy("Domain", builder =>
  46. builder.WithOrigins(hostingContext.Configuration.GetSection("AllowedOrigins").Value.Split(","))
  47. .AllowAnyMethod()
  48. .AllowAnyHeader()
  49. .AllowAnyOrigin()));
  50. })
  51. .ConfigureKestrel(ksOptions =>
  52. {
  53. ksOptions.ListenAnyIP(18888);
  54. })
  55. .ConfigureLogging((context, logging) => {
  56. //if (Environment.OSVersion.Platform == PlatformID.Unix)
  57. //{
  58. // NLog.LogManager.LoadConfiguration("Configs/nlog.unix.config");
  59. //}
  60. //else
  61. //{
  62. // NLog.LogManager.LoadConfiguration("Configs/nlog.win.config");
  63. //}
  64. //logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
  65. //logging.SetMinimumLevel(LogLevel.Trace);
  66. })
  67. .ConfigureAppConfiguration((hostingContext, config) =>
  68. {
  69. config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
  70. config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  71. })
  72. .Configure(app => {
  73. app.UseRouting();
  74. app.UseCors("Domain");
  75. app.UseEndpoints(endpoints =>
  76. {
  77. endpoints.MapControllers();
  78. });
  79. });
  80. })
  81. .ConfigureServices(services =>
  82. {
  83. services.AddJT808Configure();
  84. services.AddJT808Configure(new JT808_JTActiveSafety_Config())
  85. .AddJTActiveSafetyConfigure();
  86. services.AddJT808Configure(new JT808_JT1078_Config())
  87. .AddJT1078Configure();
  88. services.AddJT809Configure()
  89. .AddJT1078Configure();
  90. })
  91. .Build()
  92. .Run();
  93. }
  94. }
  95. public class JT808_JTActiveSafety_Config : GlobalConfigBase
  96. {
  97. public override string ConfigId { get; protected set; } = "JT808_JTActiveSafety_Config";
  98. }
  99. public class JT808_JT1078_Config : GlobalConfigBase
  100. {
  101. public override string ConfigId { get; protected set; } = "JT808_JT1078_Config";
  102. }
  103. }