|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace IPR.Hardware.Tools.Hardware |
| 10 | +{ |
| 11 | + internal static class Report |
| 12 | + { |
| 13 | + public static string GetReport(Computer computer) |
| 14 | + { |
| 15 | + using StringWriter w = new(CultureInfo.InvariantCulture); |
| 16 | + |
| 17 | + w.WriteLine(); |
| 18 | + w.WriteLine(nameof(IPR.Hardware.Tools) + " Report"); |
| 19 | + w.WriteLine(); |
| 20 | + |
| 21 | + Version version = typeof(Computer).Assembly.GetName().Version; |
| 22 | + |
| 23 | + NewSection(w); |
| 24 | + w.Write("Version: "); |
| 25 | + w.WriteLine(version.ToString()); |
| 26 | + w.WriteLine(); |
| 27 | + |
| 28 | + NewSection(w); |
| 29 | + w.Write("Common Language Runtime: "); |
| 30 | + w.WriteLine(Environment.Version.ToString()); |
| 31 | + w.Write("Operating System: "); |
| 32 | + w.WriteLine(Environment.OSVersion.ToString()); |
| 33 | + w.Write("Process Type: "); |
| 34 | + w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit"); |
| 35 | + w.WriteLine(); |
| 36 | + |
| 37 | + NewSection(w); |
| 38 | + w.WriteLine("Sensors"); |
| 39 | + w.WriteLine(); |
| 40 | + |
| 41 | + foreach (IGroup group in computer.Groups) |
| 42 | + { |
| 43 | + foreach (IHardware hardware in group.Hardware) |
| 44 | + ReportHardwareSensorTree(hardware, w, string.Empty); |
| 45 | + } |
| 46 | + |
| 47 | + w.WriteLine(); |
| 48 | + |
| 49 | + NewSection(w); |
| 50 | + w.WriteLine("Sensors Parameters"); |
| 51 | + w.WriteLine(); |
| 52 | + |
| 53 | + foreach (IGroup group in computer.Groups) |
| 54 | + { |
| 55 | + foreach (IHardware hardware in group.Hardware) |
| 56 | + ReportHardwareSensorParameterTree(hardware, w, string.Empty); |
| 57 | + } |
| 58 | + |
| 59 | + NewSection(w); |
| 60 | + w.WriteLine("Controls"); |
| 61 | + w.WriteLine(); |
| 62 | + |
| 63 | + foreach (IGroup group in computer.Groups) |
| 64 | + { |
| 65 | + foreach (IHardware hardware in group.Hardware) |
| 66 | + ReportHardwareControlTree(hardware, w, string.Empty); |
| 67 | + } |
| 68 | + |
| 69 | + w.WriteLine(); |
| 70 | + |
| 71 | + foreach (IGroup group in computer.Groups) |
| 72 | + { |
| 73 | + string report = group.GetReport(); |
| 74 | + if (!string.IsNullOrEmpty(report)) |
| 75 | + { |
| 76 | + NewSection(w); |
| 77 | + w.Write(report); |
| 78 | + } |
| 79 | + |
| 80 | + foreach (IHardware hardware in (IEnumerable<IHardware>)group.Hardware) |
| 81 | + ReportHardware(hardware, w); |
| 82 | + } |
| 83 | + |
| 84 | + return w.ToString(); |
| 85 | + } |
| 86 | + |
| 87 | + private static void ReportHardwareSensorTree(IHardware hardware, TextWriter w, string space) |
| 88 | + { |
| 89 | + w.WriteLine("{0}|", space); |
| 90 | + w.WriteLine("{0}+- {1} ({2})", space, hardware.Name, hardware.Identifier); |
| 91 | + |
| 92 | + ISensor[] sensors = hardware.Sensors |
| 93 | + .OrderBy(x => x.SensorType) |
| 94 | + .ThenBy(x => x.Index) |
| 95 | + .ToArray(); |
| 96 | + |
| 97 | + foreach (ISensor sensor in sensors) |
| 98 | + w.WriteLine("{0}| +- {1,-14} : {2,8:G6} ({3})", space, sensor.Name, sensor.Value, sensor.Identifier); |
| 99 | + |
| 100 | + foreach (IHardware subHardware in hardware.SubHardware) |
| 101 | + ReportHardwareSensorTree(subHardware, w, "| "); |
| 102 | + } |
| 103 | + |
| 104 | + private static void ReportHardwareControlTree(IHardware hardware, TextWriter w, string space) |
| 105 | + { |
| 106 | + w.WriteLine("{0}|", space); |
| 107 | + w.WriteLine("{0}+- {1} ({2})", space, hardware.Name, hardware.Identifier); |
| 108 | + |
| 109 | + IControl[] sensors = hardware.Controls |
| 110 | + .OrderBy(x => x.ControlType) |
| 111 | + .ThenBy(x => x.Index) |
| 112 | + .ToArray(); |
| 113 | + |
| 114 | + foreach (IControl sensor in sensors) |
| 115 | + w.WriteLine("{0}| +- {1,-14} : {2,8:G6} ({3})", space, sensor.Name, sensor.Value, sensor.Identifier); |
| 116 | + |
| 117 | + foreach (IHardware subHardware in hardware.SubHardware) |
| 118 | + ReportHardwareControlTree(subHardware, w, "| "); |
| 119 | + } |
| 120 | + |
| 121 | + private static void ReportHardwareSensorParameterTree(IHardware hardware, TextWriter w, string space) |
| 122 | + { |
| 123 | + w.WriteLine("{0}|", space); |
| 124 | + w.WriteLine("{0}+- {1} ({2})", space, hardware.Name, hardware.Identifier); |
| 125 | + |
| 126 | + ISensor[] sensors = hardware.Sensors |
| 127 | + .OrderBy(x => x.SensorType) |
| 128 | + .ThenBy(x => x.Index) |
| 129 | + .ToArray(); |
| 130 | + |
| 131 | + foreach (ISensor sensor in sensors) |
| 132 | + { |
| 133 | + string innerSpace = space + "| "; |
| 134 | + if (sensor.Parameters.Count > 0) |
| 135 | + { |
| 136 | + w.WriteLine("{0}|", innerSpace); |
| 137 | + w.WriteLine("{0}+- {1} ({2})", innerSpace, sensor.Name, sensor.Identifier); |
| 138 | + |
| 139 | + foreach (IParameter parameter in sensor.Parameters) |
| 140 | + { |
| 141 | + string innerInnerSpace = innerSpace + "| "; |
| 142 | + if (parameter.ParameterType == ParameterType.Value) |
| 143 | + w.WriteLine("{0}+- {1} : {2}", innerInnerSpace, parameter.Name, parameter.Value); |
| 144 | + else |
| 145 | + w.WriteLine("{0}+- {1} : {2} - {3}", innerInnerSpace, parameter.Name, parameter.MinValue, parameter.MaxValue); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + foreach (IHardware subHardware in hardware.SubHardware) |
| 151 | + ReportHardwareSensorParameterTree(subHardware, w, "| "); |
| 152 | + } |
| 153 | + |
| 154 | + private static void ReportHardware(IHardware hardware, TextWriter w) |
| 155 | + { |
| 156 | + string hardwareReport = hardware.GetReport(); |
| 157 | + if (!string.IsNullOrEmpty(hardwareReport)) |
| 158 | + { |
| 159 | + NewSection(w); |
| 160 | + w.Write(hardwareReport); |
| 161 | + } |
| 162 | + |
| 163 | + foreach (IHardware subHardware in hardware.SubHardware) |
| 164 | + ReportHardware(subHardware, w); |
| 165 | + } |
| 166 | + |
| 167 | + private static void NewSection(TextWriter writer) |
| 168 | + { |
| 169 | + for (int i = 0; i < 8; i++) |
| 170 | + writer.Write("----------"); |
| 171 | + |
| 172 | + writer.WriteLine(); |
| 173 | + writer.WriteLine(); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments