2020-10-24 23:23:22 +02:00
|
|
|
|
using Birdmap.BLL.Interfaces;
|
2020-12-03 18:52:25 +01:00
|
|
|
|
using Birdmap.BLL.Options;
|
2020-10-24 23:23:22 +02:00
|
|
|
|
using Birdmap.BLL.Services;
|
2020-12-03 18:52:25 +01:00
|
|
|
|
using Birdmap.BLL.Services.CommunicationServices.Mqtt;
|
2020-10-24 23:23:22 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-12-03 18:52:25 +01:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
2020-11-23 10:50:10 +01:00
|
|
|
|
using System.Net.Http;
|
2020-10-24 23:23:22 +02:00
|
|
|
|
|
|
|
|
|
namespace Birdmap.BLL
|
|
|
|
|
{
|
|
|
|
|
public static class Startup
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection ConfigureBLL(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddTransient<IAuthService, AuthService>();
|
2020-10-25 16:15:06 +01:00
|
|
|
|
services.AddTransient<IUserService, UserService>();
|
2020-11-01 12:25:45 +01:00
|
|
|
|
services.AddTransient<IServiceService, ServiceService>();
|
2020-10-24 23:23:22 +02:00
|
|
|
|
|
2020-11-07 14:19:29 +01:00
|
|
|
|
if (configuration.GetValue<bool>("UseDummyServices"))
|
2020-11-08 18:51:19 +01:00
|
|
|
|
{
|
|
|
|
|
services.AddTransient<IInputService, DummyDeviceAndInputService>();
|
|
|
|
|
services.AddTransient<IDeviceService, DummyDeviceAndInputService>();
|
|
|
|
|
}
|
2020-11-07 14:19:29 +01:00
|
|
|
|
else
|
2020-11-08 18:51:19 +01:00
|
|
|
|
{
|
2020-11-23 10:50:10 +01:00
|
|
|
|
var baseUrl = configuration.GetValue<string>("ServicesBaseUrl");
|
|
|
|
|
|
|
|
|
|
services.AddTransient<IInputService, LiveInputService>(serviceProvider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = serviceProvider.GetService<HttpClient>();
|
|
|
|
|
var service = new LiveInputService(baseUrl, httpClient);
|
|
|
|
|
return service;
|
|
|
|
|
});
|
|
|
|
|
services.AddTransient<IDeviceService, LiveDummyService>(serviceProvider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = serviceProvider.GetService<HttpClient>();
|
|
|
|
|
var service = new LiveDummyService(baseUrl, httpClient);
|
|
|
|
|
return service;
|
|
|
|
|
});
|
2020-11-08 18:51:19 +01:00
|
|
|
|
}
|
2020-11-07 14:19:29 +01:00
|
|
|
|
|
2020-12-03 18:52:25 +01:00
|
|
|
|
services.AddSignalR();
|
|
|
|
|
|
|
|
|
|
services.AddMqttClientServiceWithConfig(opt =>
|
|
|
|
|
{
|
|
|
|
|
var mqtt = configuration.GetSection("Mqtt");
|
|
|
|
|
|
|
|
|
|
var mqttClient = mqtt.GetSection("ClientSettings");
|
|
|
|
|
var clientSettings = new
|
|
|
|
|
{
|
|
|
|
|
Id = mqttClient.GetValue<string>("Id"),
|
|
|
|
|
Username = mqttClient.GetValue<string>("Username"),
|
|
|
|
|
Password = mqttClient.GetValue<string>("Password"),
|
|
|
|
|
Topic = mqttClient.GetValue<string>("Topic"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var mqttBrokerHost = mqtt.GetSection("BrokerHostSettings");
|
|
|
|
|
var brokerHostSettings = new
|
|
|
|
|
{
|
|
|
|
|
Host = mqttBrokerHost.GetValue<string>("Host"),
|
|
|
|
|
Port = mqttBrokerHost.GetValue<int>("Port"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
opt
|
|
|
|
|
.WithTopic(clientSettings.Topic)
|
|
|
|
|
.WithCredentials(clientSettings.Username, clientSettings.Password)
|
|
|
|
|
.WithClientId(clientSettings.Id)
|
|
|
|
|
.WithTcpServer(brokerHostSettings.Host, brokerHostSettings.Port);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptions> configureOptions)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton(serviceProvider =>
|
|
|
|
|
{
|
|
|
|
|
var optionBuilder = new AspCoreMqttClientOptions(serviceProvider);
|
|
|
|
|
configureOptions(optionBuilder);
|
|
|
|
|
return optionBuilder.Build();
|
|
|
|
|
});
|
|
|
|
|
services.AddSingleton<MqttClientService>();
|
|
|
|
|
services.AddSingleton<IHostedService>(serviceProvider =>
|
|
|
|
|
{
|
|
|
|
|
return serviceProvider.GetService<MqttClientService>();
|
|
|
|
|
});
|
|
|
|
|
services.AddSingleton(serviceProvider =>
|
|
|
|
|
{
|
|
|
|
|
var mqttClientService = serviceProvider.GetService<MqttClientService>();
|
|
|
|
|
var mqttClientServiceProvider = new MqttClientServiceProvider(mqttClientService);
|
|
|
|
|
return mqttClientServiceProvider;
|
|
|
|
|
});
|
2020-10-24 23:23:22 +02:00
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|