Hardware/Sensor.cs
author moel.mich
Tue, 17 Jul 2012 16:12:07 +0000
changeset 365 a8a8ff22d959
parent 358 7962499f9cd6
child 376 df64b56ece65
permissions -rw-r--r--
Improved the data compression for storing the recorded sensor values in the config file.
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@365
     7
  Copyright (C) 2009-2012 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@365
    79
        using (BufferedStream b = new BufferedStream(c, 65536))
moel@365
    80
        using (BinaryWriter writer = new BinaryWriter(b)) {
moel@365
    81
          long t = 0;
moel@298
    82
          foreach (SensorValue sensorValue in values) {
moel@365
    83
            long v = sensorValue.Time.ToBinary();
moel@365
    84
            writer.Write(v - t);
moel@365
    85
            t = v;
moel@298
    86
            writer.Write(sensorValue.Value);
moel@298
    87
          }
moel@365
    88
          writer.Flush();
moel@298
    89
        }
moel@298
    90
        settings.SetValue(new Identifier(Identifier, "values").ToString(),
moel@365
    91
          Convert.ToBase64String(m.ToArray()));
moel@298
    92
      }
moel@298
    93
    }
moel@298
    94
moel@298
    95
    private void GetSensorValuesFromSettings() {
moel@358
    96
      string name = new Identifier(Identifier, "values").ToString();
moel@358
    97
      string s = settings.GetValue(name, null);
moel@298
    98
moel@298
    99
      try {
moel@358
   100
        byte[] array = Convert.FromBase64String(s);
moel@358
   101
        s = null;
moel@298
   102
        using (MemoryStream m = new MemoryStream(array))
moel@298
   103
        using (GZipStream c = new GZipStream(m, CompressionMode.Decompress))
moel@298
   104
        using (BinaryReader reader = new BinaryReader(c)) {
moel@298
   105
          try {
moel@365
   106
            long t = 0;
moel@298
   107
            while (true) {
moel@365
   108
              t += reader.ReadInt64();
moel@365
   109
              DateTime time = DateTime.FromBinary(t);              
moel@298
   110
              float value = reader.ReadSingle();
moel@298
   111
              AppendValue(value, time);
moel@298
   112
            }
moel@298
   113
          } catch (EndOfStreamException) { }
moel@298
   114
        }
moel@298
   115
      } catch { }
moel@298
   116
      if (values.Count > 0)
moel@314
   117
        AppendValue(float.NaN, DateTime.UtcNow);
moel@358
   118
moel@358
   119
      // remove the value string from the settings to reduce memory usage
moel@358
   120
      settings.Remove(name);
moel@298
   121
    }
moel@298
   122
moel@298
   123
    private void AppendValue(float value, DateTime time) {
moel@298
   124
      if (values.Count >= 2 && values.Last.Value == value && 
moel@298
   125
        values[values.Count - 2].Value == value) {
moel@298
   126
        values.Last = new SensorValue(value, time);
moel@298
   127
        return;
moel@298
   128
      } 
moel@298
   129
moel@298
   130
      values.Append(new SensorValue(value, time));
moel@1
   131
    }
moel@1
   132
moel@28
   133
    public IHardware Hardware {
moel@28
   134
      get { return hardware; }
moel@28
   135
    }
moel@28
   136
moel@1
   137
    public SensorType SensorType {
moel@1
   138
      get { return sensorType; }
moel@1
   139
    }
moel@1
   140
moel@109
   141
    public Identifier Identifier {
moel@28
   142
      get {
moel@166
   143
        return new Identifier(hardware.Identifier,
moel@166
   144
          sensorType.ToString().ToLowerInvariant(),
moel@166
   145
          index.ToString(CultureInfo.InvariantCulture));
moel@28
   146
      }
moel@28
   147
    }
moel@28
   148
moel@1
   149
    public string Name {
moel@1
   150
      get { 
moel@1
   151
        return name; 
moel@1
   152
      }
moel@1
   153
      set {
moel@167
   154
        if (!string.IsNullOrEmpty(value)) 
moel@1
   155
          name = value;          
moel@1
   156
        else 
moel@1
   157
          name = defaultName;
moel@166
   158
        settings.SetValue(new Identifier(Identifier, "name").ToString(), name);
moel@1
   159
      }
moel@1
   160
    }
moel@1
   161
moel@1
   162
    public int Index {
moel@1
   163
      get { return index; }
moel@1
   164
    }
moel@1
   165
moel@109
   166
    public bool IsDefaultHidden {
moel@109
   167
      get { return defaultHidden; }
moel@109
   168
    }
moel@109
   169
moel@63
   170
    public IReadOnlyArray<IParameter> Parameters {
moel@63
   171
      get { return parameters; }
moel@63
   172
    }
moel@63
   173
moel@1
   174
    public float? Value {
moel@1
   175
      get { 
moel@167
   176
        return currentValue; 
moel@1
   177
      }
moel@1
   178
      set {
moel@314
   179
        DateTime now = DateTime.UtcNow;
moel@298
   180
        while (values.Count > 0 && (now - values.First.Time).TotalDays > 1)
moel@298
   181
          values.Remove();
moel@1
   182
moel@1
   183
        if (value.HasValue) {
moel@1
   184
          sum += value.Value;
moel@1
   185
          count++;
moel@1
   186
          if (count == 4) {
moel@298
   187
            AppendValue(sum / count, now);
moel@1
   188
            sum = 0;
moel@1
   189
            count = 0;
moel@1
   190
          }
moel@1
   191
        }
moel@1
   192
moel@167
   193
        this.currentValue = value;
moel@167
   194
        if (minValue > value || !minValue.HasValue)
moel@167
   195
          minValue = value;
moel@167
   196
        if (maxValue < value || !maxValue.HasValue)
moel@167
   197
          maxValue = value;
moel@1
   198
      }
moel@1
   199
    }
moel@1
   200
moel@167
   201
    public float? Min { get { return minValue; } }
moel@167
   202
    public float? Max { get { return maxValue; } }
moel@1
   203
moel@151
   204
    public void ResetMin() {
moel@167
   205
      minValue = null;
moel@151
   206
    }
moel@151
   207
moel@151
   208
    public void ResetMax() {
moel@167
   209
      maxValue = null;
moel@151
   210
    }
moel@151
   211
moel@159
   212
    public IEnumerable<SensorValue> Values {
moel@159
   213
      get { return values; }
moel@159
   214
    }    
moel@110
   215
moel@110
   216
    public void Accept(IVisitor visitor) {
moel@167
   217
      if (visitor == null)
moel@167
   218
        throw new ArgumentNullException("visitor");
moel@167
   219
      visitor.VisitSensor(this);
moel@110
   220
    }
moel@110
   221
moel@110
   222
    public void Traverse(IVisitor visitor) {
moel@110
   223
      foreach (IParameter parameter in parameters)
moel@110
   224
        parameter.Accept(visitor);
moel@110
   225
    }
moel@247
   226
moel@247
   227
    public IControl Control {
moel@247
   228
      get {
moel@247
   229
        return control;
moel@247
   230
      }
moel@247
   231
      internal set {
moel@247
   232
        this.control = value;
moel@247
   233
      }
moel@247
   234
    }
moel@1
   235
  }
moel@1
   236
}