NucuCar.Domain.TelemetryPublisherDisk: Add option for Separator

This commit is contained in:
Denis-Cosmin Nutiu 2020-01-24 16:12:58 +02:00
parent aad410f3ac
commit 33c2737186
2 changed files with 24 additions and 4 deletions

View file

@ -35,3 +35,20 @@ export Telemetry:ConnectionString=CONNECTION_STRING
A telemetry reader can be found in NucuCar.TestClient. You'll need a connection string that can be found in A telemetry reader can be found in NucuCar.TestClient. You'll need a connection string that can be found in
Azure's IoT Hub Build-In Endpoints setting. Azure's IoT Hub Build-In Endpoints setting.
---
### Disk Telemetry
#### Publisher
Publishes telemetry on the disk.
Example connection string:
`Filename=telemetry;FileExtension=csv;Separator=,;BufferSize=4096`
See the source code for comments on the ConnectionString.
### Reader
You will need to parse the file by yourself.

View file

@ -16,13 +16,15 @@ namespace NucuCar.Domain.Telemetry
public class TelemetryPublisherDisk : TelemetryPublisher public class TelemetryPublisherDisk : TelemetryPublisher
{ {
private readonly FileStream _fileStream; private readonly FileStream _fileStream;
private readonly string _separator;
/// <summary> /// <summary>
/// Constructs an instance of <see cref="TelemetryPublisherDisk"/>. /// Constructs an instance of <see cref="TelemetryPublisherDisk"/>.
/// <remarks> /// <remarks>
/// The connection string must contain the following options: /// The connection string must contain the following options:
/// Filename (required) - The path of the filename in which to log telemetry data. /// Filename (required) - The path of the filename in which to log telemetry data.
/// FileExtension (optional) - The extension of the filename. Default txt /// FileExtension (optional) - The extension of the filename. Default csv
/// Separator (optional) - The separator of the messages. Default ,
/// BufferSize (optional) - The buffer size for the async writer. Default: 4096 /// BufferSize (optional) - The buffer size for the async writer. Default: 4096
/// </remarks> /// </remarks>
/// </summary> /// </summary>
@ -31,9 +33,10 @@ namespace NucuCar.Domain.Telemetry
{ {
var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString);
var fileName = connectionStringParams.GetValueOrDefault("FileName"); var fileName = connectionStringParams.GetValueOrDefault("FileName");
var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "txt"); var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "csv");
var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096"); var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096");
_separator = connectionStringParams.GetValueOrDefault("Separator", ",");
_fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write, _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write,
FileShare.Read, int.Parse(bufferSize), true); FileShare.Read, int.Parse(bufferSize), true);
Logger?.LogDebug("Initialized the TelemetryPublisherDisk!"); Logger?.LogDebug("Initialized the TelemetryPublisherDisk!");
@ -44,7 +47,7 @@ namespace NucuCar.Domain.Telemetry
var data = GetTelemetry(); var data = GetTelemetry();
var messageString = JsonConvert.SerializeObject(data); var messageString = JsonConvert.SerializeObject(data);
Logger?.LogDebug($"Telemetry message: {messageString}"); Logger?.LogDebug($"Telemetry message: {messageString}");
var encodedText = Encoding.Unicode.GetBytes(messageString); var encodedText = Encoding.Unicode.GetBytes($"{messageString}{_separator}");
try try
{ {