Skip to content

Commit 74cbe6f

Browse files
committed
Rework config loading and include FallbackTemperature
1 parent 63374c0 commit 74cbe6f

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

src/FanControl.StorageSpacePlugin/Config.cs

+53-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reflection;
34
using System.IO;
45

56
namespace FanControl.StorageSpacePlugin
67
{
78
internal static class Config
89
{
9-
private static int _defaultRefreshRate = 30;
10+
private static readonly Dictionary<string, Action<string>> ConfigActions = new Dictionary<string, Action<string>>();
11+
12+
static Config()
13+
{
14+
ConfigActions.Add(
15+
ConfigValues.RefreshRateKey,
16+
value =>
17+
{
18+
var result = int.TryParse(value, out var refreshRate);
19+
_refreshRate = result ? refreshRate : Defaults.RefreshRateValue;
20+
});
21+
22+
ConfigActions.Add(
23+
ConfigValues.FallbackTemperatureKey,
24+
value =>
25+
{
26+
var result = float.TryParse(value, out var fallbackTemperature);
27+
_fallbackTemperature = result ? fallbackTemperature : Defaults.FallbackTemperatureValue;
28+
});
29+
}
1030

1131
public static string ApplicationPath()
1232
{
@@ -24,13 +44,13 @@ public static string ConfigPath()
2444
return Path.Combine(PluginsPath(), "FanControl.StorageSpacePlugin.ini");
2545
}
2646

27-
public static int RefreshRate()
47+
public static void ReadConfig()
2848
{
29-
string configText;
49+
string[] lines;
3050

3151
try
3252
{
33-
configText = File.ReadAllText(ConfigPath());
53+
lines = File.ReadAllLines(ConfigPath());
3454
}
3555
catch (Exception exception)
3656
{
@@ -41,22 +61,44 @@ public static int RefreshRate()
4161
case PathTooLongException _:
4262
case IOException _:
4363
case UnauthorizedAccessException _:
44-
return _defaultRefreshRate;
64+
return;
4565
default:
4666
throw; // Let FanControl handle and log the Exception
4767
}
4868
}
4969

50-
var configsArray = configText.Split('=');
51-
52-
var result = int.TryParse(configsArray[1], out var refreshRate);
70+
foreach (var line in lines)
71+
{
72+
var configsArray = line.Split('=');
5373

54-
return result ? refreshRate : _defaultRefreshRate;
74+
if (configsArray.Length != 2)
75+
{
76+
continue;
77+
}
78+
79+
if (ConfigActions.TryGetValue(configsArray[0], out var configUpdate))
80+
{
81+
configUpdate.Invoke(configsArray[1]);
82+
}
83+
}
5584
}
5685

86+
private static int _refreshRate = Defaults.RefreshRateValue;
87+
public static int RefreshRate => _refreshRate;
88+
89+
private static float _fallbackTemperature = Defaults.FallbackTemperatureValue;
90+
public static float FallbackTemperature => _fallbackTemperature;
91+
5792
public static class Defaults
5893
{
59-
public static float FallbackTemperature => 36; // Default reported temperature in case of wrong PowerShell value
94+
public static int RefreshRateValue => 30;
95+
public static float FallbackTemperatureValue => 36; // Default reported temperature in case of wrong PowerShell value
96+
}
97+
98+
public static class ConfigValues
99+
{
100+
public static string RefreshRateKey => "RefreshRate";
101+
public static string FallbackTemperatureKey => "FallbackTemperature";
60102
}
61103
}
62-
}
104+
}

src/FanControl.StorageSpacePlugin/DisksInfoProvider.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public DisksInfoProvider()
2626
{
2727
_powerShellRunner = new PowerShellRunner();
2828

29-
var refreshRate = Config.RefreshRate();
29+
var refreshRate = Config.RefreshRate;
3030

3131
var autoUpdater = Observable.FromEvent<bool>(
3232
eh => _autoUpdaterObserver += eh,
@@ -44,7 +44,7 @@ public DisksInfoProvider()
4444
{
4545
if (_disks.TryGetValue(serial, out var disk))
4646
{
47-
disk.Temperature = temperature == 0 ? Config.Defaults.FallbackTemperature : temperature;
47+
disk.Temperature = temperature == 0 ? Config.FallbackTemperature : temperature;
4848
}
4949
}
5050
});
@@ -143,7 +143,7 @@ private float GetTemperature(PowerShell ps, Disk disk)
143143
}
144144
}
145145

146-
return 36;
146+
return Config.Defaults.FallbackTemperatureValue;
147147
}
148148
}
149149
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
RefreshRate=30
1+
RefreshRate=30
2+
FallbackTemperature=36

src/FanControl.StorageSpacePlugin/PluginSensor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public float? Value
2222
get
2323
{
2424
var temperature = _disk.Temperature;
25-
return temperature == 0 ? Config.Defaults.FallbackTemperature : temperature;
25+
return temperature == 0 ? Config.FallbackTemperature : temperature;
2626
}
2727
}
2828

src/FanControl.StorageSpacePlugin/StorageSpacePlugin.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class StorageSpacePlugin : IPlugin
99

1010
public StorageSpacePlugin()
1111
{
12+
Config.ReadConfig();
1213
_disksInfoProvider = new DisksInfoProvider();
1314
_disksInfoProvider.Initialize();
1415
}

0 commit comments

Comments
 (0)