Hardware/Sensor.cs
author moel.mich
Tue, 15 Mar 2011 17:55:11 +0000
changeset 259 d83e927ee9d2
parent 195 0ee888c485d5
child 298 96263190189a
permissions -rw-r--r--
Fixed Issue 165.
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@247
    60
    private IControl control;
moel@1
    61
    
moel@195
    62
    private float sum;
moel@195
    63
    private int count;
moel@1
    64
moel@1
    65
    private const int MAX_MINUTES = 120;
moel@28
    66
   
moel@1
    67
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    68
      IHardware hardware, ISettings settings) : 
moel@165
    69
      this(name, index, sensorType, hardware, null, settings) { }
moel@1
    70
moel@134
    71
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    72
      IHardware hardware, ParameterDescription[] parameterDescriptions, 
moel@165
    73
      ISettings settings) :
moel@134
    74
      this(name, index, false, sensorType, hardware,
moel@165
    75
        parameterDescriptions, settings) { }
moel@63
    76
moel@109
    77
    public Sensor(string name, int index, bool defaultHidden, 
moel@134
    78
      SensorType sensorType, IHardware hardware, 
moel@165
    79
      ParameterDescription[] parameterDescriptions, ISettings settings) 
moel@165
    80
    {           
moel@1
    81
      this.index = index;
moel@109
    82
      this.defaultHidden = defaultHidden;
moel@1
    83
      this.sensorType = sensorType;
moel@1
    84
      this.hardware = hardware;
moel@109
    85
      Parameter[] parameters = new Parameter[parameterDescriptions == null ?
moel@109
    86
        0 : parameterDescriptions.Length];
moel@63
    87
      for (int i = 0; i < parameters.Length; i++ ) 
moel@165
    88
        parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
moel@63
    89
      this.parameters = parameters;
moel@63
    90
moel@165
    91
      this.settings = settings;
moel@165
    92
      this.defaultName = name; 
moel@166
    93
      this.name = settings.GetValue(
moel@165
    94
        new Identifier(Identifier, "name").ToString(), name);
moel@1
    95
    }
moel@1
    96
moel@28
    97
    public IHardware Hardware {
moel@28
    98
      get { return hardware; }
moel@28
    99
    }
moel@28
   100
moel@1
   101
    public SensorType SensorType {
moel@1
   102
      get { return sensorType; }
moel@1
   103
    }
moel@1
   104
moel@109
   105
    public Identifier Identifier {
moel@28
   106
      get {
moel@166
   107
        return new Identifier(hardware.Identifier,
moel@166
   108
          sensorType.ToString().ToLowerInvariant(),
moel@166
   109
          index.ToString(CultureInfo.InvariantCulture));
moel@28
   110
      }
moel@28
   111
    }
moel@28
   112
moel@1
   113
    public string Name {
moel@1
   114
      get { 
moel@1
   115
        return name; 
moel@1
   116
      }
moel@1
   117
      set {
moel@167
   118
        if (!string.IsNullOrEmpty(value)) 
moel@1
   119
          name = value;          
moel@1
   120
        else 
moel@1
   121
          name = defaultName;
moel@166
   122
        settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
moel@1
   123
      }
moel@1
   124
    }
moel@1
   125
moel@1
   126
    public int Index {
moel@1
   127
      get { return index; }
moel@1
   128
    }
moel@1
   129
moel@109
   130
    public bool IsDefaultHidden {
moel@109
   131
      get { return defaultHidden; }
moel@109
   132
    }
moel@109
   133
moel@63
   134
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   135
      get { return parameters; }
moel@63
   136
    }
moel@63
   137
moel@1
   138
    public float? Value {
moel@1
   139
      get { 
moel@167
   140
        return currentValue; 
moel@1
   141
      }
moel@1
   142
      set {
moel@159
   143
        while (values.Count > 0 && 
moel@159
   144
          (DateTime.Now - values.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@159
   145
          values.Dequeue();
moel@1
   146
moel@1
   147
        if (value.HasValue) {
moel@1
   148
          sum += value.Value;
moel@1
   149
          count++;
moel@1
   150
          if (count == 4) {
moel@159
   151
            values.Enqueue(new SensorValue(sum / count, DateTime.Now));
moel@1
   152
            sum = 0;
moel@1
   153
            count = 0;
moel@1
   154
          }
moel@1
   155
        }
moel@1
   156
moel@167
   157
        this.currentValue = value;
moel@167
   158
        if (minValue > value || !minValue.HasValue)
moel@167
   159
          minValue = value;
moel@167
   160
        if (maxValue < value || !maxValue.HasValue)
moel@167
   161
          maxValue = value;
moel@1
   162
      }
moel@1
   163
    }
moel@1
   164
moel@167
   165
    public float? Min { get { return minValue; } }
moel@167
   166
    public float? Max { get { return maxValue; } }
moel@1
   167
moel@151
   168
    public void ResetMin() {
moel@167
   169
      minValue = null;
moel@151
   170
    }
moel@151
   171
moel@151
   172
    public void ResetMax() {
moel@167
   173
      maxValue = null;
moel@151
   174
    }
moel@151
   175
moel@159
   176
    public IEnumerable<SensorValue> Values {
moel@159
   177
      get { return values; }
moel@159
   178
    }    
moel@110
   179
moel@110
   180
    public void Accept(IVisitor visitor) {
moel@167
   181
      if (visitor == null)
moel@167
   182
        throw new ArgumentNullException("visitor");
moel@167
   183
      visitor.VisitSensor(this);
moel@110
   184
    }
moel@110
   185
moel@110
   186
    public void Traverse(IVisitor visitor) {
moel@110
   187
      foreach (IParameter parameter in parameters)
moel@110
   188
        parameter.Accept(visitor);
moel@110
   189
    }
moel@247
   190
moel@247
   191
    public IControl Control {
moel@247
   192
      get {
moel@247
   193
        return control;
moel@247
   194
      }
moel@247
   195
      internal set {
moel@247
   196
        this.control = value;
moel@247
   197
      }
moel@247
   198
    }
moel@1
   199
  }
moel@1
   200
}