Hardware/Sensor.cs
author moel.mich
Tue, 27 Apr 2010 18:43:56 +0000
changeset 103 0845adc5402e
parent 28 9b205b2ab056
child 109 70d0c3102424
permissions -rw-r--r--
Added support for F71889ED super I/O chips.
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@1
    49
    private SensorType sensorType;
moel@1
    50
    private IHardware hardware;
moel@63
    51
    private ReadOnlyArray<IParameter> parameters;
moel@1
    52
    private float? value;
moel@1
    53
    private float? min;
moel@1
    54
    private float? max;
moel@1
    55
    private float? limit;
moel@1
    56
    private float? defaultLimit;
moel@1
    57
    private Queue<ISensorEntry> entries = 
moel@1
    58
      new Queue<ISensorEntry>(MAX_MINUTES * 15);
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@63
    66
      IHardware hardware) : this(name, index, null, sensorType, hardware, 
moel@63
    67
      new ParameterDescription[0]) { }
moel@1
    68
moel@63
    69
    public Sensor(string name, int index, float? limit,
moel@63
    70
      SensorType sensorType, IHardware hardware) : this(name, index, limit, 
moel@63
    71
      sensorType, hardware, new ParameterDescription[0]) { }    
moel@63
    72
moel@63
    73
    public Sensor(string name, int index, float? limit, SensorType sensorType, 
moel@63
    74
      IHardware hardware, ParameterDescription[] parameterDescriptions) 
moel@1
    75
    {
moel@1
    76
      this.defaultName = name;      
moel@1
    77
      this.index = index;
moel@1
    78
      this.defaultLimit = limit;
moel@1
    79
      this.sensorType = sensorType;
moel@1
    80
      this.hardware = hardware;
moel@63
    81
      Parameter[] parameters = new Parameter[parameterDescriptions.Length];
moel@63
    82
      for (int i = 0; i < parameters.Length; i++ ) 
moel@63
    83
        parameters[i] = new Parameter(parameterDescriptions[i], this);
moel@63
    84
      this.parameters = parameters;
moel@63
    85
moel@63
    86
      string configName = Config.Settings[Identifier + "/name"];
moel@1
    87
      if (configName != null)
moel@1
    88
        this.name = configName;
moel@1
    89
      else
moel@1
    90
        this.name = name;
moel@63
    91
      string configLimit = Config.Settings[Identifier + "/limit"];
moel@1
    92
      if (configLimit != null && configLimit != "")
moel@1
    93
        this.limit = float.Parse(configLimit);
moel@1
    94
      else
moel@1
    95
        this.limit = limit;
moel@1
    96
    }
moel@1
    97
moel@28
    98
    public IHardware Hardware {
moel@28
    99
      get { return hardware; }
moel@28
   100
    }
moel@28
   101
moel@1
   102
    public SensorType SensorType {
moel@1
   103
      get { return sensorType; }
moel@1
   104
    }
moel@1
   105
moel@28
   106
    public string Identifier {
moel@28
   107
      get {
moel@28
   108
        return hardware.Identifier + "/" + sensorType.ToString().ToLower() +
moel@28
   109
          "/" + index;
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@1
   118
        if (value != "") 
moel@1
   119
          name = value;          
moel@1
   120
        else 
moel@1
   121
          name = defaultName;
moel@63
   122
        Config.Settings[Identifier + "/name"] = 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@63
   130
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   131
      get { return parameters; }
moel@63
   132
    }
moel@63
   133
moel@1
   134
    public float? Value {
moel@1
   135
      get { 
moel@1
   136
        return value; 
moel@1
   137
      }
moel@1
   138
      set {
moel@1
   139
        while (entries.Count > 0 && 
moel@1
   140
          (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1
   141
          entries.Dequeue();
moel@1
   142
moel@1
   143
        if (value.HasValue) {
moel@1
   144
          sum += value.Value;
moel@1
   145
          count++;
moel@1
   146
          if (count == 4) {
moel@1
   147
            entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1
   148
            sum = 0;
moel@1
   149
            count = 0;
moel@1
   150
          }
moel@1
   151
        }
moel@1
   152
moel@1
   153
        this.value = value;
moel@1
   154
        if (min > value || !min.HasValue)
moel@1
   155
          min = value;
moel@1
   156
        if (max < value || !max.HasValue)
moel@1
   157
          max = value;
moel@1
   158
      }
moel@1
   159
    }
moel@1
   160
moel@1
   161
    public float? Min { get { return min; } }
moel@1
   162
    public float? Max { get { return max; } }
moel@1
   163
moel@1
   164
    public float? Limit {
moel@1
   165
      get {
moel@1
   166
        return limit;
moel@1
   167
      }
moel@1
   168
moel@1
   169
      set {
moel@1
   170
        if (value.HasValue) {
moel@1
   171
          limit = value;
moel@63
   172
          Config.Settings[Identifier + "/limit"] =
moel@1
   173
            limit.ToString();
moel@1
   174
        } else {
moel@1
   175
          limit = defaultLimit;
moel@63
   176
          Config.Settings[Identifier + "/limit"] = "";          
moel@1
   177
        }        
moel@1
   178
      }
moel@1
   179
    }
moel@1
   180
moel@1
   181
    public IEnumerable<ISensorEntry> Plot {
moel@1
   182
      get { return entries; }
moel@1
   183
    }
moel@1
   184
moel@1
   185
    public struct Entry : ISensorEntry {
moel@1
   186
      private float value;
moel@1
   187
      private DateTime time;
moel@1
   188
moel@1
   189
      public Entry(float value, DateTime time) {
moel@1
   190
        this.value = value;
moel@1
   191
        this.time = time;
moel@1
   192
      }
moel@1
   193
moel@1
   194
      public float Value { get { return value; } }
moel@1
   195
      public DateTime Time { get { return time; } }
moel@1
   196
    }
moel@1
   197
  }
moel@1
   198
}