Hardware/Sensor.cs
author moel.mich
Mon, 02 Jul 2012 21:14:40 +0000
changeset 357 fb8dc26f65a4
parent 314 d19c6b4d625e
child 358 7962499f9cd6
permissions -rw-r--r--
Added mainboard specific configurations for the following Gigabyte mainboards: EX58-UD3R, G41M-Combo, G41MT-S2, G41MT-S2P, GA-MA770T-UD3P, GA-MA785GM-US2H, GA-MA78LM-S2H, GA-MA790X-UD3P, H55-USB3, H55N-USB3, H61M-DS2 REV 1.2, H61M-USB3-B3 REV 2.0, H67A-USB3-B3, P55A-UD3, P67A-UD3-B3, P67A-UD3R-B3, Z68A-D3H-B3, Z68AP-D3, Z68X-UD3H-B3.
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@298
    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@298
    91
      string s = settings.GetValue(
moel@298
    92
        new Identifier(Identifier, "values").ToString(), null);
moel@298
    93
moel@298
    94
      byte[] array = null;
moel@298
    95
      try {
moel@298
    96
        array = Convert.FromBase64String(s);
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@298
   111
    }
moel@298
   112
moel@298
   113
    private void AppendValue(float value, DateTime time) {
moel@298
   114
      if (values.Count >= 2 && values.Last.Value == value && 
moel@298
   115
        values[values.Count - 2].Value == value) {
moel@298
   116
        values.Last = new SensorValue(value, time);
moel@298
   117
        return;
moel@298
   118
      } 
moel@298
   119
moel@298
   120
      values.Append(new SensorValue(value, time));
moel@1
   121
    }
moel@1
   122
moel@28
   123
    public IHardware Hardware {
moel@28
   124
      get { return hardware; }
moel@28
   125
    }
moel@28
   126
moel@1
   127
    public SensorType SensorType {
moel@1
   128
      get { return sensorType; }
moel@1
   129
    }
moel@1
   130
moel@109
   131
    public Identifier Identifier {
moel@28
   132
      get {
moel@166
   133
        return new Identifier(hardware.Identifier,
moel@166
   134
          sensorType.ToString().ToLowerInvariant(),
moel@166
   135
          index.ToString(CultureInfo.InvariantCulture));
moel@28
   136
      }
moel@28
   137
    }
moel@28
   138
moel@1
   139
    public string Name {
moel@1
   140
      get { 
moel@1
   141
        return name; 
moel@1
   142
      }
moel@1
   143
      set {
moel@167
   144
        if (!string.IsNullOrEmpty(value)) 
moel@1
   145
          name = value;          
moel@1
   146
        else 
moel@1
   147
          name = defaultName;
moel@166
   148
        settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
moel@1
   149
      }
moel@1
   150
    }
moel@1
   151
moel@1
   152
    public int Index {
moel@1
   153
      get { return index; }
moel@1
   154
    }
moel@1
   155
moel@109
   156
    public bool IsDefaultHidden {
moel@109
   157
      get { return defaultHidden; }
moel@109
   158
    }
moel@109
   159
moel@63
   160
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   161
      get { return parameters; }
moel@63
   162
    }
moel@63
   163
moel@1
   164
    public float? Value {
moel@1
   165
      get { 
moel@167
   166
        return currentValue; 
moel@1
   167
      }
moel@1
   168
      set {
moel@314
   169
        DateTime now = DateTime.UtcNow;
moel@298
   170
        while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
moel@298
   171
          values.Remove();
moel@1
   172
moel@1
   173
        if (value.HasValue) {
moel@1
   174
          sum += value.Value;
moel@1
   175
          count++;
moel@1
   176
          if (count == 4) {
moel@298
   177
            AppendValue(sum / count, now);
moel@1
   178
            sum = 0;
moel@1
   179
            count = 0;
moel@1
   180
          }
moel@1
   181
        }
moel@1
   182
moel@167
   183
        this.currentValue = value;
moel@167
   184
        if (minValue > value || !minValue.HasValue)
moel@167
   185
          minValue = value;
moel@167
   186
        if (maxValue < value || !maxValue.HasValue)
moel@167
   187
          maxValue = value;
moel@1
   188
      }
moel@1
   189
    }
moel@1
   190
moel@167
   191
    public float? Min { get { return minValue; } }
moel@167
   192
    public float? Max { get { return maxValue; } }
moel@1
   193
moel@151
   194
    public void ResetMin() {
moel@167
   195
      minValue = null;
moel@151
   196
    }
moel@151
   197
moel@151
   198
    public void ResetMax() {
moel@167
   199
      maxValue = null;
moel@151
   200
    }
moel@151
   201
moel@159
   202
    public IEnumerable<SensorValue> Values {
moel@159
   203
      get { return values; }
moel@159
   204
    }    
moel@110
   205
moel@110
   206
    public void Accept(IVisitor visitor) {
moel@167
   207
      if (visitor == null)
moel@167
   208
        throw new ArgumentNullException("visitor");
moel@167
   209
      visitor.VisitSensor(this);
moel@110
   210
    }
moel@110
   211
moel@110
   212
    public void Traverse(IVisitor visitor) {
moel@110
   213
      foreach (IParameter parameter in parameters)
moel@110
   214
        parameter.Accept(visitor);
moel@110
   215
    }
moel@247
   216
moel@247
   217
    public IControl Control {
moel@247
   218
      get {
moel@247
   219
        return control;
moel@247
   220
      }
moel@247
   221
      internal set {
moel@247
   222
        this.control = value;
moel@247
   223
      }
moel@247
   224
    }
moel@1
   225
  }
moel@1
   226
}