Hardware/Sensor.cs
author moel.mich
Fri, 01 Oct 2010 19:01:09 +0000
changeset 203 ca487ba88c24
parent 167 b7cc9d09aefe
child 247 6dc755f1970e
permissions -rw-r--r--
Fixed the initialization of the TSC estimating code.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@166
    40
using System.Globalization;
moel@165
    41
using OpenHardwareMonitor.Collections;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware {
moel@1
    44
moel@165
    45
  internal class Sensor : ISensor {
moel@1
    46
moel@195
    47
    private readonly string defaultName;
moel@1
    48
    private string name;
moel@195
    49
    private readonly int index;
moel@195
    50
    private readonly bool defaultHidden;
moel@195
    51
    private readonly SensorType sensorType;
moel@195
    52
    private readonly IHardware hardware;
moel@195
    53
    private readonly ReadOnlyArray<IParameter> parameters;
moel@167
    54
    private float? currentValue;
moel@167
    55
    private float? minValue;
moel@167
    56
    private float? maxValue;
moel@195
    57
    private readonly Queue<SensorValue> values =
moel@159
    58
      new Queue<SensorValue>(MAX_MINUTES * 15);
moel@195
    59
    private readonly ISettings settings;
moel@1
    60
    
moel@195
    61
    private float sum;
moel@195
    62
    private int count;
moel@1
    63
moel@1
    64
    private const int MAX_MINUTES = 120;
moel@28
    65
   
moel@1
    66
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    67
      IHardware hardware, ISettings settings) : 
moel@165
    68
      this(name, index, sensorType, hardware, null, settings) { }
moel@1
    69
moel@134
    70
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    71
      IHardware hardware, ParameterDescription[] parameterDescriptions, 
moel@165
    72
      ISettings settings) :
moel@134
    73
      this(name, index, false, sensorType, hardware,
moel@165
    74
        parameterDescriptions, settings) { }
moel@63
    75
moel@109
    76
    public Sensor(string name, int index, bool defaultHidden, 
moel@134
    77
      SensorType sensorType, IHardware hardware, 
moel@165
    78
      ParameterDescription[] parameterDescriptions, ISettings settings) 
moel@165
    79
    {           
moel@1
    80
      this.index = index;
moel@109
    81
      this.defaultHidden = defaultHidden;
moel@1
    82
      this.sensorType = sensorType;
moel@1
    83
      this.hardware = hardware;
moel@109
    84
      Parameter[] parameters = new Parameter[parameterDescriptions == null ?
moel@109
    85
        0 : parameterDescriptions.Length];
moel@63
    86
      for (int i = 0; i < parameters.Length; i++ ) 
moel@165
    87
        parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
moel@63
    88
      this.parameters = parameters;
moel@63
    89
moel@165
    90
      this.settings = settings;
moel@165
    91
      this.defaultName = name; 
moel@166
    92
      this.name = settings.GetValue(
moel@165
    93
        new Identifier(Identifier, "name").ToString(), name);
moel@1
    94
    }
moel@1
    95
moel@28
    96
    public IHardware Hardware {
moel@28
    97
      get { return hardware; }
moel@28
    98
    }
moel@28
    99
moel@1
   100
    public SensorType SensorType {
moel@1
   101
      get { return sensorType; }
moel@1
   102
    }
moel@1
   103
moel@109
   104
    public Identifier Identifier {
moel@28
   105
      get {
moel@166
   106
        return new Identifier(hardware.Identifier,
moel@166
   107
          sensorType.ToString().ToLowerInvariant(),
moel@166
   108
          index.ToString(CultureInfo.InvariantCulture));
moel@28
   109
      }
moel@28
   110
    }
moel@28
   111
moel@1
   112
    public string Name {
moel@1
   113
      get { 
moel@1
   114
        return name; 
moel@1
   115
      }
moel@1
   116
      set {
moel@167
   117
        if (!string.IsNullOrEmpty(value)) 
moel@1
   118
          name = value;          
moel@1
   119
        else 
moel@1
   120
          name = defaultName;
moel@166
   121
        settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
moel@1
   122
      }
moel@1
   123
    }
moel@1
   124
moel@1
   125
    public int Index {
moel@1
   126
      get { return index; }
moel@1
   127
    }
moel@1
   128
moel@109
   129
    public bool IsDefaultHidden {
moel@109
   130
      get { return defaultHidden; }
moel@109
   131
    }
moel@109
   132
moel@63
   133
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   134
      get { return parameters; }
moel@63
   135
    }
moel@63
   136
moel@1
   137
    public float? Value {
moel@1
   138
      get { 
moel@167
   139
        return currentValue; 
moel@1
   140
      }
moel@1
   141
      set {
moel@159
   142
        while (values.Count > 0 && 
moel@159
   143
          (DateTime.Now - values.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@159
   144
          values.Dequeue();
moel@1
   145
moel@1
   146
        if (value.HasValue) {
moel@1
   147
          sum += value.Value;
moel@1
   148
          count++;
moel@1
   149
          if (count == 4) {
moel@159
   150
            values.Enqueue(new SensorValue(sum / count, DateTime.Now));
moel@1
   151
            sum = 0;
moel@1
   152
            count = 0;
moel@1
   153
          }
moel@1
   154
        }
moel@1
   155
moel@167
   156
        this.currentValue = value;
moel@167
   157
        if (minValue > value || !minValue.HasValue)
moel@167
   158
          minValue = value;
moel@167
   159
        if (maxValue < value || !maxValue.HasValue)
moel@167
   160
          maxValue = value;
moel@1
   161
      }
moel@1
   162
    }
moel@1
   163
moel@167
   164
    public float? Min { get { return minValue; } }
moel@167
   165
    public float? Max { get { return maxValue; } }
moel@1
   166
moel@151
   167
    public void ResetMin() {
moel@167
   168
      minValue = null;
moel@151
   169
    }
moel@151
   170
moel@151
   171
    public void ResetMax() {
moel@167
   172
      maxValue = null;
moel@151
   173
    }
moel@151
   174
moel@159
   175
    public IEnumerable<SensorValue> Values {
moel@159
   176
      get { return values; }
moel@159
   177
    }    
moel@110
   178
moel@110
   179
    public void Accept(IVisitor visitor) {
moel@167
   180
      if (visitor == null)
moel@167
   181
        throw new ArgumentNullException("visitor");
moel@167
   182
      visitor.VisitSensor(this);
moel@110
   183
    }
moel@110
   184
moel@110
   185
    public void Traverse(IVisitor visitor) {
moel@110
   186
      foreach (IParameter parameter in parameters)
moel@110
   187
        parameter.Accept(visitor);
moel@110
   188
    }
moel@1
   189
  }
moel@1
   190
}