2010-01-26 22:37:48 +00:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the License.
|
|
|
|
|
|
|
|
|
|
The Original Code is the Open Hardware Monitor code.
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Michael Möller <m.moeller@gmx.ch>.
|
2011-06-19 12:41:18 +00:00
|
|
|
|
Portions created by the Initial Developer are Copyright (C) 2009-2011
|
2010-01-26 22:37:48 +00:00
|
|
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
|
|
|
|
|
Alternatively, the contents of this file may be used under the terms of
|
|
|
|
|
either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
|
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
|
in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
|
of those above. If you wish to allow use of your version of this file only
|
|
|
|
|
under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
|
use your version of this file under the terms of the MPL, indicate your
|
|
|
|
|
decision by deleting the provisions above and replace them with the notice
|
|
|
|
|
and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
|
the provisions above, a recipient may use your version of this file under
|
|
|
|
|
the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-08-12 20:53:27 +00:00
|
|
|
|
using System.Globalization;
|
2011-06-19 12:41:18 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2010-08-08 13:57:26 +00:00
|
|
|
|
using OpenHardwareMonitor.Collections;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.Hardware {
|
|
|
|
|
|
2010-08-08 13:57:26 +00:00
|
|
|
|
internal class Sensor : ISensor {
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly string defaultName;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
private string name;
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly int index;
|
|
|
|
|
private readonly bool defaultHidden;
|
|
|
|
|
private readonly SensorType sensorType;
|
2011-06-19 12:41:18 +00:00
|
|
|
|
private readonly Hardware hardware;
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly ReadOnlyArray<IParameter> parameters;
|
2010-08-15 14:46:58 +00:00
|
|
|
|
private float? currentValue;
|
|
|
|
|
private float? minValue;
|
|
|
|
|
private float? maxValue;
|
2011-06-19 12:41:18 +00:00
|
|
|
|
private readonly RingCollection<SensorValue>
|
|
|
|
|
values = new RingCollection<SensorValue>();
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly ISettings settings;
|
2011-01-20 21:31:54 +00:00
|
|
|
|
private IControl control;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private float sum;
|
|
|
|
|
private int count;
|
2010-02-07 16:37:15 +00:00
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
public Sensor(string name, int index, SensorType sensorType,
|
2011-06-19 12:41:18 +00:00
|
|
|
|
Hardware hardware, ISettings settings) :
|
2010-08-08 13:57:26 +00:00
|
|
|
|
this(name, index, sensorType, hardware, null, settings) { }
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-06-06 11:07:57 +00:00
|
|
|
|
public Sensor(string name, int index, SensorType sensorType,
|
2011-06-19 12:41:18 +00:00
|
|
|
|
Hardware hardware, ParameterDescription[] parameterDescriptions,
|
2010-08-08 13:57:26 +00:00
|
|
|
|
ISettings settings) :
|
2010-06-06 11:07:57 +00:00
|
|
|
|
this(name, index, false, sensorType, hardware,
|
2010-08-08 13:57:26 +00:00
|
|
|
|
parameterDescriptions, settings) { }
|
2010-02-27 15:55:17 +00:00
|
|
|
|
|
2010-05-06 19:20:38 +00:00
|
|
|
|
public Sensor(string name, int index, bool defaultHidden,
|
2011-06-19 12:41:18 +00:00
|
|
|
|
SensorType sensorType, Hardware hardware,
|
2010-08-08 13:57:26 +00:00
|
|
|
|
ParameterDescription[] parameterDescriptions, ISettings settings)
|
|
|
|
|
{
|
2010-01-26 22:37:48 +00:00
|
|
|
|
this.index = index;
|
2010-05-06 19:20:38 +00:00
|
|
|
|
this.defaultHidden = defaultHidden;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
this.sensorType = sensorType;
|
|
|
|
|
this.hardware = hardware;
|
2010-05-06 19:20:38 +00:00
|
|
|
|
Parameter[] parameters = new Parameter[parameterDescriptions == null ?
|
|
|
|
|
0 : parameterDescriptions.Length];
|
2010-02-27 15:55:17 +00:00
|
|
|
|
for (int i = 0; i < parameters.Length; i++ )
|
2010-08-08 13:57:26 +00:00
|
|
|
|
parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
|
2010-02-27 15:55:17 +00:00
|
|
|
|
this.parameters = parameters;
|
|
|
|
|
|
2010-08-08 13:57:26 +00:00
|
|
|
|
this.settings = settings;
|
|
|
|
|
this.defaultName = name;
|
2010-08-12 20:53:27 +00:00
|
|
|
|
this.name = settings.GetValue(
|
2010-08-08 13:57:26 +00:00
|
|
|
|
new Identifier(Identifier, "name").ToString(), name);
|
2011-06-19 12:41:18 +00:00
|
|
|
|
|
|
|
|
|
GetSensorValuesFromSettings();
|
|
|
|
|
|
|
|
|
|
hardware.Closing += delegate(IHardware h) {
|
|
|
|
|
SetSensorValuesToSettings();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetSensorValuesToSettings() {
|
|
|
|
|
using (MemoryStream m = new MemoryStream()) {
|
|
|
|
|
using (GZipStream c = new GZipStream(m, CompressionMode.Compress))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(c)) {
|
|
|
|
|
foreach (SensorValue sensorValue in values) {
|
|
|
|
|
writer.Write(sensorValue.Time.ToBinary());
|
|
|
|
|
writer.Write(sensorValue.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
settings.SetValue(new Identifier(Identifier, "values").ToString(),
|
|
|
|
|
Convert.ToBase64String(m.ToArray()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetSensorValuesFromSettings() {
|
|
|
|
|
string s = settings.GetValue(
|
|
|
|
|
new Identifier(Identifier, "values").ToString(), null);
|
|
|
|
|
|
|
|
|
|
byte[] array = null;
|
|
|
|
|
try {
|
|
|
|
|
array = Convert.FromBase64String(s);
|
|
|
|
|
using (MemoryStream m = new MemoryStream(array))
|
|
|
|
|
using (GZipStream c = new GZipStream(m, CompressionMode.Decompress))
|
|
|
|
|
using (BinaryReader reader = new BinaryReader(c)) {
|
|
|
|
|
try {
|
|
|
|
|
while (true) {
|
|
|
|
|
DateTime time = DateTime.FromBinary(reader.ReadInt64());
|
|
|
|
|
float value = reader.ReadSingle();
|
|
|
|
|
AppendValue(value, time);
|
|
|
|
|
}
|
|
|
|
|
} catch (EndOfStreamException) { }
|
|
|
|
|
}
|
|
|
|
|
} catch { }
|
|
|
|
|
if (values.Count > 0)
|
2011-07-24 22:12:01 +00:00
|
|
|
|
AppendValue(float.NaN, DateTime.UtcNow);
|
2011-06-19 12:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-02-07 16:37:15 +00:00
|
|
|
|
public IHardware Hardware {
|
|
|
|
|
get { return hardware; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
public SensorType SensorType {
|
|
|
|
|
get { return sensorType; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 19:20:38 +00:00
|
|
|
|
public Identifier Identifier {
|
2010-02-07 16:37:15 +00:00
|
|
|
|
get {
|
2010-08-12 20:53:27 +00:00
|
|
|
|
return new Identifier(hardware.Identifier,
|
|
|
|
|
sensorType.ToString().ToLowerInvariant(),
|
|
|
|
|
index.ToString(CultureInfo.InvariantCulture));
|
2010-02-07 16:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
public string Name {
|
|
|
|
|
get {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
set {
|
2010-08-15 14:46:58 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(value))
|
2010-01-26 22:37:48 +00:00
|
|
|
|
name = value;
|
|
|
|
|
else
|
|
|
|
|
name = defaultName;
|
2010-08-12 20:53:27 +00:00
|
|
|
|
settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Index {
|
|
|
|
|
get { return index; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 19:20:38 +00:00
|
|
|
|
public bool IsDefaultHidden {
|
|
|
|
|
get { return defaultHidden; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-27 15:55:17 +00:00
|
|
|
|
public IReadOnlyArray<IParameter> Parameters {
|
|
|
|
|
get { return parameters; }
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
public float? Value {
|
|
|
|
|
get {
|
2010-08-15 14:46:58 +00:00
|
|
|
|
return currentValue;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
set {
|
2011-07-24 22:12:01 +00:00
|
|
|
|
DateTime now = DateTime.UtcNow;
|
2011-06-19 12:41:18 +00:00
|
|
|
|
while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
|
|
|
|
|
values.Remove();
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
if (value.HasValue) {
|
|
|
|
|
sum += value.Value;
|
|
|
|
|
count++;
|
|
|
|
|
if (count == 4) {
|
2011-06-19 12:41:18 +00:00
|
|
|
|
AppendValue(sum / count, now);
|
2010-01-26 22:37:48 +00:00
|
|
|
|
sum = 0;
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-15 14:46:58 +00:00
|
|
|
|
this.currentValue = value;
|
|
|
|
|
if (minValue > value || !minValue.HasValue)
|
|
|
|
|
minValue = value;
|
|
|
|
|
if (maxValue < value || !maxValue.HasValue)
|
|
|
|
|
maxValue = value;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-15 14:46:58 +00:00
|
|
|
|
public float? Min { get { return minValue; } }
|
|
|
|
|
public float? Max { get { return maxValue; } }
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-07-04 12:49:16 +00:00
|
|
|
|
public void ResetMin() {
|
2010-08-15 14:46:58 +00:00
|
|
|
|
minValue = null;
|
2010-07-04 12:49:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetMax() {
|
2010-08-15 14:46:58 +00:00
|
|
|
|
maxValue = null;
|
2010-07-04 12:49:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-27 18:38:11 +00:00
|
|
|
|
public IEnumerable<SensorValue> Values {
|
|
|
|
|
get { return values; }
|
|
|
|
|
}
|
2010-05-09 16:22:13 +00:00
|
|
|
|
|
|
|
|
|
public void Accept(IVisitor visitor) {
|
2010-08-15 14:46:58 +00:00
|
|
|
|
if (visitor == null)
|
|
|
|
|
throw new ArgumentNullException("visitor");
|
|
|
|
|
visitor.VisitSensor(this);
|
2010-05-09 16:22:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Traverse(IVisitor visitor) {
|
|
|
|
|
foreach (IParameter parameter in parameters)
|
|
|
|
|
parameter.Accept(visitor);
|
|
|
|
|
}
|
2011-01-20 21:31:54 +00:00
|
|
|
|
|
|
|
|
|
public IControl Control {
|
|
|
|
|
get {
|
|
|
|
|
return control;
|
|
|
|
|
}
|
|
|
|
|
internal set {
|
|
|
|
|
this.control = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|