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

60 lines
2.2 KiB
C#

using Infrastructure.CloudSdk.mqttmessagecenter;
using NPOI.SS.Formula.Functions;
using NuGet.Packaging;
using OpenAuth.App.ServiceApp;
using System.Security.Cryptography;
namespace OpenAuth.WebApi.Model.mqtt
{
public class MqttHostedService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly MqttMessageCenter _mqttCenter;
private readonly AirportMaintenanceApp _app;
private readonly IConfiguration _configuration;
public MqttHostedService(IServiceProvider serviceProvider, MqttMessageCenter mqttCenter, AirportMaintenanceApp app,IConfiguration configuration)
{
_serviceProvider = serviceProvider;
_mqttCenter = mqttCenter;
_app = app;
_configuration = configuration;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
var handlers = scope.ServiceProvider.GetServices<IMqttMessageHandler>();
var serverIp = _configuration["MQTT:Server"];
var port = _configuration["MQTT:Port"];
var username = _configuration["MQTT:UserName"];
var password = _configuration["MQTT:Password"];
await _mqttCenter.InitializeAsync(
handlers,
server: serverIp,
port: int.Parse(port),
clientId: Guid.NewGuid().ToString(),
username: username,
password: password
);
string[] topicList =
{
"thing/product/+/services_reply",
"thing/product/+/events",
"thing/product/+/requests",
"thing/product/+/osd",
"sys/product/+/status",
"thing/product/+/control-operation",
"thing/product/+/onboardcase",
"ai/task/+/tasklog",
"ai/task/+/aiachievement"
};
await _mqttCenter.SubscribeAsync(topicList);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}