using OpenHardwareMonitor.Hardware; using System; using System.Collections.Generic; using System.Linq; namespace OpenHardwareMonitor { public class HardwareInfo : IDisposable { private bool _disposed = false; private Computer _computer { get; set; } private IHardware _board { get; set; } public bool Fahrenheit = false; public bool GigaHertz = false; public int Digits = 2; public HardwareInfo(bool cpu, bool fan, bool gpu, bool hdd, bool brd, bool ram) { _computer = new Computer() { CPUEnabled = cpu, FanControllerEnabled = fan, GPUEnabled = gpu, HDDEnabled = hdd, MainboardEnabled = brd, RAMEnabled = ram }; _computer.Open(); if (brd) { _board = GetHardware(HardwareType.Mainboard).FirstOrDefault(); _board.Update(); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _computer.Close(); _computer = null; _board = null; } _disposed = true; } } ~HardwareInfo() { Dispose(false); } private IEnumerable GetHardware(params HardwareType[] types) { return _computer.Hardware.Where(h => types.Contains(h.HardwareType)); } public string GetReport() { return _computer.GetReport(); } public override string ToString() { string info = null; foreach (var hardware in _computer.Hardware) { hardware.Update(); HardwareType hwtype = hardware.HardwareType; info += String.Format("{0}:{1}", hardware.Name, Environment.NewLine); foreach (var sensor in hardware.Sensors) { if (sensor.Value != null) info += String.Format(" {0}: {1}{2}", sensor.Name, NormalizeValue(sensor), Environment.NewLine); } if (hardware.SubHardware != null) { foreach (var subhardware in hardware.SubHardware) { info += String.Format("{0}:{1}", subhardware.Name, Environment.NewLine); foreach (var sensor in subhardware.Sensors) { if (sensor.Value != null) info += String.Format(" {0}: {1}{2}", sensor.Name, NormalizeValue(sensor), Environment.NewLine); } } } info += Environment.NewLine; } return info; } private string NormalizeValue(ISensor sensor) { DataType dt = DataType.Dynamic; double value = (double)sensor.Value; if (sensor.SensorType == SensorType.Temperature && Fahrenheit) CelciusToFahrenheit.Instance.Convert(ref value, out value, out dt); if (sensor.SensorType == SensorType.Clock && GigaHertz) MHzToGHz.Instance.Convert(ref value, out value, out dt); return String.Format("{0}{1}", (decimal)Math.Round(value, Digits), GetValueType(sensor.SensorType).GetAppend()); } private DataType GetValueType(SensorType stype) { DataType dt = DataType.Dynamic; switch (stype) { default: case SensorType.Load: case SensorType.Control: case SensorType.Level: case SensorType.Flow: case SensorType.Factor: case SensorType.Power: dt = DataType.Percent; break; case SensorType.Voltage: dt = DataType.Voltage; break; case SensorType.Clock: if (GigaHertz) dt = DataType.GHz; else dt = DataType.MHz; break; case SensorType.Temperature: if (Fahrenheit) dt = DataType.Fahrenheit; else dt = DataType.Celcius; break; case SensorType.Fan: dt = DataType.RPM; break; case SensorType.Data: dt = DataType.Gigabyte; break; case SensorType.SmallData: dt = DataType.Megabyte; break; } return dt; } } public static class Extensions { public static string GetAppend(this DataType type) { switch (type) { case DataType.Bit: return " b"; case DataType.Kilobit: return " kb"; case DataType.Megabit: return " mb"; case DataType.Gigabit: return " gb"; case DataType.Byte: return " B"; case DataType.Kilobyte: return " KB"; case DataType.Megabyte: return " MB"; case DataType.Gigabyte: return " GB"; case DataType.bps: return " bps"; case DataType.kbps: return " kbps"; case DataType.Mbps: return " Mbps"; case DataType.Gbps: return " Gbps"; case DataType.Bps: return " B/s"; case DataType.kBps: return " kB/s"; case DataType.MBps: return " MB/s"; case DataType.GBps: return " GB/s"; case DataType.MHz: return " MHz"; case DataType.GHz: return " GHz"; case DataType.Voltage: return " V"; case DataType.Percent: return "%"; case DataType.RPM: return " RPM"; case DataType.Celcius: return " C"; case DataType.Fahrenheit: return " F"; case DataType.Dynamic: case DataType.IP: return string.Empty; default: throw new ArgumentException("Invalid DataType."); } } public static double Round(this double value) { return Math.Round(value, 2); } } public enum DataType : byte { Dynamic, Bit, Kilobit, Megabit, Gigabit, Byte, Kilobyte, Megabyte, Gigabyte, bps, kbps, Mbps, Gbps, Bps, kBps, MBps, GBps, MHz, GHz, Voltage, Percent, RPM, Celcius, Fahrenheit, IP } public interface iConverter { void Convert(ref double value); void Convert(ref double value, out double normalized, out DataType targetType); DataType TargetType { get; } bool IsDynamic { get; } } public class CelciusToFahrenheit : iConverter { private CelciusToFahrenheit() { } public void Convert(ref double value) { value = value * 1.8d + 32d; } public void Convert(ref double value, out double normalized, out DataType targetType) { Convert(ref value); normalized = value; targetType = TargetType; } public DataType TargetType { get { return DataType.Fahrenheit; } } public bool IsDynamic { get { return false; } } private static CelciusToFahrenheit _instance = null; public static CelciusToFahrenheit Instance { get { if (_instance == null) { _instance = new CelciusToFahrenheit(); } return _instance; } } } public class MHzToGHz : iConverter { private MHzToGHz() { } public void Convert(ref double value) { value = value / 1000d; } public void Convert(ref double value, out double normalized, out DataType targetType) { Convert(ref value); normalized = value; targetType = TargetType; } public DataType TargetType { get { return DataType.GHz; } } public bool IsDynamic { get { return false; } } private static MHzToGHz _instance = null; public static MHzToGHz Instance { get { if (_instance == null) { _instance = new MHzToGHz(); } return _instance; } } } public class BitsPerSecondConverter : iConverter { private BitsPerSecondConverter() { } public void Convert(ref double value) { double _normalized; DataType _dataType; Convert(ref value, out _normalized, out _dataType); } public void Convert(ref double value, out double normalized, out DataType targetType) { normalized = value /= 128d; if (value < 1024d) { targetType = DataType.kbps; return; } else if (value < 1048576d) { value /= 1024d; targetType = DataType.Mbps; return; } else { value /= 1048576d; targetType = DataType.Gbps; return; } } public DataType TargetType { get { return DataType.kbps; } } public bool IsDynamic { get { return true; } } private static BitsPerSecondConverter _instance = null; public static BitsPerSecondConverter Instance { get { if (_instance == null) { _instance = new BitsPerSecondConverter(); } return _instance; } } } public class BytesPerSecondConverter : iConverter { private BytesPerSecondConverter() { } public void Convert(ref double value) { double _normalized; DataType _dataType; Convert(ref value, out _normalized, out _dataType); } public void Convert(ref double value, out double normalized, out DataType targetType) { normalized = value /= 1024d; if (value < 1024d) { targetType = DataType.kBps; return; } else if (value < 1048576d) { value /= 1024d; targetType = DataType.MBps; return; } else { value /= 1048576d; targetType = DataType.GBps; return; } } public DataType TargetType { get { return DataType.kBps; } } public bool IsDynamic { get { return true; } } private static BytesPerSecondConverter _instance = null; public static BytesPerSecondConverter Instance { get { if (_instance == null) { _instance = new BytesPerSecondConverter(); } return _instance; } } } }