Use percentage battery in frontend and not levels

This commit is contained in:
Simon Fondrie-Teitler
2025-08-27 13:02:26 -04:00
committed by Cooper Quintin
parent 663d0abb57
commit 16447ed8bf
4 changed files with 22 additions and 79 deletions

View File

@@ -9,18 +9,9 @@ pub mod orbic;
pub mod tmobile;
pub mod wingtech;
#[derive(Clone, Copy, PartialEq, Debug, Serialize)]
pub enum BatteryLevel {
VeryLow,
Low,
Medium,
High,
Full,
}
#[derive(Clone, Copy, PartialEq, Debug, Serialize)]
pub struct BatteryState {
level: BatteryLevel,
level: u8,
is_plugged_in: bool,
}
@@ -37,20 +28,13 @@ async fn is_plugged_in_from_file(path: &Path) -> Result<bool, RayhunterError> {
}
}
async fn get_level_from_percentage_file(path: &Path) -> Result<BatteryLevel, RayhunterError> {
match tokio::fs::read_to_string(path)
async fn get_level_from_percentage_file(path: &Path) -> Result<u8, RayhunterError> {
tokio::fs::read_to_string(path)
.await
.map_err(RayhunterError::TokioError)?
.trim_end()
.parse()
{
Ok(0..=10) => Ok(BatteryLevel::VeryLow),
Ok(11..=25) => Ok(BatteryLevel::Low),
Ok(26..=50) => Ok(BatteryLevel::Medium),
Ok(51..=75) => Ok(BatteryLevel::High),
Ok(76..=100) => Ok(BatteryLevel::Full),
_ => Err(RayhunterError::BatteryLevelParseError),
}
.or(Err(RayhunterError::BatteryLevelParseError))
}
pub async fn get_battery_status(device: &Device) -> Result<BatteryState, RayhunterError> {

View File

@@ -1,7 +1,7 @@
use std::path::Path;
use crate::{
battery::{BatteryLevel, BatteryState, is_plugged_in_from_file},
battery::{BatteryState, is_plugged_in_from_file},
error::RayhunterError,
};
@@ -16,11 +16,11 @@ pub async fn get_battery_state() -> Result<BatteryState, RayhunterError> {
.chars()
.next()
{
Some('1') => Ok(BatteryLevel::VeryLow),
Some('2') => Ok(BatteryLevel::Low),
Some('3') => Ok(BatteryLevel::Medium),
Some('4') => Ok(BatteryLevel::High),
Some('5') => Ok(BatteryLevel::Full),
Some('1') => Ok(10),
Some('2') => Ok(25),
Some('3') => Ok(50),
Some('4') => Ok(75),
Some('5') => Ok(100),
_ => Err(RayhunterError::BatteryLevelParseError),
}?,
is_plugged_in: is_plugged_in_from_file(Path::new(PLUGGED_IN_STATE_FILE)).await?,

View File

@@ -1,5 +1,5 @@
<script lang="ts">
import { BatteryLevel, type SystemStats } from '$lib/systemStats';
import { type SystemStats } from '$lib/systemStats';
let {
stats,
}: {
@@ -8,61 +8,28 @@
const table_cell_classes = 'border p-1 lg:p-2';
let battery_level = $derived.by(() => {
if (stats.battery_status === undefined) {
return 0;
}
switch (stats.battery_status.level) {
case BatteryLevel.Full:
return 100;
case BatteryLevel.High:
return 75;
case BatteryLevel.Medium:
return 50;
case BatteryLevel.Low:
return 25;
default:
return 10;
}
});
let battery_level = $derived(stats.battery_status ? stats.battery_status.level : 0);
let bar_color = $derived.by(() => {
if (stats.battery_status === undefined) {
return '';
}
switch (stats.battery_status.level) {
case BatteryLevel.Low:
return 'fill-yellow-300';
case BatteryLevel.VeryLow:
return 'fill-red-500';
default:
return 'fill-green-500';
if (battery_level <= 10) {
return 'fill-red-500';
}
if (battery_level <= 25) {
return 'fill-yellow-300';
}
return 'fill-green-500';
});
let title_text = $derived.by(() => {
if (stats.battery_status === undefined) {
return 'Rayhunter does not yet support displaying the battery level for this device.';
}
let text = '';
switch (stats.battery_status.level) {
case BatteryLevel.Full:
text = 'Full';
break;
case BatteryLevel.High:
text = 'High';
break;
case BatteryLevel.Medium:
text = 'Medium';
break;
case BatteryLevel.Low:
text = 'Low';
break;
case BatteryLevel.VeryLow:
text = 'Very low';
break;
}
let text = `Battery is ${stats.battery_status.level}% full`;
if (stats.battery_status.is_plugged_in) {
text += ', plugged in';
text += ' and plugged in';
}
return text;
});

View File

@@ -27,14 +27,6 @@ export interface MemoryStats {
}
export interface BatteryStatus {
level: BatteryLevel;
level: number;
is_plugged_in: boolean;
}
export enum BatteryLevel {
VeryLow = 'VeryLow',
Low = 'Low',
Medium = 'Medium',
High = 'High',
Full = 'Full',
}