Hardware/Control.cs
author moel.mich
Tue, 15 Mar 2011 17:20:22 +0000
changeset 257 f4bcb097746d
child 344 3145aadca3d2
permissions -rw-r--r--
Fixed Issue 172.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Globalization;
    40 
    41 namespace OpenHardwareMonitor.Hardware {
    42 
    43   internal delegate void ControlEventHandler(Control control);
    44 
    45   internal class Control : IControl {
    46 
    47     private readonly Identifier identifier;
    48     private readonly ISettings settings;
    49     private ControlMode mode;
    50     private float softwareValue;
    51     private float minSoftwareValue;
    52     private float maxSoftwareValue;
    53 
    54     public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
    55       float maxSoftwareValue) 
    56     {
    57       this.identifier = new Identifier(sensor.Identifier, "control");
    58       this.settings = settings;
    59       this.minSoftwareValue = minSoftwareValue;
    60       this.maxSoftwareValue = maxSoftwareValue;
    61 
    62       if (!float.TryParse(settings.GetValue(
    63           new Identifier(identifier, "value").ToString(), "0"),
    64         NumberStyles.Float, CultureInfo.InvariantCulture,
    65         out this.softwareValue)) 
    66       {
    67         this.softwareValue = 0;
    68       }
    69       int mode;
    70       if (!int.TryParse(settings.GetValue(
    71           new Identifier(identifier, "mode").ToString(),
    72           ((int)ControlMode.Default).ToString(CultureInfo.InvariantCulture)),
    73         NumberStyles.Integer, CultureInfo.InvariantCulture,
    74         out mode)) 
    75       {
    76         this.mode = ControlMode.Default;
    77       } else {
    78         this.mode = (ControlMode)mode;
    79       }
    80     }
    81 
    82     public Identifier Identifier {
    83       get {
    84         return identifier;
    85       }
    86     }
    87 
    88     public ControlMode ControlMode {
    89       get {
    90         return mode;
    91       }
    92       private set {
    93         if (mode != value) {
    94           mode = value;
    95           if (ControlModeChanged != null)
    96             ControlModeChanged(this);
    97           this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
    98             ((int)mode).ToString(CultureInfo.InvariantCulture));
    99         }
   100       }
   101     }
   102 
   103     public float SoftwareValue {
   104       get {
   105         return softwareValue;
   106       }
   107       private set {
   108         if (softwareValue != value) {
   109           softwareValue = value;
   110           if (SoftwareControlValueChanged != null)
   111             SoftwareControlValueChanged(this);
   112           this.settings.SetValue(new Identifier(identifier,
   113             "value").ToString(),
   114             value.ToString(CultureInfo.InvariantCulture));
   115         }
   116       }
   117     }
   118 
   119     public void SetDefault() {
   120       ControlMode = ControlMode.Default;
   121     }
   122 
   123     public float MinSoftwareValue {
   124       get {
   125         return minSoftwareValue;
   126       }
   127     }
   128 
   129     public float MaxSoftwareValue {
   130       get {
   131         return maxSoftwareValue;
   132       }
   133     }
   134 
   135     public void SetSoftware(float value) {
   136       ControlMode = ControlMode.Software;
   137       SoftwareValue = value;
   138     }
   139 
   140     internal event ControlEventHandler ControlModeChanged;
   141     internal event ControlEventHandler SoftwareControlValueChanged;
   142   }
   143 }