Skip to content

Commit

Permalink
完善心跳指令各字段的赋值
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 23, 2024
1 parent 320b983 commit 9279e6b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 47 deletions.
48 changes: 30 additions & 18 deletions NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using NewLife.Caching;
Expand Down Expand Up @@ -175,8 +177,8 @@ public virtual async Task<TResult> OnInvokeAsync<TResult>(String action, Object?

rs = await http.InvokeAsync<TResult>(method, action, args, null, cancellationToken);
}

rs = await _client!.InvokeAsync<TResult>(action, args, cancellationToken);
else
rs = await _client!.InvokeAsync<TResult>(action, args, cancellationToken);

if (Log != null && Log.Level <= LogLevel.Debug) WriteLog("[{0}]<={1}", action, rs?.ToJson());

Expand Down Expand Up @@ -256,6 +258,14 @@ protected virtual void SetToken(String? token)
WriteLog("下发密钥:{0}/{1}", rs.Code, rs.Secret);
Code = rs.Code;
Secret = rs.Secret;

var set = Setting;
if (set != null)
{
set.Code = rs.Code;
set.Secret = rs.Secret;
set.Save();
}
}

FixTime(rs.Time, rs.Time);
Expand All @@ -266,14 +276,6 @@ protected virtual void SetToken(String? token)

OnLogined?.Invoke(this, new(request, rs));

var set = Setting;
if (set != null && !rs.Code.IsNullOrEmpty())
{
set.Code = rs.Code;
set.Secret = rs.Secret;
set.Save();
}

StartTimer();

return rs;
Expand Down Expand Up @@ -457,21 +459,31 @@ public virtual IPingRequest BuildPingRequest()

if (request is PingRequest req)
{
req.Delay = Delay;
req.Uptime = Environment.TickCount / 1000;

// 开始时间 Environment.TickCount 很容易溢出,导致开机24天后变成负数。
// 后来在 netcore3.0 增加了Environment.TickCount64
// 现在借助 Stopwatch 来解决
if (Stopwatch.IsHighResolution) req.Uptime = (Int32)(Stopwatch.GetTimestamp() / Stopwatch.Frequency);

var path = ".".GetFullPath();
var driveInfo = DriveInfo.GetDrives().FirstOrDefault(e => path.StartsWithIgnoreCase(e.Name));
var mi = MachineInfo.GetCurrent();
mi.Refresh();

req.Memory = mi.Memory;
req.AvailableMemory = mi.AvailableMemory;
req.TotalSize = (UInt64)(driveInfo?.TotalSize ?? 0);
req.AvailableFreeSpace = (UInt64)(driveInfo?.AvailableFreeSpace ?? 0);
req.CpuRate = Math.Round(mi.CpuRate, 3);
req.Temperature = Math.Round(mi.Temperature, 1);
req.Battery = Math.Round(mi.Battery, 3);
req.UplinkSpeed = mi.UplinkSpeed;
req.DownlinkSpeed = mi.DownlinkSpeed;

var ip = NetHelper.GetIPs().Where(ip => ip.IsIPv4() && !IPAddress.IsLoopback(ip) && ip.GetAddressBytes()[0] != 169).Join();
req.IP = ip;

req.Delay = Delay;
req.Uptime = Environment.TickCount / 1000;

// 开始时间 Environment.TickCount 很容易溢出,导致开机24天后变成负数。
// 后来在 netcore3.0 增加了Environment.TickCount64
// 现在借助 Stopwatch 来解决
if (Stopwatch.IsHighResolution) req.Uptime = (Int32)(Stopwatch.GetTimestamp() / Stopwatch.Frequency);
}

return request;
Expand Down
32 changes: 7 additions & 25 deletions NewLife.Remoting/Models/PingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public interface IPingRequest
public class PingRequest : IPingRequest
{
#region 属性
/// <summary>内存大小</summary>
public UInt64 Memory { get; set; }

/// <summary>可用内存大小</summary>
public UInt64 AvailableMemory { get; set; }

///// <summary>磁盘可用空间。应用所在盘</summary>
//public UInt64 AvailableFreeSpace { get; set; }
/// <summary>磁盘大小。应用所在盘</summary>
public UInt64 TotalSize { get; set; }

///// <summary>驱动器信息。各分区大小,逗号分隔</summary>
//public String? DriveInfo { get; set; }
/// <summary>磁盘可用空间。应用所在盘</summary>
public UInt64 AvailableFreeSpace { get; set; }

/// <summary>CPU占用率</summary>
public Double CpuRate { get; set; }
Expand All @@ -44,30 +47,9 @@ public class PingRequest : IPingRequest
/// <summary>下行速度。网络接收速度,字节每秒</summary>
public UInt64 DownlinkSpeed { get; set; }

///// <summary>MAC地址</summary>
//public String? Macs { get; set; }

///// <summary>框架。本地支持的所有版本框架</summary>
//public String? Framework { get; set; }

/// <summary>本地IP地址。随着网卡变动,可能改变</summary>
public String? IP { get; set; }

///// <summary>进程列表</summary>
//public String? Processes { get; set; }

///// <summary>进程个数</summary>
//public Int32 ProcessCount { get; set; }

///// <summary>正在传输的Tcp连接数</summary>
//public Int32 TcpConnections { get; set; }

///// <summary>主动关闭的Tcp连接数</summary>
//public Int32 TcpTimeWait { get; set; }

///// <summary>被动关闭的Tcp连接数</summary>
//public Int32 TcpCloseWait { get; set; }

/// <summary>开机时间,单位s</summary>
public Int32 Uptime { get; set; }

Expand Down
12 changes: 8 additions & 4 deletions Samples/IoTZero/Clients/HttpDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class HttpDevice : ClientBase
/// <summary>产品编码。从IoT管理平台获取</summary>
public String ProductKey { get; set; }

/// <summary>产品密钥</summary>
public String ProductSecret { get; set; }

private readonly ClientSetting _setting;
#endregion

Expand All @@ -28,6 +31,7 @@ public HttpDevice(ClientSetting setting) : base(setting)
_setting = setting;

ProductKey = setting.ProductKey;
ProductSecret = setting.DeviceSecret;
}
#endregion

Expand Down Expand Up @@ -59,11 +63,10 @@ public override ILoginRequest BuildLoginRequest()
if (request is LoginInfo info)
{
info.ProductKey = ProductKey;
info.ProductSecret = ProductSecret;
info.Name = Environment.MachineName;
info.IP = NetHelper.MyIP() + "";

var mi = MachineInfo.GetCurrent();
info.UUID = mi.BuildCode();
info.UUID = MachineInfo.GetCurrent().BuildCode();
}

return request;
Expand All @@ -76,7 +79,8 @@ public override IPingRequest BuildPingRequest()
var request = base.BuildPingRequest();
if (request is PingInfo info)
{

info.Memory = 0;
info.TotalSize = 0;
}

return request;
Expand Down

0 comments on commit 9279e6b

Please sign in to comment.