Skip to content

Commit 5419239

Browse files
committed
Added controls to report.
1 parent 8d569ee commit 5419239

File tree

2 files changed

+178
-140
lines changed

2 files changed

+178
-140
lines changed

IPR.Hardware.Tools/Hardware/Computer.cs

Lines changed: 2 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace IPR.Hardware.Tools.Hardware
3434
internal class Computer : IComputer
3535
{
3636
private readonly List<IGroup> _groups = new();
37+
internal List<IGroup> Groups { get { return _groups; } }
3738
private readonly object _lock = new();
3839
private CancellationTokenSource _cancellationTokenSource = new ();
3940

@@ -286,69 +287,7 @@ public SMBios SMBios
286287
///
287288
public string GetReport()
288289
{
289-
lock (_lock)
290-
{
291-
using StringWriter w = new(CultureInfo.InvariantCulture);
292-
293-
w.WriteLine();
294-
w.WriteLine(nameof(IPR.Hardware.Tools) + " Report");
295-
w.WriteLine();
296-
297-
Version version = typeof(Computer).Assembly.GetName().Version;
298-
299-
NewSection(w);
300-
w.Write("Version: ");
301-
w.WriteLine(version.ToString());
302-
w.WriteLine();
303-
304-
NewSection(w);
305-
w.Write("Common Language Runtime: ");
306-
w.WriteLine(Environment.Version.ToString());
307-
w.Write("Operating System: ");
308-
w.WriteLine(Environment.OSVersion.ToString());
309-
w.Write("Process Type: ");
310-
w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
311-
w.WriteLine();
312-
313-
NewSection(w);
314-
w.WriteLine("Sensors");
315-
w.WriteLine();
316-
317-
foreach (IGroup group in _groups)
318-
{
319-
foreach (IHardware hardware in group.Hardware)
320-
ReportHardwareSensorTree(hardware, w, string.Empty);
321-
}
322-
323-
w.WriteLine();
324-
325-
NewSection(w);
326-
w.WriteLine("Parameters");
327-
w.WriteLine();
328-
329-
foreach (IGroup group in _groups)
330-
{
331-
foreach (IHardware hardware in group.Hardware)
332-
ReportHardwareParameterTree(hardware, w, string.Empty);
333-
}
334-
335-
w.WriteLine();
336-
337-
foreach (IGroup group in _groups)
338-
{
339-
string report = group.GetReport();
340-
if (!string.IsNullOrEmpty(report))
341-
{
342-
NewSection(w);
343-
w.Write(report);
344-
}
345-
346-
foreach (IHardware hardware in (IEnumerable<IHardware>)group.Hardware)
347-
ReportHardware(hardware, w);
348-
}
349-
350-
return w.ToString();
351-
}
290+
return Report.GetReport(this);
352291
}
353292

354293
private void Add(IGroup group)
@@ -456,83 +395,6 @@ private void AddGroups()
456395
Add(new BatteryGroup());
457396
}
458397

459-
private static void NewSection(TextWriter writer)
460-
{
461-
for (int i = 0; i < 8; i++)
462-
writer.Write("----------");
463-
464-
writer.WriteLine();
465-
writer.WriteLine();
466-
}
467-
468-
private static int CompareSensor(ISensor a, ISensor b)
469-
{
470-
int c = a.SensorType.CompareTo(b.SensorType);
471-
if (c == 0)
472-
return a.Index.CompareTo(b.Index);
473-
474-
return c;
475-
}
476-
477-
private static void ReportHardwareSensorTree(IHardware hardware, TextWriter w, string space)
478-
{
479-
w.WriteLine("{0}|", space);
480-
w.WriteLine("{0}+- {1} ({2})", space, hardware.Name, hardware.Identifier);
481-
482-
ISensor[] sensors = hardware.Sensors;
483-
Array.Sort(sensors, CompareSensor);
484-
485-
foreach (ISensor sensor in sensors)
486-
w.WriteLine("{0}| +- {1,-14} : {2,8:G6} ({3})", space, sensor.Name, sensor.Value, sensor.Identifier);
487-
488-
foreach (IHardware subHardware in hardware.SubHardware)
489-
ReportHardwareSensorTree(subHardware, w, "| ");
490-
}
491-
492-
private static void ReportHardwareParameterTree(IHardware hardware, TextWriter w, string space)
493-
{
494-
w.WriteLine("{0}|", space);
495-
w.WriteLine("{0}+- {1} ({2})", space, hardware.Name, hardware.Identifier);
496-
497-
ISensor[] sensors = hardware.Sensors;
498-
Array.Sort(sensors, CompareSensor);
499-
500-
foreach (ISensor sensor in sensors)
501-
{
502-
string innerSpace = space + "| ";
503-
if (sensor.Parameters.Count > 0)
504-
{
505-
w.WriteLine("{0}|", innerSpace);
506-
w.WriteLine("{0}+- {1} ({2})", innerSpace, sensor.Name, sensor.Identifier);
507-
508-
foreach (IParameter parameter in sensor.Parameters)
509-
{
510-
string innerInnerSpace = innerSpace + "| ";
511-
if (parameter.ParameterType == ParameterType.Value)
512-
w.WriteLine("{0}+- {1} : {2}", innerInnerSpace, parameter.Name, parameter.Value);
513-
else
514-
w.WriteLine("{0}+- {1} : {2} - {3}", innerInnerSpace, parameter.Name, parameter.MinValue, parameter.MaxValue);
515-
}
516-
}
517-
}
518-
519-
foreach (IHardware subHardware in hardware.SubHardware)
520-
ReportHardwareParameterTree(subHardware, w, "| ");
521-
}
522-
523-
private static void ReportHardware(IHardware hardware, TextWriter w)
524-
{
525-
string hardwareReport = hardware.GetReport();
526-
if (!string.IsNullOrEmpty(hardwareReport))
527-
{
528-
NewSection(w);
529-
w.Write(hardwareReport);
530-
}
531-
532-
foreach (IHardware subHardware in hardware.SubHardware)
533-
ReportHardware(subHardware, w);
534-
}
535-
536398
/// <inheritdoc/>
537399
public void Monitor()
538400
{
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)