2021-08-02 18:28:02 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-24 15:47:17 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-26 18:24:27 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-08-01 15:07:13 +00:00
|
|
|
using NucuCar.Telemetry.Abstractions;
|
2019-12-28 15:09:17 +00:00
|
|
|
|
2019-11-30 15:19:56 +00:00
|
|
|
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
|
2020-04-17 15:11:07 +00:00
|
|
|
namespace NucuCar.Telemetry
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
2021-08-02 18:28:02 +00:00
|
|
|
public class TelemetryPublisherProxy : ITelemetryPublisher
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
2021-08-02 18:42:25 +00:00
|
|
|
// TODO: Add support for chaining publishers.
|
|
|
|
private ITelemetryPublisher Publisher { get; }
|
2019-11-24 15:47:17 +00:00
|
|
|
|
2020-05-01 08:29:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Class used together with the DI, holds a Publisher instance that's being create by options from
|
|
|
|
/// TelemetryConfig.
|
|
|
|
/// </summary>
|
2021-08-02 18:28:02 +00:00
|
|
|
public TelemetryPublisherProxy()
|
2019-11-30 15:19:56 +00:00
|
|
|
{
|
|
|
|
}
|
2019-12-28 15:09:17 +00:00
|
|
|
|
2021-08-02 18:28:02 +00:00
|
|
|
public TelemetryPublisherProxy(ILogger<TelemetryPublisherProxy> logger, IOptions<TelemetryConfig> options)
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
2019-11-26 18:24:27 +00:00
|
|
|
if (options.Value.ServiceEnabled)
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
2019-12-28 15:09:17 +00:00
|
|
|
Publisher = TelemetryPublisherFactory.Create(options.Value.Publisher, options.Value.ConnectionString,
|
2019-11-25 09:40:17 +00:00
|
|
|
"NucuCar.Sensors", logger);
|
2019-11-24 15:47:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Publisher = null;
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 18:28:02 +00:00
|
|
|
|
|
|
|
public Task PublishAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return Publisher.PublishAsync(cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool RegisterTelemeter(ITelemeter t)
|
|
|
|
{
|
|
|
|
return Publisher.RegisterTelemeter(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool UnRegisterTelemeter(ITelemeter t)
|
|
|
|
{
|
|
|
|
return Publisher.UnRegisterTelemeter(t);
|
|
|
|
}
|
2019-11-24 15:47:17 +00:00
|
|
|
}
|
|
|
|
}
|