Files
pilot/pilot-v2/src/platform/linux/mod.rs
T
Gilles Soulier c5381b7112 Pilot v2: Core implementation + battery telemetry
Major updates:
- Complete Rust rewrite (pilot-v2/) with working MQTT client
- Fixed MQTT event loop deadlock (background task pattern)
- Battery telemetry for Linux (auto-detected via /sys/class/power_supply)
- Home Assistant auto-discovery for all sensors and switches
- Comprehensive documentation (AVANCEMENT.md, CLAUDE.md, roadmap)
- Docker test environment with Mosquitto broker
- Helper scripts for development and testing

Features working:
 MQTT connectivity with LWT
 YAML configuration with validation
 Telemetry: CPU, memory, IP, battery (Linux)
 Commands: shutdown, reboot, sleep, screen (dry-run tested)
 HA discovery and integration
 Allowlist and cooldown protection

Ready for testing on real hardware.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-30 06:23:00 +01:00

76 lines
2.6 KiB
Rust

// Implementations Linux (logind, sudoers, gnome busctl, x11 xset).
use anyhow::{bail, Context, Result};
use std::process::Command;
use crate::commands::{CommandAction, CommandValue};
// Execute une commande d'alimentation selon le backend choisi.
pub fn execute_power(backend: &str, action: CommandAction) -> Result<()> {
match backend {
"linux_logind_polkit" => match action {
CommandAction::Shutdown => run("systemctl", &["poweroff"]),
CommandAction::Reboot => run("systemctl", &["reboot"]),
CommandAction::Sleep => run("systemctl", &["suspend"]),
CommandAction::Screen => bail!("screen action not supported in power backend"),
},
"linux_sudoers" => match action {
CommandAction::Shutdown => run("shutdown", &["-h", "now"]),
CommandAction::Reboot => run("reboot", &[]),
CommandAction::Sleep => run("systemctl", &["suspend"]),
CommandAction::Screen => bail!("screen action not supported in power backend"),
},
_ => bail!("unknown linux power backend"),
}
}
// Execute une commande d'ecran selon le backend choisi.
pub fn execute_screen(backend: &str, value: CommandValue) -> Result<()> {
match backend {
"gnome_busctl" => match value {
CommandValue::Off => run(
"busctl",
&[
"--user",
"set-property",
"org.gnome.Mutter.DisplayConfig",
"/org/gnome/Mutter/DisplayConfig",
"org.gnome.Mutter.DisplayConfig",
"PowerSaveMode",
"i",
"1",
],
),
CommandValue::On => run(
"busctl",
&[
"--user",
"set-property",
"org.gnome.Mutter.DisplayConfig",
"/org/gnome/Mutter/DisplayConfig",
"org.gnome.Mutter.DisplayConfig",
"PowerSaveMode",
"i",
"0",
],
),
},
"x11_xset" => match value {
CommandValue::Off => run("xset", &["dpms", "force", "off"]),
CommandValue::On => run("xset", &["dpms", "force", "on"]),
},
_ => bail!("unknown linux screen backend"),
}
}
fn run(cmd: &str, args: &[&str]) -> Result<()> {
let status = Command::new(cmd)
.args(args)
.status()
.with_context(|| format!("failed to run {cmd}"))?;
if status.success() {
Ok(())
} else {
bail!("command failed: {cmd}")
}
}