Hardware/Sensor.cs
author moel.mich
Sun, 08 Jul 2012 19:26:00 +0000
changeset 359 30bb76a1da24
parent 344 3145aadca3d2
child 365 a8a8ff22d959
permissions -rw-r--r--
Added a mainboard specific configuration for the Asus P8Z77-V (Issue 347) and added an additional temperature for the Gigabyte GA-MA78LM-S2H (Issue 358).
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@344
     7
  Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@1
    12
using System.Collections.Generic;
moel@166
    13
using System.Globalization;
moel@298
    14
using System.IO;
moel@298
    15
using System.IO.Compression;
moel@165
    16
using OpenHardwareMonitor.Collections;
moel@1
    17
moel@1
    18
namespace OpenHardwareMonitor.Hardware {
moel@1
    19
moel@165
    20
  internal class Sensor : ISensor {
moel@1
    21
moel@195
    22
    private readonly string defaultName;
moel@1
    23
    private string name;
moel@195
    24
    private readonly int index;
moel@195
    25
    private readonly bool defaultHidden;
moel@195
    26
    private readonly SensorType sensorType;
moel@298
    27
    private readonly Hardware hardware;
moel@195
    28
    private readonly ReadOnlyArray<IParameter> parameters;
moel@167
    29
    private float? currentValue;
moel@167
    30
    private float? minValue;
moel@167
    31
    private float? maxValue;
moel@298
    32
    private readonly RingCollection<SensorValue> 
moel@298
    33
      values = new RingCollection<SensorValue>();
moel@195
    34
    private readonly ISettings settings;
moel@247
    35
    private IControl control;
moel@1
    36
    
moel@195
    37
    private float sum;
moel@195
    38
    private int count;
moel@28
    39
   
moel@1
    40
    public Sensor(string name, int index, SensorType sensorType,
moel@298
    41
      Hardware hardware, ISettings settings) : 
moel@165
    42
      this(name, index, sensorType, hardware, null, settings) { }
moel@1
    43
moel@134
    44
    public Sensor(string name, int index, SensorType sensorType,
moel@298
    45
      Hardware hardware, ParameterDescription[] parameterDescriptions, 
moel@165
    46
      ISettings settings) :
moel@134
    47
      this(name, index, false, sensorType, hardware,
moel@165
    48
        parameterDescriptions, settings) { }
moel@63
    49
moel@109
    50
    public Sensor(string name, int index, bool defaultHidden, 
moel@298
    51
      SensorType sensorType, Hardware hardware, 
moel@165
    52
      ParameterDescription[] parameterDescriptions, ISettings settings) 
moel@165
    53
    {           
moel@1
    54
      this.index = index;
moel@109
    55
      this.defaultHidden = defaultHidden;
moel@1
    56
      this.sensorType = sensorType;
moel@1
    57
      this.hardware = hardware;
moel@109
    58
      Parameter[] parameters = new Parameter[parameterDescriptions == null ?
moel@109
    59
        0 : parameterDescriptions.Length];
moel@63
    60
      for (int i = 0; i < parameters.Length; i++ ) 
moel@165
    61
        parameters[i] = new Parameter(parameterDescriptions[i], this, settings);
moel@63
    62
      this.parameters = parameters;
moel@63
    63
moel@165
    64
      this.settings = settings;
moel@165
    65
      this.defaultName = name; 
moel@166
    66
      this.name = settings.GetValue(
moel@165
    67
        new Identifier(Identifier, "name").ToString(), name);
moel@298
    68
moel@358
    69
      GetSensorValuesFromSettings();      
moel@298
    70
moel@298
    71
      hardware.Closing += delegate(IHardware h) {
moel@298
    72
        SetSensorValuesToSettings();
moel@298
    73
      };
moel@298
    74
    }
moel@298
    75
moel@298
    76
    private void SetSensorValuesToSettings() {
moel@298
    77
      using (MemoryStream m = new MemoryStream()) {
moel@298
    78
        using (GZipStream c = new GZipStream(m, CompressionMode.Compress))
moel@298
    79
        using (BinaryWriter writer = new BinaryWriter(c)) {
moel@298
    80
          foreach (SensorValue sensorValue in values) {
moel@298
    81
            writer.Write(sensorValue.Time.ToBinary());
moel@298
    82
            writer.Write(sensorValue.Value);
moel@298
    83
          }
moel@298
    84
        }
moel@298
    85
        settings.SetValue(new Identifier(Identifier, "values").ToString(),
moel@298
    86
           Convert.ToBase64String(m.ToArray()));
moel@298
    87
      }
moel@298
    88
    }
moel@298
    89
moel@298
    90
    private void GetSensorValuesFromSettings() {
moel@358
    91
      string name = new Identifier(Identifier, "values").ToString();
moel@358
    92
      string s = settings.GetValue(name, null);
moel@298
    93
moel@298
    94
      try {
moel@358
    95
        byte[] array = Convert.FromBase64String(s);
moel@358
    96
        s = null;
moel@298
    97
        using (MemoryStream m = new MemoryStream(array))
moel@298
    98
        using (GZipStream c = new GZipStream(m, CompressionMode.Decompress))
moel@298
    99
        using (BinaryReader reader = new BinaryReader(c)) {
moel@298
   100
          try {
moel@298
   101
            while (true) {
moel@298
   102
              DateTime time = DateTime.FromBinary(reader.ReadInt64());
moel@298
   103
              float value = reader.ReadSingle();
moel@298
   104
              AppendValue(value, time);
moel@298
   105
            }
moel@298
   106
          } catch (EndOfStreamException) { }
moel@298
   107
        }
moel@298
   108
      } catch { }
moel@298
   109
      if (values.Count > 0)
moel@314
   110
        AppendValue(float.NaN, DateTime.UtcNow);
moel@358
   111
moel@358
   112
      // remove the value string from the settings to reduce memory usage
moel@358
   113
      settings.Remove(name);
moel@298
   114
    }
moel@298
   115
moel@298
   116
    private void AppendValue(float value, DateTime time) {
moel@298
   117
      if (values.Count >= 2 && values.Last.Value == value && 
moel@298
   118
        values[values.Count - 2].Value == value) {
moel@298
   119
        values.Last = new SensorValue(value, time);
moel@298
   120
        return;
moel@298
   121
      } 
moel@298
   122
moel@298
   123
      values.Append(new SensorValue(value, time));
moel@1
   124
    }
moel@1
   125
moel@28
   126
    public IHardware Hardware {
moel@28
   127
      get { return hardware; }
moel@28
   128
    }
moel@28
   129
moel@1
   130
    public SensorType SensorType {
moel@1
   131
      get { return sensorType; }
moel@1
   132
    }
moel@1
   133
moel@109
   134
    public Identifier Identifier {
moel@28
   135
      get {
moel@166
   136
        return new Identifier(hardware.Identifier,
moel@166
   137
          sensorType.ToString().ToLowerInvariant(),
moel@166
   138
          index.ToString(CultureInfo.InvariantCulture));
moel@28
   139
      }
moel@28
   140
    }
moel@28
   141
moel@1
   142
    public string Name {
moel@1
   143
      get { 
moel@1
   144
        return name; 
moel@1
   145
      }
moel@1
   146
      set {
moel@167
   147
        if (!string.IsNullOrEmpty(value)) 
moel@1
   148
          name = value;          
moel@1
   149
        else 
moel@1
   150
          name = defaultName;
moel@166
   151
        settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
moel@1
   152
      }
moel@1
   153
    }
moel@1
   154
moel@1
   155
    public int Index {
moel@1
   156
      get { return index; }
moel@1
   157
    }
moel@1
   158
moel@109
   159
    public bool IsDefaultHidden {
moel@109
   160
      get { return defaultHidden; }
moel@109
   161
    }
moel@109
   162
moel@63
   163
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   164
      get { return parameters; }
moel@63
   165
    }
moel@63
   166
moel@1
   167
    public float? Value {
moel@1
   168
      get { 
moel@167
   169
        return currentValue; 
moel@1
   170
      }
moel@1
   171
      set {
moel@314
   172
        DateTime now = DateTime.UtcNow;
moel@298
   173
        while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
moel@298
   174
          values.Remove();
moel@1
   175
moel@1
   176
        if (value.HasValue) {
moel@1
   177
          sum += value.Value;
moel@1
   178
          count++;
moel@1
   179
          if (count == 4) {
moel@298
   180
            AppendValue(sum / count, now);
moel@1
   181
            sum = 0;
moel@1
   182
            count = 0;
moel@1
   183
          }
moel@1
   184
        }
moel@1
   185
moel@167
   186
        this.currentValue = value;
moel@167
   187
        if (minValue > value || !minValue.HasValue)
moel@167
   188
          minValue = value;
moel@167
   189
        if (maxValue < value || !maxValue.HasValue)
moel@167
   190
          maxValue = value;
moel@1
   191
      }
moel@1
   192
    }
moel@1
   193
moel@167
   194
    public float? Min { get { return minValue; } }
moel@167
   195
    public float? Max { get { return maxValue; } }
moel@1
   196
moel@151
   197
    public void ResetMin() {
moel@167
   198
      minValue = null;
moel@151
   199
    }
moel@151
   200
moel@151
   201
    public void ResetMax() {
moel@167
   202
      maxValue = null;
moel@151
   203
    }
moel@151
   204
moel@159
   205
    public IEnumerable<SensorValue> Values {
moel@159
   206
      get { return values; }
moel@159
   207
    }    
moel@110
   208
moel@110
   209
    public void Accept(IVisitor visitor) {
moel@167
   210
      if (visitor == null)
moel@167
   211
        throw new ArgumentNullException("visitor");
moel@167
   212
      visitor.VisitSensor(this);
moel@110
   213
    }
moel@110
   214
moel@110
   215
    public void Traverse(IVisitor visitor) {
moel@110
   216
      foreach (IParameter parameter in parameters)
moel@110
   217
        parameter.Accept(visitor);
moel@110
   218
    }
moel@247
   219
moel@247
   220
    public IControl Control {
moel@247
   221
      get {
moel@247
   222
        return control;
moel@247
   223
      }
moel@247
   224
      internal set {
moel@247
   225
        this.control = value;
moel@247
   226
      }
moel@247
   227
    }
moel@1
   228
  }
moel@1
   229
}