Hardware/Sensor.cs
author moel.mich
Sun, 11 Jul 2010 15:52:50 +0000
changeset 152 98187e7a08b2
parent 134 8b3b9b2e28e5
child 159 eda3e3458cf4
permissions -rw-r--r--
An attempt at adding a mainboard specific configuration for the ASUS P6X58D-E.
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@63
    40
using OpenHardwareMonitor.Utilities;
moel@1
    41
moel@1
    42
namespace OpenHardwareMonitor.Hardware {
moel@1
    43
moel@1
    44
  public 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@1
    56
    private Queue<ISensorEntry> entries = 
moel@1
    57
      new Queue<ISensorEntry>(MAX_MINUTES * 15);
moel@1
    58
    
moel@1
    59
    private float sum = 0;
moel@1
    60
    private int count = 0;
moel@1
    61
moel@1
    62
    private const int MAX_MINUTES = 120;
moel@28
    63
   
moel@1
    64
    public Sensor(string name, int index, SensorType sensorType,
moel@134
    65
      IHardware hardware) : this(name, index, sensorType, hardware, 
moel@109
    66
      null) { }
moel@1
    67
moel@134
    68
    public Sensor(string name, int index, SensorType sensorType,
moel@109
    69
      IHardware hardware, ParameterDescription[] parameterDescriptions) :
moel@134
    70
      this(name, index, false, sensorType, hardware,
moel@109
    71
        parameterDescriptions) { }
moel@63
    72
moel@109
    73
    public Sensor(string name, int index, bool defaultHidden, 
moel@134
    74
      SensorType sensorType, IHardware hardware, 
moel@109
    75
      ParameterDescription[] parameterDescriptions) 
moel@1
    76
    {
moel@1
    77
      this.defaultName = name;      
moel@1
    78
      this.index = index;
moel@109
    79
      this.defaultHidden = defaultHidden;
moel@1
    80
      this.sensorType = sensorType;
moel@1
    81
      this.hardware = hardware;
moel@109
    82
      Parameter[] parameters = new Parameter[parameterDescriptions == null ?
moel@109
    83
        0 : parameterDescriptions.Length];
moel@63
    84
      for (int i = 0; i < parameters.Length; i++ ) 
moel@63
    85
        parameters[i] = new Parameter(parameterDescriptions[i], this);
moel@63
    86
      this.parameters = parameters;
moel@63
    87
moel@109
    88
      string configName = Config.Settings[
moel@109
    89
        new Identifier(Identifier, "name").ToString()];
moel@1
    90
      if (configName != null)
moel@1
    91
        this.name = configName;
moel@1
    92
      else
moel@1
    93
        this.name = 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@109
   106
        return new Identifier(hardware.Identifier, 
moel@109
   107
          sensorType.ToString().ToLower(), index.ToString());
moel@28
   108
      }
moel@28
   109
    }
moel@28
   110
moel@1
   111
    public string Name {
moel@1
   112
      get { 
moel@1
   113
        return name; 
moel@1
   114
      }
moel@1
   115
      set {
moel@1
   116
        if (value != "") 
moel@1
   117
          name = value;          
moel@1
   118
        else 
moel@1
   119
          name = defaultName;
moel@109
   120
        Config.Settings[new Identifier(Identifier, "name").ToString()] = name;
moel@1
   121
      }
moel@1
   122
    }
moel@1
   123
moel@1
   124
    public int Index {
moel@1
   125
      get { return index; }
moel@1
   126
    }
moel@1
   127
moel@109
   128
    public bool IsDefaultHidden {
moel@109
   129
      get { return defaultHidden; }
moel@109
   130
    }
moel@109
   131
moel@63
   132
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   133
      get { return parameters; }
moel@63
   134
    }
moel@63
   135
moel@1
   136
    public float? Value {
moel@1
   137
      get { 
moel@1
   138
        return value; 
moel@1
   139
      }
moel@1
   140
      set {
moel@1
   141
        while (entries.Count > 0 && 
moel@1
   142
          (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1
   143
          entries.Dequeue();
moel@1
   144
moel@1
   145
        if (value.HasValue) {
moel@1
   146
          sum += value.Value;
moel@1
   147
          count++;
moel@1
   148
          if (count == 4) {
moel@1
   149
            entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1
   150
            sum = 0;
moel@1
   151
            count = 0;
moel@1
   152
          }
moel@1
   153
        }
moel@1
   154
moel@1
   155
        this.value = value;
moel@1
   156
        if (min > value || !min.HasValue)
moel@1
   157
          min = value;
moel@1
   158
        if (max < value || !max.HasValue)
moel@1
   159
          max = value;
moel@1
   160
      }
moel@1
   161
    }
moel@1
   162
moel@1
   163
    public float? Min { get { return min; } }
moel@1
   164
    public float? Max { get { return max; } }
moel@1
   165
moel@151
   166
    public void ResetMin() {
moel@151
   167
      min = null;
moel@151
   168
    }
moel@151
   169
moel@151
   170
    public void ResetMax() {
moel@151
   171
      max = null;
moel@151
   172
    }
moel@151
   173
moel@1
   174
    public IEnumerable<ISensorEntry> Plot {
moel@1
   175
      get { return entries; }
moel@1
   176
    }
moel@1
   177
moel@1
   178
    public struct Entry : ISensorEntry {
moel@1
   179
      private float value;
moel@1
   180
      private DateTime time;
moel@1
   181
moel@1
   182
      public Entry(float value, DateTime time) {
moel@1
   183
        this.value = value;
moel@1
   184
        this.time = time;
moel@1
   185
      }
moel@1
   186
moel@1
   187
      public float Value { get { return value; } }
moel@1
   188
      public DateTime Time { get { return time; } }
moel@1
   189
    }
moel@110
   190
moel@110
   191
    public void Accept(IVisitor visitor) {
moel@110
   192
      visitor.VisitSensor(this);
moel@110
   193
    }
moel@110
   194
moel@110
   195
    public void Traverse(IVisitor visitor) {
moel@110
   196
      foreach (IParameter parameter in parameters)
moel@110
   197
        parameter.Accept(visitor);
moel@110
   198
    }
moel@1
   199
  }
moel@1
   200
}