diff --git a/NucuCar.Domain/Readme.md b/NucuCar.Domain/Readme.md index 90b6c4d..41289ab 100644 --- a/NucuCar.Domain/Readme.md +++ b/NucuCar.Domain/Readme.md @@ -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 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. \ No newline at end of file diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs index 049a17a..fae15f0 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs @@ -16,13 +16,15 @@ namespace NucuCar.Domain.Telemetry public class TelemetryPublisherDisk : TelemetryPublisher { private readonly FileStream _fileStream; + private readonly string _separator; /// /// Constructs an instance of . /// /// The connection string must contain the following options: /// 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 /// /// @@ -31,9 +33,10 @@ namespace NucuCar.Domain.Telemetry { var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); var fileName = connectionStringParams.GetValueOrDefault("FileName"); - var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "txt"); + var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "csv"); var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096"); - + _separator = connectionStringParams.GetValueOrDefault("Separator", ","); + _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write, FileShare.Read, int.Parse(bufferSize), true); Logger?.LogDebug("Initialized the TelemetryPublisherDisk!"); @@ -44,7 +47,7 @@ namespace NucuCar.Domain.Telemetry var data = GetTelemetry(); var messageString = JsonConvert.SerializeObject(data); Logger?.LogDebug($"Telemetry message: {messageString}"); - var encodedText = Encoding.Unicode.GetBytes(messageString); + var encodedText = Encoding.Unicode.GetBytes($"{messageString}{_separator}"); try {