This commit is contained in:
Pierre HUBERT 2024-06-30 22:54:23 +02:00
parent f468f192d8
commit 1f14cf8212
9 changed files with 3941 additions and 0 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# SolarEnergy
WIP project

View File

@ -6,20 +6,24 @@ use std::path::{Path, PathBuf};
pub enum ConsumptionBackend {
/// Constant consumption value
Constant {
/// The constant value to use
#[clap(short, long, default_value_t = 500)]
value: i32,
},
/// Generate random consumption value
Random {
/// Minimum acceptable generated value
#[clap(long, default_value_t = -5000)]
min: i32,
/// Maximum acceptable generated value
#[clap(long, default_value_t = 20000)]
max: i32,
},
/// Read consumption value in a file, on the filesystem
File {
/// The path to the file that will be read to process consumption values
#[clap(short, long, default_value = "/dev/shm/consumption.txt")]
path: String,
},

2
custom_consumption/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
target

3815
custom_consumption/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
[package]
name = "custom_consumption"
version = "0.1.0"
edition = "2021"
[dependencies]
env_logger = "0.11.3"
clap = { version = "4.5.8", features = ["derive", "env"] }
egui = "0.27.2"
eframe = "0.27.2"
lazy_static = "1.5.0"

View File

@ -0,0 +1,6 @@
# Custom consumption
## Install dependencies
```bash
sudo apt-get install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
```

View File

@ -0,0 +1,48 @@
use clap::Parser;
use std::path::{Path, PathBuf};
/// Solar system central backend
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct AppConfig {
/// The path to the file to write to update the value
#[clap(short, long, default_value = "/dev/shm/consumption.txt")]
file_path: String,
/// Minimal value
#[clap( long, default_value_t = -5000)]
pub min: i64,
/// Maximal value
#[clap(long, default_value_t = 5000)]
pub max: i64,
}
lazy_static::lazy_static! {
static ref ARGS: AppConfig = {
AppConfig::parse()
};
}
impl AppConfig {
/// Get parsed command line arguments
pub fn get() -> &'static AppConfig {
&ARGS
}
/// Path to the file to update the value
pub fn file_path(&self) -> PathBuf {
Path::new(&self.file_path).to_path_buf()
}
}
#[cfg(test)]
mod test {
use crate::app_config::AppConfig;
#[test]
fn verify_cli() {
use clap::CommandFactory;
AppConfig::command().debug_assert()
}
}

View File

@ -0,0 +1 @@
pub mod app_config;

View File

@ -0,0 +1,52 @@
use custom_consumption::app_config::AppConfig;
fn main() {
env_logger::init();
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([220.0, 60.0]),
..Default::default()
};
eframe::run_native(
"Custom consumption",
options,
Box::new(|_cc| Box::<MyApp>::default()),
)
.unwrap()
}
struct MyApp {
consumption: i64,
}
impl Default for MyApp {
fn default() -> Self {
let mut prev_consumption = 42;
if AppConfig::get().file_path().is_file() {
prev_consumption = std::fs::read_to_string(AppConfig::get().file_path())
.unwrap_or("43".to_string())
.parse()
.unwrap_or(44)
}
Self {
consumption: prev_consumption,
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Custom consumption");
ui.add(
egui::Slider::new(
&mut self.consumption,
AppConfig::get().min..=AppConfig::get().max,
)
.text("W"),
);
});
}
}