Skip to content

Commit

Permalink
fix: bool value
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyc committed Dec 8, 2024
1 parent 39196fe commit f32e3e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions pisugar-server/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum Cmds {

SetButtonEnable {
mode: ButtonMode,
enable: bool,
enable: BoolValue,
},

SetButtonShell {
Expand Down Expand Up @@ -161,10 +161,38 @@ pub struct BatteryRangeArgs {
pub max: f32,
}

#[derive(Debug, Args, PartialEq)]
#[derive(Debug, Args, PartialEq, Clone)]
pub struct BoolArg {
#[arg(action = ArgAction::Set)]
pub enable: bool,
pub enable: BoolValue,
}

impl BoolArg {
const TRUE: Self = Self {
enable: BoolValue(true),
};
const FALSE: Self = Self {
enable: BoolValue(false),
};

pub fn value(&self) -> bool {
*self == Self::TRUE
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct BoolValue(pub bool);

impl From<String> for BoolValue {
fn from(value: String) -> Self {
if let Ok(b) = bool::from_str(&value) {
return Self(b);
}
if let Ok(n) = u32::from_str(&value) {
return Self(n != 0);
}
return Self(false);
}
}

#[cfg(test)]
Expand All @@ -182,8 +210,9 @@ mod tests {
#[case("get button_enable single", Cmds::Get(GetCmds::ButtonEnable{ mode: ButtonMode::Single }))]
#[case("get button_shell long", Cmds::Get(GetCmds::ButtonShell { mode: ButtonMode::Long } ))]
#[case("set_battery_charging_range 30.0,80.0", Cmds::SetBatteryChargingRange{ range: vec![30.0, 80.0]})]
#[case("set_battery_output true", Cmds::SetBatteryOutput(BoolArg{ enable: true }))]
#[case("set_battery_output false", Cmds::SetBatteryOutput(BoolArg { enable: false }))]
#[case("set_battery_output true", Cmds::SetBatteryOutput(BoolArg::TRUE))]
#[case("set_battery_output false", Cmds::SetBatteryOutput(BoolArg::FALSE))]
#[case("set_button_enable single 1", Cmds::SetButtonEnable { mode: ButtonMode::Single, enable: BoolValue(true) })]
#[case("set_button_shell single echo hello", Cmds::SetButtonShell { mode: ButtonMode::Single, shell: vec!["echo".to_string(), "hello".to_string()] })]
#[case("set_soft_poweroff_shell shutdown -a", Cmds::SetSoftPoweroffShell { shell: vec!["shutdown".to_string(), "-a".to_string()] })]
#[case("set_soft_poweroff_shell bash \"shutdown -a\"", Cmds::SetSoftPoweroffShell { shell: vec!["bash".to_string(), "shutdown -a".to_string()] })]
Expand Down
38 changes: 19 additions & 19 deletions pisugar-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::str::FromStr;
use anyhow::{anyhow, bail, Result};
use chrono::prelude::*;
use clap::{Arg, ArgAction, Command};
use cmds::{BoolArg, ButtonMode, Cmds};
use cmds::{ButtonMode, Cmds};
use digest_auth::{AuthContext, AuthorizationHeader, Charset, Qop, WwwAuthenticateHeader};
use env_logger::Env;
use futures::prelude::*;
Expand Down Expand Up @@ -168,18 +168,18 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
core.set_charging_range(charging_range)
.map(|_| format!("{}: done\n", parts[0]))
}
Cmds::SetBatteryInputProtect(BoolArg { enable }) => core
.toggle_input_protected(*enable)
Cmds::SetBatteryInputProtect(b) => core
.toggle_input_protected(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::SetBatteryOutput(BoolArg { enable }) => core
.toggle_output_enabled(*enable)
Cmds::SetBatteryOutput(b) => core
.toggle_output_enabled(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::SetFullChargeDuration { seconds } => {
core.config_mut().full_charge_duration = Some(*seconds);
core.save_config().map(|_| format!("{}: done\n", parts[0]))
}
Cmds::SetAllowCharging(BoolArg { enable }) => core
.toggle_allow_charging(*enable)
Cmds::SetAllowCharging(b) => core
.toggle_allow_charging(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::RtcClearFlag => core.clear_alarm_flag().map(|_| format!("{}: done\n", parts[0])),
Cmds::RtcPi2rtc => core.write_time(Local::now()).map(|_| format!("{}: done\n", parts[0])),
Expand Down Expand Up @@ -256,9 +256,9 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
.map(|_| format!("{}: wakeup after 1 min 30 sec\n", parts[0])),
Cmds::SetButtonEnable { mode, enable } => {
match *mode {
ButtonMode::Single => core.config_mut().single_tap_enable = *enable,
ButtonMode::Double => core.config_mut().double_tap_enable = *enable,
ButtonMode::Long => core.config_mut().long_tap_enable = *enable,
ButtonMode::Single => core.config_mut().single_tap_enable = enable.0,
ButtonMode::Double => core.config_mut().double_tap_enable = enable.0,
ButtonMode::Long => core.config_mut().long_tap_enable = enable.0,
}
if let Err(e) = core.save_config() {
log::error!("{}", e);
Expand All @@ -277,8 +277,8 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
}
Ok(format!("{}: done\n", parts[0]))
}
Cmds::SetAutoPowerOn(BoolArg { enable }) => core
.toggle_auto_power_on(*enable)
Cmds::SetAutoPowerOn(b) => core
.toggle_auto_power_on(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::SetAuth { username, password } => {
if let (Some(username), Some(password)) = (username, password) {
Expand All @@ -291,11 +291,11 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
core.save_config().map(|_| format!("{}: done\n", parts[0]))
}
Cmds::ForceShutdown => core.force_shutdown().map(|_| format!("{}: done\n", parts[0])),
Cmds::SetAntiMistouch(BoolArg { enable }) => core
.toggle_anti_mistouch(*enable)
Cmds::SetAntiMistouch(b) => core
.toggle_anti_mistouch(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::SetSoftPoweroff(BoolArg { enable }) => core
.toggle_soft_poweroff(*enable)
Cmds::SetSoftPoweroff(b) => core
.toggle_soft_poweroff(b.value())
.map(|_| format!("{}: done\n", parts[0])),
Cmds::SetSoftPoweroffShell { shell } => {
let script = shell.join(" ");
Expand All @@ -306,8 +306,8 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
};
core.save_config().map(|_| format!("{}: done\n", parts[0]))
}
Cmds::SetInputProtect(BoolArg { enable }) => core
.toggle_input_protected(*enable)
Cmds::SetInputProtect(b) => core
.toggle_input_protected(b.value())
.map(|_| format!("{}: done\n", parts[0])),
};

Expand All @@ -319,7 +319,7 @@ fn handle_request(core: Arc<Mutex<PiSugarCore>>, req: &str) -> String {
r
}
Err(e) => {
log::warn!("{}", e);
log::warn!("Request: {}, error: {}", req, e);
err
}
}
Expand Down

0 comments on commit f32e3e1

Please sign in to comment.