|
|
@@ -7,6 +7,7 @@ using BPA.Component.CAP; |
|
|
|
using BPA.Component.Extensions; |
|
|
|
using BPA.Component.LogClient.Extensions; |
|
|
|
using BPA.Component.SDKCommon; |
|
|
|
using BPA.Component.WebApiExtensions; |
|
|
|
using BPA.Component.WebApiExtensions.Extensions; |
|
|
|
using BPA.MQTTClient; |
|
|
|
using BPA.SaaS.Order.Api.Model; |
|
|
@@ -18,11 +19,17 @@ using Microsoft.AspNetCore.Http; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Hosting; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.OpenApi.Models; |
|
|
|
using MQTTnet.Client; |
|
|
|
using MQTTnet.Client.Connecting; |
|
|
|
using MQTTnet.Client.Disconnecting; |
|
|
|
using MQTTnet.Client.Options; |
|
|
|
using MQTTnet.Client.Receiving; |
|
|
|
using NLog; |
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger; |
|
|
|
using IPAddress = System.Net.IPAddress; |
|
|
|
using MqttClientTcpOptions = BPA.MQTTClient.Options.MqttClientTcpOptions; |
|
|
|
|
|
|
|
namespace BPA.SaaS.Order.Api.Bootstrap; |
|
|
|
|
|
|
@@ -123,6 +130,31 @@ public static class SetupBootstrap |
|
|
|
serviceProvider.PreHeatRunRabbitMQ(); |
|
|
|
serviceProvider.SetDebug(true); |
|
|
|
serviceProvider.GetService<ICapPublisher>(); |
|
|
|
|
|
|
|
var logger = serviceProvider.GetService<ILogger<AppDomain>>(); |
|
|
|
var mqttOption = serviceProvider.GetService<MqttClientTcpOptions>(); |
|
|
|
if (mqttOption != null) |
|
|
|
{ |
|
|
|
mqttOption.mqttClientConnectedHandlerDelegate = new(_ => { logger!.LogInformation("MQTT连接成功"); }); |
|
|
|
mqttOption.mqttClientDisconnectedHandlerDelegate = new(async d => |
|
|
|
{ |
|
|
|
logger!.LogError("### MQTT断开连接 ###"); |
|
|
|
try |
|
|
|
{ |
|
|
|
await Task.Delay(TimeSpan.FromSeconds(5)); |
|
|
|
await serviceProvider.GetService<IMqttClient>().ConnectAsync(serviceProvider.GetService<IMqttClientOptions>()); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
logger!.LogError("### MQTT重试连接失败 ###"); |
|
|
|
logger!.LogError(e.ToString()); |
|
|
|
} |
|
|
|
}); |
|
|
|
mqttOption.MqttApplicationMessageReceivedHandler = new(e => |
|
|
|
{ |
|
|
|
if (e is {ApplicationMessage: { }}) logger!.LogDebug(Encoding.Unicode.GetString(e.ApplicationMessage.Payload)); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
@@ -186,15 +218,6 @@ public static class SetupBootstrap |
|
|
|
options.Password = preConfig.MqttClientConfig.Password; |
|
|
|
options.Port = preConfig.MqttClientConfig.Port; |
|
|
|
options.Server = preConfig.MqttClientConfig.Server; |
|
|
|
options.mqttClientConnectedHandlerDelegate = new MqttClientConnectedHandlerDelegate(_ => { Console.WriteLine("MQTT连接成功"); }); |
|
|
|
options.mqttClientDisconnectedHandlerDelegate = new MqttClientDisconnectedHandlerDelegate(d => |
|
|
|
{ |
|
|
|
Console.WriteLine("MQTT断开连接"); |
|
|
|
Console.WriteLine($"{d.Reason}"); |
|
|
|
Task.Delay(TimeSpan.FromSeconds(5)); |
|
|
|
// TODO:重试机制 |
|
|
|
}); |
|
|
|
options.MqttApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(_ => { }); |
|
|
|
}); |
|
|
|
services.AddCap(x => |
|
|
|
{ |
|
|
|