LASAPlatform/OpenAuth.WebApi/Model/mqtt/MqttHostedService.cs

83 lines
3.1 KiB
C#
Raw Normal View History

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-06-19 14:47:46 +08:00
//查询网关,订阅主题
var topics = new List<string>();
2025-06-24 17:10:12 +08:00
var gatewayList = await _app.GetGatewaysnList();
2025-06-17 13:53:54 +08:00
2025-06-19 14:47:46 +08:00
foreach (var gateway in gatewayList)
{
topics.AddRange(new[]
{
2025-06-24 17:10:12 +08:00
$"thing/product/{gateway}/osd",
2025-06-19 14:47:46 +08:00
$"thing/product/{gateway}/events",
$"thing/product/{gateway}/requests",
2025-06-23 17:19:25 +08:00
$"thing/product/{gateway}/services_reply",
2025-06-27 15:49:25 +08:00
//$"thing/product/{gateway}/drc/up",
//$"thing/product/{gateway}/drc/down",
$"sys/product/{gateway}/status"
2025-06-19 14:47:46 +08:00
});
}
2025-07-04 09:57:44 +08:00
var snList = await _app.GetUavSn();
foreach (var sn in snList)
2025-07-04 09:48:53 +08:00
{
2025-07-04 09:57:44 +08:00
topics.AddRange(new[]
{
$"thing/product/{sn}/osd"
});
}
2025-06-19 14:47:46 +08:00
await _mqttCenter.ConnectAndSubscribeAsync(topics.ToArray());
2025-06-17 13:53:54 +08:00
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}