Skip to content

Commit

Permalink
Merge pull request #69 from wiiznokes/dev
Browse files Browse the repository at this point in the history
Opti for Windows
  • Loading branch information
wiiznokes committed Jan 4, 2024
2 parents 842171b + 5b91119 commit adf6a5e
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .config/settings.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
theme = "System"
theme = "Light"
update_delay = 1500
current_config = "test"
current_config = "fake"
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ derive_more = { version = "1.0.0-beta.6", default-features = false, features = [
[workspace.dependencies.cargo-packager-resource-resolver]
git = "https://github.com/wiiznokes/cargo-packager.git"
branch = "resource-resolver"
features = ["auto-detect-formats"]
#path = "../cargo-packager/crates/resource-resolver/"
#package = "resource-resolver"
2 changes: 2 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
]
```
- make a similar crate of https://github.com/mxre/winres, but with no dependencies. This will add an icon to .exe on Windows https://github.com/crabnebula-dev/cargo-packager/issues/107
- find out how to do blocking operation on the command pool of Iced, without blocking the thread
- support const value in trait that use enum_dispatch



Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ sudo sensors-detect
Also, make sure to execute the program in sudo mode.

## Config files
- Windows: `C:\Users\wiiz\AppData\Roaming\wiiznokes\fan-control\config`
- Linux: `/home/wiiz/.config/fan-control` or `/root/.config/fan-control`
- Windows: [`C:\Users\wiiz\AppData\Roaming\wiiznokes\fan-control\config`](file:///C:\Users\wiiz\AppData\Roaming\wiiznokes\fan-control\config)
- Linux: [`/home/wiiz/.config/fan-control`](file:///home/wiiz/.config/fan-control) or [`/root/.config/fan-control`](file:///root/.config/fan-control)

## Repo structure
- [hardware](./hardware/README.md): define an abstraction around the hardware.
Expand Down
4 changes: 2 additions & 2 deletions data/src/app_graph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;

use hardware::Hardware;

Expand All @@ -9,7 +9,7 @@ use crate::id::{Id, IdGenerator};
use crate::node::{self, Node, NodeType, NodeTypeLight, ToNode};
use crate::utils::RemoveElem;

pub type Nodes = HashMap<Id, Node>;
pub type Nodes = BTreeMap<Id, Node>;
pub type RootNodes = Vec<Id>;

#[derive(Debug)]
Expand Down
15 changes: 9 additions & 6 deletions data/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ impl Update {
Ok(())
}

pub fn all(
pub fn all_except_root_nodes(
&mut self,
nodes: &mut Nodes,
root_nodes: &RootNodes,
bridge: &mut HardwareBridge,
) -> Result<()> {
bridge.update()?;

let ids_to_update_sorted: Vec<Id>;
{
let mut key_values = nodes.iter().collect::<Vec<_>>();
Expand Down Expand Up @@ -124,9 +121,15 @@ impl Update {
}
}

bridge.update()?;
Ok(())
}

// update printed value of root nodes
pub fn root_nodes(
&mut self,
nodes: &mut Nodes,
root_nodes: &RootNodes,
bridge: &mut HardwareBridge,
) -> Result<()> {
for id in root_nodes {
match nodes.get_mut(id) {
Some(node) => {
Expand Down
1 change: 1 addition & 0 deletions hardware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ cargo-packager-resource-resolver.workspace = true
#num_enum = "0.7"

[dev-dependencies]
env_logger.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Start()

public void Stop()
{
Logger.Info("Shutdown Lhm");
Logger.Info("Shutdown lhm");

if (!_isStarted)
return;
Expand Down
11 changes: 8 additions & 3 deletions hardware/LibreHardwareMonitorWrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,23 @@
}



var updateCts = new CancellationTokenSource();

try
{
Logger.Info("start waiting for commands");
server.WaitAndHandleCommands(hardwareManager);
Logger.Info("Start waiting for commands");
server.WaitAndHandleCommands(hardwareManager, updateCts);
}
catch (Exception e)
{
Logger.Error("can't wait for commands" + e.Message);
Logger.Error("Can't wait for commands: " + e.Message);
updateCts.Cancel();
ShutDown();
return 1;
}

updateCts.Cancel();
ShutDown();
return 0;

Expand Down
13 changes: 11 additions & 2 deletions hardware/LibreHardwareMonitorWrapper/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public Server()
listener.Close();
}

public void WaitAndHandleCommands(HardwareManager hardwareManager)
public void WaitAndHandleCommands(HardwareManager hardwareManager, CancellationTokenSource updateCts)
{
var isUpdateRunning = false;
while (true)
{
var res = block_read();
Expand Down Expand Up @@ -71,7 +72,15 @@ public void WaitAndHandleCommands(HardwareManager hardwareManager)
case Command.Shutdown:
return;
case Command.Update:
hardwareManager.Update();
if (!isUpdateRunning)
{
Task.Run(() =>
{
isUpdateRunning = true;
hardwareManager.Update();
isUpdateRunning = false;
}, updateCts.Token);
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(command), command, "Unknown command");
Expand Down
11 changes: 10 additions & 1 deletion hardware/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use derive_more::Display;
use enum_dispatch::enum_dispatch;
use serde::Serialize;
use std::{fmt::Debug, rc::Rc};
use std::{fmt::Debug, rc::Rc, time::Duration};
use thiserror::Error;

#[macro_use]
Expand Down Expand Up @@ -131,3 +131,12 @@ pub trait HardwareBridgeT {
Ok(())
}
}

// todo: move this 2 line in HardwareBridgeT when enum_dispatch support const value

/// Approximative time to update sensors on my pc
#[cfg(all(not(feature = "fake_hardware"), target_os = "windows"))]
pub const TIME_TO_UPDATE: Duration = Duration::from_millis(200);

#[cfg(any(feature = "fake_hardware", target_os = "linux"))]
pub const TIME_TO_UPDATE: Duration = Duration::from_millis(0);
18 changes: 13 additions & 5 deletions hardware/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum WindowsError {
#[error("No connection was found")]
NoConnectionFound,
#[error(transparent)]
Resource(#[from] resource_resolver::error::Error),
Resource(#[from] resource_resolver::Error),
#[error("Failed to parse hardware struct: {0}")]
JSONConfigParseError(#[from] serde_json::Error),
}
Expand All @@ -47,7 +47,8 @@ fn spawn_windows_server() -> Result<std::process::Child> {
}
#[cfg(not(test))]
{
match resource_resolver::resource_dir_with_suffix(resource_suffix) {
let package_format = resource_resolver::current_format();
match resource_resolver::resource_dir_with_suffix(package_format, resource_suffix) {
Ok(resource_path) => resource_path,
Err(e) => {
error!("Can't find resource path: {e}. Fall back to current dir.");
Expand Down Expand Up @@ -377,14 +378,20 @@ mod test {
time::{Duration, Instant},
};

fn init_test_logging() {
let _ = env_logger::builder().format_timestamp(None).try_init();
}

#[test]
fn test_time() {
init_test_logging();

let now = Instant::now();

let mut bridge = WindowsBridge::new().unwrap();
let hardware = bridge.hardware().clone();

println!("generation took {} millis", now.elapsed().as_millis());
info!("generation took {} millis", now.elapsed().as_millis());

for _ in 0..5 {
bench(
Expand All @@ -396,10 +403,11 @@ mod test {
);
sleep(Duration::from_millis(500))
}
bridge.shutdown().unwrap();
}

fn update(bridge: &mut WindowsBridge, hardware: &Hardware) {
println!();
info!("");

bench(
|| {
Expand Down Expand Up @@ -437,7 +445,7 @@ mod test {
fn bench(f: impl FnOnce() -> String, info: &str) {
let now = Instant::now();
let output = f();
println!(
info!(
"{}: {} in {} millis",
info,
output,
Expand Down
2 changes: 1 addition & 1 deletion i18n/en/ui.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_target = Take 5 variables:
# Config
config_name = Configuration name
save_config = Save/Replace configuration
save_config = Save active configuration
delete_config = Delete configuration
create_config = Create configuration
Expand Down
2 changes: 1 addition & 1 deletion i18n/fr/ui.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_target = Prendre 5 variables :
# Config
config_name = Nom de la configuration
save_config = Enregistrer/Remplacer la configuration
save_config = Enregistrer la configuration active
delete_config = Supprimer la configuration
create_config = Créer une configuration
Expand Down
7 changes: 5 additions & 2 deletions ui/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use once_cell::sync::Lazy;
use cargo_packager_resource_resolver as resource_resolver;

lazy_static::lazy_static! {
static ref RESSOURCE_PATH: PathBuf = resource_resolver::resource_dir_with_suffix("resource")
static ref RESSOURCE_PATH: PathBuf = {
let package_format = resource_resolver::current_format();
resource_resolver::resource_dir_with_suffix(package_format, "resource")
.unwrap_or(PathBuf::from("resource"))
.join("icons/");
.join("icons/")
};
}

static EXTENSION: &str = "px.svg";
Expand Down
5 changes: 4 additions & 1 deletion ui/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn items_view<'a>(
) -> Element<'a, AppMsg> {
let mut controls = Vec::new();
let mut behaviors = Vec::new();
let mut custom_temps = Vec::new();
let mut temps = Vec::new();
let mut fans = Vec::new();

Expand All @@ -51,17 +52,19 @@ pub fn items_view<'a>(
match node.node_type.to_light() {
NodeTypeLight::Control => controls.push(content),
NodeTypeLight::Fan => fans.push(content),
NodeTypeLight::Temp | NodeTypeLight::CustomTemp => temps.push(content),
NodeTypeLight::Temp => temps.push(content),
NodeTypeLight::Graph => {}
NodeTypeLight::Flat | NodeTypeLight::Linear | NodeTypeLight::Target => {
behaviors.push(content)
}
NodeTypeLight::CustomTemp => custom_temps.push(content),
}
}

let list_views = vec![
list_view(controls),
list_view(behaviors),
list_view(custom_temps),
list_view(temps),
list_view(fans),
];
Expand Down
Loading

0 comments on commit adf6a5e

Please sign in to comment.