-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputerSystem.cs
197 lines (178 loc) · 8.39 KB
/
ComputerSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace powerLabel
{
public class ComputerSystem
{
public int id { get; set; }
public Motherboard motherboard { get; set; }
public Bios bios { get; set; }
public int processorAmount { get; set; }
public Processor processor { get; set; }
public List<MemoryConfig> memoryModules { get; set; }
public List<DiskConfig> diskConfigs { get; set; }
public List<VideoControllerConfig> videoControllerConfigs { get; set; }
public OS operatingSystem { get; set; }
public static ComputerSystem system { get; set; }
public static ComputerSystem GetSystem()
{
system = new ComputerSystem();
system.motherboard = Motherboard.GetMotherboard();
system.bios = Bios.GetBios();
system.processor = Processor.GetProcessor(system);
system.memoryModules = MemoryConfig.GetMemory(system);
system.diskConfigs = DiskConfig.GetDisks(system);
system.videoControllerConfigs = VideoControllerConfig.GetVideoControllers(system);
system.operatingSystem = OS.GetOS();
using (var db = new ComputerSystemContext())
{
if (db.ComputerSystems
.Include(a => a.motherboard)
.Include(a => a.bios)
.Include(a => a.processor)
.Include(a => a.memoryModules).ThenInclude(a => a.module)
.Include(a => a.diskConfigs).ThenInclude(a => a.disk)
.Include(a => a.videoControllerConfigs).ThenInclude(a => a.videoController)
.Include(a => a.operatingSystem)
.ToList()
.Any(a => a.Equals(system)))
{
//This system config has been scanned before
//List of all the events with this motherboard ordered by date
List<Event> events = db.Events.ToList().Where(a => a.computerSystem.motherboard.id == db.Motherboards.ToList().Where(b => b.Equals(system.motherboard)).First().id).ToList().OrderBy(a => a.date).ToList();
//The last config this motherboard had
ComputerSystem lastConfig = db.ComputerSystems.ToList().Where(a => a.id == events.Last().computerSystem.id).Single();
if (system.Equals(lastConfig))
{
//The config hasn't changed since the last system scan, so set the system to the last config (in order to have the correct id's)
system = lastConfig;
}
}
}
ComputerSystem.system = system;
return system;
}
public string getString()
{
if (system == null)
{
return "";
}
// Model Label string processing
string modelString = motherboard.model;
modelString = getShortString(modelString, new string[] {
@"(?<ZLine>HP Z\w+)(?:(?!G)[a-zA-Z ])*(?<screensize>\d{2}\w?)?(?:(?![G])[A-Za-z .\d])*(?<generation>G\d)?", // HP Systems (HP Z840, HP ZBook 15 G3, HP ZBook 14U G5)
@"Precision \w* \w*" // DELL Systems (Precision WorkStation T3500, Precision Tower 3620)
});
// CPU Label string processing
string cpuString = processor.name;
cpuString = getShortString(cpuString, new string[] {
@"(Platinum|Gold|Silver|Bronze)(?: )(\w*-*\d+\w*)", // Xeon gold, silver etc.
@"(\w+-*\d{3,}\w*)(?: )*(v\d)*", // Core i and Xeon non metal
});
if (processorAmount > 1)
{
cpuString = cpuString.Insert(0, "2x ");
}
// RAM Label string processing
string ramString = memoryModules.Sum(item => Convert.ToInt64(item.module.capacity)) / 1073741824 + "GB (" + memoryModules.Count + ") " + MemoryModule.memoryTypeLookup[memoryModules.First().module.memoryType];
// Disk Label string processing
string diskString = "";
List<string> disks = new List<string>();
List<string> doneDisks = new List<string>();
foreach (DiskConfig disk in system.diskConfigs)
{
disks.Add(disk.ToString());
}
foreach (string disk in disks)
{
if (!doneDisks.Any(a => a == disk))
{
if (disks.Where(a => a == disk).Count() > 1)
{
int multiplier = disks.Where(a => a == disk).Count();
diskString += $"{multiplier}x {disk}.";
doneDisks.Add(disk);
}
else
{
diskString += disk + ".";
doneDisks.Add(disk);
}
}
}
// GPU Label string processing
string gpuString = "";
foreach (VideoControllerConfig gpu in videoControllerConfigs)
{
gpuString += getShortString(gpu.videoController.name, new string[] {
@"\w{2,3} Graphics \w+", // Intel intergrated graphics (HD Graphics 405, Pro Graphics 600)
@"(Quadro|RTX) *(\w+) ?(\d+)?", // Quadro's (Quadro RTX 4000, Quadro K2200, Quadro M2000M)
@"(GeForce) (\wTX?) (\d{3,})(?: (\w+))*" // Nvidia GeForce GTX / RTX 3060 Ti
}) + "\r\n";
}
// Final string formatting
// '.' will be replaced by zero width space
// spaces will be replaced with a non brakeable space
return modelString + "\r\n." + cpuString + " | " + ramString + "\r\n." + diskString + " ." + gpuString;
}
public static string getShortString(string input, string[] regexes)
{
Regex[] regex = new Regex[regexes.Length];
int i = 0;
foreach (string str in regexes)
{
regex[i] = new Regex(@str);
i++;
}
foreach (Regex item in regex)
{
if (item.IsMatch(input))
{
var match = item.Match(input);
if (match.Groups.Count == 1)
return match.Groups[0].Value;
var s = "";
for(int j = 1; j < match.Groups.Count; j++)
{
s += match.Groups[j].Value;
if (j < match.Groups.Count)
{
s += " ";
}
}
return s;
}
}
return input;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
ComputerSystem sys = (ComputerSystem)obj;
try
{
if (((this.motherboard == null && sys.motherboard == null) || this.motherboard.Equals(sys.motherboard)) &&
((this.bios == null && sys.bios == null) || this.bios.Equals(sys.bios)) &&
(this.processorAmount == sys.processorAmount) &&
((this.processor == null && sys.processor == null) || this.processor.Equals(sys.processor)) &&
((this.memoryModules == null && sys.memoryModules == null) || this.memoryModules.SequenceEqual(sys.memoryModules)) &&
((this.diskConfigs == null && sys.diskConfigs == null) || this.diskConfigs.SequenceEqual(sys.diskConfigs)) &&
((this.videoControllerConfigs == null && sys.videoControllerConfigs == null) || this.videoControllerConfigs.SequenceEqual(sys.videoControllerConfigs)) &&
((this.operatingSystem == null && sys.operatingSystem == null) || this.operatingSystem.Equals(sys.operatingSystem))
)
{
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
}
}