1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Hardware/Sensor.cs Tue Jan 26 22:37:48 2010 +0000
1.3 @@ -0,0 +1,185 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2009-2010
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 +
1.27 + Alternatively, the contents of this file may be used under the terms of
1.28 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.29 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.30 + in which case the provisions of the GPL or the LGPL are applicable instead
1.31 + of those above. If you wish to allow use of your version of this file only
1.32 + under the terms of either the GPL or the LGPL, and not to allow others to
1.33 + use your version of this file under the terms of the MPL, indicate your
1.34 + decision by deleting the provisions above and replace them with the notice
1.35 + and other provisions required by the GPL or the LGPL. If you do not delete
1.36 + the provisions above, a recipient may use your version of this file under
1.37 + the terms of any one of the MPL, the GPL or the LGPL.
1.38 +
1.39 +*/
1.40 +
1.41 +using System;
1.42 +using System.Collections.Generic;
1.43 +
1.44 +namespace OpenHardwareMonitor.Hardware {
1.45 +
1.46 + public class Sensor : ISensor {
1.47 +
1.48 + private string defaultName;
1.49 + private string name;
1.50 + private int index;
1.51 + private SensorType sensorType;
1.52 + private IHardware hardware;
1.53 + private float? value;
1.54 + private float? min;
1.55 + private float? max;
1.56 + private float? limit;
1.57 + private float? defaultLimit;
1.58 + private Queue<ISensorEntry> entries =
1.59 + new Queue<ISensorEntry>(MAX_MINUTES * 15);
1.60 +
1.61 + private float sum = 0;
1.62 + private int count = 0;
1.63 +
1.64 + private const int MAX_MINUTES = 120;
1.65 +
1.66 + private string GetIdentifier() {
1.67 + string s = "";
1.68 + switch (sensorType) {
1.69 + case SensorType.Voltage: s = "voltage"; break;
1.70 + case SensorType.Clock: s = "clock"; break;
1.71 + case SensorType.Temperature: s = "temperature"; break;
1.72 + case SensorType.Fan: s = "fan"; break;
1.73 + }
1.74 + return hardware.Identifier + "/" + s + "/" + index;
1.75 + }
1.76 +
1.77 + public Sensor(string name, int index, SensorType sensorType,
1.78 + IHardware hardware)
1.79 + : this(name, index, null, sensorType, hardware) { }
1.80 +
1.81 + public Sensor(string name, int index, float? limit,
1.82 + SensorType sensorType, IHardware hardware)
1.83 + {
1.84 + this.defaultName = name;
1.85 + this.index = index;
1.86 + this.defaultLimit = limit;
1.87 + this.sensorType = sensorType;
1.88 + this.hardware = hardware;
1.89 + string configName =
1.90 + Utilities.Config.Settings[GetIdentifier() + "/name"];
1.91 + if (configName != null)
1.92 + this.name = configName;
1.93 + else
1.94 + this.name = name;
1.95 + string configLimit =
1.96 + Utilities.Config.Settings[GetIdentifier() + "/limit"];
1.97 + if (configLimit != null && configLimit != "")
1.98 + this.limit = float.Parse(configLimit);
1.99 + else
1.100 + this.limit = limit;
1.101 + }
1.102 +
1.103 + public SensorType SensorType {
1.104 + get { return sensorType; }
1.105 + }
1.106 +
1.107 + public string Name {
1.108 + get {
1.109 + return name;
1.110 + }
1.111 + set {
1.112 + if (value != "")
1.113 + name = value;
1.114 + else
1.115 + name = defaultName;
1.116 + Utilities.Config.Settings[GetIdentifier() + "/name"] = name;
1.117 + }
1.118 + }
1.119 +
1.120 + public int Index {
1.121 + get { return index; }
1.122 + }
1.123 +
1.124 + public float? Value {
1.125 + get {
1.126 + return value;
1.127 + }
1.128 + set {
1.129 + while (entries.Count > 0 &&
1.130 + (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
1.131 + entries.Dequeue();
1.132 +
1.133 + if (value.HasValue) {
1.134 + sum += value.Value;
1.135 + count++;
1.136 + if (count == 4) {
1.137 + entries.Enqueue(new Entry(sum / count, DateTime.Now));
1.138 + sum = 0;
1.139 + count = 0;
1.140 + }
1.141 + }
1.142 +
1.143 + this.value = value;
1.144 + if (min > value || !min.HasValue)
1.145 + min = value;
1.146 + if (max < value || !max.HasValue)
1.147 + max = value;
1.148 + }
1.149 + }
1.150 +
1.151 + public float? Min { get { return min; } }
1.152 + public float? Max { get { return max; } }
1.153 +
1.154 + public float? Limit {
1.155 + get {
1.156 + return limit;
1.157 + }
1.158 +
1.159 + set {
1.160 + if (value.HasValue) {
1.161 + limit = value;
1.162 + Utilities.Config.Settings[GetIdentifier() + "/limit"] =
1.163 + limit.ToString();
1.164 + } else {
1.165 + limit = defaultLimit;
1.166 + Utilities.Config.Settings[GetIdentifier() + "/limit"] = "";
1.167 + }
1.168 + }
1.169 + }
1.170 +
1.171 + public IEnumerable<ISensorEntry> Plot {
1.172 + get { return entries; }
1.173 + }
1.174 +
1.175 + public struct Entry : ISensorEntry {
1.176 + private float value;
1.177 + private DateTime time;
1.178 +
1.179 + public Entry(float value, DateTime time) {
1.180 + this.value = value;
1.181 + this.time = time;
1.182 + }
1.183 +
1.184 + public float Value { get { return value; } }
1.185 + public DateTime Time { get { return time; } }
1.186 + }
1.187 + }
1.188 +}