GUI/HardwareNode.cs
author moel.mich
Sun, 22 Aug 2010 21:53:11 +0000
changeset 171 81ab5e53122e
parent 165 813d8bc3192f
child 176 c16fd81b520a
permissions -rw-r--r--
Added a first implementation for the Heatmaster fan controller.
     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) 2009-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.Collections.Generic;
    40 using System.Drawing;
    41 using OpenHardwareMonitor.Hardware;
    42 
    43 namespace OpenHardwareMonitor.GUI {
    44   public class HardwareNode : Node {
    45 
    46     private PersistentSettings settings;
    47     private UnitManager unitManager;
    48     private IHardware hardware;
    49 
    50     private List<TypeNode> typeNodes = new List<TypeNode>();
    51 
    52     public HardwareNode(IHardware hardware, PersistentSettings settings, 
    53       UnitManager unitManager) : base(hardware.Name) 
    54     {
    55       this.settings = settings;
    56       this.unitManager = unitManager;
    57       this.hardware = hardware;
    58       switch (hardware.HardwareType) {
    59         case HardwareType.CPU: 
    60           this.Image = Utilities.EmbeddedResources.GetImage("cpu.png");
    61           break;
    62         case HardwareType.GPU:
    63           if (hardware.Identifier.ToString().Contains("nvidia"))
    64             this.Image = Utilities.EmbeddedResources.GetImage("nvidia.png");
    65           else
    66             this.Image = Utilities.EmbeddedResources.GetImage("ati.png");
    67           break;
    68         case HardwareType.HDD: 
    69           this.Image = Utilities.EmbeddedResources.GetImage("hdd.png");
    70           break;
    71         case HardwareType.Heatmaster:
    72           this.Image = Utilities.EmbeddedResources.GetImage("bigng.png");
    73           break;
    74         case HardwareType.Mainboard: 
    75           this.Image = Utilities.EmbeddedResources.GetImage("mainboard.png");
    76           break;
    77         case HardwareType.SuperIO: 
    78           this.Image = Utilities.EmbeddedResources.GetImage("chip.png");
    79           break;
    80         case HardwareType.TBalancer: 
    81           this.Image = Utilities.EmbeddedResources.GetImage("bigng.png");
    82           break;
    83       }
    84 
    85       typeNodes.Add(new TypeNode(SensorType.Voltage));
    86       typeNodes.Add(new TypeNode(SensorType.Clock));      
    87       typeNodes.Add(new TypeNode(SensorType.Temperature));
    88       typeNodes.Add(new TypeNode(SensorType.Load));
    89       typeNodes.Add(new TypeNode(SensorType.Fan));
    90       typeNodes.Add(new TypeNode(SensorType.Flow));
    91       typeNodes.Add(new TypeNode(SensorType.Control));
    92       
    93       foreach (ISensor sensor in hardware.Sensors)
    94         SensorAdded(sensor);
    95 
    96       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    97       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    98     }
    99 
   100     public IHardware Hardware {
   101       get { return hardware; }
   102     }
   103 
   104     private void UpdateNode(TypeNode node) {  
   105       if (node.Nodes.Count > 0) {
   106         if (!Nodes.Contains(node)) {
   107           int i = 0;
   108           while (i < Nodes.Count &&
   109             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
   110             i++;
   111           Nodes.Insert(i, node);  
   112         }
   113       } else {
   114         if (Nodes.Contains(node))
   115           Nodes.Remove(node);
   116       }
   117     }
   118 
   119     private void SensorRemoved(ISensor sensor) {
   120       foreach (TypeNode typeNode in typeNodes)
   121         if (typeNode.SensorType == sensor.SensorType) { 
   122           SensorNode sensorNode = null;
   123           foreach (Node node in typeNode.Nodes) {
   124             SensorNode n = node as SensorNode;
   125             if (n != null && n.Sensor == sensor)
   126               sensorNode = n;
   127           }
   128           typeNode.Nodes.Remove(sensorNode);
   129           UpdateNode(typeNode);
   130         }
   131     }
   132 
   133     private void InsertSorted(Node node, ISensor sensor) {
   134       int i = 0;
   135       while (i < node.Nodes.Count &&
   136         ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
   137         i++;
   138       SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
   139       node.Nodes.Insert(i, sensorNode);
   140     }
   141 
   142     private void SensorAdded(ISensor sensor) {
   143       foreach (TypeNode typeNode in typeNodes)
   144         if (typeNode.SensorType == sensor.SensorType) {
   145           InsertSorted(typeNode, sensor);
   146           UpdateNode(typeNode);          
   147         }
   148     }    
   149   }
   150 }