Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove static on windows + add tasks + cheks #9

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
LMSENSORS_STATIC = "1"
LMSENSORS_INCLUDE_DIR = { value = "target/libsensors_build/include", relative = true }
LMSENSORS_LIB_DIR = { value = "target/libsensors_build/lib", relative = true }
RUST_LOG = "fan_control=debug,data=info,hardware=info,ui=debug"
RUST_LOG = "fan_control=debug,data=info,hardware=debug,ui=debug"
#RUST_BACKTRACE = "1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# fan-control

# Steps
- [ ] finish hardware crate (windows, upgrade abstraction)
- [x] finish hardware crate (windows, upgrade abstraction)
- [ ] Upgrade Value struct (allow different type: °C, %, ...)
- [ ] package Msi, Deb, Rpm, Snap, Flatpak [cargo-bundle](https://github.com/burtonageo/cargo-bundle)
- [ ] CI for packaging
Expand Down
55 changes: 38 additions & 17 deletions hardware/LibreHardwareMonitorWrapper/HardwareManager.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using LibreHardwareMonitorWrapper.Hardware;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using LibreHardwareMonitorWrapper.Hardware;
using LibreHardwareMonitorWrapper.Lhm;

namespace LibreHardwareMonitorWrapper;

public static class HardwareManager
public class HardwareManager
{
private static readonly HardwareResearcher HardwareResearcher = new();
private readonly List<BaseHardware> _hardwareList;

private readonly HardwareResearcher _hardwareResearcher = new();

public static void Start()

public HardwareManager()
{
HardwareResearcher.Start();
HardwareResearcher.CreateHardware();
_hardwareResearcher.Start();
_hardwareList = _hardwareResearcher.GetHardwareList();
}

public static int GetValue(int index)
public int GetValue(int index)
{
HardwareResearcher.Update();
var hardware = State.Hardwares[index];
_hardwareResearcher.Update();
var hardware = _hardwareList[index];
return hardware.Type switch
{
HardwareType.Control => (hardware as Control)!.Value(),
Expand All @@ -27,26 +32,42 @@ public static int GetValue(int index)
};
}

public static void SetValue(int index, int value)
public void SetValue(int index, int value)
{
var control = State.Hardwares[index] as Control;
var control = _hardwareList[index] as Control;
control!.SetSpeed(value);
}

public static void SetAuto(int index)
public void SetAuto(int index)
{
var control = State.Hardwares[index] as Control;
var control = _hardwareList[index] as Control;
control!.SetAuto();
}

public static void Stop()
public void Stop()
{
HardwareResearcher.Stop();
_hardwareResearcher.Stop();
}


public static void Update()
public void Update()
{
_hardwareResearcher.Update();
}

public string ToJson()
{
HardwareResearcher.Update();
var serializerOptions = new JsonSerializerOptions
{
Converters =
{
new JsonStringEnumConverter()
}
};
var jsonText = JsonSerializer.Serialize(_hardwareList, serializerOptions);

var stringBuilder = new StringBuilder(jsonText);
stringBuilder.Append('\n');
return stringBuilder.ToString();
}
}
13 changes: 7 additions & 6 deletions hardware/LibreHardwareMonitorWrapper/Lhm/HardwareResearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ public void Stop()
}


public void CreateHardware()
public List<BaseHardware> GetHardwareList()
{
if (!_isStarted)
return;
throw new Exception();

var nbControl = 0;
var nbFan = 0;
var nbTemp = 0;
var nbTot = 0;

var hardwareList = new List<BaseHardware>();

var hardwareArray = _mComputer.Hardware;
foreach (var hardware in hardwareArray)
Expand All @@ -87,7 +88,7 @@ public void CreateHardware()
Console.WriteLine("nbControl: " + nbControl);
Console.WriteLine("nbFan: " + nbFan);
Console.WriteLine("nbTemp: " + nbTemp);
return;
return hardwareList;

void AddHardware(ISensor sensor)
{
Expand All @@ -101,17 +102,17 @@ void AddHardware(ISensor sensor)
{
case SensorType.Control:
id ??= SensorType.Control.ToString() + nbControl;
State.Hardwares.Add(new Control(id, name, name, sensor, nbTot));
hardwareList.Add(new Control(id, name, name, sensor, nbTot));
nbControl += 1;
break;
case SensorType.Fan:
id ??= SensorType.Fan.ToString() + nbFan;
State.Hardwares.Add(new Sensor(id, name, name, sensor, nbTot, HardwareType.Fan));
hardwareList.Add(new Sensor(id, name, name, sensor, nbTot, HardwareType.Fan));
nbFan += 1;
break;
case SensorType.Temperature:
id ??= SensorType.Temperature.ToString() + nbTemp;
State.Hardwares.Add(new Sensor(id, name, name, sensor, nbTot, HardwareType.Temp));
hardwareList.Add(new Sensor(id, name, name, sensor, nbTot, HardwareType.Temp));
nbTemp += 1;
break;

Expand Down
26 changes: 7 additions & 19 deletions hardware/LibreHardwareMonitorWrapper/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using LibreHardwareMonitorWrapper;
using LibreHardwareMonitorWrapper;

HardwareManager.Start();
var connectTask = Task.Run(() => new Server());

var hardwareManager = new HardwareManager();
var jsonText = hardwareManager.ToJson();

var serializerOptions = new JsonSerializerOptions
{
Converters =
{
new JsonStringEnumConverter()
}
};


var jsonText = JsonSerializer.Serialize(State.Hardwares, serializerOptions);

var server = new Server();
var server = await connectTask;

server.SendHardware(jsonText);

server.WaitForCommand();

server.WaitForCommand(hardwareManager);
server.Shutdown();
HardwareManager.Stop();
hardwareManager.Stop();
72 changes: 49 additions & 23 deletions hardware/LibreHardwareMonitorWrapper/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,44 @@

namespace LibreHardwareMonitorWrapper;

public enum Command
{
SetAuto = 1,
SetValue = 2,
GetValue = 3,
Shutdown = 4
}

public class Server
{
private const string Address = "127.0.0.1";
private readonly byte[] _buffer = new byte[4];
private const int DefaultPort = 55555;
private const string Check = "fan-control-check";
private const string CheckResponse = "fan-control-ok";
private readonly Socket _client;
private readonly Socket _listener;
private int _port = 55555;
private readonly int _port;
private readonly byte[] _buffer = new byte[4];

public Server()
{
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
BindPort();
_listener.Listen(1);
Console.WriteLine("Server Started");
_client = _listener.Accept();
Console.WriteLine("Client Connected");
_port = StartServer();
_client = AcceptClient();
}

public void SendHardware(string jsonText)
{
var stringBuilder = new StringBuilder(jsonText);
stringBuilder.Append('\n');

var jsonTextWithLineDelimiter = stringBuilder.ToString();

var stream = new NetworkStream(_client);
var bytes = Encoding.UTF8.GetBytes(jsonTextWithLineDelimiter);
var bytes = Encoding.UTF8.GetBytes(jsonText);
Console.WriteLine("Sending hardware");
stream.Write(bytes);
stream.Close();
Console.WriteLine("Hardware send");
}

public void WaitForCommand()
public void WaitForCommand(HardwareManager hardwareManager)
{
while (true)
{
Expand All @@ -55,20 +59,20 @@ public void WaitForCommand()
case Command.SetAuto:
index = block_read();
if (index < 0) return;
HardwareManager.SetAuto(index);
hardwareManager.SetAuto(index);
break;
case Command.SetValue:
index = block_read();
if (index < 0) return;
value = block_read();
if (value < 0) return;
HardwareManager.SetValue(index, value);
hardwareManager.SetValue(index, value);
break;
case Command.GetValue:
index = block_read();
if (index < 0) return;
Console.WriteLine("Receive index: " + index);
value = HardwareManager.GetValue(index);
value = hardwareManager.GetValue(index);
var bytes = BitConverter.GetBytes(value);
Console.WriteLine("sending value: " + value);
if (!block_send(bytes)) return;
Expand Down Expand Up @@ -113,14 +117,17 @@ private int block_read()
}
}

private void BindPort()
// return port
private int StartServer()
{
var p = _port;
var p = DefaultPort;
for (; p <= 65535; p++)
{
try
{
_listener.Bind(new IPEndPoint(IPAddress.Parse(Address), p));
_listener.Listen(1);

}
catch (SocketException e)
{
Expand All @@ -133,15 +140,34 @@ private void BindPort()
break;
}

Console.WriteLine("SelectPort: valid port " + p);
break;
Console.WriteLine("Server Started on " + Address + ":" + p);
return p;
}

if (p > 65535)
throw new ArgumentException("No valid port can be found for " + Address);
_port = p;
throw new ArgumentException("No valid port can be found for " + Address);

}


// return client
private Socket AcceptClient()
{
var client = _listener.Accept();
var checkBytes = Encoding.UTF8.GetBytes(Check);
var readBuf = new byte[checkBytes.Length];
var res = client.Receive(readBuf);

var str = Encoding.UTF8.GetString(readBuf);
if (str != Check)
{
throw new Exception("invalid client. Check : " + str + "byte received: " + res);
}

client.Send(Encoding.UTF8.GetBytes(CheckResponse));

Console.WriteLine("Client accepted!");
return client;
}

public void Shutdown()
{
Expand Down
13 changes: 0 additions & 13 deletions hardware/LibreHardwareMonitorWrapper/State.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
namespace LibreHardwareMonitorWrapper;

public static class State
{
public static readonly List<BaseHardware> Hardwares = new();
}

public enum HardwareType
{
Control = 1,
Fan = 2,
Temp = 3
}

public enum Command
{
SetAuto = 1,
SetValue = 2,
GetValue = 3,
Shutdown = 4
}

public abstract class BaseHardware
{
protected BaseHardware(string id, string name, string info, int index, HardwareType type)
Expand Down
Loading
Loading