Hardware/Control.cs
author moel.mich
Tue, 30 Dec 2014 21:04:54 +0000
changeset 430 6b24e39f1b84
parent 344 3145aadca3d2
permissions -rw-r--r--
Fixed Issue 651.
     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) 2010-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Globalization;
    13 
    14 namespace OpenHardwareMonitor.Hardware {
    15 
    16   internal delegate void ControlEventHandler(Control control);
    17 
    18   internal class Control : IControl {
    19 
    20     private readonly Identifier identifier;
    21     private readonly ISettings settings;
    22     private ControlMode mode;
    23     private float softwareValue;
    24     private float minSoftwareValue;
    25     private float maxSoftwareValue;
    26 
    27     public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
    28       float maxSoftwareValue) 
    29     {
    30       this.identifier = new Identifier(sensor.Identifier, "control");
    31       this.settings = settings;
    32       this.minSoftwareValue = minSoftwareValue;
    33       this.maxSoftwareValue = maxSoftwareValue;
    34 
    35       if (!float.TryParse(settings.GetValue(
    36           new Identifier(identifier, "value").ToString(), "0"),
    37         NumberStyles.Float, CultureInfo.InvariantCulture,
    38         out this.softwareValue)) 
    39       {
    40         this.softwareValue = 0;
    41       }
    42       int mode;
    43       if (!int.TryParse(settings.GetValue(
    44           new Identifier(identifier, "mode").ToString(),
    45           ((int)ControlMode.Undefined).ToString(CultureInfo.InvariantCulture)),
    46         NumberStyles.Integer, CultureInfo.InvariantCulture,
    47         out mode)) 
    48       {
    49         this.mode = ControlMode.Undefined;
    50       } else {
    51         this.mode = (ControlMode)mode;
    52       }
    53     }
    54 
    55     public Identifier Identifier {
    56       get {
    57         return identifier;
    58       }
    59     }
    60 
    61     public ControlMode ControlMode {
    62       get {
    63         return mode;
    64       }
    65       private set {
    66         if (mode != value) {
    67           mode = value;
    68           if (ControlModeChanged != null)
    69             ControlModeChanged(this);
    70           this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
    71             ((int)mode).ToString(CultureInfo.InvariantCulture));
    72         }
    73       }
    74     }
    75 
    76     public float SoftwareValue {
    77       get {
    78         return softwareValue;
    79       }
    80       private set {
    81         if (softwareValue != value) {
    82           softwareValue = value;
    83           if (SoftwareControlValueChanged != null)
    84             SoftwareControlValueChanged(this);
    85           this.settings.SetValue(new Identifier(identifier,
    86             "value").ToString(),
    87             value.ToString(CultureInfo.InvariantCulture));
    88         }
    89       }
    90     }
    91 
    92     public void SetDefault() {
    93       ControlMode = ControlMode.Default;
    94     }
    95 
    96     public float MinSoftwareValue {
    97       get {
    98         return minSoftwareValue;
    99       }
   100     }
   101 
   102     public float MaxSoftwareValue {
   103       get {
   104         return maxSoftwareValue;
   105       }
   106     }
   107 
   108     public void SetSoftware(float value) {
   109       ControlMode = ControlMode.Software;
   110       SoftwareValue = value;
   111     }
   112 
   113     internal event ControlEventHandler ControlModeChanged;
   114     internal event ControlEventHandler SoftwareControlValueChanged;
   115   }
   116 }