2025-06-17 13:53:54 +08:00
|
|
|
|
using Infrastructure.CloudSdk.mqttmessagecenter;
|
2025-07-04 09:48:53 +08:00
|
|
|
|
using NPOI.SS.Formula.Functions;
|
2025-06-19 14:47:46 +08:00
|
|
|
|
using NuGet.Packaging;
|
|
|
|
|
|
using OpenAuth.App.ServiceApp;
|
2025-07-04 09:48:53 +08:00
|
|
|
|
using System.Security.Cryptography;
|
2025-06-17 13:53:54 +08:00
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.WebApi.Model.mqtt
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MqttHostedService : IHostedService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-06-19 14:47:46 +08:00
|
|
|
|
private readonly MqttMessageCenter _mqttCenter;
|
|
|
|
|
|
private readonly AirportMaintenanceApp _app;
|
2025-06-17 13:53:54 +08:00
|
|
|
|
|
2025-07-10 13:52:27 +08:00
|
|
|
|
|
2025-06-19 14:47:46 +08:00
|
|
|
|
public MqttHostedService(IServiceProvider serviceProvider, MqttMessageCenter mqttCenter, AirportMaintenanceApp app)
|
2025-06-17 13:53:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
2025-06-19 14:47:46 +08:00
|
|
|
|
_mqttCenter = mqttCenter;
|
|
|
|
|
|
_app = app;
|
2025-06-17 13:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
|
|
|
|
|
var handlers = scope.ServiceProvider.GetServices<IMqttMessageHandler>();
|
2025-07-10 13:52:27 +08:00
|
|
|
|
// 创建配置构建器
|
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
|
.AddJsonFile(
|
|
|
|
|
|
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json",
|
|
|
|
|
|
optional: true)
|
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
// 构建配置
|
|
|
|
|
|
var configuration = builder.Build();
|
|
|
|
|
|
// 读取连接字符串
|
|
|
|
|
|
var serverIp = configuration["MQTT:Server"];
|
|
|
|
|
|
var port = configuration["MQTT:Port"];
|
|
|
|
|
|
var username = configuration["MQTT:UserName"];
|
|
|
|
|
|
var password = configuration["MQTT:Password"];
|
2025-06-17 13:53:54 +08:00
|
|
|
|
|
2025-06-19 14:47:46 +08:00
|
|
|
|
await _mqttCenter.InitializeAsync(
|
2025-06-17 13:53:54 +08:00
|
|
|
|
handlers,
|
2025-07-10 13:52:27 +08:00
|
|
|
|
server: serverIp,
|
|
|
|
|
|
port: int.Parse(port),
|
2025-07-03 15:44:11 +08:00
|
|
|
|
clientId: Guid.NewGuid().ToString(),
|
2025-07-10 13:52:27 +08:00
|
|
|
|
username: username,
|
|
|
|
|
|
password: password
|
2025-06-17 13:53:54 +08:00
|
|
|
|
);
|
2025-10-09 10:35:27 +08:00
|
|
|
|
string[] topicList =
|
2025-07-04 09:57:44 +08:00
|
|
|
|
{
|
2025-10-09 10:35:27 +08:00
|
|
|
|
"thing/product/+/services_reply",
|
|
|
|
|
|
"thing/product/+/events",
|
|
|
|
|
|
"thing/product/+/requests",
|
|
|
|
|
|
"thing/product/+/osd",
|
2025-11-28 13:39:02 +08:00
|
|
|
|
"sys/product/+/status",
|
2026-01-08 14:24:29 +08:00
|
|
|
|
"thing/product/+/control-operation",
|
|
|
|
|
|
"ai/task/+/tasklog"
|
2025-10-09 10:35:27 +08:00
|
|
|
|
};
|
|
|
|
|
|
await _mqttCenter.SubscribeAsync(topicList);
|
2025-06-17 13:53:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|