Hardware/Parameter.cs
author moel.mich
Sun, 28 Oct 2012 14:06:50 +0000
changeset 386 7094d5dd924b
parent 195 0ee888c485d5
permissions -rw-r--r--
Changed the source formatting of the HttpServer class to match the rest of the project.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Globalization;
    13 
    14 namespace OpenHardwareMonitor.Hardware {
    15 
    16   internal struct ParameterDescription {
    17     private readonly string name;
    18     private readonly string description;
    19     private readonly float defaultValue;    
    20 
    21     public ParameterDescription(string name, string description, 
    22       float defaultValue) {
    23       this.name = name;
    24       this.description = description;
    25       this.defaultValue = defaultValue;
    26     }
    27 
    28     public string Name { get { return name; } }
    29 
    30     public string Description { get { return description; } }
    31 
    32     public float DefaultValue { get { return defaultValue; } }
    33   }
    34 
    35   internal class Parameter : IParameter {
    36     private readonly ISensor sensor;
    37     private ParameterDescription description;
    38     private float value;
    39     private bool isDefault;
    40     private readonly ISettings settings;
    41 
    42     public Parameter(ParameterDescription description, ISensor sensor, 
    43       ISettings settings) 
    44     {
    45       this.sensor = sensor;
    46       this.description = description;
    47       this.settings = settings;
    48       this.isDefault = !settings.Contains(Identifier.ToString());
    49       this.value = description.DefaultValue;
    50       if (!this.isDefault) {
    51         if (!float.TryParse(settings.GetValue(Identifier.ToString(), "0"),
    52           NumberStyles.Float,
    53           CultureInfo.InvariantCulture,
    54           out this.value))
    55           this.value = description.DefaultValue;
    56       }
    57     }
    58 
    59     public ISensor Sensor {
    60       get {
    61         return sensor;
    62       }
    63     }
    64 
    65     public Identifier Identifier {
    66       get {
    67         return new Identifier(sensor.Identifier, "parameter",
    68           Name.Replace(" ", "").ToLowerInvariant());
    69       }
    70     }
    71 
    72     public string Name { get { return description.Name; } }
    73 
    74     public string Description { get { return description.Description; } }
    75 
    76     public float Value {
    77       get {
    78         return value;
    79       }
    80       set {
    81         this.isDefault = false;
    82         this.value = value;
    83         this.settings.SetValue(Identifier.ToString(), value.ToString(
    84           CultureInfo.InvariantCulture));
    85       }
    86     }
    87 
    88     public float DefaultValue { 
    89       get { return description.DefaultValue; } 
    90     }
    91 
    92     public bool IsDefault {
    93       get { return isDefault; }
    94       set {
    95         this.isDefault = value;
    96         if (value) {
    97           this.value = description.DefaultValue;
    98           this.settings.Remove(Identifier.ToString());
    99         }
   100       }
   101     }
   102 
   103     public void Accept(IVisitor visitor) {
   104       if (visitor == null)
   105         throw new ArgumentNullException("visitor");
   106       visitor.VisitParameter(this);
   107     }
   108 
   109     public void Traverse(IVisitor visitor) { }
   110   }
   111 }