Skip to content

Commit

Permalink
MachineInfo实现IExtend接口,直接扩展更多机器信息数据
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 15, 2024
1 parent 1995bde commit 5f04164
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 47 deletions.
19 changes: 14 additions & 5 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.Versioning;
using System.Diagnostics.CodeAnalysis;
using NewLife.Windows;
using NewLife.Data;

#if NETFRAMEWORK
using System.Management;
Expand Down Expand Up @@ -40,7 +41,7 @@ public interface IMachineInfo
///
/// 刷新信息成本较高,建议采用单例模式
/// </remarks>
public class MachineInfo
public class MachineInfo : IExtend
{
#region 属性
/// <summary>系统名称</summary>
Expand Down Expand Up @@ -96,7 +97,7 @@ public class MachineInfo

/// <summary>CPU占用率</summary>
[DisplayName("CPU占用率")]
public Single CpuRate { get; set; }
public Double CpuRate { get; set; }

/// <summary>网络上行速度。字节每秒,初始化后首次读取为0</summary>
[DisplayName("网络上行速度")]
Expand All @@ -113,6 +114,14 @@ public class MachineInfo
/// <summary>电池剩余。小于1的小数,常用百分比表示</summary>
[DisplayName("电池剩余")]
public Double Battery { get; set; }

private readonly Dictionary<String, Object?> _items = [];
IDictionary<String, Object?> IExtend.Items => _items;

/// <summary>获取 或 设置 扩展属性数据</summary>
/// <param name="key"></param>
/// <returns></returns>
public Object? this[String key] { get => _items.TryGetValue(key, out var obj) ? obj : null; set => _items[key] = value; }
#endregion

#region 全局静态
Expand Down Expand Up @@ -521,7 +530,7 @@ private void LoadLinuxInfo()
if (Board.IsNullOrEmpty() && dic2.TryGetValue("Board", out str)) Board = str;
}

if (!Processor.IsNullOrEmpty())
if (Processor.IsNullOrEmpty())
{
// 识别全志sunxi平台
if (TryRead("/sys/class/sunxi_info/sys_info", out value))
Expand Down Expand Up @@ -614,7 +623,7 @@ private void RefreshWindows()
var total = current.TotalTime - (_systemTime?.TotalTime ?? 0);
_systemTime = current;

CpuRate = total == 0 ? 0 : (Single)Math.Round((Single)(total - idle) / total, 4);
CpuRate = total == 0 ? 0 : Math.Round((Double)(total - idle) / total, 4);

var power = new PowerStatus();

Expand Down Expand Up @@ -771,7 +780,7 @@ private void RefreshLinux()
var total = current.TotalTime - (_systemTime?.TotalTime ?? 0);
_systemTime = current;

CpuRate = total == 0 ? 0 : (Single)Math.Round((Single)(total - idle) / total, 4);
CpuRate = total == 0 ? 0 : Math.Round((Double)(total - idle) / total, 4);
}
}
catch
Expand Down
118 changes: 76 additions & 42 deletions XUnitTest.Core/Common/MachineInfoTests.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,90 @@
using System;
using System.IO;
using System.IO;
using NewLife;
using NewLife.Log;
using NewLife.Model;
using NewLife.Serialization;
using Xunit;

namespace XUnitTest.Common
namespace XUnitTest.Common;

public class MachineInfoTests
{
public class MachineInfoTests
[Fact(DisplayName = "基础测试")]
public void BasicTest()
{
[Fact(DisplayName = "基础测试")]
public void BasicTest()
{
var mi = new MachineInfo();
mi.Init();

Assert.NotEmpty(mi.OSName);
Assert.NotEmpty(mi.OSVersion);
Assert.NotEmpty(mi.Product);
Assert.NotEmpty(mi.Processor);
//Assert.NotEmpty(mi.CpuID);
Assert.NotEmpty(mi.UUID);
Assert.NotEmpty(mi.Guid);
//Assert.NotEmpty(mi.Serial);
//Assert.NotEmpty(mi.DiskID);

Assert.True(mi.Memory > 1L * 1024 * 1024 * 1024);
Assert.True(mi.AvailableMemory > 1L * 1024 * 1024);
//Assert.True(mi.CpuRate > 0.001);
Assert.Equal(0UL, mi.UplinkSpeed);
Assert.Equal(0UL, mi.DownlinkSpeed);
}
var mi = new MachineInfo();
mi.Init();

[Fact]
public void RegisterTest()
{
//MachineInfo.Current = null;
var task = MachineInfo.RegisterAsync();
var mi = task.Result;
Assert.Equal(mi, MachineInfo.Current);
Assert.NotEmpty(mi.OSName);
Assert.NotEmpty(mi.OSVersion);
Assert.NotEmpty(mi.Product);
Assert.NotEmpty(mi.Processor);
//Assert.NotEmpty(mi.CpuID);
Assert.NotEmpty(mi.UUID);
Assert.NotEmpty(mi.Guid);
//Assert.NotEmpty(mi.Serial);
//Assert.NotEmpty(mi.DiskID);

Assert.True(mi.Memory > 1L * 1024 * 1024 * 1024);
Assert.True(mi.AvailableMemory > 1L * 1024 * 1024);
//Assert.True(mi.CpuRate > 0.001);
Assert.Equal(0UL, mi.UplinkSpeed);
Assert.Equal(0UL, mi.DownlinkSpeed);
}

var mi2 = ObjectContainer.Current.Resolve<MachineInfo>();
Assert.Equal(mi, mi2);
[Fact]
public void RegisterTest()
{
//MachineInfo.Current = null;
var task = MachineInfo.RegisterAsync();
var mi = task.Result;

Check warning on line 39 in XUnitTest.Core/Common/MachineInfoTests.cs

View workflow job for this annotation

GitHub Actions / test

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 39 in XUnitTest.Core/Common/MachineInfoTests.cs

View workflow job for this annotation

GitHub Actions / test

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)

Check warning on line 39 in XUnitTest.Core/Common/MachineInfoTests.cs

View workflow job for this annotation

GitHub Actions / build-publish

Test methods should not use blocking task operations, as they can cause deadlocks. Use an async test method and await instead. (https://xunit.net/xunit.analyzers/rules/xUnit1031)
Assert.Equal(mi, MachineInfo.Current);

var mi2 = ObjectContainer.Current.Resolve<MachineInfo>();
Assert.Equal(mi, mi2);

var file = Path.GetTempPath().CombinePath("machine_info.json").GetFullPath();
Assert.True(File.Exists(file));

var file = Path.GetTempPath().CombinePath("machine_info.json").GetFullPath();
Assert.True(File.Exists(file));
var mi3 = File.ReadAllText(file).ToJsonEntity<MachineInfo>();
Assert.Equal(mi.OSName, mi3.OSName);
Assert.Equal(mi.UUID, mi3.UUID);
Assert.Equal(mi.Guid, mi3.Guid);
}

[Fact]
public void ProviderTest()
{
MachineInfo.Provider = new MyProvider();

var mi3 = File.ReadAllText(file).ToJsonEntity<MachineInfo>();
Assert.Equal(mi.OSName, mi3.OSName);
Assert.Equal(mi.UUID, mi3.UUID);
Assert.Equal(mi.Guid, mi3.Guid);
var mi = new MachineInfo();
mi.Init();

Assert.Equal("NewLife", mi.Product);
Assert.Equal(98, mi["Signal"]);
Assert.True(mi.CpuRate > 0.01);

var js = mi.ToJson();
var dic = JsonParser.Decode(js);
var rs = dic.TryGetValue("Signal", out var obj);
Assert.True(rs);
Assert.Equal(98, obj);

mi.Refresh();

Assert.Equal(0.168f, mi.CpuRate);
}

class MyProvider : IMachineInfo
{
public void Init(MachineInfo info)
{
info.Product = "NewLife";
info["Signal"] = 98;
}

public void Refresh(MachineInfo info)
{
info.CpuRate = 0.168f;
}
}
}

0 comments on commit 5f04164

Please sign in to comment.