Hardware/Computer.cs
author moel.mich
Tue, 17 Jul 2012 16:12:07 +0000
changeset 365 a8a8ff22d959
parent 344 3145aadca3d2
child 370 8e4dedc41924
permissions -rw-r--r--
Improved the data compression for storing the recorded sensor values in the config file.
     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) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.IO;
    15 using System.Security.Permissions;
    16 using System.Reflection;
    17 
    18 namespace OpenHardwareMonitor.Hardware {
    19 
    20   public class Computer : IComputer {
    21 
    22     private readonly List<IGroup> groups = new List<IGroup>();
    23     private readonly ISettings settings;
    24 
    25     private bool open;
    26 
    27     private bool mainboardEnabled;
    28     private bool cpuEnabled;
    29     private bool gpuEnabled;
    30     private bool fanControllerEnabled;
    31     private bool hddEnabled;        
    32 
    33     public Computer() {
    34       this.settings = new Settings();
    35     }
    36 
    37     public Computer(ISettings settings) {
    38       this.settings = settings ?? new Settings();
    39     }
    40 
    41     private void Add(IGroup group) {
    42       if (groups.Contains(group))
    43         return;
    44 
    45       groups.Add(group);
    46 
    47       if (HardwareAdded != null)
    48         foreach (IHardware hardware in group.Hardware)
    49           HardwareAdded(hardware);
    50     }
    51 
    52     private void Remove(IGroup group) {
    53       if (!groups.Contains(group))
    54         return;
    55 
    56       groups.Remove(group);
    57 
    58       if (HardwareRemoved != null)
    59         foreach (IHardware hardware in group.Hardware)
    60           HardwareRemoved(hardware);
    61 
    62       group.Close();
    63     }
    64 
    65     private void RemoveType<T>() where T : IGroup {
    66       List<IGroup> list = new List<IGroup>();
    67       foreach (IGroup group in groups)
    68         if (group is T)
    69           list.Add(group);
    70       foreach (IGroup group in list)
    71         Remove(group);
    72     }
    73 
    74     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    75     public void Open() {
    76       if (open)
    77         return;
    78 
    79       Ring0.Open();
    80       Opcode.Open();
    81 
    82       if (mainboardEnabled)
    83         Add(new Mainboard.MainboardGroup(settings));
    84       
    85       if (cpuEnabled)
    86         Add(new CPU.CPUGroup(settings));
    87 
    88       if (gpuEnabled) {
    89         Add(new ATI.ATIGroup(settings));
    90         Add(new Nvidia.NvidiaGroup(settings));
    91       }
    92 
    93       if (fanControllerEnabled) {
    94         Add(new TBalancer.TBalancerGroup(settings));
    95         Add(new Heatmaster.HeatmasterGroup(settings));
    96       }
    97 
    98       if (hddEnabled)
    99         Add(new HDD.HarddriveGroup(settings));
   100 
   101       open = true;
   102     }
   103 
   104     public bool MainboardEnabled {
   105       get { return mainboardEnabled; }
   106 
   107       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   108       set {
   109         if (open && value != mainboardEnabled) {
   110           if (value)
   111             Add(new Mainboard.MainboardGroup(settings));
   112           else
   113             RemoveType<Mainboard.MainboardGroup>();
   114         }
   115         mainboardEnabled = value;
   116       }
   117     }
   118 
   119     public bool CPUEnabled {
   120       get { return cpuEnabled; }
   121 
   122       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   123       set {
   124         if (open && value != cpuEnabled) {
   125           if (value)
   126             Add(new CPU.CPUGroup(settings));
   127           else
   128             RemoveType<CPU.CPUGroup>();
   129         }
   130         cpuEnabled = value;
   131       }
   132     }
   133 
   134     public bool GPUEnabled {
   135       get { return gpuEnabled; }
   136 
   137       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   138       set {
   139         if (open && value != gpuEnabled) {
   140           if (value) {
   141             Add(new ATI.ATIGroup(settings));
   142             Add(new Nvidia.NvidiaGroup(settings));
   143           } else {
   144             RemoveType<ATI.ATIGroup>();
   145             RemoveType<Nvidia.NvidiaGroup>();
   146           }
   147         }
   148         gpuEnabled = value;
   149       }
   150     }
   151 
   152     public bool FanControllerEnabled {
   153       get { return fanControllerEnabled; }
   154 
   155       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   156       set {
   157         if (open && value != fanControllerEnabled) {
   158           if (value) {
   159             Add(new TBalancer.TBalancerGroup(settings));
   160             Add(new Heatmaster.HeatmasterGroup(settings));
   161           } else {
   162             RemoveType<TBalancer.TBalancerGroup>();
   163             RemoveType<Heatmaster.HeatmasterGroup>();
   164           }
   165         }
   166         fanControllerEnabled = value;
   167       }
   168     }
   169 
   170     public bool HDDEnabled {
   171       get { return hddEnabled; }
   172 
   173       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   174       set {
   175         if (open && value != hddEnabled) {
   176           if (value)
   177             Add(new HDD.HarddriveGroup(settings));
   178           else
   179             RemoveType<HDD.HarddriveGroup>();
   180         }
   181         hddEnabled = value;
   182       }
   183     }
   184 
   185     public IHardware[] Hardware {
   186       get {
   187         List<IHardware> list = new List<IHardware>();
   188         foreach (IGroup group in groups)
   189           foreach (IHardware hardware in group.Hardware)
   190             list.Add(hardware);
   191         return list.ToArray();
   192       }
   193     }
   194 
   195     private static void NewSection(TextWriter writer) {
   196       for (int i = 0; i < 8; i++)
   197         writer.Write("----------");
   198       writer.WriteLine();
   199       writer.WriteLine();
   200     }
   201 
   202     private static int CompareSensor(ISensor a, ISensor b) {
   203       int c = a.SensorType.CompareTo(b.SensorType);
   204       if (c == 0)
   205         return a.Index.CompareTo(b.Index);
   206       else
   207         return c;
   208     }
   209 
   210     private static void ReportHardwareSensorTree(
   211       IHardware hardware, TextWriter w, string space) 
   212     {
   213       w.WriteLine("{0}|", space);
   214       w.WriteLine("{0}+- {1} ({2})",
   215         space, hardware.Name, hardware.Identifier);
   216       ISensor[] sensors = hardware.Sensors;
   217       Array.Sort(sensors, CompareSensor);
   218       foreach (ISensor sensor in sensors) {
   219         w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
   220           space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
   221           sensor.Identifier);
   222       }
   223       foreach (IHardware subHardware in hardware.SubHardware)
   224         ReportHardwareSensorTree(subHardware, w, "|  ");
   225     }
   226 
   227     private static void ReportHardwareParameterTree(
   228       IHardware hardware, TextWriter w, string space) {
   229       w.WriteLine("{0}|", space);
   230       w.WriteLine("{0}+- {1} ({2})",
   231         space, hardware.Name, hardware.Identifier);
   232       ISensor[] sensors = hardware.Sensors;
   233       Array.Sort(sensors, CompareSensor);
   234       foreach (ISensor sensor in sensors) {
   235         string innerSpace = space + "|  ";
   236         if (sensor.Parameters.Length > 0) {
   237           w.WriteLine("{0}|", innerSpace);
   238           w.WriteLine("{0}+- {1} ({2})",
   239             innerSpace, sensor.Name, sensor.Identifier);
   240           foreach (IParameter parameter in sensor.Parameters) {
   241             string innerInnerSpace = innerSpace + "|  ";
   242             w.WriteLine("{0}+- {1} : {2}",
   243               innerInnerSpace, parameter.Name,
   244               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   245                 parameter.DefaultValue, parameter.Value));
   246           }
   247         }
   248       }
   249       foreach (IHardware subHardware in hardware.SubHardware)
   250         ReportHardwareParameterTree(subHardware, w, "|  ");
   251     }
   252 
   253     private static void ReportHardware(IHardware hardware, TextWriter w) {
   254       string hardwareReport = hardware.GetReport();
   255       if (!string.IsNullOrEmpty(hardwareReport)) {
   256         NewSection(w);
   257         w.Write(hardwareReport);
   258       }
   259       foreach (IHardware subHardware in hardware.SubHardware)
   260         ReportHardware(subHardware, w);
   261     }
   262 
   263     public string GetReport() {
   264 
   265       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   266 
   267         w.WriteLine();
   268         w.WriteLine("Open Hardware Monitor Report");
   269         w.WriteLine();
   270 
   271         Version version = typeof(Computer).Assembly.GetName().Version;
   272 
   273         NewSection(w);
   274         w.Write("Version: "); w.WriteLine(version.ToString());
   275         w.WriteLine();
   276 
   277         NewSection(w);
   278         w.Write("Common Language Runtime: ");
   279         w.WriteLine(Environment.Version.ToString());
   280         w.Write("Operating System: ");
   281         w.WriteLine(Environment.OSVersion.ToString());
   282         w.Write("Process Type: ");
   283         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   284         w.WriteLine();
   285 
   286         string r = Ring0.GetReport();
   287         if (r != null) {
   288           NewSection(w);
   289           w.Write(r);
   290           w.WriteLine();
   291         }
   292 
   293         NewSection(w);
   294         w.WriteLine("Sensors");
   295         w.WriteLine();
   296         foreach (IGroup group in groups) {
   297           foreach (IHardware hardware in group.Hardware)
   298             ReportHardwareSensorTree(hardware, w, "");
   299         }
   300         w.WriteLine();
   301 
   302         NewSection(w);
   303         w.WriteLine("Parameters");
   304         w.WriteLine();
   305         foreach (IGroup group in groups) {
   306           foreach (IHardware hardware in group.Hardware)
   307             ReportHardwareParameterTree(hardware, w, "");
   308         }
   309         w.WriteLine();
   310 
   311         foreach (IGroup group in groups) {
   312           string report = group.GetReport();
   313           if (!string.IsNullOrEmpty(report)) {
   314             NewSection(w);
   315             w.Write(report);
   316           }
   317 
   318           IHardware[] hardwareArray = group.Hardware;
   319           foreach (IHardware hardware in hardwareArray)
   320             ReportHardware(hardware, w);
   321 
   322         }
   323         return w.ToString();
   324       }
   325     }
   326 
   327     public void Close() {      
   328       if (!open)
   329         return;
   330 
   331       while (groups.Count > 0) {
   332         IGroup group = groups[groups.Count - 1];
   333         Remove(group);         
   334       } 
   335 
   336       Opcode.Close();
   337       Ring0.Close();
   338 
   339       open = false;
   340     }
   341 
   342     public event HardwareEventHandler HardwareAdded;
   343     public event HardwareEventHandler HardwareRemoved;
   344 
   345     public void Accept(IVisitor visitor) {
   346       if (visitor == null)
   347         throw new ArgumentNullException("visitor");
   348       visitor.VisitComputer(this);
   349     }
   350 
   351     public void Traverse(IVisitor visitor) {
   352       foreach (IGroup group in groups)
   353         foreach (IHardware hardware in group.Hardware) 
   354           hardware.Accept(visitor);
   355     }
   356 
   357     private class Settings : ISettings {
   358 
   359       public bool Contains(string name) {
   360         return false;
   361       }
   362 
   363       public void SetValue(string name, string value) { }
   364 
   365       public string GetValue(string name, string value) {
   366         return value;
   367       }
   368 
   369       public void Remove(string name) { }
   370     }
   371   }
   372 }