Skip to content

Commit

Permalink
v2.3.2024.1106 使用IPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 6, 2024
1 parent c7fbcae commit 1249276
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
4 changes: 3 additions & 1 deletion NewLife.IoT/Clients/IServiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static async Task<ServiceReplyModel> ExecuteService(this IServiceHandler
if (result is ServiceReplyModel reply)
{
reply.Id = model.Id;
if (reply.Status == ServiceStatus.就绪 || reply.Status == ServiceStatus.处理中)
if (reply.Status is ServiceStatus.就绪 or ServiceStatus.处理中)
reply.Status = ServiceStatus.已完成;

return reply;
Expand Down Expand Up @@ -131,9 +131,11 @@ public static async Task<ServiceReplyModel> ExecuteService(this IServiceHandler
private static async Task<Object?> OnService(IServiceHandler client, ServiceModel model)
{
if (!client.Services.TryGetValue(model.Name, out var d))
{
// 通用方法
if (!client.Services.TryGetValue("*", out d))
throw new ApiException(400, $"找不到服务[{model.Name}]");
}

if (d is Func<String?, String?> func) return func(model.InputData);
if (d is Func<ServiceModel, ServiceReplyModel> func2) return func2(model);
Expand Down
18 changes: 11 additions & 7 deletions NewLife.IoT/Controllers/DefaultSerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ void OnReceiveSerial(Object sender, SerialDataReceivedEventArgs e)
if (rs != null)
{
Received?.Invoke(this, new ReceivedEventArgs { Packet = rs });

// 回收内存池
rs.TryDispose();
}
}

Expand Down Expand Up @@ -102,7 +105,7 @@ public virtual Int32 Read(Byte[] buffer, Int32 offset, Int32 count)
/// <param name="request">待发送数据</param>
/// <param name="minLength">等待响应数据的最小长度,默认1</param>
/// <returns></returns>
public virtual Packet Invoke(Packet? request, Int32 minLength)
public virtual IPacket Invoke(IPacket? request, Int32 minLength)
{
Open();

Expand All @@ -111,21 +114,22 @@ public virtual Packet Invoke(Packet? request, Int32 minLength)
// 清空缓冲区
_port.DiscardInBuffer();

if (request.Next == null)
_port.Write(request.Data, request.Offset, request.Count);
if (request.Next == null && request is ArrayPacket ap)
_port.Write(ap.Buffer, ap.Offset, ap.Length);
else
_port.Write(request.ToArray(), 0, request.Total);
_port.Write(request.ReadBytes(), 0, request.Total);

if (ByteTimeout > 10) Thread.Sleep(ByteTimeout);
}

// 串口速度较慢,等待收完数据
WaitMore(_port, minLength);

var buf = new Byte[BufferSize];
var rs = _port.Read(buf, 0, buf.Length);
var p = new OwnerPacket(BufferSize);
var rs = _port.Read(p.Buffer, p.Offset, p.Length);
p.Resize(rs);

return new Packet(buf, 0, rs);
return p;
}

private void WaitMore(SerialPort sp, Int32 minLength)
Expand Down
16 changes: 15 additions & 1 deletion NewLife.IoT/Controllers/IBoard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NewLife.IoT.Controllers;
using NewLife.Reflection;

namespace NewLife.IoT.Controllers;

/// <summary>板卡接口。约定板卡所具备的一些基础功能</summary>
/// <remarks>
Expand Down Expand Up @@ -55,6 +57,18 @@ public virtual ISerialPort CreateSerial(String portName, Int32 baudrate = 9600)
Baudrate = baudrate,
};
return sp;
#else
var type = "DefaultSerialPort".GetTypeEx();
if (type != null)
{
if (type.CreateInstance() is ISerialPort sp)
{
sp.PortName = portName;
sp.Baudrate = baudrate;

return sp;
}
}
#endif

throw new NotImplementedException();
Expand Down
8 changes: 4 additions & 4 deletions NewLife.IoT/Controllers/IModbus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ public interface IModbus
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">线圈数量。一般要求8的倍数</param>
/// <returns>线圈状态字节数组</returns>
Packet ReadCoil(Byte host, UInt16 address, UInt16 count);
IPacket ReadCoil(Byte host, UInt16 address, UInt16 count);

/// <summary>读离散量输入,0x02</summary>
/// <param name="host">主机。一般是1</param>
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">输入数量。一般要求8的倍数</param>
/// <returns>输入状态字节数组</returns>
Packet ReadDiscrete(Byte host, UInt16 address, UInt16 count);
IPacket ReadDiscrete(Byte host, UInt16 address, UInt16 count);

/// <summary>读取保持寄存器,0x03</summary>
/// <param name="host">主机。一般是1</param>
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">寄存器数量。每个寄存器2个字节</param>
/// <returns>寄存器值数组</returns>
Packet ReadRegister(Byte host, UInt16 address, UInt16 count);
IPacket ReadRegister(Byte host, UInt16 address, UInt16 count);

/// <summary>读取输入寄存器,0x04</summary>
/// <param name="host">主机。一般是1</param>
/// <param name="address">地址。例如0x0002</param>
/// <param name="count">输入寄存器数量。每个寄存器2个字节</param>
/// <returns>输入寄存器值数组</returns>
Packet ReadInput(Byte host, UInt16 address, UInt16 count);
IPacket ReadInput(Byte host, UInt16 address, UInt16 count);
#endregion

#region 写入
Expand Down
5 changes: 2 additions & 3 deletions NewLife.IoT/Controllers/ISerialPort.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using NewLife.Data;
using NewLife.Data;
using NewLife.Net;

namespace NewLife.IoT.Controllers;
Expand Down Expand Up @@ -42,5 +41,5 @@ public interface ISerialPort
/// <param name="request">待发送数据</param>
/// <param name="minLength">等待响应数据的最小长度,默认1</param>
/// <returns></returns>
Packet Invoke(Packet? request, Int32 minLength = 1);
IPacket Invoke(IPacket? request, Int32 minLength = 1);
}
15 changes: 4 additions & 11 deletions NewLife.IoT/Drivers/DriverAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
namespace NewLife.IoT.Drivers;

/// <summary>驱动特性</summary>
public class DriverAttribute : Attribute
/// <param name="name">驱动名称</param>
public class DriverAttribute(String name) : Attribute
{
/// <summary>
/// 名称
/// </summary>
public String Name { get; set; }

/// <summary>
/// 指定驱动名称
/// </summary>
/// <param name="name"></param>
public DriverAttribute(String name) => Name = name;
/// <summary>名称</summary>
public String Name { get; set; } = name;
}
6 changes: 3 additions & 3 deletions NewLife.IoT/Models/DeviceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class DeviceModel : IDeviceInfo
/// <summary>设备参数。Xml/Json格式配置,根据协议驱动来解析</summary>
public String? Parameter { get; set; }

/// <summary>是否启用</summary>
public Boolean Enable { get; set; }

/// <summary>更新时间。用于判断数据变化</summary>
public DateTime UpdateTime { get; set; }

/// <summary>是否启用 true 启用 false 停用</summary>
public Boolean Enable { get; set; }
}
6 changes: 3 additions & 3 deletions NewLife.IoT/NewLife.IoT.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net40;net45;net461;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0;net45;net461;net6.0;net7.0;net8.0</TargetFrameworks>
<AssemblyTitle>IoT Standard</AssemblyTitle>
<Description>IoT standard library, which defines various communication protocol standards and specifications in the Internet of Things field, without specific implementation. Used for IoT platform construction and unifying various hardware driver protocols. IoT标准库,定义物联网领域的各种通信协议标准规范,不含具体实现。用于IoT平台建设,以及统一各种硬件驱动协议</Description>
<Company>新生命开发团队</Company>
<Copyright>©2002-2024 新生命开发团队</Copyright>
<VersionPrefix>2.2</VersionPrefix>
<VersionPrefix>2.3</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
Expand All @@ -28,7 +28,7 @@
<RepositoryUrl>https://github.com/NewLifeX/NewLife.IoT</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>物联网;IoT;边缘计算;Edge;新生命团队;NewLife;$(AssemblyName)</PackageTags>
<PackageReleaseNotes>全面完善字节数组到数字类型的转换,支持各种字节序</PackageReleaseNotes>
<PackageReleaseNotes>使用IPacket</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down
6 changes: 3 additions & 3 deletions NewLife.IoT/ThingModels/ServiceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class ServiceModel
/// <summary>设备编码。服务调用请求发送给网关设备时,该参数指定子设备编码</summary>
public String? DeviceCode { get; set; }

/// <summary>链路追踪</summary>
public String? TraceId { get; set; }

/// <summary>服务类型</summary>
public String? Type { get; set; }

/// <summary>链路追踪</summary>
public String? TraceId { get; set; }
}
2 changes: 1 addition & 1 deletion NewLife.IoT/ThingSpecification/ServiceSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static PropertySpec Create(ParameterInfo member)

var ps = new PropertySpec
{
Id = member.Name,
Id = member.Name!,
DataType = new TypeSpec { Type = member.ParameterType.Name }
};

Expand Down

0 comments on commit 1249276

Please sign in to comment.