initial commit

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ib131388c1056b6708b730a35011811026a6a6964
This commit is contained in:
raf 2026-02-18 20:13:00 +03:00
commit 033e253259
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
33 changed files with 3126 additions and 0 deletions

View file

@ -0,0 +1,94 @@
#![allow(improper_ctypes)]
use pscand_core::helpers::PowerHelper;
use pscand_core::scanner::{MetricValue, Scanner};
use std::collections::HashMap;
use std::time::Duration;
struct PowerScanner;
#[unsafe(no_mangle)]
pub extern "C" fn pscand_scanner() -> Box<dyn Scanner> {
Box::new(PowerScanner)
}
impl Default for PowerScanner {
fn default() -> Self {
Self
}
}
impl Scanner for PowerScanner {
fn name(&self) -> &'static str {
"power"
}
fn interval(&self) -> Duration {
Duration::from_secs(2)
}
fn init(&mut self, _config: &toml::Value) -> pscand_core::Result<()> {
Ok(())
}
fn collect(&self) -> pscand_core::Result<HashMap<String, MetricValue>> {
let mut metrics = HashMap::new();
if let Ok(Some(battery)) = PowerHelper::battery_info() {
metrics.insert(
"battery_present".to_string(),
MetricValue::from_bool(battery.present),
);
metrics.insert(
"battery_charge_percent".to_string(),
MetricValue::Integer(battery.charge_percent as i64),
);
metrics.insert(
"battery_voltage_v".to_string(),
MetricValue::from_f64(battery.voltage),
);
metrics.insert(
"battery_power_now_mw".to_string(),
MetricValue::Integer(battery.power_now),
);
metrics.insert(
"battery_status".to_string(),
MetricValue::from_str(&battery.status),
);
}
if let Ok(supplies) = PowerHelper::power_supplies() {
for (name, info) in supplies {
if let Some(status) = info.get("status") {
metrics.insert(
format!("supply_{}_status", name),
MetricValue::from_str(status),
);
}
if let Some(online) = info.get("online") {
metrics.insert(
format!("supply_{}_online", name),
MetricValue::from_bool(online == "1"),
);
}
if let Some(capacity) = info.get("capacity") {
if let Ok(cap) = capacity.parse::<u32>() {
metrics.insert(
format!("supply_{}_capacity", name),
MetricValue::Integer(cap as i64),
);
}
}
}
}
if let Ok(state) = PowerHelper::suspend_state() {
metrics.insert("suspend_state".to_string(), MetricValue::from_str(&state));
}
Ok(metrics)
}
fn cleanup(&mut self) -> pscand_core::Result<()> {
Ok(())
}
}