Skip to content

Commit

Permalink
Add battery state and icon
Browse files Browse the repository at this point in the history
  • Loading branch information
user890104 committed Oct 23, 2024
1 parent da033ad commit 6104f7e
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/widgets/SensorReading/SensorReading.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 (<Col>
<Card bg="primary" text={isCurrent ? 'white' : 'secondary'}>
Expand All @@ -64,7 +93,11 @@ const SensorReading = ({
{values?.temperature && values?.humidity && ' '}
{values?.humidity && <SensorReadingValue {...values.humidity} />}
</div>
<div>{label}</div>
<div>
{values?.battery && <i className={'fa-solid fa-battery-' + values.battery.batteryState}
title={values.battery.formattedTimestamp} />}
{' '}{label}
</div>
</Col>
</Row>
</Container>
Expand Down

0 comments on commit 6104f7e

Please sign in to comment.