Improved the data compression for storing the recorded sensor values in the config file.
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/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
15 using System.Security.Permissions;
16 using System.Reflection;
18 namespace OpenHardwareMonitor.Hardware {
20 public class Computer : IComputer {
22 private readonly List<IGroup> groups = new List<IGroup>();
23 private readonly ISettings settings;
27 private bool mainboardEnabled;
28 private bool cpuEnabled;
29 private bool gpuEnabled;
30 private bool fanControllerEnabled;
31 private bool hddEnabled;
34 this.settings = new Settings();
37 public Computer(ISettings settings) {
38 this.settings = settings ?? new Settings();
41 private void Add(IGroup group) {
42 if (groups.Contains(group))
47 if (HardwareAdded != null)
48 foreach (IHardware hardware in group.Hardware)
49 HardwareAdded(hardware);
52 private void Remove(IGroup group) {
53 if (!groups.Contains(group))
58 if (HardwareRemoved != null)
59 foreach (IHardware hardware in group.Hardware)
60 HardwareRemoved(hardware);
65 private void RemoveType<T>() where T : IGroup {
66 List<IGroup> list = new List<IGroup>();
67 foreach (IGroup group in groups)
70 foreach (IGroup group in list)
74 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
83 Add(new Mainboard.MainboardGroup(settings));
86 Add(new CPU.CPUGroup(settings));
89 Add(new ATI.ATIGroup(settings));
90 Add(new Nvidia.NvidiaGroup(settings));
93 if (fanControllerEnabled) {
94 Add(new TBalancer.TBalancerGroup(settings));
95 Add(new Heatmaster.HeatmasterGroup(settings));
99 Add(new HDD.HarddriveGroup(settings));
104 public bool MainboardEnabled {
105 get { return mainboardEnabled; }
107 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
109 if (open && value != mainboardEnabled) {
111 Add(new Mainboard.MainboardGroup(settings));
113 RemoveType<Mainboard.MainboardGroup>();
115 mainboardEnabled = value;
119 public bool CPUEnabled {
120 get { return cpuEnabled; }
122 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
124 if (open && value != cpuEnabled) {
126 Add(new CPU.CPUGroup(settings));
128 RemoveType<CPU.CPUGroup>();
134 public bool GPUEnabled {
135 get { return gpuEnabled; }
137 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
139 if (open && value != gpuEnabled) {
141 Add(new ATI.ATIGroup(settings));
142 Add(new Nvidia.NvidiaGroup(settings));
144 RemoveType<ATI.ATIGroup>();
145 RemoveType<Nvidia.NvidiaGroup>();
152 public bool FanControllerEnabled {
153 get { return fanControllerEnabled; }
155 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
157 if (open && value != fanControllerEnabled) {
159 Add(new TBalancer.TBalancerGroup(settings));
160 Add(new Heatmaster.HeatmasterGroup(settings));
162 RemoveType<TBalancer.TBalancerGroup>();
163 RemoveType<Heatmaster.HeatmasterGroup>();
166 fanControllerEnabled = value;
170 public bool HDDEnabled {
171 get { return hddEnabled; }
173 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
175 if (open && value != hddEnabled) {
177 Add(new HDD.HarddriveGroup(settings));
179 RemoveType<HDD.HarddriveGroup>();
185 public IHardware[] Hardware {
187 List<IHardware> list = new List<IHardware>();
188 foreach (IGroup group in groups)
189 foreach (IHardware hardware in group.Hardware)
191 return list.ToArray();
195 private static void NewSection(TextWriter writer) {
196 for (int i = 0; i < 8; i++)
197 writer.Write("----------");
202 private static int CompareSensor(ISensor a, ISensor b) {
203 int c = a.SensorType.CompareTo(b.SensorType);
205 return a.Index.CompareTo(b.Index);
210 private static void ReportHardwareSensorTree(
211 IHardware hardware, TextWriter w, string space)
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,
223 foreach (IHardware subHardware in hardware.SubHardware)
224 ReportHardwareSensorTree(subHardware, w, "| ");
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));
249 foreach (IHardware subHardware in hardware.SubHardware)
250 ReportHardwareParameterTree(subHardware, w, "| ");
253 private static void ReportHardware(IHardware hardware, TextWriter w) {
254 string hardwareReport = hardware.GetReport();
255 if (!string.IsNullOrEmpty(hardwareReport)) {
257 w.Write(hardwareReport);
259 foreach (IHardware subHardware in hardware.SubHardware)
260 ReportHardware(subHardware, w);
263 public string GetReport() {
265 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
268 w.WriteLine("Open Hardware Monitor Report");
271 Version version = typeof(Computer).Assembly.GetName().Version;
274 w.Write("Version: "); w.WriteLine(version.ToString());
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");
286 string r = Ring0.GetReport();
294 w.WriteLine("Sensors");
296 foreach (IGroup group in groups) {
297 foreach (IHardware hardware in group.Hardware)
298 ReportHardwareSensorTree(hardware, w, "");
303 w.WriteLine("Parameters");
305 foreach (IGroup group in groups) {
306 foreach (IHardware hardware in group.Hardware)
307 ReportHardwareParameterTree(hardware, w, "");
311 foreach (IGroup group in groups) {
312 string report = group.GetReport();
313 if (!string.IsNullOrEmpty(report)) {
318 IHardware[] hardwareArray = group.Hardware;
319 foreach (IHardware hardware in hardwareArray)
320 ReportHardware(hardware, w);
327 public void Close() {
331 while (groups.Count > 0) {
332 IGroup group = groups[groups.Count - 1];
342 public event HardwareEventHandler HardwareAdded;
343 public event HardwareEventHandler HardwareRemoved;
345 public void Accept(IVisitor visitor) {
347 throw new ArgumentNullException("visitor");
348 visitor.VisitComputer(this);
351 public void Traverse(IVisitor visitor) {
352 foreach (IGroup group in groups)
353 foreach (IHardware hardware in group.Hardware)
354 hardware.Accept(visitor);
357 private class Settings : ISettings {
359 public bool Contains(string name) {
363 public void SetValue(string name, string value) { }
365 public string GetValue(string name, string value) {
369 public void Remove(string name) { }