feat(sdk): connect + Power/Settings клиенты (bus-тест — План 3)
connect (system/session по SHTURMAN_BUS); PowerClient/SettingsClient — типизированные обёртки proxy. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Alexander <akotenev2003@gmail.com>
This commit is contained in:
Generated
+935
-5
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,13 @@
|
||||
//! Бутстрап D-Bus-соединения. Прод/VM — **системная шина устройства** (ipc §1);
|
||||
//! dev/тесты — сессионная (env `SHTURMAN_BUS=session`, см. План 3).
|
||||
|
||||
use zbus::Connection;
|
||||
|
||||
/// Подключиться к шине Штурмана.
|
||||
/// По умолчанию — системная; `SHTURMAN_BUS=session` → сессионная (герметичные тесты/dev).
|
||||
pub async fn connect() -> zbus::Result<Connection> {
|
||||
match std::env::var("SHTURMAN_BUS").as_deref() {
|
||||
Ok("session") => Connection::session().await,
|
||||
_ => Connection::system().await,
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
//! Публичный SDK Штурмана (F01): клиентские обёртки над `ipc`-proxy + схема манифеста.
|
||||
//! First-party апы и сторонние плагины строятся на нём (architecture §1, principles #9, plugin-sdk).
|
||||
|
||||
pub mod connect;
|
||||
pub mod manifest;
|
||||
pub mod power;
|
||||
pub mod settings;
|
||||
|
||||
pub use connect::connect;
|
||||
pub use manifest::Manifest;
|
||||
pub use power::PowerClient;
|
||||
pub use settings::SettingsClient;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
//! Клиент `Power` — типизированные обёртки над `Power1Proxy` (enum вместо строк на проводе).
|
||||
|
||||
use core::str::FromStr;
|
||||
use shturman_ipc::proxy::Power1Proxy;
|
||||
use shturman_ipc::types::{IgnitionState, PowerSource, PowerState};
|
||||
use zbus::Connection;
|
||||
|
||||
/// Эргономичный клиент `ru.shturman.Power`.
|
||||
pub struct PowerClient {
|
||||
proxy: Power1Proxy<'static>,
|
||||
}
|
||||
|
||||
impl PowerClient {
|
||||
pub async fn new(conn: &Connection) -> zbus::Result<Self> {
|
||||
Ok(Self {
|
||||
proxy: Power1Proxy::new(conn).await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn power_state(&self) -> zbus::Result<PowerState> {
|
||||
let s = self.proxy.get_power_state().await?;
|
||||
PowerState::from_str(&s)
|
||||
.map_err(|_| zbus::Error::Failure(format!("unknown PowerState: {s}")))
|
||||
}
|
||||
|
||||
pub async fn ignition_state(&self) -> zbus::Result<IgnitionState> {
|
||||
let s = self.proxy.ignition_state().await?;
|
||||
IgnitionState::from_str(&s)
|
||||
.map_err(|_| zbus::Error::Failure(format!("unknown IgnitionState: {s}")))
|
||||
}
|
||||
|
||||
pub async fn power_source(&self) -> zbus::Result<PowerSource> {
|
||||
let s = self.proxy.power_source().await?;
|
||||
PowerSource::from_str(&s)
|
||||
.map_err(|_| zbus::Error::Failure(format!("unknown PowerSource: {s}")))
|
||||
}
|
||||
|
||||
pub async fn uptime(&self) -> zbus::Result<u64> {
|
||||
self.proxy.uptime().await
|
||||
}
|
||||
|
||||
/// Нижележащий proxy — для подписок на сигналы (`acc_changed`, `shutdown_imminent`, …).
|
||||
pub fn proxy(&self) -> &Power1Proxy<'static> {
|
||||
&self.proxy
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//! Клиент `Settings` — обёртки над `Settings1Proxy`.
|
||||
|
||||
use shturman_ipc::proxy::Settings1Proxy;
|
||||
use zbus::zvariant::{OwnedValue, Value};
|
||||
use zbus::Connection;
|
||||
|
||||
/// Эргономичный клиент `ru.shturman.Settings`.
|
||||
pub struct SettingsClient {
|
||||
proxy: Settings1Proxy<'static>,
|
||||
}
|
||||
|
||||
impl SettingsClient {
|
||||
pub async fn new(conn: &Connection) -> zbus::Result<Self> {
|
||||
Ok(Self {
|
||||
proxy: Settings1Proxy::new(conn).await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get(&self, key: &str) -> zbus::Result<OwnedValue> {
|
||||
self.proxy.get(key).await
|
||||
}
|
||||
|
||||
pub async fn set(&self, key: &str, value: &Value<'_>) -> zbus::Result<()> {
|
||||
self.proxy.set(key, value).await
|
||||
}
|
||||
|
||||
pub async fn list(&self, prefix: &str) -> zbus::Result<Vec<String>> {
|
||||
self.proxy.list(prefix).await
|
||||
}
|
||||
|
||||
pub async fn reset(&self, key: &str) -> zbus::Result<()> {
|
||||
self.proxy.reset(key).await
|
||||
}
|
||||
|
||||
/// Нижележащий proxy — для подписки на `changed`.
|
||||
pub fn proxy(&self) -> &Settings1Proxy<'static> {
|
||||
&self.proxy
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user