feat: system_info daemon call

This commit is contained in:
Ilya Zlobintsev
2023-01-06 18:50:03 +02:00
parent 96b046fc29
commit b37240a6e1
5 changed files with 31 additions and 13 deletions

View File

@@ -157,7 +157,7 @@ impl<'a> Handler {
) -> anyhow::Result<()> {
let settings = if enabled {
let settings = {
let curve = curve.map_or_else(FanCurve::default, |curve| FanCurve(curve));
let curve = curve.map_or_else(FanCurve::default, FanCurve);
curve.validate()?;
let mut config_guard = self.config.write().map_err(|err| anyhow!("{err}"))?;

View File

@@ -5,8 +5,10 @@ mod vulkan;
use self::handler::Handler;
use crate::{config::Config, socket};
use lact_schema::{Pong, Request, Response};
use anyhow::Context;
use lact_schema::{Pong, Request, Response, SystemInfo};
use serde::Serialize;
use std::process::Command;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
net::{UnixListener, UnixStream},
@@ -77,6 +79,7 @@ pub async fn handle_stream(stream: UnixStream, handler: Handler) -> anyhow::Resu
async fn handle_request<'a>(request: Request<'a>, handler: &'a Handler) -> anyhow::Result<Vec<u8>> {
match request {
Request::Ping => ok_response(ping()),
Request::SystemInfo => ok_response(system_info()?),
Request::ListDevices => ok_response(handler.list_devices()),
Request::DeviceInfo { id } => ok_response(handler.get_device_info(id)?),
Request::DeviceStats { id } => ok_response(handler.get_gpu_stats(id)?),
@@ -91,13 +94,29 @@ fn ok_response<T: Serialize>(data: T) -> anyhow::Result<Vec<u8>> {
Ok(serde_json::to_vec(&Response::Ok(data))?)
}
fn ping() -> Pong<'static> {
fn ping() -> Pong {
Pong
}
fn system_info() -> anyhow::Result<SystemInfo<'static>> {
let version = env!("CARGO_PKG_VERSION");
let profile = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
let kernel_output = Command::new("uname")
.arg("-r")
.output()
.context("Could not read kernel version")?;
let kernel_version = String::from_utf8(kernel_output.stdout)
.context("Invalid kernel version output")?
.trim()
.to_owned();
Pong { version, profile }
Ok(SystemInfo {
version,
profile,
kernel_version,
})
}

View File

@@ -26,9 +26,13 @@ use std::{
pub type FanCurveMap = BTreeMap<i32, f32>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Pong<'a> {
pub struct Pong;
#[derive(Serialize, Deserialize, Debug)]
pub struct SystemInfo<'a> {
pub version: &'a str,
pub profile: &'a str,
pub kernel_version: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]

View File

@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
pub enum Request<'a> {
Ping,
ListDevices,
SystemInfo,
DeviceInfo {
id: &'a str,
},

View File

@@ -15,15 +15,9 @@ fn ping_requset() {
fn pong_response() {
let expected_response = json!({
"status": "ok",
"data": {
"version": "0.0.1",
"profile": "debug"
}
});
let response = Response::Ok(Pong {
version: "0.0.1",
profile: "debug",
"data": null
});
let response = Response::Ok(Pong);
assert_eq!(serde_json::to_value(&response).unwrap(), expected_response);
}