2019-11-17 16:27:58 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-10-03 20:37:53 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using NucuCar.Telemetry.Abstractions;
|
2019-11-17 16:27:58 +00:00
|
|
|
|
2021-10-03 20:37:53 +00:00
|
|
|
namespace NucuCar.Telemetry.Publishers
|
2019-11-17 16:27:58 +00:00
|
|
|
{
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The TelemetryPublisher is an abstract class, which provides a base for implementing telemetry publishers.
|
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
public abstract class BasePublisher : IDisposable, ITelemetryPublisher
|
2019-11-17 16:27:58 +00:00
|
|
|
{
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Raw connection string that is used to connect to the cloud service. Should be parsed if required.
|
|
|
|
/// </summary>
|
2019-11-23 18:53:04 +00:00
|
|
|
protected string ConnectionString { get; set; }
|
2019-11-24 13:12:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Telemetry source that indicates the source of the telemetry data.
|
|
|
|
/// </summary>
|
2019-11-23 18:53:04 +00:00
|
|
|
protected string TelemetrySource { get; set; }
|
2019-11-24 13:12:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A list containing entries to the telemeters that want to publish data to the cloud.
|
|
|
|
/// </summary>
|
2019-11-17 16:27:58 +00:00
|
|
|
protected readonly List<ITelemeter> RegisteredTelemeters;
|
2019-11-24 13:12:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The logging instance, if it's null then the module won't log anything.
|
|
|
|
/// </summary>
|
2019-11-23 17:02:53 +00:00
|
|
|
// ReSharper disable once UnassignedField.Global
|
2019-11-23 18:53:04 +00:00
|
|
|
protected readonly ILogger Logger;
|
2019-11-17 16:27:58 +00:00
|
|
|
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
2019-12-18 20:57:33 +00:00
|
|
|
/// Parameter less constructor, mainly used for testing.
|
2019-11-24 13:12:12 +00:00
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
public BasePublisher()
|
2019-11-17 16:27:58 +00:00
|
|
|
{
|
2020-08-01 14:49:20 +00:00
|
|
|
RegisteredTelemeters = new List<ITelemeter>(10);
|
2019-11-17 16:27:58 +00:00
|
|
|
}
|
2021-08-02 18:28:02 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// Constructor for <see cref="BasePublisher"/>.
|
2021-08-02 18:28:02 +00:00
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <param name="opts">TelemetryPublisher options, see: <see cref="PublisherOptions"/></param>
|
|
|
|
protected BasePublisher(PublisherOptions opts)
|
2021-08-02 18:28:02 +00:00
|
|
|
{
|
|
|
|
ConnectionString = opts.ConnectionString;
|
|
|
|
TelemetrySource = opts.TelemetrySource;
|
|
|
|
Logger = opts.Logger;
|
|
|
|
RegisteredTelemeters = new List<ITelemeter>(10);
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:27:58 +00:00
|
|
|
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Method that sends all data from the (<see cref="RegisteredTelemeters"/>) to the cloud.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cancellationToken">A cancellation token.</param>
|
|
|
|
/// <returns>A task</returns>
|
2019-11-17 16:27:58 +00:00
|
|
|
public abstract Task PublishAsync(CancellationToken cancellationToken);
|
2019-11-24 13:12:12 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Method that releases all managed resources.
|
|
|
|
/// </summary>
|
|
|
|
public abstract void Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Method that adds a telemeter to the <see cref="RegisteredTelemeters"/> collection.
|
|
|
|
/// The telemeter can register only once.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="t">The <see cref="ITelemeter"/></param>
|
|
|
|
/// <returns>Returns true if the telemeter has registered successfully and false otherwise.</returns>
|
2019-11-17 16:27:58 +00:00
|
|
|
public bool RegisterTelemeter(ITelemeter t)
|
|
|
|
{
|
2020-01-26 10:52:09 +00:00
|
|
|
if (RegisteredTelemeters.Contains(t) || !t.IsTelemetryEnabled()) return false;
|
2021-08-01 18:01:19 +00:00
|
|
|
Logger?.LogDebug("Registering telemeter {Identifier}", t.GetIdentifier());
|
2019-11-17 16:27:58 +00:00
|
|
|
RegisteredTelemeters.Add(t);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-24 13:12:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Method that deletes a telemeter from the <see cref="RegisteredTelemeters"/> collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="t">The <see cref="ITelemeter"/></param>
|
|
|
|
/// <returns>Returns true if the telemeter has unregistered successfully and false otherwise.</returns>
|
2019-11-17 16:27:58 +00:00
|
|
|
public bool UnRegisterTelemeter(ITelemeter t)
|
|
|
|
{
|
|
|
|
if (!RegisteredTelemeters.Contains(t)) return false;
|
2021-08-01 18:01:19 +00:00
|
|
|
Logger?.LogDebug("UnRegistering telemeter {Identifier}", t.GetIdentifier());
|
2019-11-17 16:27:58 +00:00
|
|
|
RegisteredTelemeters.Remove(t);
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-02 18:28:02 +00:00
|
|
|
|
2019-12-18 20:57:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Iterates through the registered telemeters and returns the telemetry data as dictionary.
|
|
|
|
/// It also adds metadata information such as: source and timestamp.
|
|
|
|
/// </summary>
|
2021-10-03 20:37:53 +00:00
|
|
|
/// <returns>A dictionary containing all telemetry data. <see cref="DataAggregate"/></returns>
|
|
|
|
protected virtual DataAggregate GetTelemetry()
|
2019-12-18 20:57:33 +00:00
|
|
|
{
|
2021-10-03 20:37:53 +00:00
|
|
|
var source = TelemetrySource ?? nameof(BasePublisher);
|
|
|
|
var allTelemetryData = new List<JObject>();
|
2019-12-18 20:57:33 +00:00
|
|
|
foreach (var telemeter in RegisteredTelemeters)
|
|
|
|
{
|
2021-10-03 15:08:07 +00:00
|
|
|
var telemetryData = telemeter.GetTelemetryJson();
|
2019-12-18 20:57:33 +00:00
|
|
|
if (telemetryData == null)
|
|
|
|
{
|
2021-08-01 18:01:19 +00:00
|
|
|
Logger?.LogWarning("Warning! Data for {Identifier} is null!", telemeter.GetIdentifier());
|
2019-12-18 20:57:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-03 20:37:53 +00:00
|
|
|
telemetryData["sensor_name"] = telemeter.GetIdentifier();
|
|
|
|
allTelemetryData.Add(telemetryData);
|
2019-12-18 20:57:33 +00:00
|
|
|
}
|
2021-10-03 20:37:53 +00:00
|
|
|
return new DataAggregate(source, allTelemetryData);
|
2019-12-18 20:57:33 +00:00
|
|
|
}
|
2019-11-17 16:27:58 +00:00
|
|
|
}
|
|
|
|
}
|