123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;
- using System.Text.RegularExpressions;
- using Vinno.IUS.Common.Log;
- namespace Vinno.vCloud.Terminal
- {
- public enum CpuLevel
- {
- Unknown,
- High,
- Normal,
- Low,
- NormalLow,
- Lowest,
- LowestPlus
- }
- public class CpuInfo
- {
- private readonly static Dictionary<string, CpuLevel> _cpuLevels = new Dictionary<string, CpuLevel>()
- {
- { "I3-4160", CpuLevel.Normal },
- { "I3-4170", CpuLevel.Normal},
- { "I5-4570", CpuLevel.High},
- { "I5-4590", CpuLevel.High},
- { "I3-6100", CpuLevel.Normal},
- { "I5-6500", CpuLevel.High},
- { "G5400", CpuLevel.Normal},
- { "I3-8100", CpuLevel.Normal},
- { "I5-8400", CpuLevel.High},
- { "I7-4650U", CpuLevel.Low},
- { "I3-6100U", CpuLevel.Low},
- { "I5-6300U", CpuLevel.NormalLow},
- { "I3-7100U", CpuLevel.NormalLow},
- { "PENTIUM(R)", CpuLevel.LowestPlus},
- { "I7-9700", CpuLevel.High}
- };
- private static CpuLevel _currentCpuLevel = CpuLevel.Unknown;
- private static string _processorId;
- private static string _machineId;
- public static CpuLevel CurrentCpuLevel
- {
- get
- {
- return _currentCpuLevel;
- }
- }
- /// <summary>
- /// Initialize to cpuinfo ,must to initialize for app
- /// </summary>
- public static void Initialize()
- {
- _currentCpuLevel =GetCpuLevel();
- _machineId = GetSystemDiskNo();
- }
-
- /// <summary>
- /// Processor ID
- /// </summary>
- public static string Id
- {
- get
- {
- if (string.IsNullOrEmpty(_processorId))
- {
- _processorId = GetCpuID();
- }
- return _processorId;
- }
- }
- /// <summary>
- /// Machine ID
- /// </summary>
- public static string MachineId
- {
- get
- {
- if (string.IsNullOrEmpty(_machineId))
- {
- _machineId = GetSystemDiskNo();
- }
- return _machineId;
- }
- }
- private static CpuLevel GetCpuLevel()
- {
- var cpuLevel = CpuLevel.Lowest;
- try
- {
- var cpuName = GetCpuName();//spend 200ms-1s.
- Logger.WriteLineInfo($"CpuName:{cpuName}");
- if (!string.IsNullOrEmpty(cpuName))
- {
- if (_cpuLevels.ContainsKey(cpuName))
- {
- cpuLevel = _cpuLevels[cpuName];
- }
- else
- {
- var cpus = cpuName.Split('-');
- if (cpus.Length > 1)
- {
- var cpuCoreLevel = Convert.ToInt32(Regex.Replace(cpus[0], @"[^\d.\d]", ""));
- if (cpuCoreLevel > 5)
- {
- cpuLevel = CpuLevel.High;
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Terminal GetCpuLevel failed ,to set { CpuLevel.Lowest} ex:{ex}");
- }
- return cpuLevel;
- }
- private static string GetCpuName()
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
- var cpus = searcher.Get();
- foreach (var cpu in cpus)
- {
- var cpuName = cpu["Name"].ToString();
- //Try get cpu level.
- var keywords = cpuName.Split(' ');
- var keywordList = new List<string>();
- foreach (var keyword in keywords)
- {
- keywordList.Add(keyword.Trim());
- }
- var cpuKeyWordIndex = keywordList.IndexOf("CPU");
- if (cpuKeyWordIndex != -1)
- {
- if (cpuKeyWordIndex - 1 >= 0)
- {
- var cpuModelKeyword = keywordList[cpuKeyWordIndex - 1];
- return cpuModelKeyword.ToUpper();
- }
- }
- }
- return null;
- }
- static string GetCpuID()
- {
- try
- {
- string processorId = "";//cpu ID
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- processorId = mo.Properties["ProcessorId"].Value.ToString();
- if (!string.IsNullOrEmpty(processorId))
- {
- break;
- }
- }
- moc.Dispose();
- moc = null;
- mc.Dispose();
- mc = null;
- return processorId;
- }
- catch(Exception e)
- {
- Logger.WriteLineError($"Get ProcessorId ex:{e}");
- }
- return string.Empty;
- }
- /// <summary>
- /// 获取系统硬盘ID
- /// </summary>
- /// <returns></returns>
- public static string GetSystemDiskNo()
- {
- try
- {
- ManagementClass cimObject = new ManagementClass("Win32_PhysicalMedia");
- ManagementObjectCollection moc = cimObject.GetInstances();
- Dictionary<string, string> dict = new Dictionary<string, string>();
- foreach (ManagementObject mo in moc)
- {
- string tag = mo.Properties["Tag"].Value.ToString().ToLower().Trim();
- string hdId = (string)mo.Properties["SerialNumber"].Value ?? string.Empty;
- hdId = hdId.Trim();
- dict.Add(tag, hdId);
- }
- cimObject = new ManagementClass("Win32_OperatingSystem");
- moc = cimObject.GetInstances();
- string currentSysRunDisk = string.Empty;
- foreach (ManagementObject mo in moc)
- {
- currentSysRunDisk = Regex.Match(mo.Properties["Name"].Value.ToString().ToLower(), @"harddisk\d+").Value;
- }
- var results = dict.Where(x => Regex.IsMatch(x.Key, @"physicaldrive" + Regex.Match(currentSysRunDisk, @"\d+$").Value));
- moc.Dispose();
- moc = null;
- if (results.Any())
- {
- var id = results.ElementAt(0).Value;
- id = Regex.Replace(id, "[^a-zA-Z0-9]", "");
- return id;
- };
- }
- catch (Exception ex)
- {
- }
- return "";
- }
- /// <summary>
- /// 获取主板ID
- /// </summary>
- /// <returns></returns>
- public static string GetMainBordID()
- {
- ManagementClass mc = new ManagementClass("Win32_BaseBoard");
- ManagementObjectCollection moc = mc.GetInstances();
- string strID = null;
- foreach (ManagementObject mo in moc)
- {
- strID = mo.Properties["SerialNumber"].Value.ToString();
- break;
- }
- return strID;
- }
- }
- }
|