201 lines
6.1 KiB
C#
Raw Normal View History

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;
using System.Globalization;
using OpenHardwareMonitor.Collections;
2010-01-26 22:37:48 +00:00
namespace OpenHardwareMonitor.Hardware {
internal class Sensor : ISensor {
2010-01-26 22:37:48 +00:00
private readonly string defaultName;
2010-01-26 22:37:48 +00:00
private string name;
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;
private readonly Queue<SensorValue> values =
new Queue<SensorValue>(MAX_MINUTES * 15);
private readonly ISettings settings;
private IControl control;
2010-01-26 22:37:48 +00:00
private float sum;
private int count;
2010-01-26 22:37:48 +00:00
private const int MAX_MINUTES = 120;
2010-01-26 22:37:48 +00:00
public Sensor(string name, int index, SensorType sensorType,
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,
IHardware hardware, ParameterDescription[] parameterDescriptions,
ISettings settings) :
2010-06-06 11:07:57 +00:00
this(name, index, false, sensorType, hardware,
parameterDescriptions, settings) { }
public Sensor(string name, int index, bool defaultHidden,
2010-06-06 11:07:57 +00:00
SensorType sensorType, IHardware hardware,
ParameterDescription[] parameterDescriptions, ISettings settings)
{
2010-01-26 22:37:48 +00:00
this.index = index;
this.defaultHidden = defaultHidden;
2010-01-26 22:37:48 +00:00
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);
2010-01-26 22:37:48 +00:00
}
public IHardware Hardware {
get { return hardware; }
}
2010-01-26 22:37:48 +00:00
public SensorType SensorType {
get { return sensorType; }
}
public Identifier Identifier {
get {
return new Identifier(hardware.Identifier,
sensorType.ToString().ToLowerInvariant(),
index.ToString(CultureInfo.InvariantCulture));
}
}
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;
settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
2010-01-26 22:37:48 +00:00
}
}
public int Index {
get { return index; }
}
public bool IsDefaultHidden {
get { return defaultHidden; }
}
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 {
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) {
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
}
public IEnumerable<SensorValue> Values {
get { return values; }
}
public void Accept(IVisitor visitor) {
2010-08-15 14:46:58 +00:00
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;
}
}
2010-01-26 22:37:48 +00:00
}
}