Hardware/Sensor.cs
author moel.mich
Tue, 02 Mar 2010 22:26:07 +0000
changeset 69 5f539c00e925
parent 28 9b205b2ab056
child 109 70d0c3102424
permissions -rw-r--r--
Reading TjMax for each core on Intel Core i3/i5/i7 CPUs.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using OpenHardwareMonitor.Utilities;
    41 
    42 namespace OpenHardwareMonitor.Hardware {
    43 
    44   public class Sensor : ISensor {
    45 
    46     private string defaultName;
    47     private string name;
    48     private int index;
    49     private SensorType sensorType;
    50     private IHardware hardware;
    51     private ReadOnlyArray<IParameter> parameters;
    52     private float? value;
    53     private float? min;
    54     private float? max;
    55     private float? limit;
    56     private float? defaultLimit;
    57     private Queue<ISensorEntry> entries = 
    58       new Queue<ISensorEntry>(MAX_MINUTES * 15);
    59     
    60     private float sum = 0;
    61     private int count = 0;
    62 
    63     private const int MAX_MINUTES = 120;
    64    
    65     public Sensor(string name, int index, SensorType sensorType,
    66       IHardware hardware) : this(name, index, null, sensorType, hardware, 
    67       new ParameterDescription[0]) { }
    68 
    69     public Sensor(string name, int index, float? limit,
    70       SensorType sensorType, IHardware hardware) : this(name, index, limit, 
    71       sensorType, hardware, new ParameterDescription[0]) { }    
    72 
    73     public Sensor(string name, int index, float? limit, SensorType sensorType, 
    74       IHardware hardware, ParameterDescription[] parameterDescriptions) 
    75     {
    76       this.defaultName = name;      
    77       this.index = index;
    78       this.defaultLimit = limit;
    79       this.sensorType = sensorType;
    80       this.hardware = hardware;
    81       Parameter[] parameters = new Parameter[parameterDescriptions.Length];
    82       for (int i = 0; i < parameters.Length; i++ ) 
    83         parameters[i] = new Parameter(parameterDescriptions[i], this);
    84       this.parameters = parameters;
    85 
    86       string configName = Config.Settings[Identifier + "/name"];
    87       if (configName != null)
    88         this.name = configName;
    89       else
    90         this.name = name;
    91       string configLimit = Config.Settings[Identifier + "/limit"];
    92       if (configLimit != null && configLimit != "")
    93         this.limit = float.Parse(configLimit);
    94       else
    95         this.limit = limit;
    96     }
    97 
    98     public IHardware Hardware {
    99       get { return hardware; }
   100     }
   101 
   102     public SensorType SensorType {
   103       get { return sensorType; }
   104     }
   105 
   106     public string Identifier {
   107       get {
   108         return hardware.Identifier + "/" + sensorType.ToString().ToLower() +
   109           "/" + index;
   110       }
   111     }
   112 
   113     public string Name {
   114       get { 
   115         return name; 
   116       }
   117       set {
   118         if (value != "") 
   119           name = value;          
   120         else 
   121           name = defaultName;
   122         Config.Settings[Identifier + "/name"] = name;
   123       }
   124     }
   125 
   126     public int Index {
   127       get { return index; }
   128     }
   129 
   130     public IReadOnlyArray<IParameter> Parameters {
   131       get { return parameters; }
   132     }
   133 
   134     public float? Value {
   135       get { 
   136         return value; 
   137       }
   138       set {
   139         while (entries.Count > 0 && 
   140           (DateTime.Now - entries.Peek().Time).TotalMinutes > MAX_MINUTES)
   141           entries.Dequeue();
   142 
   143         if (value.HasValue) {
   144           sum += value.Value;
   145           count++;
   146           if (count == 4) {
   147             entries.Enqueue(new Entry(sum / count, DateTime.Now));
   148             sum = 0;
   149             count = 0;
   150           }
   151         }
   152 
   153         this.value = value;
   154         if (min > value || !min.HasValue)
   155           min = value;
   156         if (max < value || !max.HasValue)
   157           max = value;
   158       }
   159     }
   160 
   161     public float? Min { get { return min; } }
   162     public float? Max { get { return max; } }
   163 
   164     public float? Limit {
   165       get {
   166         return limit;
   167       }
   168 
   169       set {
   170         if (value.HasValue) {
   171           limit = value;
   172           Config.Settings[Identifier + "/limit"] =
   173             limit.ToString();
   174         } else {
   175           limit = defaultLimit;
   176           Config.Settings[Identifier + "/limit"] = "";          
   177         }        
   178       }
   179     }
   180 
   181     public IEnumerable<ISensorEntry> Plot {
   182       get { return entries; }
   183     }
   184 
   185     public struct Entry : ISensorEntry {
   186       private float value;
   187       private DateTime time;
   188 
   189       public Entry(float value, DateTime time) {
   190         this.value = value;
   191         this.time = time;
   192       }
   193 
   194       public float Value { get { return value; } }
   195       public DateTime Time { get { return time; } }
   196     }
   197   }
   198 }