Add NullObject & FileNotFound exception handling for publishing telemetry data
This commit is contained in:
parent
572c12d43f
commit
70c1dc290c
4 changed files with 46 additions and 19 deletions
|
@ -104,9 +104,17 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
$"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH");
|
||||
}
|
||||
|
||||
public string GetIdentifier()
|
||||
{
|
||||
return nameof(EnvironmentSensor);
|
||||
}
|
||||
|
||||
public Dictionary<string, double> GetTelemetryData()
|
||||
{
|
||||
return new Dictionary<string, double>
|
||||
Dictionary<string, double> returnValue = null;
|
||||
if (_lastMeasurement != null)
|
||||
{
|
||||
returnValue = new Dictionary<string, double>
|
||||
{
|
||||
["temperature"] = _lastMeasurement.Temperature,
|
||||
["humidity"] = _lastMeasurement.Humidity,
|
||||
|
@ -114,5 +122,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
|||
["voc"] = _lastMeasurement.VolatileOrganicCompound
|
||||
};
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NucuCar.Sensors.Telemetry
|
||||
{
|
||||
public interface ITelemetrySensor
|
||||
{
|
||||
string GetIdentifier();
|
||||
/* Dictionary containing the topic and the value */
|
||||
Dictionary<string, double> GetTelemetryData();
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ namespace NucuCar.Sensors.Telemetry
|
|||
jwt = Jose.JWT.Encode(new Dictionary<string, object>()
|
||||
{
|
||||
["iat"] = DateTime.UtcNow,
|
||||
["exp"] = DateTime.UtcNow.AddMinutes(60),
|
||||
["exp"] = DateTime.UtcNow.AddDays(60),
|
||||
["aud"] = ProjectId
|
||||
}, rsa, Jose.JwsAlgorithm.RS256);
|
||||
}
|
||||
|
@ -74,7 +74,11 @@ namespace NucuCar.Sensors.Telemetry
|
|||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
var options = new ManagedMqttClientOptionsBuilder()
|
||||
_logger.LogInformation("Starting the MQTT client.");
|
||||
ManagedMqttClientOptions options;
|
||||
try
|
||||
{
|
||||
options = new ManagedMqttClientOptionsBuilder()
|
||||
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
|
||||
.WithClientOptions(new MqttClientOptionsBuilder()
|
||||
.WithClientId($"projects/{ProjectId}/locations/{Region}/registries/{RegistryId}/devices/{DeviceId}")
|
||||
|
@ -82,9 +86,15 @@ namespace NucuCar.Sensors.Telemetry
|
|||
.WithTcpServer("mqtt.googleapis.com")
|
||||
.WithTls().Build())
|
||||
.Build();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_logger.LogCritical(e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Starting the MQTT client.");
|
||||
await _mqttClient.StartAsync(options);
|
||||
_logger.LogInformation("Started the MQTT client!");
|
||||
}
|
||||
|
||||
public async Task PublishDataAsync(CancellationToken cancellationToken)
|
||||
|
@ -92,6 +102,11 @@ namespace NucuCar.Sensors.Telemetry
|
|||
foreach (var sensor in _registeredSensors)
|
||||
{
|
||||
var data = sensor.GetTelemetryData();
|
||||
if (data == null)
|
||||
{
|
||||
_logger.LogWarning($"Warning! Data for {sensor.GetIdentifier()} is null!");
|
||||
continue;
|
||||
}
|
||||
await UploadData(data, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
{
|
||||
"Telemetry": {
|
||||
"Enabled": true,
|
||||
"Interval": 5000,
|
||||
"Interval": 3000,
|
||||
"ProjectId": "playground-239221",
|
||||
"DeviceId": "3087236321317137",
|
||||
"RegistryId": "Development",
|
||||
"Region": "europe-west1",
|
||||
"RS256File": "Certificates\\key.key"
|
||||
"RS256File": "/home/denis/Projects/RiderProjects/NucuCar/NucuCar.Sensors/Certificates/jwt.key"
|
||||
},
|
||||
"EnvironmentSensor": {
|
||||
"Enabled": true,
|
||||
"Telemetry": false,
|
||||
"Telemetry": true,
|
||||
"MeasurementInterval": 1000
|
||||
},
|
||||
"Logging": {
|
||||
|
|
Loading…
Reference in a new issue