diff --git a/NucuCar.Sensors/BaseSensorConfig.cs b/NucuCar.Sensors/BaseSensorConfig.cs new file mode 100644 index 0000000..5884c81 --- /dev/null +++ b/NucuCar.Sensors/BaseSensorConfig.cs @@ -0,0 +1,11 @@ +namespace NucuCar.Sensors +{ + public class BaseSensorConfig + { + public bool Enabled { get; set; } = false; + public bool Telemetry { get; set; } = false; + public bool Grpc { get; set; } = false; + + public int MeasurementInterval { get; set; } = 3000; + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Environment/Bme680Config.cs b/NucuCar.Sensors/Modules/Environment/Bme680Config.cs index c37c79b..f6c24c1 100644 --- a/NucuCar.Sensors/Modules/Environment/Bme680Config.cs +++ b/NucuCar.Sensors/Modules/Environment/Bme680Config.cs @@ -2,10 +2,7 @@ namespace NucuCar.Sensors.Modules.Environment { - public class Bme680Config + public class Bme680Config : BaseSensorConfig { - public bool Enabled { get; set; } - public bool Telemetry { get; set; } - public bool Grpc { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Environment/Bme680Worker.cs b/NucuCar.Sensors/Modules/Environment/Bme680Worker.cs index e4439f0..dc0aa7b 100644 --- a/NucuCar.Sensors/Modules/Environment/Bme680Worker.cs +++ b/NucuCar.Sensors/Modules/Environment/Bme680Worker.cs @@ -1,14 +1,16 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; namespace NucuCar.Sensors.Modules.Environment { public class Bme680Worker : SensorWorker { - public Bme680Worker(ILogger logger, Telemetry.Telemetry telemetry, ISensor sensor) + public Bme680Worker(ILogger logger, Telemetry.Telemetry telemetry, ISensor sensor, + IOptions options) { Logger = logger; - MeasurementInterval = 3000; + MeasurementInterval = options.Value.MeasurementInterval; TelemetryPublisher = telemetry.Publisher; Sensor = sensor.Object; } diff --git a/NucuCar.Sensors/Modules/Health/CpuTempConfig.cs b/NucuCar.Sensors/Modules/Health/CpuTempConfig.cs index 4e21ac4..b218977 100644 --- a/NucuCar.Sensors/Modules/Health/CpuTempConfig.cs +++ b/NucuCar.Sensors/Modules/Health/CpuTempConfig.cs @@ -1,10 +1,7 @@ // ReSharper disable UnusedAutoPropertyAccessor.Global namespace NucuCar.Sensors.Modules.Health { - public class CpuTempConfig + public class CpuTempConfig : BaseSensorConfig { - public bool Enabled { get; set; } - public bool Telemetry { get; set; } - public bool Grpc { get; set; } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Health/CpuTempWorker.cs b/NucuCar.Sensors/Modules/Health/CpuTempWorker.cs index eb5777d..7038871 100644 --- a/NucuCar.Sensors/Modules/Health/CpuTempWorker.cs +++ b/NucuCar.Sensors/Modules/Health/CpuTempWorker.cs @@ -1,14 +1,16 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; namespace NucuCar.Sensors.Modules.Health { public class CpuTempWorker : SensorWorker { - public CpuTempWorker(ILogger logger, Telemetry.Telemetry telemetry, ISensor sensor) + public CpuTempWorker(ILogger logger, Telemetry.Telemetry telemetry, + ISensor sensor, IOptions options) { Logger = logger; - MeasurementInterval = 3000; + MeasurementInterval = options.Value.MeasurementInterval; TelemetryPublisher = telemetry.Publisher; Sensor = sensor.Object; } diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs index 53ec13d..74ccd1a 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatConfig.cs @@ -1,9 +1,7 @@ namespace NucuCar.Sensors.Modules.Heartbeat { - public class HeartbeatConfig + public class HeartbeatConfig : BaseSensorConfig { - public bool Enabled { get; set; } - public bool Telemetry { get; set; } public bool Grpc { get; } = false; } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs index 6c68aea..2f4d5ae 100644 --- a/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs +++ b/NucuCar.Sensors/Modules/Heartbeat/HeartbeatWorker.cs @@ -1,16 +1,18 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NucuCar.Sensors.Abstractions; namespace NucuCar.Sensors.Modules.Heartbeat { - public class HeartbeatWorker: SensorWorker + public class HeartbeatWorker : SensorWorker { - public HeartbeatWorker(ILogger logger, Telemetry.Telemetry telemetry, ISensor sensor) - { - Logger = logger; - MeasurementInterval = 3000; - TelemetryPublisher = telemetry.Publisher; - Sensor = sensor.Object; - } + public HeartbeatWorker(ILogger logger, Telemetry.Telemetry telemetry, + ISensor sensor, IOptions options) + { + Logger = logger; + MeasurementInterval = options.Value.MeasurementInterval; + TelemetryPublisher = telemetry.Publisher; + Sensor = sensor.Object; + } } } \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Config.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Config.cs new file mode 100644 index 0000000..b85a483 --- /dev/null +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Config.cs @@ -0,0 +1,6 @@ +namespace NucuCar.Sensors.Modules.PMS5003 +{ + public class Pms5003Config : BaseSensorConfig + { + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs new file mode 100644 index 0000000..808736e --- /dev/null +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003GrpcService.cs @@ -0,0 +1,7 @@ +namespace NucuCar.Sensors.Modules.PMS5003 +{ + public class Pms5003GrpcService + { + + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs new file mode 100644 index 0000000..b7e7c6a --- /dev/null +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Sensor.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using NucuCar.Sensors.Abstractions; +using NucuCarSensorsProto; +using PMS5003; +using PMS5003.Exceptions; + +namespace NucuCar.Sensors.Modules.PMS5003 +{ + public class Pms5003Sensor : GenericTelemeterSensor, IDisposable, ISensor + { + private Pms5003 _pms5003; + private Pms5003Data _pms5003Data; + + public Pms5003Sensor() + { + } + + public Pms5003Sensor(ILogger logger, IOptions options) + { + CurrentState = SensorStateEnum.Uninitialized; + Logger = logger; + if (!options.Value.Enabled) + { + Logger?.LogDebug("Pms5003 Sensor is disabled!"); + CurrentState = SensorStateEnum.Disabled; + } + + TelemetryEnabled = options.Value.Telemetry; + } + + public override void Initialize() + { + _pms5003 = new Pms5003(23, 24); + } + + public override Task TakeMeasurementAsync() + { + try + { + _pms5003Data = _pms5003.ReadData(); + } + catch (ReadFailedException e) + { + Logger?.LogError("{message}", e.Message); + } + + return Task.CompletedTask; + } + + public override NucuCarSensorResponse GetMeasurement() + { + string jsonResponse = null; + jsonResponse = _pms5003Data != null ? JsonConvert.SerializeObject(_pms5003Data) : "{}"; + + return new NucuCarSensorResponse() + { + State = GetState(), + JsonData = jsonResponse + }; + } + + public override SensorStateEnum GetState() + { + return CurrentState; + } + + public override string GetIdentifier() + { + return "Pms5003"; + } + + public override Dictionary GetTelemetryData() + { + Dictionary returnValue = null; + if (_pms5003Data != null && TelemetryEnabled) + { + returnValue = new Dictionary + { + ["sensor_state"] = GetState(), + ["Pm1Atmospheric"] = _pms5003Data.Pm1Atmospheric, + ["Pm1Standard"] = _pms5003Data.Pm1Standard, + ["Pm10Atmospheric"] = _pms5003Data.Pm10Atmospheric, + ["Pm10Standard"] = _pms5003Data.Pm10Standard, + ["Pm2Dot5Atmospheric"] = _pms5003Data.Pm2Dot5Atmospheric, + ["Pm2Dot5Standard"] = _pms5003Data.Pm2Dot5Standard, + ["ParticlesDiameterBeyond0Dot3"] = _pms5003Data.ParticlesDiameterBeyond0Dot3, + ["ParticlesDiameterBeyond0Dot5"] = _pms5003Data.ParticlesDiameterBeyond0Dot5, + ["ParticlesDiameterBeyond1Dot0"] = _pms5003Data.ParticlesDiameterBeyond1Dot0, + ["ParticlesDiameterBeyond2Dot5"] = _pms5003Data.ParticlesDiameterBeyond2Dot5, + ["ParticlesDiameterBeyond5Dot0"] = _pms5003Data.ParticlesDiameterBeyond5Dot0, + ["ParticlesDiameterBeyond10Dot0"] = _pms5003Data.ParticlesDiameterBeyond10Dot0, + }; + } + + return returnValue; + } + + public override bool IsTelemetryEnabled() + { + return TelemetryEnabled; + } + + public void Dispose() + { + _pms5003 = null; + } + + public Pms5003Sensor Object => this; + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs b/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs new file mode 100644 index 0000000..fa12550 --- /dev/null +++ b/NucuCar.Sensors/Modules/PMS5003/Pms5003Worker.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using NucuCar.Sensors.Abstractions; + +namespace NucuCar.Sensors.Modules.PMS5003 +{ + public class Pms5003Worker : SensorWorker + { + public Pms5003Worker(ILogger logger, Telemetry.Telemetry telemetry, + ISensor sensor, IOptions options) + { + Logger = logger; + MeasurementInterval = options.Value.MeasurementInterval; + TelemetryPublisher = telemetry.Publisher; + Sensor = sensor.Object; + } + } +} \ No newline at end of file diff --git a/NucuCar.Sensors/NucuCar.Sensors.csproj b/NucuCar.Sensors/NucuCar.Sensors.csproj index 2c48c56..9bbe212 100644 --- a/NucuCar.Sensors/NucuCar.Sensors.csproj +++ b/NucuCar.Sensors/NucuCar.Sensors.csproj @@ -6,12 +6,13 @@ - - - - - - + + + + + + + diff --git a/NucuCar.UnitTests/NucuCar.Sensors.Tests/SensorWorker.cs b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs similarity index 91% rename from NucuCar.UnitTests/NucuCar.Sensors.Tests/SensorWorker.cs rename to NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs index a3afa66..9c20f4d 100644 --- a/NucuCar.UnitTests/NucuCar.Sensors.Tests/SensorWorker.cs +++ b/NucuCar.UnitTests/NucuCar.Sensors.Tests/Bme680WorkerTest.cs @@ -43,13 +43,14 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests { Enabled = true, }); - var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object); + var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object, + new OptionsWrapper(new Bme680Config())); await service.StartAsync(_cts.Token); _mockTestBme680Sensor.Verify(s => s.Initialize(), Times.AtLeastOnce); await service.StopAsync(_cts.Token); } - + [Fact] public async Task Test_Bme680Worker_SensorIsBeingMeasured() { @@ -58,8 +59,9 @@ namespace NucuCar.UnitTests.NucuCar.Sensors.Tests Enabled = true, }); _mockTestBme680Sensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized); - - var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object); + + var service = new Bme680Worker(_mockLogger.Object, _mockSensorTelemetry.Object, _mockBme680ISensor.Object, + new OptionsWrapper(new Bme680Config())); await service.StartAsync(_cts.Token); _mockTestBme680Sensor.Verify(s => s.Initialize(), Times.AtLeastOnce); _mockTestBme680Sensor.Verify(s => s.TakeMeasurementAsync(), Times.AtLeastOnce);