Hardware/Sensor.cs
author moel.mich
Tue, 16 Feb 2010 21:44:25 +0000
changeset 47 1d02fde1bb23
parent 27 beed5a9e1b78
child 63 1a7c13ac7348
permissions -rw-r--r--
Restored CTSHolding check and added more report output for T-Balancer enumeration.
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@1
    40
moel@1
    41
namespace OpenHardwareMonitor.Hardware {
moel@1
    42
moel@1
    43
  public class Sensor : ISensor {
moel@1
    44
moel@1
    45
    private string defaultName;
moel@1
    46
    private string name;
moel@1
    47
    private int index;
moel@1
    48
    private SensorType sensorType;
moel@1
    49
    private IHardware hardware;
moel@1
    50
    private float? value;
moel@1
    51
    private float? min;
moel@1
    52
    private float? max;
moel@1
    53
    private float? limit;
moel@1
    54
    private float? defaultLimit;
moel@1
    55
    private Queue<ISensorEntry> entries = 
moel@1
    56
      new Queue<ISensorEntry>(MAX_MINUTES * 15);
moel@1
    57
    
moel@1
    58
    private float sum = 0;
moel@1
    59
    private int count = 0;
moel@1
    60
moel@1
    61
    private const int MAX_MINUTES = 120;
moel@28
    62
   
moel@1
    63
    public Sensor(string name, int index, SensorType sensorType,
moel@1
    64
      IHardware hardware)
moel@1
    65
      : this(name, index, null, sensorType, hardware) { }
moel@1
    66
moel@1
    67
    public Sensor(string name, int index, float? limit, 
moel@1
    68
      SensorType sensorType, IHardware hardware) 
moel@1
    69
    {
moel@1
    70
      this.defaultName = name;      
moel@1
    71
      this.index = index;
moel@1
    72
      this.defaultLimit = limit;
moel@1
    73
      this.sensorType = sensorType;
moel@1
    74
      this.hardware = hardware;
moel@28
    75
      string configName =
moel@28
    76
        Utilities.Config.Settings[Identifier + "/name"];
moel@1
    77
      if (configName != null)
moel@1
    78
        this.name = configName;
moel@1
    79
      else
moel@1
    80
        this.name = name;
moel@1
    81
      string configLimit =
moel@28
    82
        Utilities.Config.Settings[Identifier + "/limit"];
moel@1
    83
      if (configLimit != null && configLimit != "")
moel@1
    84
        this.limit = float.Parse(configLimit);
moel@1
    85
      else
moel@1
    86
        this.limit = limit;
moel@1
    87
    }
moel@1
    88
moel@28
    89
    public IHardware Hardware {
moel@28
    90
      get { return hardware; }
moel@28
    91
    }
moel@28
    92
moel@1
    93
    public SensorType SensorType {
moel@1
    94
      get { return sensorType; }
moel@1
    95
    }
moel@1
    96
moel@28
    97
    public string Identifier {
moel@28
    98
      get {
moel@28
    99
        return hardware.Identifier + "/" + sensorType.ToString().ToLower() +
moel@28
   100
          "/" + index;
moel@28
   101
      }
moel@28
   102
    }
moel@28
   103
moel@1
   104
    public string Name {
moel@1
   105
      get { 
moel@1
   106
        return name; 
moel@1
   107
      }
moel@1
   108
      set {
moel@1
   109
        if (value != "") 
moel@1
   110
          name = value;          
moel@1
   111
        else 
moel@1
   112
          name = defaultName;
moel@28
   113
        Utilities.Config.Settings[Identifier + "/name"] = name;
moel@1
   114
      }
moel@1
   115
    }
moel@1
   116
moel@1
   117
    public int Index {
moel@1
   118
      get { return index; }
moel@1
   119
    }
moel@1
   120
moel@1
   121
    public float? Value {
moel@1
   122
      get { 
moel@1
   123
        return value; 
moel@1
   124
      }
moel@1
   125
      set {
moel@1
   126
        while (entries.Count > 0 && 
moel@1
   127
          (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1
   128
          entries.Dequeue();
moel@1
   129
moel@1
   130
        if (value.HasValue) {
moel@1
   131
          sum += value.Value;
moel@1
   132
          count++;
moel@1
   133
          if (count == 4) {
moel@1
   134
            entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1
   135
            sum = 0;
moel@1
   136
            count = 0;
moel@1
   137
          }
moel@1
   138
        }
moel@1
   139
moel@1
   140
        this.value = value;
moel@1
   141
        if (min > value || !min.HasValue)
moel@1
   142
          min = value;
moel@1
   143
        if (max < value || !max.HasValue)
moel@1
   144
          max = value;
moel@1
   145
      }
moel@1
   146
    }
moel@1
   147
moel@1
   148
    public float? Min { get { return min; } }
moel@1
   149
    public float? Max { get { return max; } }
moel@1
   150
moel@1
   151
    public float? Limit {
moel@1
   152
      get {
moel@1
   153
        return limit;
moel@1
   154
      }
moel@1
   155
moel@1
   156
      set {
moel@1
   157
        if (value.HasValue) {
moel@1
   158
          limit = value;
moel@28
   159
          Utilities.Config.Settings[Identifier + "/limit"] =
moel@1
   160
            limit.ToString();
moel@1
   161
        } else {
moel@1
   162
          limit = defaultLimit;
moel@28
   163
          Utilities.Config.Settings[Identifier + "/limit"] = "";          
moel@1
   164
        }        
moel@1
   165
      }
moel@1
   166
    }
moel@1
   167
moel@1
   168
    public IEnumerable<ISensorEntry> Plot {
moel@1
   169
      get { return entries; }
moel@1
   170
    }
moel@1
   171
moel@1
   172
    public struct Entry : ISensorEntry {
moel@1
   173
      private float value;
moel@1
   174
      private DateTime time;
moel@1
   175
moel@1
   176
      public Entry(float value, DateTime time) {
moel@1
   177
        this.value = value;
moel@1
   178
        this.time = time;
moel@1
   179
      }
moel@1
   180
moel@1
   181
      public float Value { get { return value; } }
moel@1
   182
      public DateTime Time { get { return time; } }
moel@1
   183
    }
moel@1
   184
  }
moel@1
   185
}