Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

176 рядки
8.2 KiB

  1. using JT808.Gateway.Abstractions;
  2. using JT808.Gateway.Authorization;
  3. using JT808.Gateway.Abstractions.Configurations;
  4. using JT808.Gateway.Handlers;
  5. using JT808.Gateway.Internal;
  6. using JT808.Gateway.Services;
  7. using JT808.Gateway.Session;
  8. using JT808.Protocol;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.DependencyInjection.Extensions;
  12. using System;
  13. using System.Runtime.CompilerServices;
  14. using System.Linq;
  15. [assembly: InternalsVisibleTo("JT808.Gateway.TestHosting")]
  16. [assembly: InternalsVisibleTo("JT808.Gateway.Test")]
  17. namespace JT808.Gateway
  18. {
  19. /// <summary>
  20. /// JT808网关注册扩展
  21. /// </summary>
  22. public static partial class JT808GatewayExtensions
  23. {
  24. /// <summary>
  25. /// 添加808网关
  26. /// </summary>
  27. /// <param name="jT808Builder"></param>
  28. /// <param name="config"></param>
  29. /// <returns></returns>
  30. public static IJT808GatewayBuilder AddGateway(this IJT808Builder jT808Builder, Action<JT808Configuration> config)
  31. {
  32. JT808GatewayBuilderDefault jT808GatewayBuilderDefault = new JT808GatewayBuilderDefault(jT808Builder);
  33. jT808GatewayBuilderDefault.JT808Builder.Services.Configure(config);
  34. jT808GatewayBuilderDefault.AddJT808Core();
  35. return jT808GatewayBuilderDefault;
  36. }
  37. /// <summary>
  38. /// 添加808网关
  39. /// </summary>
  40. /// <param name="jT808Builder"></param>
  41. /// <param name="configuration"></param>
  42. /// <returns></returns>
  43. public static IJT808GatewayBuilder AddGateway(this IJT808Builder jT808Builder, IConfiguration configuration)
  44. {
  45. JT808GatewayBuilderDefault jT808GatewayBuilderDefault = new JT808GatewayBuilderDefault(jT808Builder);
  46. jT808GatewayBuilderDefault.JT808Builder.Services.Configure<JT808Configuration>(configuration.GetSection("JT808Configuration"));
  47. jT808GatewayBuilderDefault.AddJT808Core();
  48. return jT808GatewayBuilderDefault;
  49. }
  50. /// <summary>
  51. /// 添加tcp服务器
  52. /// </summary>
  53. /// <param name="config"></param>
  54. /// <returns></returns>
  55. public static IJT808GatewayBuilder AddTcp(this IJT808GatewayBuilder config)
  56. {
  57. config.JT808Builder.Services.AddHostedService<JT808TcpServer>();
  58. config.JT808Builder.Services.AddHostedService<JT808TcpReceiveTimeoutHostedService>();
  59. return config;
  60. }
  61. /// <summary>
  62. /// 添加udp服务器
  63. /// </summary>
  64. /// <param name="config"></param>
  65. /// <returns></returns>
  66. public static IJT808GatewayBuilder AddUdp(this IJT808GatewayBuilder config)
  67. {
  68. config.JT808Builder.Services.AddHostedService<JT808UdpServer>();
  69. config.JT808Builder.Services.AddHostedService<JT808UdpReceiveTimeoutHostedService>();
  70. return config;
  71. }
  72. /// <summary>
  73. /// 添加http服务器
  74. /// </summary>
  75. /// <param name="config"></param>
  76. /// <returns></returns>
  77. public static IJT808GatewayBuilder AddHttp(this IJT808GatewayBuilder config)
  78. {
  79. config.JT808Builder.Services.AddSingleton<IJT808Authorization, JT808AuthorizationDefault>();
  80. config.JT808Builder.Services.AddSingleton<JT808MsgIdDefaultWebApiHandler>();
  81. config.JT808Builder.Services.AddHostedService<JT808HttpServer>();
  82. return config;
  83. }
  84. /// <summary>
  85. /// 添加http服务器
  86. /// </summary>
  87. /// <typeparam name="TJT808MsgIdDefaultWebApiHandler"></typeparam>
  88. /// <param name="config"></param>
  89. /// <returns></returns>
  90. public static IJT808GatewayBuilder AddHttp<TJT808MsgIdDefaultWebApiHandler>(this IJT808GatewayBuilder config)
  91. where TJT808MsgIdDefaultWebApiHandler: JT808MsgIdDefaultWebApiHandler
  92. {
  93. config.JT808Builder.Services.AddSingleton<IJT808Authorization, JT808AuthorizationDefault>();
  94. config.JT808Builder.Services.AddSingleton(typeof(JT808MsgIdDefaultWebApiHandler),typeof(TJT808MsgIdDefaultWebApiHandler));
  95. config.JT808Builder.Services.AddHostedService<JT808HttpServer>();
  96. return config;
  97. }
  98. /// <summary>
  99. /// 添加消息业务处理程序
  100. /// </summary>
  101. /// <typeparam name="TJT808MessageHandler"></typeparam>
  102. /// <param name="config"></param>
  103. /// <returns></returns>
  104. public static IJT808GatewayBuilder AddMessageHandler<TJT808MessageHandler>(this IJT808GatewayBuilder config)
  105. where TJT808MessageHandler : JT808MessageHandler
  106. {
  107. config.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(JT808MessageHandler), typeof(TJT808MessageHandler), ServiceLifetime.Singleton));
  108. return config;
  109. }
  110. /// <summary>
  111. /// 添加Http服务认证机制
  112. /// </summary>
  113. /// <typeparam name="TJT808Authorization"></typeparam>
  114. /// <param name="config"></param>
  115. /// <returns></returns>
  116. public static IJT808GatewayBuilder AddHttpAuthorization<TJT808Authorization>(this IJT808GatewayBuilder config)
  117. where TJT808Authorization : IJT808Authorization
  118. {
  119. config.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808Authorization), typeof(TJT808Authorization), ServiceLifetime.Singleton));
  120. return config;
  121. }
  122. /// <summary>
  123. /// 添加消息生产者
  124. /// </summary>
  125. /// <typeparam name="TJT808MsgProducer"></typeparam>
  126. /// <param name="config"></param>
  127. /// <returns></returns>
  128. public static IJT808GatewayBuilder AddMsgProducer<TJT808MsgProducer>(this IJT808GatewayBuilder config)
  129. where TJT808MsgProducer : IJT808MsgProducer
  130. {
  131. config.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808MsgProducer), typeof(TJT808MsgProducer), ServiceLifetime.Singleton));
  132. return config;
  133. }
  134. /// <summary>
  135. /// 添加消息应答后的应答生产者
  136. /// </summary>
  137. /// <typeparam name="TJT808MsgReplyLoggingProducer"></typeparam>
  138. /// <param name="config"></param>
  139. /// <returns></returns>
  140. public static IJT808GatewayBuilder AddMsgReplyLoggingProducer<TJT808MsgReplyLoggingProducer>(this IJT808GatewayBuilder config)
  141. where TJT808MsgReplyLoggingProducer : IJT808MsgReplyLoggingProducer
  142. {
  143. config.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808MsgReplyLoggingProducer), typeof(TJT808MsgReplyLoggingProducer), ServiceLifetime.Singleton));
  144. return config;
  145. }
  146. /// <summary>
  147. /// 添加消息应答消费者
  148. /// </summary>
  149. /// <typeparam name="TJT808MsgReplyConsumer"></typeparam>
  150. /// <param name="config"></param>
  151. /// <returns></returns>
  152. public static IJT808GatewayBuilder AddMsgReplyConsumer<TJT808MsgReplyConsumer>(this IJT808GatewayBuilder config)
  153. where TJT808MsgReplyConsumer : IJT808MsgReplyConsumer
  154. {
  155. config.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808MsgReplyConsumer), typeof(TJT808MsgReplyConsumer), ServiceLifetime.Singleton));
  156. return config;
  157. }
  158. /// <summary>
  159. /// 添加公共模块
  160. /// </summary>
  161. /// <param name="config"></param>
  162. /// <returns></returns>
  163. private static IJT808GatewayBuilder AddJT808Core(this IJT808GatewayBuilder config)
  164. {
  165. config.JT808Builder.Services.AddSingleton<JT808MessageHandler>();
  166. config.JT808Builder.Services.AddSingleton<JT808BlacklistManager>();
  167. config.JT808Builder.Services.AddSingleton<JT808SessionManager>();
  168. config.JT808Builder.Services.AddSingleton<IJT808MsgProducer, JT808MsgProducer_Empty>();
  169. config.JT808Builder.Services.AddSingleton<IJT808MsgReplyLoggingProducer, JT808MsgReplyLoggingProducer_Empty>();
  170. config.JT808Builder.Services.AddSingleton<IJT808MsgReplyConsumer, JT808MsgReplyConsumer_Empry>();
  171. config.JT808Builder.Services.AddHostedService<JT808MsgReplyHostedService>();
  172. return config;
  173. }
  174. }
  175. }