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;
|
2026-02-28 10:48:20 +08:00
|
|
|
|
private readonly IConfiguration _configuration;
|
2025-06-17 13:53:54 +08:00
|
|
|
|
|
2025-07-10 13:52:27 +08:00
|
|
|
|
|
2026-02-28 10:48:20 +08:00
|
|
|
|
public MqttHostedService(IServiceProvider serviceProvider, MqttMessageCenter mqttCenter, AirportMaintenanceApp app,IConfiguration configuration)
|
2025-06-17 13:53:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
2025-06-19 14:47:46 +08:00
|
|
|
|
_mqttCenter = mqttCenter;
|
|
|
|
|
|
_app = app;
|
2026-02-28 10:48:20 +08:00
|
|
|
|
_configuration = configuration;
|
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>();
|
2026-02-28 10:48:20 +08:00
|
|
|
|
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",
|
2026-03-02 10:47:02 +08:00
|
|
|
|
"thing/product/+/onboardcase",
|
2026-01-09 10:57:14 +08:00
|
|
|
|
"ai/task/+/tasklog",
|
|
|
|
|
|
"ai/task/+/aiachievement"
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|