forked from openhardwaremonitor/openhardwaremonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensor.cs
239 lines (202 loc) · 6.96 KB
/
Sensor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2012 Michael Möller <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using OpenHardwareMonitor.Collections;
namespace OpenHardwareMonitor.Hardware {
internal class Sensor : ISensor {
private readonly string defaultName;
private string name;
private readonly int index;
private readonly bool defaultHidden;
private readonly SensorType sensorType;
private readonly Hardware hardware;
private readonly ReadOnlyArray<IParameter> parameters;
private float? currentValue;
private float? minValue;
private float? maxValue;
private readonly RingCollection<SensorValue>
values = new RingCollection<SensorValue>();
private readonly ISettings settings;
private IControl control;
private float sum;
private int count;
public Sensor(string name, int index, SensorType sensorType,
Hardware hardware, ISettings settings) :
this(name, index, sensorType, hardware, null, settings) { }
public Sensor(string name, int index, SensorType sensorType,
Hardware hardware, ParameterDescription[] parameterDescriptions,
ISettings settings) :
this(name, index, false, sensorType, hardware,
parameterDescriptions, settings) { }
public Sensor(string name, int index, bool defaultHidden,
SensorType sensorType, Hardware hardware,
ParameterDescription[] parameterDescriptions, ISettings settings)
{
this.index = index;
this.defaultHidden = defaultHidden;
this.sensorType = sensorType;
this.hardware = hardware;
Parameter[] parameters = new Parameter[parameterDescriptions == null ?
0 : parameterDescriptions.Length];
for (int i = 0; i < parameters.Length; i++ )
parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
this.parameters = parameters;
this.settings = settings;
this.defaultName = name;
this.name = settings.GetValue(
new Identifier(Identifier, "name").ToString(), name);
GetSensorValuesFromSettings();
hardware.Closing += delegate(IHardware h) {
SetSensorValuesToSettings();
};
}
private void SetSensorValuesToSettings() {
using (MemoryStream m = new MemoryStream()) {
using (GZipStream c = new GZipStream(m, CompressionMode.Compress))
using (BufferedStream b = new BufferedStream(c, 65536))
using (BinaryWriter writer = new BinaryWriter(b)) {
long t = 0;
foreach (SensorValue sensorValue in values) {
long v = sensorValue.Time.ToBinary();
writer.Write(v - t);
t = v;
writer.Write(sensorValue.Value);
}
writer.Flush();
}
settings.SetValue(new Identifier(Identifier, "values").ToString(),
Convert.ToBase64String(m.ToArray()));
}
}
private void GetSensorValuesFromSettings() {
string name = new Identifier(Identifier, "values").ToString();
string s = settings.GetValue(name, null);
try {
byte[] array = Convert.FromBase64String(s);
s = null;
DateTime now = DateTime.UtcNow;
using (MemoryStream m = new MemoryStream(array))
using (GZipStream c = new GZipStream(m, CompressionMode.Decompress))
using (BinaryReader reader = new BinaryReader(c)) {
try {
long t = 0;
while (true) {
t += reader.ReadInt64();
DateTime time = DateTime.FromBinary(t);
if (time > now)
break;
float value = reader.ReadSingle();
AppendValue(value, time);
}
} catch (EndOfStreamException) { }
}
} catch { }
if (values.Count > 0)
AppendValue(float.NaN, DateTime.UtcNow);
// remove the value string from the settings to reduce memory usage
settings.Remove(name);
}
private void AppendValue(float value, DateTime time) {
if (values.Count >= 2 && values.Last.Value == value &&
values[values.Count - 2].Value == value) {
values.Last = new SensorValue(value, time);
return;
}
values.Append(new SensorValue(value, time));
}
public IHardware Hardware {
get { return hardware; }
}
public SensorType SensorType {
get { return sensorType; }
}
public Identifier Identifier {
get {
return new Identifier(hardware.Identifier,
sensorType.ToString().ToLowerInvariant(),
index.ToString(CultureInfo.InvariantCulture));
}
}
public string Name {
get {
return name;
}
set {
if (!string.IsNullOrEmpty(value))
name = value;
else
name = defaultName;
settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
}
}
public int Index {
get { return index; }
}
public bool IsDefaultHidden {
get { return defaultHidden; }
}
public IReadOnlyArray<IParameter> Parameters {
get { return parameters; }
}
public float? Value {
get {
return currentValue;
}
set {
DateTime now = DateTime.UtcNow;
while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
values.Remove();
if (value.HasValue) {
sum += value.Value;
count++;
if (count == 4) {
AppendValue(sum / count, now);
sum = 0;
count = 0;
}
}
this.currentValue = value;
if (minValue > value || !minValue.HasValue)
minValue = value;
if (maxValue < value || !maxValue.HasValue)
maxValue = value;
}
}
public float? Min { get { return minValue; } }
public float? Max { get { return maxValue; } }
public void ResetMin() {
minValue = null;
}
public void ResetMax() {
maxValue = null;
}
public IEnumerable<SensorValue> Values {
get { return values; }
}
public void Accept(IVisitor visitor) {
if (visitor == null)
throw new ArgumentNullException("visitor");
visitor.VisitSensor(this);
}
public void Traverse(IVisitor visitor) {
foreach (IParameter parameter in parameters)
parameter.Accept(visitor);
}
public IControl Control {
get {
return control;
}
internal set {
this.control = value;
}
}
}
}