moel@247: /*
moel@247:  
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@247:  
moel@344:   Copyright (C) 2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@247: */
moel@247: 
moel@247: using System;
moel@247: using System.Globalization;
moel@247: 
moel@247: namespace OpenHardwareMonitor.Hardware {
moel@247: 
moel@247:   internal delegate void ControlEventHandler(Control control);
moel@247: 
moel@247:   internal class Control : IControl {
moel@247: 
moel@247:     private readonly Identifier identifier;
moel@247:     private readonly ISettings settings;
moel@247:     private ControlMode mode;
moel@247:     private float softwareValue;
moel@247:     private float minSoftwareValue;
moel@247:     private float maxSoftwareValue;
moel@247: 
moel@247:     public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
moel@247:       float maxSoftwareValue) 
moel@247:     {
moel@247:       this.identifier = new Identifier(sensor.Identifier, "control");
moel@247:       this.settings = settings;
moel@247:       this.minSoftwareValue = minSoftwareValue;
moel@247:       this.maxSoftwareValue = maxSoftwareValue;
moel@247: 
moel@247:       if (!float.TryParse(settings.GetValue(
moel@247:           new Identifier(identifier, "value").ToString(), "0"),
moel@247:         NumberStyles.Float, CultureInfo.InvariantCulture,
moel@247:         out this.softwareValue)) 
moel@247:       {
moel@247:         this.softwareValue = 0;
moel@247:       }
moel@247:       int mode;
moel@247:       if (!int.TryParse(settings.GetValue(
moel@247:           new Identifier(identifier, "mode").ToString(),
moel@247:           ((int)ControlMode.Default).ToString(CultureInfo.InvariantCulture)),
moel@247:         NumberStyles.Integer, CultureInfo.InvariantCulture,
moel@247:         out mode)) 
moel@247:       {
moel@247:         this.mode = ControlMode.Default;
moel@247:       } else {
moel@247:         this.mode = (ControlMode)mode;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public Identifier Identifier {
moel@247:       get {
moel@247:         return identifier;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public ControlMode ControlMode {
moel@247:       get {
moel@247:         return mode;
moel@247:       }
moel@247:       private set {
moel@247:         if (mode != value) {
moel@247:           mode = value;
moel@247:           if (ControlModeChanged != null)
moel@247:             ControlModeChanged(this);
moel@247:           this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
moel@247:             ((int)mode).ToString(CultureInfo.InvariantCulture));
moel@247:         }
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public float SoftwareValue {
moel@247:       get {
moel@247:         return softwareValue;
moel@247:       }
moel@247:       private set {
moel@247:         if (softwareValue != value) {
moel@247:           softwareValue = value;
moel@247:           if (SoftwareControlValueChanged != null)
moel@247:             SoftwareControlValueChanged(this);
moel@247:           this.settings.SetValue(new Identifier(identifier,
moel@247:             "value").ToString(),
moel@247:             value.ToString(CultureInfo.InvariantCulture));
moel@247:         }
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public void SetDefault() {
moel@247:       ControlMode = ControlMode.Default;
moel@247:     }
moel@247: 
moel@247:     public float MinSoftwareValue {
moel@247:       get {
moel@247:         return minSoftwareValue;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public float MaxSoftwareValue {
moel@247:       get {
moel@247:         return maxSoftwareValue;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public void SetSoftware(float value) {
moel@247:       ControlMode = ControlMode.Software;
moel@247:       SoftwareValue = value;
moel@247:     }
moel@247: 
moel@247:     internal event ControlEventHandler ControlModeChanged;
moel@247:     internal event ControlEventHandler SoftwareControlValueChanged;
moel@247:   }
moel@247: }