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>.
|
|
|
|
|
Portions created by the Initial Developer are Copyright (C) 2009-2010
|
|
|
|
|
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;
|
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;
|
|
|
|
|
private readonly IHardware hardware;
|
|
|
|
|
private readonly ReadOnlyArray<IParameter> parameters;
|
2010-08-15 14:46:58 +00:00
|
|
|
|
private float? currentValue;
|
|
|
|
|
private float? minValue;
|
|
|
|
|
private float? maxValue;
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly Queue<SensorValue> values =
|
2010-07-27 18:38:11 +00:00
|
|
|
|
new Queue<SensorValue>(MAX_MINUTES * 15);
|
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-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
private const int MAX_MINUTES = 120;
|
2010-02-07 16:37:15 +00:00
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
public Sensor(string name, int index, SensorType sensorType,
|
2010-08-08 13:57:26 +00:00
|
|
|
|
IHardware hardware, ISettings settings) :
|
|
|
|
|
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,
|
2010-08-08 13:57:26 +00:00
|
|
|
|
IHardware hardware, ParameterDescription[] parameterDescriptions,
|
|
|
|
|
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,
|
2010-06-06 11:07:57 +00:00
|
|
|
|
SensorType sensorType, IHardware 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);
|
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 {
|
2010-07-27 18:38:11 +00:00
|
|
|
|
while (values.Count > 0 &&
|
|
|
|
|
(DateTime.Now - values.Peek().Time).TotalMinutes > MAX_MINUTES)
|
|
|
|
|
values.Dequeue();
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
if (value.HasValue) {
|
|
|
|
|
sum += value.Value;
|
|
|
|
|
count++;
|
|
|
|
|
if (count == 4) {
|
2010-07-27 18:38:11 +00:00
|
|
|
|
values.Enqueue(new SensorValue(sum / count, DateTime.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
|
|
|
|
}
|
|
|
|
|
}
|