Skip to content

Commit

Permalink
屏蔽掉无法访问的数据,避免刷新时反复访问浪费资源
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Mar 23, 2021
1 parent 0ce1471 commit 0382228
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ private void LoadLinuxInfo()
}
}

private ICollection<String> _excludes = new List<String>();

/// <summary>获取实时数据,如CPU、内存、温度</summary>
public void Refresh()
{
Expand Down Expand Up @@ -436,33 +438,54 @@ public void Refresh()
CpuRate = total == 0 ? 0 : ((Single)(total - idle) / total);

#if __CORE__
var temp = ReadWmic(@"/namespace:\\root\wmi path MSAcpi_ThermalZoneTemperature", "CurrentTemperature");
if (temp != null)
if (!_excludes.Contains(nameof(Temperature)))
{
if (temp.TryGetValue("CurrentTemperature", out var str)) Temperature = (str.SplitAsInt().Average() - 2732) / 10.0;
var temp = ReadWmic(@"/namespace:\\root\wmi path MSAcpi_ThermalZoneTemperature", "CurrentTemperature");
if (temp != null && temp.Count > 0)
{
if (temp.TryGetValue("CurrentTemperature", out var str)) Temperature = (str.SplitAsInt().Average() - 2732) / 10.0;
}
else
_excludes.Add(nameof(Temperature));
}

var battery = ReadWmic("path win32_battery", "EstimatedChargeRemaining");
if (battery != null)
if (!_excludes.Contains(nameof(Battery)))
{
if (battery.TryGetValue("EstimatedChargeRemaining", out var str)) Battery = str.SplitAsInt().Average() / 100.0;
var battery = ReadWmic("path win32_battery", "EstimatedChargeRemaining");
if (battery != null && battery.Count > 0)
{
if (battery.TryGetValue("EstimatedChargeRemaining", out var str)) Battery = str.SplitAsInt().Average() / 100.0;
}
else
_excludes.Add(nameof(Battery));
}
#else
// 读取主板温度,不太准。标准方案是ring0通过IOPort读取CPU温度,太难在基础类库实现
var str = GetInfo("Win32_TemperatureProbe", "CurrentReading");
if (!str.IsNullOrEmpty())
if (!_excludes.Contains(nameof(Temperature)))
{
Temperature = str.SplitAsInt().Average();
// 读取主板温度,不太准。标准方案是ring0通过IOPort读取CPU温度,太难在基础类库实现
var str = GetInfo("Win32_TemperatureProbe", "CurrentReading");
if (!str.IsNullOrEmpty())
{
Temperature = str.SplitAsInt().Average();
}
else
{
str = GetInfo("MSAcpi_ThermalZoneTemperature", "CurrentTemperature", "root/wmi");
if (!str.IsNullOrEmpty()) Temperature = (str.SplitAsInt().Average() - 2732) / 10.0;
}

if (str.IsNullOrEmpty()) _excludes.Add(nameof(Temperature));
}
else

if (!_excludes.Contains(nameof(Battery)))
{
str = GetInfo("MSAcpi_ThermalZoneTemperature", "CurrentTemperature", "root/wmi");
if (!str.IsNullOrEmpty()) Temperature = (str.SplitAsInt().Average() - 2732) / 10.0;
// 电池剩余
var str = GetInfo("Win32_Battery", "EstimatedChargeRemaining");
if (!str.IsNullOrEmpty())
Battery = str.SplitAsInt().Average() / 100.0;
else
_excludes.Add(nameof(Battery));
}

// 电池剩余
str = GetInfo("Win32_Battery", "EstimatedChargeRemaining");
if (!str.IsNullOrEmpty()) Battery = str.SplitAsInt().Average() / 100.0;
#endif
}
}
Expand Down

0 comments on commit 0382228

Please sign in to comment.