Skip to content

Commit

Permalink
ADD project SmartIOT.Connector.DependecyInjection. Refactor App proje…
Browse files Browse the repository at this point in the history
…ct to use DI.
  • Loading branch information
luca-domenichini committed Dec 15, 2023
1 parent f5127e4 commit 9355f3c
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 164 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dotnet-prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
run: |
dotnet nuget push Connectors\SmartIOT.Connector.Mqtt\bin\Release\SmartIOT.Connector.Mqtt.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Connectors\SmartIOT.Connector.Tcp\bin\Release\SmartIOT.Connector.Tcp.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.DependencyInjection\bin\Release\SmartIOT.Connector.DependencyInjection.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Messages\bin\Release\SmartIOT.Connector.Messages.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Prometheus\bin\Release\SmartIOT.Connector.Prometheus.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Core\bin\Release\SmartIOT.Connector.Core.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/dotnet-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
run: |
dotnet nuget push Connectors\SmartIOT.Connector.Mqtt\bin\Release\SmartIOT.Connector.Mqtt.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Connectors\SmartIOT.Connector.Tcp\bin\Release\SmartIOT.Connector.Tcp.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.DependencyInjection\bin\Release\SmartIOT.Connector.DependencyInjection.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Messages\bin\Release\SmartIOT.Connector.Messages.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Prometheus\bin\Release\SmartIOT.Connector.Prometheus.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
dotnet nuget push Core\SmartIOT.Connector.Core\bin\Release\SmartIOT.Connector.Core.${{ steps.version.outputs.PRODUCT_VERSION }}.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
Expand Down
23 changes: 0 additions & 23 deletions Apps/SmartIOT.Connector.App/AspNetExtensions.cs

This file was deleted.

21 changes: 19 additions & 2 deletions Apps/SmartIOT.Connector.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Asp.Versioning.ApiExplorer;
using Serilog;
using SmartIOT.Connector.DependencyInjection;
using SmartIOT.Connector.Prometheus;
using SmartIOT.Connector.RestApi;
using System.Diagnostics;
using System.Text.Json;
Expand Down Expand Up @@ -33,7 +35,7 @@ public static void Main(string[] args)
{
ReadCommentHandling = JsonCommentHandling.Skip
});
if (configuration == null)
if (configuration?.Configuration is null)
{
Console.WriteLine($"Configuration not valid for file {path}");
return;
Expand All @@ -48,7 +50,12 @@ public static void Main(string[] args)
builder.Logging.AddSerilog(dispose: true);

// Add SmartIOT.Connector services to the container.
builder.Services.AddSmartIotConnectorRunner(configuration);
builder.Services.AddSmartIOTConnector(cfg =>
{
cfg.WithAutoDiscoverConnectorFactories()
.WithAutoDiscoverDeviceDriverFactories()
.WithConfiguration(configuration.Configuration);
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
Expand All @@ -69,8 +76,17 @@ public static void Main(string[] args)
o.ServiceName = typeof(Program).Assembly.GetName().Name!;
});

// ----------------------------------------------------------------------------
// build app
var app = builder.Build();

// configure more things on SmartIOT.Connector
app.UseSmartIOTConnector(cfg =>
{
if (configuration.PrometheusConfiguration is not null)
cfg.AddPrometheus(configuration.PrometheusConfiguration);
});

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI(options =>
Expand All @@ -93,6 +109,7 @@ public static void Main(string[] args)
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("{NewLine}{NewLine} --> SmartIOT.Connector v{version}{NewLine}", Environment.NewLine, Environment.NewLine, version, Environment.NewLine);

// --------------------------------------------------------------------------------
app.Run();
}

Expand Down
112 changes: 0 additions & 112 deletions Apps/SmartIOT.Connector.App/Runner.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Apps/SmartIOT.Connector.App/SmartIOT.Connector.App.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
Expand Down Expand Up @@ -35,6 +35,7 @@
<ProjectReference Include="..\..\Connectors\SmartIOT.Connector.Mqtt\SmartIOT.Connector.Mqtt.csproj" />
<ProjectReference Include="..\..\Connectors\SmartIOT.Connector.Tcp\SmartIOT.Connector.Tcp.csproj" />
<ProjectReference Include="..\..\Core\SmartIOT.Connector.Core\SmartIOT.Connector.Core.csproj" />
<ProjectReference Include="..\..\Core\SmartIOT.Connector.DependencyInjection\SmartIOT.Connector.DependencyInjection.csproj" />
<ProjectReference Include="..\..\Core\SmartIOT.Connector.Prometheus\SmartIOT.Connector.Prometheus.csproj" />
<ProjectReference Include="..\..\Core\SmartIOT.Connector.RestApi\SmartIOT.Connector.RestApi.csproj" />
<ProjectReference Include="..\..\Devices\SmartIOT.Connector.Plc.S7Net\SmartIOT.Connector.Plc.S7Net.csproj" />
Expand Down
4 changes: 4 additions & 0 deletions Core/SmartIOT.Connector.Core/SmartIOT.Connector.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SmartIOT.Connector.Messages\SmartIOT.Connector.Messages.csproj" />
</ItemGroup>
Expand Down
52 changes: 32 additions & 20 deletions Core/SmartIOT.Connector.Core/SmartIotConnectorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma warning disable S3885 // "Assembly.Load" should be used

using Microsoft.Extensions.DependencyInjection;
using SmartIOT.Connector.Core.Conf;
using SmartIOT.Connector.Core.Factory;
using SmartIOT.Connector.Core.Scheduler;
Expand All @@ -13,6 +14,7 @@ public class SmartIotConnectorBuilder
private readonly List<IDeviceDriver> _deviceDrivers = new List<IDeviceDriver>();
private bool _autoDiscoverConnectorFactory;
private readonly List<IConnector> _connectors = new List<IConnector>();

public ITimeService TimeService { get; private set; } = new TimeService();
public ISchedulerFactory SchedulerFactory { get; private set; } = new SchedulerFactory();
public SmartIotConnectorConfiguration? Configuration { get; private set; }
Expand Down Expand Up @@ -80,19 +82,19 @@ public SmartIotConnectorBuilder WithTimeService(ITimeService timeService)
return this;
}

public SmartIotConnector Build()
public SmartIotConnector Build(IServiceProvider? serviceProvider = null)
{
if (Configuration == null)
throw new InvalidOperationException("Error building module: Configuration is not set");

if (_autoDiscoverDeviceDriverFactory)
DeviceDriverFactory.AddRange(AutoDiscoverDeviceDriverFactories());
DeviceDriverFactory.AddRange(AutoDiscoverDeviceDriverFactories(serviceProvider));

if (!DeviceDriverFactory.Any() && _deviceDrivers.Count == 0)
throw new ArgumentException($"Nessuna {nameof(IDeviceDriverFactory)} o {nameof(IDeviceDriver)} presente in configurazione");
throw new ArgumentException($"No {nameof(IDeviceDriverFactory)} or {nameof(IDeviceDriver)} configured");

if (_autoDiscoverConnectorFactory)
ConnectorFactory.AddRange(AutoDiscoverConnectorFactories());
ConnectorFactory.AddRange(AutoDiscoverConnectorFactories(serviceProvider));

var schedulers = BuildSchedulers();
var connectors = BuildConnectors();
Expand Down Expand Up @@ -145,7 +147,7 @@ private List<ITagScheduler> BuildSchedulers()
return schedulers;
}

private List<IDeviceDriverFactory> AutoDiscoverDeviceDriverFactories()
private List<IDeviceDriverFactory> AutoDiscoverDeviceDriverFactories(IServiceProvider? serviceProvider)
{
var list = new List<IDeviceDriverFactory>();

Expand All @@ -157,20 +159,25 @@ private List<IDeviceDriverFactory> AutoDiscoverDeviceDriverFactories()

foreach (var type in assembly.ExportedTypes)
{
// le factory già presenti in elenco non li aggiungiamo nuovamente
bool alreadyAvailable = DeviceDriverFactory.Any(x => x.GetType() == type);
if (!alreadyAvailable
&& typeof(IDeviceDriverFactory).IsAssignableFrom(type)
if (typeof(IDeviceDriverFactory).IsAssignableFrom(type)
&& type != typeof(DeviceDriverFactory)
&& !DeviceDriverFactory.Any(x => x.GetType() == type) // do not add already added factories
&& !type.IsAbstract
&& type.IsClass
&& !type.IsInterface
&& type.IsPublic
&& type.IsVisible)
{
var ctor = type.GetConstructor(Array.Empty<Type>());
if (ctor != null)
list.Add((IDeviceDriverFactory)ctor.Invoke(Array.Empty<object>()));
if (serviceProvider is not null)
{
list.Add((IDeviceDriverFactory)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, type));
}
else
{
var ctor = type.GetConstructor(Array.Empty<Type>());
if (ctor != null)
list.Add((IDeviceDriverFactory)ctor.Invoke(Array.Empty<object>()));
}
}
}
}
Expand All @@ -187,7 +194,7 @@ ex is BadImageFormatException
return list;
}

private List<IConnectorFactory> AutoDiscoverConnectorFactories()
private List<IConnectorFactory> AutoDiscoverConnectorFactories(IServiceProvider? serviceProvider)
{
var list = new List<IConnectorFactory>();

Expand All @@ -199,20 +206,25 @@ private List<IConnectorFactory> AutoDiscoverConnectorFactories()

foreach (var type in assembly.ExportedTypes)
{
// le factory già presenti in elenco non li aggiungiamo nuovamente
bool alreadyAvailable = ConnectorFactory.Any(x => x.GetType() == type);
if (!alreadyAvailable
&& typeof(IConnectorFactory).IsAssignableFrom(type)
if (typeof(IConnectorFactory).IsAssignableFrom(type)
&& type != typeof(ConnectorFactory)
&& !ConnectorFactory.Any(x => x.GetType() == type) // do not add already added factories
&& !type.IsAbstract
&& type.IsClass
&& !type.IsInterface
&& type.IsPublic
&& type.IsVisible)
{
var ctor = type.GetConstructor(Array.Empty<Type>());
if (ctor != null)
list.Add((IConnectorFactory)ctor.Invoke(Array.Empty<object>()));
if (serviceProvider is not null)
{
list.Add((IConnectorFactory)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, type));
}
else
{
var ctor = type.GetConstructor(Array.Empty<Type>());
if (ctor != null)
list.Add((IConnectorFactory)ctor.Invoke(Array.Empty<object>()));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace SmartIOT.Connector.Core;

public class SmartIotConnectorConfiguration
{
public IList<string> ConnectorConnectionStrings { get; set; } = new List<string>();
public IList<DeviceConfiguration> DeviceConfigurations { get; set; } = new List<DeviceConfiguration>();
public List<string> ConnectorConnectionStrings { get; set; } = new List<string>();
public List<DeviceConfiguration> DeviceConfigurations { get; set; } = new List<DeviceConfiguration>();
public SchedulerConfiguration SchedulerConfiguration { get; set; } = new SchedulerConfiguration();

public static SmartIotConnectorConfiguration? FromJson(string json)
Expand Down
Loading

0 comments on commit 9355f3c

Please sign in to comment.