From 6104f7e090be74860db101563f6d0e2c9648066c Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Wed, 23 Oct 2024 19:32:34 +0300 Subject: [PATCH] Add battery state and icon --- src/widgets/SensorReading/SensorReading.jsx | 37 +++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/widgets/SensorReading/SensorReading.jsx b/src/widgets/SensorReading/SensorReading.jsx index 1f59ced..2991ffa 100644 --- a/src/widgets/SensorReading/SensorReading.jsx +++ b/src/widgets/SensorReading/SensorReading.jsx @@ -16,6 +16,34 @@ const units = { const temperatureThresholds = [18, 24, 26, 32]; +/** +* @see https://fontawesome.com/search?q=battery&o=r&m=free +**/ +function getBatteryState(level) { + if (!level) { + return 0; + } + + if (level >= 100) { + return 5; + } + + if (level >= 75) { + return 4; + } + + if (level >= 50) { + return 3; + } + + if (level >= 25) { + return 2; + } + + // show full by default + return 5; +} + const SensorReading = ({ label, values: rawValues, @@ -31,6 +59,7 @@ const SensorReading = ({ ...value, dt: new Date(value.timestamp), thermometerState: type === 'temperature' ? temperatureThresholds.filter(threshold => threshold < value.value).length : 0, + batteryState: type === 'battery' ? getBatteryState(value.value) : 0, unit: units[type], }]).map(([type, value]) => [type, { ...value, @@ -48,7 +77,7 @@ const SensorReading = ({ } const isCurrent = values?.temperature?.isCurrent || values?.humidity?.isCurrent; - const thermometerState = values?.temperature?.thermometerState; + const thermometerState = values?.temperature?.thermometerState || 0; return ( @@ -64,7 +93,11 @@ const SensorReading = ({ {values?.temperature && values?.humidity && ' '} {values?.humidity && } -
{label}
+
+ {values?.battery && } + {' '}{label} +