Hardware/Sensor.cs
author moel.mich
Sun, 08 Aug 2010 13:57:26 +0000
changeset 165 813d8bc3192f
parent 159 eda3e3458cf4
child 166 fa9dfbfc4145
permissions -rw-r--r--
Refactored the hardware monitoring code into a library (Issue 101).
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@165
    40
using OpenHardwareMonitor.Collections;
moel@1
    41
moel@1
    42
namespace OpenHardwareMonitor.Hardware {
moel@1
    43
moel@165
    44
  internal class Sensor : ISensor {
moel@1
    45
moel@1
    46
    private string defaultName;
moel@1
    47
    private string name;
moel@1
    48
    private int index;
moel@109
    49
    private bool defaultHidden;
moel@1
    50
    private SensorType sensorType;
moel@1
    51
    private IHardware hardware;
moel@63
    52
    private ReadOnlyArray<IParameter> parameters;
moel@1
    53
    private float? value;
moel@1
    54
    private float? min;
moel@1
    55
    private float? max;
moel@159
    56
    private Queue<SensorValue> values =
moel@159
    57
      new Queue<SensorValue>(MAX_MINUTES * 15);
moel@165
    58
    private ISettings settings;
moel@1
    59
    
moel@1
    60
    private float sum = 0;
moel@1
    61
    private int count = 0;
moel@1
    62
moel@1
    63
    private const int MAX_MINUTES = 120;
moel@28
    64
   
moel@1
    65
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    66
      IHardware hardware, ISettings settings) : 
moel@165
    67
      this(name, index, sensorType, hardware, null, settings) { }
moel@1
    68
moel@134
    69
    public Sensor(string name, int index, SensorType sensorType,
moel@165
    70
      IHardware hardware, ParameterDescription[] parameterDescriptions, 
moel@165
    71
      ISettings settings) :
moel@134
    72
      this(name, index, false, sensorType, hardware,
moel@165
    73
        parameterDescriptions, settings) { }
moel@63
    74
moel@109
    75
    public Sensor(string name, int index, bool defaultHidden, 
moel@134
    76
      SensorType sensorType, IHardware hardware, 
moel@165
    77
      ParameterDescription[] parameterDescriptions, ISettings settings) 
moel@165
    78
    {           
moel@1
    79
      this.index = index;
moel@109
    80
      this.defaultHidden = defaultHidden;
moel@1
    81
      this.sensorType = sensorType;
moel@1
    82
      this.hardware = hardware;
moel@109
    83
      Parameter[] parameters = new Parameter[parameterDescriptions == null ?
moel@109
    84
        0 : parameterDescriptions.Length];
moel@63
    85
      for (int i = 0; i < parameters.Length; i++ ) 
moel@165
    86
        parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
moel@63
    87
      this.parameters = parameters;
moel@63
    88
moel@165
    89
      this.settings = settings;
moel@165
    90
      this.defaultName = name; 
moel@165
    91
      this.name = settings.Get(
moel@165
    92
        new Identifier(Identifier, "name").ToString(), name);
moel@1
    93
    }
moel@1
    94
moel@28
    95
    public IHardware Hardware {
moel@28
    96
      get { return hardware; }
moel@28
    97
    }
moel@28
    98
moel@1
    99
    public SensorType SensorType {
moel@1
   100
      get { return sensorType; }
moel@1
   101
    }
moel@1
   102
moel@109
   103
    public Identifier Identifier {
moel@28
   104
      get {
moel@109
   105
        return new Identifier(hardware.Identifier, 
moel@109
   106
          sensorType.ToString().ToLower(), index.ToString());
moel@28
   107
      }
moel@28
   108
    }
moel@28
   109
moel@1
   110
    public string Name {
moel@1
   111
      get { 
moel@1
   112
        return name; 
moel@1
   113
      }
moel@1
   114
      set {
moel@1
   115
        if (value != "") 
moel@1
   116
          name = value;          
moel@1
   117
        else 
moel@1
   118
          name = defaultName;
moel@165
   119
        settings.Set(new Identifier(Identifier, "name").ToString(), name);
moel@1
   120
      }
moel@1
   121
    }
moel@1
   122
moel@1
   123
    public int Index {
moel@1
   124
      get { return index; }
moel@1
   125
    }
moel@1
   126
moel@109
   127
    public bool IsDefaultHidden {
moel@109
   128
      get { return defaultHidden; }
moel@109
   129
    }
moel@109
   130
moel@63
   131
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   132
      get { return parameters; }
moel@63
   133
    }
moel@63
   134
moel@1
   135
    public float? Value {
moel@1
   136
      get { 
moel@1
   137
        return value; 
moel@1
   138
      }
moel@1
   139
      set {
moel@159
   140
        while (values.Count > 0 && 
moel@159
   141
          (DateTime.Now - values.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@159
   142
          values.Dequeue();
moel@1
   143
moel@1
   144
        if (value.HasValue) {
moel@1
   145
          sum += value.Value;
moel@1
   146
          count++;
moel@1
   147
          if (count == 4) {
moel@159
   148
            values.Enqueue(new SensorValue(sum / count, DateTime.Now));
moel@1
   149
            sum = 0;
moel@1
   150
            count = 0;
moel@1
   151
          }
moel@1
   152
        }
moel@1
   153
moel@1
   154
        this.value = value;
moel@1
   155
        if (min > value || !min.HasValue)
moel@1
   156
          min = value;
moel@1
   157
        if (max < value || !max.HasValue)
moel@1
   158
          max = value;
moel@1
   159
      }
moel@1
   160
    }
moel@1
   161
moel@1
   162
    public float? Min { get { return min; } }
moel@1
   163
    public float? Max { get { return max; } }
moel@1
   164
moel@151
   165
    public void ResetMin() {
moel@151
   166
      min = null;
moel@151
   167
    }
moel@151
   168
moel@151
   169
    public void ResetMax() {
moel@151
   170
      max = null;
moel@151
   171
    }
moel@151
   172
moel@159
   173
    public IEnumerable<SensorValue> Values {
moel@159
   174
      get { return values; }
moel@159
   175
    }    
moel@110
   176
moel@110
   177
    public void Accept(IVisitor visitor) {
moel@110
   178
      visitor.VisitSensor(this);
moel@110
   179
    }
moel@110
   180
moel@110
   181
    public void Traverse(IVisitor visitor) {
moel@110
   182
      foreach (IParameter parameter in parameters)
moel@110
   183
        parameter.Accept(visitor);
moel@110
   184
    }
moel@1
   185
  }
moel@1
   186
}