NucuCar/NucuCar.Sensors/Telemetry/SensorTelemetryPublisher.cs

44 lines
1.3 KiB
C#
Raw Normal View History

using System;
using Microsoft.Extensions.Logging;
using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors.Telemetry
{
public class SensorTelemetryPublisher : IDisposable
{
private static object _palock = new object();
public static TelemetryPublisher Instance { get; private set; }
2019-11-24 13:12:12 +00:00
/// <summary>
/// Creates a telemetry publisher instance see <see cref="TelemetryPublisher"/>.
/// </summary>
public static TelemetryPublisher CreateSingleton(string connectionString, string telemetrySource,
ILogger logger)
{
2019-11-24 13:12:12 +00:00
lock (_palock)
{
2019-11-24 13:12:12 +00:00
if (Instance != null) return Instance;
var telemetryPublisher =
TelemetryPublisherAzure.CreateFromConnectionString(connectionString, telemetrySource, logger);
Instance = telemetryPublisher;
return Instance;
}
}
private static void ReleaseUnmanagedResources()
{
Instance?.Dispose();
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~SensorTelemetryPublisher()
{
ReleaseUnmanagedResources();
}
}
}