Hardware/Sensor.cs
author moel.mich
Thu, 06 May 2010 19:20:38 +0000
changeset 109 70d0c3102424
parent 63 1a7c13ac7348
child 110 411b72b73d8f
permissions -rw-r--r--
Added an Identifier class for IHardware, ISensor and IParameter Identifier properties.
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 float? limit;
moel@1
    57
    private float? defaultLimit;
moel@1
    58
    private Queue<ISensorEntry> entries = 
moel@1
    59
      new Queue<ISensorEntry>(MAX_MINUTES * 15);
moel@1
    60
    
moel@1
    61
    private float sum = 0;
moel@1
    62
    private int count = 0;
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@63
    67
      IHardware hardware) : this(name, index, null, sensorType, hardware, 
moel@109
    68
      null) { }
moel@1
    69
moel@109
    70
    public Sensor(string name, int index, float? limit, SensorType sensorType,
moel@109
    71
      IHardware hardware, ParameterDescription[] parameterDescriptions) :
moel@109
    72
      this(name, index, false, limit, sensorType, hardware,
moel@109
    73
        parameterDescriptions) { }
moel@63
    74
moel@109
    75
    public Sensor(string name, int index, bool defaultHidden, 
moel@109
    76
      float? limit, SensorType sensorType, IHardware hardware, 
moel@109
    77
      ParameterDescription[] parameterDescriptions) 
moel@1
    78
    {
moel@1
    79
      this.defaultName = name;      
moel@1
    80
      this.index = index;
moel@109
    81
      this.defaultHidden = defaultHidden;
moel@1
    82
      this.defaultLimit = limit;
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@63
    88
        parameters[i] = new Parameter(parameterDescriptions[i], this);
moel@63
    89
      this.parameters = parameters;
moel@63
    90
moel@109
    91
      string configName = Config.Settings[
moel@109
    92
        new Identifier(Identifier, "name").ToString()];
moel@1
    93
      if (configName != null)
moel@1
    94
        this.name = configName;
moel@1
    95
      else
moel@1
    96
        this.name = name;
moel@109
    97
      string configLimit = Config.Settings[
moel@109
    98
        new Identifier(Identifier, "limit").ToString()];
moel@1
    99
      if (configLimit != null && configLimit != "")
moel@1
   100
        this.limit = float.Parse(configLimit);
moel@1
   101
      else
moel@1
   102
        this.limit = limit;
moel@1
   103
    }
moel@1
   104
moel@28
   105
    public IHardware Hardware {
moel@28
   106
      get { return hardware; }
moel@28
   107
    }
moel@28
   108
moel@1
   109
    public SensorType SensorType {
moel@1
   110
      get { return sensorType; }
moel@1
   111
    }
moel@1
   112
moel@109
   113
    public Identifier Identifier {
moel@28
   114
      get {
moel@109
   115
        return new Identifier(hardware.Identifier, 
moel@109
   116
          sensorType.ToString().ToLower(), index.ToString());
moel@28
   117
      }
moel@28
   118
    }
moel@28
   119
moel@1
   120
    public string Name {
moel@1
   121
      get { 
moel@1
   122
        return name; 
moel@1
   123
      }
moel@1
   124
      set {
moel@1
   125
        if (value != "") 
moel@1
   126
          name = value;          
moel@1
   127
        else 
moel@1
   128
          name = defaultName;
moel@109
   129
        Config.Settings[new Identifier(Identifier, "name").ToString()] = name;
moel@1
   130
      }
moel@1
   131
    }
moel@1
   132
moel@1
   133
    public int Index {
moel@1
   134
      get { return index; }
moel@1
   135
    }
moel@1
   136
moel@109
   137
    public bool IsDefaultHidden {
moel@109
   138
      get { return defaultHidden; }
moel@109
   139
    }
moel@109
   140
moel@63
   141
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   142
      get { return parameters; }
moel@63
   143
    }
moel@63
   144
moel@1
   145
    public float? Value {
moel@1
   146
      get { 
moel@1
   147
        return value; 
moel@1
   148
      }
moel@1
   149
      set {
moel@1
   150
        while (entries.Count > 0 && 
moel@1
   151
          (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1
   152
          entries.Dequeue();
moel@1
   153
moel@1
   154
        if (value.HasValue) {
moel@1
   155
          sum += value.Value;
moel@1
   156
          count++;
moel@1
   157
          if (count == 4) {
moel@1
   158
            entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1
   159
            sum = 0;
moel@1
   160
            count = 0;
moel@1
   161
          }
moel@1
   162
        }
moel@1
   163
moel@1
   164
        this.value = value;
moel@1
   165
        if (min > value || !min.HasValue)
moel@1
   166
          min = value;
moel@1
   167
        if (max < value || !max.HasValue)
moel@1
   168
          max = value;
moel@1
   169
      }
moel@1
   170
    }
moel@1
   171
moel@1
   172
    public float? Min { get { return min; } }
moel@1
   173
    public float? Max { get { return max; } }
moel@1
   174
moel@1
   175
    public float? Limit {
moel@1
   176
      get {
moel@1
   177
        return limit;
moel@1
   178
      }
moel@1
   179
moel@1
   180
      set {
moel@1
   181
        if (value.HasValue) {
moel@1
   182
          limit = value;
moel@109
   183
          Config.Settings[new Identifier(Identifier, "limit").ToString()] =
moel@1
   184
            limit.ToString();
moel@1
   185
        } else {
moel@1
   186
          limit = defaultLimit;
moel@109
   187
          Config.Settings[new Identifier(Identifier, "limit").ToString()] = "";          
moel@1
   188
        }        
moel@1
   189
      }
moel@1
   190
    }
moel@1
   191
moel@1
   192
    public IEnumerable<ISensorEntry> Plot {
moel@1
   193
      get { return entries; }
moel@1
   194
    }
moel@1
   195
moel@1
   196
    public struct Entry : ISensorEntry {
moel@1
   197
      private float value;
moel@1
   198
      private DateTime time;
moel@1
   199
moel@1
   200
      public Entry(float value, DateTime time) {
moel@1
   201
        this.value = value;
moel@1
   202
        this.time = time;
moel@1
   203
      }
moel@1
   204
moel@1
   205
      public float Value { get { return value; } }
moel@1
   206
      public DateTime Time { get { return time; } }
moel@1
   207
    }
moel@1
   208
  }
moel@1
   209
}