Hardware/Parameter.cs
author moel.mich
Tue, 30 Dec 2014 21:04:54 +0000
changeset 430 6b24e39f1b84
parent 195 0ee888c485d5
permissions -rw-r--r--
Fixed Issue 651.
moel@63
     1
/*
moel@63
     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@63
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@63
     9
*/
moel@63
    10
moel@63
    11
using System;
moel@166
    12
using System.Globalization;
moel@63
    13
moel@63
    14
namespace OpenHardwareMonitor.Hardware {
moel@63
    15
moel@167
    16
  internal struct ParameterDescription {
moel@195
    17
    private readonly string name;
moel@195
    18
    private readonly string description;
moel@195
    19
    private readonly float defaultValue;    
moel@63
    20
moel@63
    21
    public ParameterDescription(string name, string description, 
moel@63
    22
      float defaultValue) {
moel@63
    23
      this.name = name;
moel@63
    24
      this.description = description;
moel@63
    25
      this.defaultValue = defaultValue;
moel@63
    26
    }
moel@63
    27
moel@63
    28
    public string Name { get { return name; } }
moel@63
    29
moel@63
    30
    public string Description { get { return description; } }
moel@63
    31
moel@63
    32
    public float DefaultValue { get { return defaultValue; } }
moel@63
    33
  }
moel@63
    34
moel@167
    35
  internal class Parameter : IParameter {
moel@195
    36
    private readonly ISensor sensor;
moel@63
    37
    private ParameterDescription description;
moel@63
    38
    private float value;
moel@63
    39
    private bool isDefault;
moel@195
    40
    private readonly ISettings settings;
moel@63
    41
moel@165
    42
    public Parameter(ParameterDescription description, ISensor sensor, 
moel@165
    43
      ISettings settings) 
moel@165
    44
    {
moel@63
    45
      this.sensor = sensor;
moel@63
    46
      this.description = description;
moel@165
    47
      this.settings = settings;
moel@165
    48
      this.isDefault = !settings.Contains(Identifier.ToString());
moel@165
    49
      this.value = description.DefaultValue;
moel@165
    50
      if (!this.isDefault) {
moel@166
    51
        if (!float.TryParse(settings.GetValue(Identifier.ToString(), "0"),
moel@166
    52
          NumberStyles.Float,
moel@166
    53
          CultureInfo.InvariantCulture,
moel@165
    54
          out this.value))
moel@165
    55
          this.value = description.DefaultValue;
moel@165
    56
      }
moel@63
    57
    }
moel@63
    58
moel@63
    59
    public ISensor Sensor {
moel@63
    60
      get {
moel@63
    61
        return sensor;
moel@63
    62
      }
moel@63
    63
    }
moel@63
    64
moel@109
    65
    public Identifier Identifier {
moel@63
    66
      get {
moel@166
    67
        return new Identifier(sensor.Identifier, "parameter",
moel@166
    68
          Name.Replace(" ", "").ToLowerInvariant());
moel@63
    69
      }
moel@63
    70
    }
moel@63
    71
moel@63
    72
    public string Name { get { return description.Name; } }
moel@63
    73
moel@63
    74
    public string Description { get { return description.Description; } }
moel@63
    75
moel@63
    76
    public float Value {
moel@63
    77
      get {
moel@63
    78
        return value;
moel@63
    79
      }
moel@63
    80
      set {
moel@63
    81
        this.isDefault = false;
moel@165
    82
        this.value = value;
moel@166
    83
        this.settings.SetValue(Identifier.ToString(), value.ToString(
moel@166
    84
          CultureInfo.InvariantCulture));
moel@63
    85
      }
moel@63
    86
    }
moel@63
    87
moel@63
    88
    public float DefaultValue { 
moel@63
    89
      get { return description.DefaultValue; } 
moel@63
    90
    }
moel@63
    91
moel@63
    92
    public bool IsDefault {
moel@63
    93
      get { return isDefault; }
moel@63
    94
      set {
moel@63
    95
        this.isDefault = value;
moel@63
    96
        if (value) {
moel@63
    97
          this.value = description.DefaultValue;
moel@165
    98
          this.settings.Remove(Identifier.ToString());
moel@63
    99
        }
moel@63
   100
      }
moel@110
   101
    }
moel@110
   102
moel@110
   103
    public void Accept(IVisitor visitor) {
moel@167
   104
      if (visitor == null)
moel@167
   105
        throw new ArgumentNullException("visitor");
moel@167
   106
      visitor.VisitParameter(this);
moel@110
   107
    }
moel@110
   108
moel@110
   109
    public void Traverse(IVisitor visitor) { }
moel@63
   110
  }
moel@63
   111
}