diff --git a/backend/Cargo.lock b/backend/Cargo.lock index aceef7c..ade4e43 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -567,7 +567,7 @@ dependencies = [ [[package]] name = "powertools-rs" -version = "1.0.0-beta4" +version = "1.0.0-rc1" dependencies = [ "log", "serde", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index ddacce4..08184c2 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "powertools-rs" -version = "1.0.0-beta4" +version = "1.0.0-rc1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/backend/src/api/battery.rs b/backend/src/api/battery.rs index 6f717af..6140902 100644 --- a/backend/src/api/battery.rs +++ b/backend/src/api/battery.rs @@ -6,7 +6,22 @@ use crate::utility::{unwrap_lock, unwrap_maybe_fatal}; /// Current current (ha!) web method pub fn current_now(_: super::ApiParameterType) -> super::ApiParameterType { - super::utility::map_result(crate::settings::Battery::current_now()) + super::utility::map_result(crate::settings::Battery::read_current_now()) +} + +/// Charge now web method +pub fn charge_now(_: super::ApiParameterType) -> super::ApiParameterType { + super::utility::map_result(crate::settings::Battery::read_charge_now()) +} + +/// Charge full web method +pub fn charge_full(_: super::ApiParameterType) -> super::ApiParameterType { + super::utility::map_result(crate::settings::Battery::read_charge_full()) +} + +/// Charge design web method +pub fn charge_design(_: super::ApiParameterType) -> super::ApiParameterType { + super::utility::map_result(crate::settings::Battery::read_charge_design()) } /// Generate set battery charge rate web method diff --git a/backend/src/main.rs b/backend/src/main.rs index ffb887a..472adb0 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -17,11 +17,14 @@ use usdpl_back::core::serdes::Primitive; use usdpl_back::Instance; fn main() -> Result<(), ()> { + #[cfg(debug_assertions)] let log_filepath = format!("/home/deck/{}.log", PACKAGE_NAME); + #[cfg(not(debug_assertions))] + let log_filepath = format!("/tmp/{}.log", PACKAGE_NAME); #[cfg(debug_assertions)] { if std::path::Path::new(&log_filepath).exists() { - std::fs::copy(&log_filepath, "/home/deck/powertools.log.old").unwrap(); + std::fs::copy(&log_filepath, format!("/home/deck/{}.log.old", PACKAGE_NAME)).unwrap(); } } WriteLogger::init( @@ -59,6 +62,9 @@ fn main() -> Result<(), ()> { }) // battery API functions .register("BATTERY_current_now", api::battery::current_now) + .register("BATTERY_charge_now", api::battery::charge_now) + .register("BATTERY_charge_full", api::battery::charge_full) + .register("BATTERY_charge_design", api::battery::charge_design) .register( "BATTERY_set_charge_rate", api::battery::set_charge_rate(loaded_settings.battery.clone(), save_sender.clone()), diff --git a/backend/src/settings/battery.rs b/backend/src/settings/battery.rs index bc32b21..ae0dd63 100644 --- a/backend/src/settings/battery.rs +++ b/backend/src/settings/battery.rs @@ -9,8 +9,13 @@ pub struct Battery { state: crate::state::Battery, } +const BATTERY_VOLTAGE: f64 = 7.7; + const BATTERY_CHARGE_RATE_PATH: &str = "/sys/class/hwmon/hwmon5/maximum_battery_charge_rate"; // write-only const BATTERY_CURRENT_NOW_PATH: &str = "/sys/class/power_supply/BAT1/current_now"; // read-only +const BATTERY_CHARGE_NOW_PATH: &str = "/sys/class/hwmon/hwmon2/device/charge_now"; // read-only +const BATTERY_CHARGE_FULL_PATH: &str = "/sys/class/hwmon/hwmon2/device/charge_full"; // read-only +const BATTERY_CHARGE_DESIGN_PATH: &str = "/sys/class/hwmon/hwmon2/device/charge_full_design"; // read-only impl Battery { #[inline] @@ -57,7 +62,7 @@ impl Battery { } } - pub fn current_now() -> Result { + pub fn read_current_now() -> Result { match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CURRENT_NOW_PATH) { Err((Some(e), None)) => Err(SettingError { msg: format!("Failed to read from `{}`: {}", BATTERY_CURRENT_NOW_PATH, e), @@ -77,6 +82,63 @@ impl Battery { } } + pub fn read_charge_now() -> Result { + match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_NOW_PATH) { + Err((Some(e), None)) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_NOW_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err((None, Some(e))) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_NOW_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err(_) => panic!( + "Invalid error while reading from `{}`", + BATTERY_CHARGE_NOW_PATH + ), + // convert to Wh + Ok(val) => Ok((val as f64) / 1000000.0 * BATTERY_VOLTAGE), + } + } + + pub fn read_charge_full() -> Result { + match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_FULL_PATH) { + Err((Some(e), None)) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_FULL_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err((None, Some(e))) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_FULL_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err(_) => panic!( + "Invalid error while reading from `{}`", + BATTERY_CHARGE_NOW_PATH + ), + // convert to Wh + Ok(val) => Ok((val as f64) / 1000000.0 * BATTERY_VOLTAGE), + } + } + + pub fn read_charge_design() -> Result { + match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_DESIGN_PATH) { + Err((Some(e), None)) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_DESIGN_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err((None, Some(e))) => Err(SettingError { + msg: format!("Failed to read from `{}`: {}", BATTERY_CHARGE_DESIGN_PATH, e), + setting: super::SettingVariant::Battery, + }), + Err(_) => panic!( + "Invalid error while reading from `{}`", + BATTERY_CHARGE_NOW_PATH + ), + // convert to Wh + Ok(val) => Ok((val as f64) / 1000000.0 * BATTERY_VOLTAGE), + } + } + pub fn system_default() -> Self { Self { charge_rate: None, diff --git a/main.py b/main.py index 6c1ab39..3e7acb9 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,6 @@ class Plugin: # Asyncio-compatible long-running code, executed in a task when the plugin is loaded async def _main(self): # startup - #self.backend_proc = subprocess.Popen([PARENT_DIR + "/bin/backend"]) + self.backend_proc = subprocess.Popen([PARENT_DIR + "/bin/backend"]) while True: await asyncio.sleep(1) diff --git a/package.json b/package.json index 91b250c..00249d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "PowerTools", - "version": "1.0.0-alpha", + "version": "1.0.0-rc1", "description": "Power tweaks for power users", "scripts": { "build": "shx rm -rf dist && rollup -c", diff --git a/plugin.json b/plugin.json index d770877..99dadc6 100644 --- a/plugin.json +++ b/plugin.json @@ -1,5 +1,5 @@ { - "name": "PowerTools", + "name": "PowerTools", "author": "NGnius", "flags": ["root", "debug"], "publish": { diff --git a/src/backend.ts b/src/backend.ts index 4da3178..60d72fb 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -36,6 +36,18 @@ export async function getBatteryCurrent(): Promise { return (await call_backend("BATTERY_current_now", []))[0]; } +export async function getBatteryChargeNow(): Promise { + return (await call_backend("BATTERY_charge_now", []))[0]; +} + +export async function getBatteryChargeFull(): Promise { + return (await call_backend("BATTERY_charge_full", []))[0]; +} + +export async function getBatteryChargeDesign(): Promise { + return (await call_backend("BATTERY_charge_design", []))[0]; +} + export async function getBatteryChargeRate(): Promise { return (await call_backend("BATTERY_get_charge_rate", []))[0]; } diff --git a/src/index.tsx b/src/index.tsx index 50205c6..4aa4ad6 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -37,6 +37,9 @@ const BACKEND_INFO = "VINFO"; const CURRENT_BATT = "BATTERY_current_now"; const CHARGE_RATE_BATT = "BATTERY_charge_rate"; +const CHARGE_NOW_BATT = "BATTERY_charge_now"; +const CHARGE_FULL_BATT = "BATTERY_charge_full"; +const CHARGE_DESIGN_BATT = "BATTERY_charge_design" const TOTAL_CPUS = "CPUs_total"; const ONLINE_CPUS = "CPUs_online"; @@ -58,6 +61,9 @@ const reload = function() { backend.resolve(backend.getBatteryCurrent(), (rate: number) => { set_value(CURRENT_BATT, rate) }); backend.resolve(backend.getBatteryChargeRate(), (rate: number) => { set_value(CHARGE_RATE_BATT, rate) }); + backend.resolve(backend.getBatteryChargeNow(), (rate: number) => { set_value(CHARGE_NOW_BATT, rate) }); + backend.resolve(backend.getBatteryChargeFull(), (rate: number) => { set_value(CHARGE_FULL_BATT, rate) }); + backend.resolve(backend.getBatteryChargeDesign(), (rate: number) => { set_value(CHARGE_DESIGN_BATT, rate) }); backend.resolve(backend.getCpuCount(), (count: number) => { set_value(TOTAL_CPUS, count)}); backend.resolve(backend.getCpusOnline(), (statii: boolean[]) => { @@ -130,6 +136,8 @@ const reload = function() { const periodicals = function() { backend.resolve(backend.getBatteryCurrent(), (rate: number) => { set_value(CURRENT_BATT, rate) }); + backend.resolve(backend.getBatteryChargeNow(), (rate: number) => { set_value(CHARGE_NOW_BATT, rate) }); + backend.resolve(backend.getBatteryChargeFull(), (rate: number) => { set_value(CHARGE_FULL_BATT, rate) }); backend.resolve(backend.getGeneralPersistent(), (value: boolean) => { set_value(PERSISTENT_GEN, value) }); backend.resolve(backend.getGeneralSettingsName(), (name: string) => { @@ -457,30 +465,30 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => {
Battery
- { false && +
Now (Charge)
- {/* TODO: (7.7 * chargeNowGlobal / 1000000).toFixed(1).toString() + " Wh (" + (100 * chargeNowGlobal / chargeFullGlobal).toFixed(1).toString() + "%)"*/} + {get_value(CHARGE_NOW_BATT).toFixed(1)} Wh ({(100 * get_value(CHARGE_NOW_BATT) / get_value(CHARGE_FULL_BATT)).toFixed(1)}%)
-
} - { false && + +
Max (Design)
- {/* TODO: (7.7 * chargeFullGlobal / 1000000).toFixed(1).toString() + " Wh (" + (100 * chargeFullGlobal / chargeDesignGlobal).toFixed(1).toString() + "%)"*/} + {get_value(CHARGE_FULL_BATT).toFixed(1)} Wh ({(100 * get_value(CHARGE_FULL_BATT) / get_value(CHARGE_DESIGN_BATT)).toFixed(1)}%)
-
} +
= ({}) => {
- Now Playing + Profile
{get_value(NAME_GEN)}