Skip to content

Commit

Permalink
结合星尘和IoT平台需要,重新整理登录心跳模型。尽量简化登录心跳模型接口,以支持下游系统的多样式。同时,模型默认实现增加一些常用字段,并默…
Browse files Browse the repository at this point in the history
…认赋值。
  • Loading branch information
nnhy committed Jun 23, 2024
1 parent 605d4ac commit be8ee54
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public virtual ILoginResponse Login([FromBody] ILoginRequest request)
var tm = _tokenService.IssueToken(dv.Code, request.ClientId);

rs.Token = tm.AccessToken;
rs.Expire = tm.ExpireIn;
}

return rs;
Expand Down
3 changes: 2 additions & 1 deletion NewLife.Remoting.Extensions/RemotingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static IServiceCollection AddRemoting(this IServiceCollection services, I
// 注册密码提供者,用于通信过程中保护密钥,避免明文传输
services.TryAddSingleton<IPasswordProvider>(new SaltPasswordProvider { Algorithm = "md5", SaltTime = 60 });

services.TryAddSingleton<ICache, MemoryCache>();
// 注册缓存提供者,必须有默认实现
services.TryAddSingleton<ICacheProvider, CacheProvider>();

// 添加模型绑定器
//var binderProvider = new ServiceModelBinderProvider();
Expand Down
31 changes: 24 additions & 7 deletions NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ public virtual ILoginRequest BuildLoginRequest()
info.Code = Code;
info.Secret = Secret;
info.ClientId = $"{NetHelper.MyIP()}@{Process.GetCurrentProcess().Id}";
info.Version = _version;

if (info is LoginRequest request)
{
request.Version = _version;
request.Time = DateTime.UtcNow.ToLong();
}

return info;
}
Expand Down Expand Up @@ -446,14 +451,26 @@ public virtual IPingRequest BuildPingRequest()
Init();

var request = GetService<IPingRequest>() ?? new PingRequest();
request.Uptime = Environment.TickCount / 1000;
request.Time = DateTime.UtcNow.ToLong();
request.Delay = Delay;

// 开始时间 Environment.TickCount 很容易溢出,导致开机24天后变成负数。
// 后来在 netcore3.0 增加了Environment.TickCount64
// 现在借助 Stopwatch 来解决
if (Stopwatch.IsHighResolution) request.Uptime = (Int32)(Stopwatch.GetTimestamp() / Stopwatch.Frequency);
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 mi = MachineInfo.GetCurrent();
req.AvailableMemory = mi.AvailableMemory;
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;
}

return request;
}
Expand Down
14 changes: 10 additions & 4 deletions NewLife.Remoting/Models/LoginRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@
public interface ILoginRequest
{
/// <summary>编码</summary>
String Code { get; set; }
String? Code { get; set; }

/// <summary>密钥</summary>
String? Secret { get; set; }

/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
String? ClientId { get; set; }

/// <summary>版本</summary>
String? Version { get; set; }
///// <summary>版本</summary>
//String? Version { get; set; }

///// <summary>本地UTC时间</summary>
//Int64 Time { get; set; }
}

/// <summary>登录请求</summary>
public class LoginRequest : ILoginRequest
{
#region 属性
/// <summary>编码</summary>
public String Code { get; set; } = null!;
public String? Code { get; set; }

/// <summary>密钥</summary>
public String? Secret { get; set; }
Expand All @@ -31,5 +34,8 @@ public class LoginRequest : ILoginRequest

/// <summary>版本</summary>
public String? Version { get; set; }

/// <summary>本地UTC时间</summary>
public Int64 Time { get; set; }
#endregion
}
6 changes: 6 additions & 0 deletions NewLife.Remoting/Models/LoginResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public interface ILoginResponse
/// <summary>令牌</summary>
String? Token { get; set; }

/// <summary>令牌过期时间。单位秒</summary>
Int32 Expire { get; set; }

/// <summary>服务器时间。Unix毫秒(UTC)</summary>
Int64 Time { get; set; }
}
Expand All @@ -32,6 +35,9 @@ public class LoginResponse : ILoginResponse
/// <summary>令牌</summary>
public String? Token { get; set; }

/// <summary>令牌过期时间。单位秒</summary>
public Int32 Expire { get; set; }

/// <summary>服务器时间。Unix毫秒(UTC)</summary>
public Int64 Time { get; set; }
#endregion
Expand Down
51 changes: 24 additions & 27 deletions NewLife.Remoting/Models/PingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/// <summary>心跳请求</summary>
public interface IPingRequest
{
/// <summary>开机时间,单位s</summary>
Int32 Uptime { get; set; }
///// <summary>开机时间,单位s</summary>
//Int32 Uptime { get; set; }

/// <summary>本地UTC时间。Unix毫秒(UTC)</summary>
Int64 Time { get; set; }

/// <summary>延迟。请求到服务端并返回的延迟时间。单位ms</summary>
Int32 Delay { get; set; }
///// <summary>延迟。请求到服务端并返回的延迟时间。单位ms</summary>
//Int32 Delay { get; set; }
}

/// <summary>心跳请求</summary>
Expand All @@ -20,11 +20,11 @@ public class PingRequest : IPingRequest
/// <summary>可用内存大小</summary>
public UInt64 AvailableMemory { get; set; }

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

/// <summary>驱动器信息。各分区大小,逗号分隔</summary>
public String? DriveInfo { get; set; }
///// <summary>驱动器信息。各分区大小,逗号分隔</summary>
//public String? DriveInfo { get; set; }

/// <summary>CPU占用率</summary>
public Double CpuRate { get; set; }
Expand All @@ -35,41 +35,38 @@ public class PingRequest : IPingRequest
/// <summary>电量</summary>
public Double Battery { get; set; }

/// <summary>信号强度。WiFi/4G</summary>
public Int32 Signal { get; set; }
///// <summary>信号强度。WiFi/4G</summary>
//public Int32 Signal { get; set; }

/// <summary>上行速度。网络发送速度,字节每秒</summary>
public UInt64 UplinkSpeed { get; set; }

/// <summary>下行速度。网络接收速度,字节每秒</summary>
public UInt64 DownlinkSpeed { get; set; }

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

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

/// <summary>框架。本地支持的所有版本框架</summary>
public String? Framework { 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 String? Processes { get; set; }

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

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

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

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

/// <summary>开机时间,单位s</summary>
public Int32 Uptime { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Remoting/Models/PingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IPingResponse
/// <summary>服务器时间。Unix毫秒(UTC)</summary>
Int64 ServerTime { get; set; }

/// <summary>采样周期。单位秒</summary>
/// <summary>心跳周期。单位秒</summary>
Int32 Period { get; set; }

/// <summary>令牌。现有令牌即将过期时,颁发新的令牌</summary>
Expand All @@ -28,7 +28,7 @@ public class PingResponse : IPingResponse
/// <summary>服务器时间。Unix毫秒(UTC)</summary>
public Int64 ServerTime { get; set; }

/// <summary>采样周期。单位秒</summary>
/// <summary>心跳周期。单位秒</summary>
public Int32 Period { get; set; }

/// <summary>令牌。现有令牌即将过期时,颁发新的令牌</summary>
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/NewLife.Remoting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<PackageReference Include="NewLife.Core" Version="10.10.2024.0601" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="NewLife.Core" Version="10.10.2024.0620-net40" />
<PackageReference Include="NewLife.Core" Version="10.10.2024.0623-net40" />
</ItemGroup>

<ItemGroup>
Expand Down
58 changes: 58 additions & 0 deletions NewLife.Remoting/RemotingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using NewLife.Log;

namespace NewLife.Remoting;

/// <summary>扩展方法</summary>
public static class RemotingExtensions
{
/// <summary>生成设备代码</summary>
/// <param name="machineInfo"></param>
/// <param name="formula"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static String? BuildCode(this MachineInfo machineInfo, String formula = "MD5({UUID}@{Guid}@{Serial}@{DiskID}@{Macs})")
{
if (formula.IsNullOrEmpty()) throw new ArgumentNullException(nameof(formula));

var ss = (formula + "").Split('(', ')');
if (ss.Length < 2) throw new ArgumentOutOfRangeException(nameof(formula));

var uid = ss[1];
foreach (var pi in machineInfo.GetType().GetProperties())
{
var name = $"{{{pi.Name}}}";
if (uid.Contains(name))
uid = uid.Replace(name, pi.GetValue(machineInfo, null) + "");
}

{
var name = "{Macs}";
if (uid.Contains(name))
{
var mcs = NetHelper.GetMacs().Select(e => e.ToHex("-")).OrderBy(e => e).Join(",");
uid = uid.Replace(name, mcs);
}
}

if (uid.Contains('{') || uid.Contains('}')) XTrace.WriteLine("编码公式有误,存在未解析变量,uid={0}", uid);
if (!uid.IsNullOrEmpty())
{
// 使用产品类别加密一下,确保不同类别有不同编码
var buf = uid.GetBytes();
switch (ss[0].ToLower())
{
case "crc": buf = buf.Crc().GetBytes(); break;
case "crc16": buf = buf.Crc16().GetBytes(); break;
case "md5": buf = buf.MD5(); break;
case "md5_16": buf = uid.MD5_16().ToHex(); break;
default:
break;
}

return buf.ToHex();
}

return null;
}
}
9 changes: 8 additions & 1 deletion Samples/IoTZero/Clients/HttpDevice.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using NewLife.IoT.Models;
using NewLife;
using NewLife.IoT.Models;
using NewLife.IoT.ThingModels;
using NewLife.Log;
using NewLife.Model;
using NewLife.Remoting;
using NewLife.Remoting.Clients;
using NewLife.Remoting.Models;
using NewLife.Security;
Expand Down Expand Up @@ -57,6 +59,11 @@ public override ILoginRequest BuildLoginRequest()
if (request is LoginInfo info)
{
info.ProductKey = ProductKey;
info.Name = Environment.MachineName;
info.IP = NetHelper.MyIP() + "";

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

return request;
Expand Down
22 changes: 11 additions & 11 deletions Samples/IoTZero/Models/LoginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
namespace NewLife.IoT.Models;

/// <summary>节点登录信息</summary>
public class LoginInfo : ILoginRequest
public class LoginInfo : LoginRequest
{
#region 属性
/// <summary>设备编码</summary>
public String Code { get; set; }
///// <summary>设备编码</summary>
//public String Code { get; set; }

/// <summary>设备密钥</summary>
public String Secret { get; set; }
///// <summary>设备密钥</summary>
//public String Secret { get; set; }

/// <summary>产品证书</summary>
public String ProductKey { get; set; }

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

/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
public String ClientId { get; set; }
///// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
//public String ClientId { get; set; }

/// <summary>名称。可用于标识设备的名称</summary>
public String Name { get; set; }

/// <summary>版本</summary>
public String Version { get; set; }
///// <summary>版本</summary>
//public String Version { get; set; }

/// <summary>本地IP地址</summary>
public String IP { get; set; }

/// <summary>唯一标识</summary>
public String UUID { get; set; }

/// <summary>本地UTC时间</summary>
public Int64 Time { get; set; }
///// <summary>本地UTC时间</summary>
//public Int64 Time { get; set; }
#endregion
}
Loading

0 comments on commit be8ee54

Please sign in to comment.