-
Notifications
You must be signed in to change notification settings - Fork 0
/
FanLightController.cs
98 lines (80 loc) · 2.21 KB
/
FanLightController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using LibreHardwareMonitor.Hardware;
namespace flaas;
public class FanLightController
{
private readonly ISensor _sensor;
private float _brightness = 75;
private bool _isOn;
public FanLightController(ISensor sensor)
{
_sensor = sensor;
_ = Get();
}
public void On()
{
_isOn = true;
SetBrightness(_brightness);
}
public void Off()
{
_isOn = false;
_sensor.Control.SetSoftware(0);
}
public State Get()
{
_sensor.Hardware.Update();
_isOn = _sensor.Value > 0;
if(_isOn)
_brightness = (int)(_sensor.Value ?? 0);
return new State(_isOn, _brightness);
}
public void Set(State state)
{
_brightness = state.Brightness;
_isOn = state.IsOn;
if(_isOn)
On();
else
Off();
}
public void SetBrightness(float level)
{
_brightness = level;
if(_isOn)
_sensor.Control.SetSoftware(_brightness);
}
public static FanLightController CreateFanLightController(string name)
{
Computer computer = new Computer
{
IsCpuEnabled = false,
IsGpuEnabled = false,
IsMemoryEnabled = false,
IsMotherboardEnabled = true,
IsControllerEnabled = false,
IsNetworkEnabled = false,
IsStorageEnabled = false
};
computer.Open();
computer.Accept(new UpdateVisitor());
var sensor = computer.Hardware.SelectMany(h => h.SubHardware).SelectMany(sh => sh.Sensors)
.SingleOrDefault(s => s.SensorType == SensorType.Control && s.Name == name);
return new FanLightController(sensor);
}
}
public record State(bool IsOn, float Brightness);
public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware)
subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
}