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.
 
 

137 line
6.7 KiB

  1. using JT809.DotNetty.Abstractions;
  2. using JT809.DotNetty.Core.Codecs;
  3. using JT809.DotNetty.Core.Configurations;
  4. using JT809.DotNetty.Core.Converters;
  5. using JT809.DotNetty.Core.Enums;
  6. using JT809.DotNetty.Core.Handlers;
  7. using JT809.DotNetty.Core.Interfaces;
  8. using JT809.DotNetty.Core.Internal;
  9. using JT809.DotNetty.Core.Clients;
  10. using JT809.DotNetty.Core.Metadata;
  11. using JT809.DotNetty.Core.Services;
  12. using JT809.DotNetty.Core.Servers;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.Extensions.DependencyInjection.Extensions;
  16. using Newtonsoft.Json;
  17. using System;
  18. using System.Runtime.CompilerServices;
  19. using Microsoft.Extensions.Options;
  20. using JT808.DotNetty.WebApi;
  21. using JT809.DotNetty.Core.Session;
  22. [assembly: InternalsVisibleTo("JT809.DotNetty.Core.Test")]
  23. namespace JT809.DotNetty.Core
  24. {
  25. public static class JT809CoreDotnettyExtensions
  26. {
  27. static JT809CoreDotnettyExtensions()
  28. {
  29. JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
  30. {
  31. Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
  32. //日期类型默认格式化处理
  33. settings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
  34. settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  35. //空值处理
  36. settings.NullValueHandling = NullValueHandling.Ignore;
  37. settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  38. settings.Converters.Add(new JsonIPAddressConverter());
  39. settings.Converters.Add(new JsonIPEndPointConverter());
  40. return settings;
  41. });
  42. }
  43. public static IServiceCollection AddJT809Core(this IServiceCollection serviceDescriptors,IConfiguration configuration, Newtonsoft.Json.JsonSerializerSettings settings = null)
  44. {
  45. if (settings != null)
  46. {
  47. JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
  48. {
  49. settings.Converters.Add(new JsonIPAddressConverter());
  50. settings.Converters.Add(new JsonIPEndPointConverter());
  51. settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  52. return settings;
  53. });
  54. }
  55. serviceDescriptors.Configure<JT809Configuration>(configuration.GetSection("JT809Configuration"));
  56. serviceDescriptors.TryAddSingleton<JT809SimpleSystemCollectService>();
  57. //JT809计数器服务工厂
  58. serviceDescriptors.TryAddSingleton<JT809AtomicCounterServiceFactory>();
  59. //JT809编解码器
  60. serviceDescriptors.TryAddScoped<JT809Decoder>();
  61. serviceDescriptors.TryAddScoped<JT809Encoder>();
  62. return serviceDescriptors;
  63. }
  64. /// <summary>
  65. /// 下级平台
  66. /// 主链路为客户端
  67. /// 从链路为服务端
  68. /// </summary>
  69. /// <param name="serviceDescriptors"></param>
  70. /// <returns></returns>
  71. public static IServiceCollection AddJT809InferiorPlatform(this IServiceCollection serviceDescriptors, Action<JT809InferiorPlatformOptions> options)
  72. {
  73. serviceDescriptors.Configure(options);
  74. //主从链路客户端和服务端连接处理器
  75. serviceDescriptors.TryAddScoped<JT809MainClientConnectionHandler>();
  76. serviceDescriptors.TryAddScoped<JT809SubordinateServerConnectionHandler>();
  77. //主链路服务端会话管理
  78. //serviceDescriptors.TryAddSingleton<JT809MainSessionManager>();
  79. //主从链路接收消息默认业务处理器
  80. serviceDescriptors.TryAddSingleton<JT809InferiorMsgIdReceiveHandlerBase, JT809InferiorMsgIdReceiveDefaultHandler>();
  81. //主从链路消息接收处理器
  82. serviceDescriptors.TryAddScoped<JT809MainServerHandler>();
  83. serviceDescriptors.TryAddScoped<JT809SubordinateServerHandler>();
  84. //主链路客户端
  85. serviceDescriptors.TryAddSingleton<JT809MainClient>();
  86. //从链路服务端
  87. serviceDescriptors.AddHostedService<JT809SubordinateServerHost>();
  88. return serviceDescriptors;
  89. }
  90. /// <summary>
  91. /// 上级平台
  92. /// 主链路为服务端
  93. /// 从链路为客户端
  94. /// </summary>
  95. /// <param name="serviceDescriptors"></param>
  96. /// <returns></returns>
  97. public static IServiceCollection AddJT809SuperiorPlatform(this IServiceCollection serviceDescriptors, IConfiguration superiorPlatformConfiguration=null, Action<JT809SuperiorPlatformOptions> options=null)
  98. {
  99. if (superiorPlatformConfiguration != null)
  100. {
  101. serviceDescriptors.Configure<JT809SuperiorPlatformOptions>(superiorPlatformConfiguration.GetSection("JT809SuperiorPlatformConfiguration"));
  102. }
  103. if (options != null)
  104. {
  105. serviceDescriptors.Configure(options);
  106. }
  107. serviceDescriptors.TryAddSingleton<IJT809VerifyCodeGenerator, JT809VerifyCodeGeneratorDefaultImpl>();
  108. //主从链路客户端和服务端连接处理器
  109. serviceDescriptors.TryAddScoped<JT809MainServerConnectionHandler>();
  110. serviceDescriptors.TryAddScoped<JT809SubordinateClientConnectionHandler>();
  111. //主链路服务端会话管理
  112. serviceDescriptors.TryAddSingleton<JT809SuperiorMainSessionManager>();
  113. //主从链路接收消息默认业务处理器
  114. serviceDescriptors.TryAddSingleton<JT809SuperiorMsgIdReceiveHandlerBase, JT809SuperiorMsgIdReceiveDefaultHandler>();
  115. //主从链路消息接收处理器
  116. serviceDescriptors.TryAddScoped<JT809MainServerHandler>();
  117. serviceDescriptors.TryAddScoped<JT809SubordinateClientHandler>();
  118. serviceDescriptors.TryAddSingleton<IJT809SubordinateLoginService, JT809SubordinateLoginImplService>();
  119. serviceDescriptors.TryAddSingleton<IJT809SubordinateLinkNotifyService, JT809SubordinateLinkNotifyImplService>();
  120. //从链路客户端
  121. serviceDescriptors.TryAddSingleton<JT809SubordinateClient>();
  122. //主链路服务端
  123. serviceDescriptors.AddHostedService<JT809MainServerHost>();
  124. //上级平台webapi
  125. serviceDescriptors.TryAddSingleton<JT809SuperiorWebAPIHandlerBase, JT809SuperiorWebAPIDefaultHandler>();
  126. serviceDescriptors.TryAddScoped<JT809SuperiorWebAPIServerHandler>();
  127. serviceDescriptors.AddHostedService<JT809MainWebAPIServerHost>();
  128. return serviceDescriptors;
  129. }
  130. }
  131. }