moel@63: /*
moel@63:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@63:  
moel@344:   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@63: */
moel@63: 
moel@63: using System;
moel@166: using System.Globalization;
moel@63: 
moel@63: namespace OpenHardwareMonitor.Hardware {
moel@63: 
moel@167:   internal struct ParameterDescription {
moel@195:     private readonly string name;
moel@195:     private readonly string description;
moel@195:     private readonly float defaultValue;    
moel@63: 
moel@63:     public ParameterDescription(string name, string description, 
moel@63:       float defaultValue) {
moel@63:       this.name = name;
moel@63:       this.description = description;
moel@63:       this.defaultValue = defaultValue;
moel@63:     }
moel@63: 
moel@63:     public string Name { get { return name; } }
moel@63: 
moel@63:     public string Description { get { return description; } }
moel@63: 
moel@63:     public float DefaultValue { get { return defaultValue; } }
moel@63:   }
moel@63: 
moel@167:   internal class Parameter : IParameter {
moel@195:     private readonly ISensor sensor;
moel@63:     private ParameterDescription description;
moel@63:     private float value;
moel@63:     private bool isDefault;
moel@195:     private readonly ISettings settings;
moel@63: 
moel@165:     public Parameter(ParameterDescription description, ISensor sensor, 
moel@165:       ISettings settings) 
moel@165:     {
moel@63:       this.sensor = sensor;
moel@63:       this.description = description;
moel@165:       this.settings = settings;
moel@165:       this.isDefault = !settings.Contains(Identifier.ToString());
moel@165:       this.value = description.DefaultValue;
moel@165:       if (!this.isDefault) {
moel@166:         if (!float.TryParse(settings.GetValue(Identifier.ToString(), "0"),
moel@166:           NumberStyles.Float,
moel@166:           CultureInfo.InvariantCulture,
moel@165:           out this.value))
moel@165:           this.value = description.DefaultValue;
moel@165:       }
moel@63:     }
moel@63: 
moel@63:     public ISensor Sensor {
moel@63:       get {
moel@63:         return sensor;
moel@63:       }
moel@63:     }
moel@63: 
moel@109:     public Identifier Identifier {
moel@63:       get {
moel@166:         return new Identifier(sensor.Identifier, "parameter",
moel@166:           Name.Replace(" ", "").ToLowerInvariant());
moel@63:       }
moel@63:     }
moel@63: 
moel@63:     public string Name { get { return description.Name; } }
moel@63: 
moel@63:     public string Description { get { return description.Description; } }
moel@63: 
moel@63:     public float Value {
moel@63:       get {
moel@63:         return value;
moel@63:       }
moel@63:       set {
moel@63:         this.isDefault = false;
moel@165:         this.value = value;
moel@166:         this.settings.SetValue(Identifier.ToString(), value.ToString(
moel@166:           CultureInfo.InvariantCulture));
moel@63:       }
moel@63:     }
moel@63: 
moel@63:     public float DefaultValue { 
moel@63:       get { return description.DefaultValue; } 
moel@63:     }
moel@63: 
moel@63:     public bool IsDefault {
moel@63:       get { return isDefault; }
moel@63:       set {
moel@63:         this.isDefault = value;
moel@63:         if (value) {
moel@63:           this.value = description.DefaultValue;
moel@165:           this.settings.Remove(Identifier.ToString());
moel@63:         }
moel@63:       }
moel@110:     }
moel@110: 
moel@110:     public void Accept(IVisitor visitor) {
moel@167:       if (visitor == null)
moel@167:         throw new ArgumentNullException("visitor");
moel@167:       visitor.VisitParameter(this);
moel@110:     }
moel@110: 
moel@110:     public void Traverse(IVisitor visitor) { }
moel@63:   }
moel@63: }