Hardware/Sensor.cs
author moel.mich
Fri, 05 Feb 2010 22:45:15 +0000
changeset 27 beed5a9e1b78
parent 1 361e324a0ed4
child 28 9b205b2ab056
permissions -rw-r--r--
Changed minimizing to system tray on Windows systems.
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@1
    62
moel@1
    63
    private string GetIdentifier() {
moel@27
    64
      return hardware.Identifier + "/" + sensorType.ToString().ToLower() + 
moel@27
    65
        "/" + index;
moel@1
    66
    }
moel@1
    67
moel@1
    68
    public Sensor(string name, int index, SensorType sensorType,
moel@1
    69
      IHardware hardware)
moel@1
    70
      : this(name, index, null, sensorType, hardware) { }
moel@1
    71
moel@1
    72
    public Sensor(string name, int index, float? limit, 
moel@1
    73
      SensorType sensorType, IHardware hardware) 
moel@1
    74
    {
moel@1
    75
      this.defaultName = name;      
moel@1
    76
      this.index = index;
moel@1
    77
      this.defaultLimit = limit;
moel@1
    78
      this.sensorType = sensorType;
moel@1
    79
      this.hardware = hardware;
moel@1
    80
      string configName = 
moel@1
    81
        Utilities.Config.Settings[GetIdentifier() + "/name"];
moel@1
    82
      if (configName != null)
moel@1
    83
        this.name = configName;
moel@1
    84
      else
moel@1
    85
        this.name = name;
moel@1
    86
      string configLimit =
moel@1
    87
        Utilities.Config.Settings[GetIdentifier() + "/limit"];
moel@1
    88
      if (configLimit != null && configLimit != "")
moel@1
    89
        this.limit = float.Parse(configLimit);
moel@1
    90
      else
moel@1
    91
        this.limit = limit;
moel@1
    92
    }
moel@1
    93
moel@1
    94
    public SensorType SensorType {
moel@1
    95
      get { return sensorType; }
moel@1
    96
    }
moel@1
    97
moel@1
    98
    public string Name {
moel@1
    99
      get { 
moel@1
   100
        return name; 
moel@1
   101
      }
moel@1
   102
      set {
moel@1
   103
        if (value != "") 
moel@1
   104
          name = value;          
moel@1
   105
        else 
moel@1
   106
          name = defaultName;
moel@1
   107
        Utilities.Config.Settings[GetIdentifier() + "/name"] = name;
moel@1
   108
      }
moel@1
   109
    }
moel@1
   110
moel@1
   111
    public int Index {
moel@1
   112
      get { return index; }
moel@1
   113
    }
moel@1
   114
moel@1
   115
    public float? Value {
moel@1
   116
      get { 
moel@1
   117
        return value; 
moel@1
   118
      }
moel@1
   119
      set {
moel@1
   120
        while (entries.Count > 0 && 
moel@1
   121
          (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
moel@1
   122
          entries.Dequeue();
moel@1
   123
moel@1
   124
        if (value.HasValue) {
moel@1
   125
          sum += value.Value;
moel@1
   126
          count++;
moel@1
   127
          if (count == 4) {
moel@1
   128
            entries.Enqueue(new Entry(sum / count, DateTime.Now));
moel@1
   129
            sum = 0;
moel@1
   130
            count = 0;
moel@1
   131
          }
moel@1
   132
        }
moel@1
   133
moel@1
   134
        this.value = value;
moel@1
   135
        if (min > value || !min.HasValue)
moel@1
   136
          min = value;
moel@1
   137
        if (max < value || !max.HasValue)
moel@1
   138
          max = value;
moel@1
   139
      }
moel@1
   140
    }
moel@1
   141
moel@1
   142
    public float? Min { get { return min; } }
moel@1
   143
    public float? Max { get { return max; } }
moel@1
   144
moel@1
   145
    public float? Limit {
moel@1
   146
      get {
moel@1
   147
        return limit;
moel@1
   148
      }
moel@1
   149
moel@1
   150
      set {
moel@1
   151
        if (value.HasValue) {
moel@1
   152
          limit = value;
moel@1
   153
          Utilities.Config.Settings[GetIdentifier() + "/limit"] =
moel@1
   154
            limit.ToString();
moel@1
   155
        } else {
moel@1
   156
          limit = defaultLimit;
moel@1
   157
          Utilities.Config.Settings[GetIdentifier() + "/limit"] = "";          
moel@1
   158
        }        
moel@1
   159
      }
moel@1
   160
    }
moel@1
   161
moel@1
   162
    public IEnumerable<ISensorEntry> Plot {
moel@1
   163
      get { return entries; }
moel@1
   164
    }
moel@1
   165
moel@1
   166
    public struct Entry : ISensorEntry {
moel@1
   167
      private float value;
moel@1
   168
      private DateTime time;
moel@1
   169
moel@1
   170
      public Entry(float value, DateTime time) {
moel@1
   171
        this.value = value;
moel@1
   172
        this.time = time;
moel@1
   173
      }
moel@1
   174
moel@1
   175
      public float Value { get { return value; } }
moel@1
   176
      public DateTime Time { get { return time; } }
moel@1
   177
    }
moel@1
   178
  }
moel@1
   179
}