diff --git a/NucuCar.Sensors/EnvironmentSensor/Sensor.cs b/NucuCar.Sensors/EnvironmentSensor/Sensor.cs index d910f95..c505fe1 100644 --- a/NucuCar.Sensors/EnvironmentSensor/Sensor.cs +++ b/NucuCar.Sensors/EnvironmentSensor/Sensor.cs @@ -104,15 +104,25 @@ namespace NucuCar.Sensors.EnvironmentSensor $"{_lastMeasurement.Temperature:N2} \u00B0C | {_lastMeasurement.Pressure:N2} hPa | {_lastMeasurement.Humidity:N2} %rH"); } + public string GetIdentifier() + { + return nameof(EnvironmentSensor); + } + public Dictionary GetTelemetryData() { - return new Dictionary + Dictionary returnValue = null; + if (_lastMeasurement != null) { - ["temperature"] = _lastMeasurement.Temperature, - ["humidity"] = _lastMeasurement.Humidity, - ["pressure"] = _lastMeasurement.Pressure, - ["voc"] = _lastMeasurement.VolatileOrganicCompound - }; + returnValue = new Dictionary + { + ["temperature"] = _lastMeasurement.Temperature, + ["humidity"] = _lastMeasurement.Humidity, + ["pressure"] = _lastMeasurement.Pressure, + ["voc"] = _lastMeasurement.VolatileOrganicCompound + }; + } + return returnValue; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Telemetry/ITelemetrySensor.cs b/NucuCar.Sensors/Telemetry/ITelemetrySensor.cs index c038782..afe43a3 100644 --- a/NucuCar.Sensors/Telemetry/ITelemetrySensor.cs +++ b/NucuCar.Sensors/Telemetry/ITelemetrySensor.cs @@ -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 GetTelemetryData(); } diff --git a/NucuCar.Sensors/Telemetry/TelemetryService.cs b/NucuCar.Sensors/Telemetry/TelemetryService.cs index 339c5b6..bb5ae14 100644 --- a/NucuCar.Sensors/Telemetry/TelemetryService.cs +++ b/NucuCar.Sensors/Telemetry/TelemetryService.cs @@ -53,7 +53,7 @@ namespace NucuCar.Sensors.Telemetry jwt = Jose.JWT.Encode(new Dictionary() { ["iat"] = DateTime.UtcNow, - ["exp"] = DateTime.UtcNow.AddMinutes(60), + ["exp"] = DateTime.UtcNow.AddDays(60), ["aud"] = ProjectId }, rsa, Jose.JwsAlgorithm.RS256); } @@ -74,17 +74,27 @@ namespace NucuCar.Sensors.Telemetry public async Task StartAsync() { - var options = new ManagedMqttClientOptionsBuilder() - .WithAutoReconnectDelay(TimeSpan.FromSeconds(5)) - .WithClientOptions(new MqttClientOptionsBuilder() - .WithClientId($"projects/{ProjectId}/locations/{Region}/registries/{RegistryId}/devices/{DeviceId}") - .WithCredentials("unused", GetMqttPassword()) - .WithTcpServer("mqtt.googleapis.com") - .WithTls().Build()) - .Build(); - _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}") + .WithCredentials("unused", GetMqttPassword()) + .WithTcpServer("mqtt.googleapis.com") + .WithTls().Build()) + .Build(); + } + catch (IOException e) + { + _logger.LogCritical(e.Message); + throw; + } + 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); } } diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index a994b28..a7b069d 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -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": {