add system power api

This commit is contained in:
Samuel Lorch 2024-07-29 23:49:50 +02:00
parent fb03a17d36
commit 1e66729e59

27
src/api/system/power.rs Normal file
View file

@ -0,0 +1,27 @@
use crate::api::ApiError;
use crate::state::RpcState;
use jsonrpsee::types::Params;
use jsonrpsee::RpcModule;
use std::sync::Arc;
pub fn register_methods(module: &mut RpcModule<RpcState>) {
module
.register_async_method("system.power.shutdown", halt_system)
.unwrap();
module
.register_async_method("system.power.restart", reboot_system)
.unwrap();
}
pub async fn halt_system<'a>(_: Params<'a>, _state: Arc<RpcState>) -> Result<(), ApiError> {
let systemd_manager = zbus_systemd::systemd1::ManagerProxy::new(&_state.dbus_conn).await?;
systemd_manager.halt().await?;
Ok(())
}
pub async fn reboot_system<'a>(_: Params<'a>, _state: Arc<RpcState>) -> Result<(), ApiError> {
let systemd_manager = zbus_systemd::systemd1::ManagerProxy::new(&_state.dbus_conn).await?;
systemd_manager.reboot().await?;
Ok(())
}