Skip to content

Commit

Permalink
增加IMachineInfo,用于扩展MachineInfo功能,具体应用自定义各字段获取方式;识别全志sunxi平台;
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 15, 2024
1 parent 493ae88 commit 1995bde
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@

namespace NewLife;

/// <summary>机器信息接口</summary>
/// <remarks>用于扩展MachineInfo功能,具体应用自定义各字段获取方式</remarks>
public interface IMachineInfo
{
/// <summary>初始化静态数据</summary>
void Init(MachineInfo info);

/// <summary>刷新动态数据</summary>
void Refresh(MachineInfo info);
}

/// <summary>机器信息</summary>
/// <remarks>
/// 文档 https://newlifex.com/core/machine_info
Expand Down Expand Up @@ -104,10 +115,13 @@ public class MachineInfo
public Double Battery { get; set; }
#endregion

#region 构造
#region 全局静态
/// <summary>当前机器信息。默认null,在RegisterAsync后才能使用</summary>
public static MachineInfo? Current { get; set; }

/// <summary>机器信息提供者。外部实现可修改部分行为</summary>
public static IMachineInfo? Provider { get; set; }

//static MachineInfo() => RegisterAsync().Wait(100);

private static Task<MachineInfo>? _task;
Expand Down Expand Up @@ -182,7 +196,7 @@ public static Task<MachineInfo> RegisterAsync()
#endregion

#region 方法
/// <summary>刷新</summary>
/// <summary>初始化静态数据。可能是实例化后执行,也可能是Json反序列化后执行</summary>
public void Init()
{
var osv = Environment.OSVersion;
Expand All @@ -205,6 +219,8 @@ public void Init()
else if (Runtime.Linux)
LoadLinuxInfo();
#endif

Provider?.Init(this);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -504,6 +520,30 @@ private void LoadLinuxInfo()
if (Serial.IsNullOrEmpty() && dic2.TryGetValue("Serial", out str)) Serial = str;
if (Board.IsNullOrEmpty() && dic2.TryGetValue("Board", out str)) Board = str;
}

if (!Processor.IsNullOrEmpty())
{
// 识别全志sunxi平台
if (TryRead("/sys/class/sunxi_info/sys_info", out value))
{
var dic2 = value.SplitAsDictionary(":", Environment.NewLine, true);
if (dic2.TryGetValue("sunxi_platform", out var txt) && !txt.IsNullOrEmpty())
{
Processor = txt switch
{
"sun50iw9" => "H616",
"sun50iw10" => "A133",
_ => txt,
};
if (Product.IsNullOrEmpty() && txt.StartsWith("sun50i")) Product = "Cortex-A53";
if (Vendor.IsNullOrEmpty()) Vendor = "Allwinner";
}
if (uuid.IsNullOrEmpty() && dic2.TryGetValue("sunxi_chipid", out txt) && !txt.IsNullOrEmpty())
UUID = txt;
if (Serial.IsNullOrEmpty() && dic2.TryGetValue("sunxi_serial", out txt) && !txt.IsNullOrEmpty())
Serial = txt;
}
}
}

private void LoadMacInfo()
Expand Down Expand Up @@ -536,7 +576,7 @@ private void LoadMacInfo()
}
}

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

/// <summary>获取实时数据,如CPU、内存、温度</summary>
public void Refresh()
Expand All @@ -548,6 +588,8 @@ public void Refresh()
RefreshLinux();

RefreshSpeed();

Provider?.Refresh(this);
}

private void RefreshWindows()
Expand Down Expand Up @@ -940,7 +982,7 @@ public static IDictionary<String, String> ReadWmic(String type, params String[]
if (!k.IsNullOrEmpty() && !v.IsNullOrEmpty())
{
if (!dic.TryGetValue(k, out var list))
dic[k] = list = new List<String>();
dic[k] = list = [];

list.Add(v);
}
Expand Down

0 comments on commit 1995bde

Please sign in to comment.