From 1249276a229ceebd6b2e7669ff30c6ceb0bd9d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 7 Nov 2024 00:36:30 +0800 Subject: [PATCH] =?UTF-8?q?v2.3.2024.1106=20=E4=BD=BF=E7=94=A8IPacket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.IoT/Clients/IServiceHandler.cs | 4 +++- NewLife.IoT/Controllers/DefaultSerialPort.cs | 18 +++++++++++------- NewLife.IoT/Controllers/IBoard.cs | 16 +++++++++++++++- NewLife.IoT/Controllers/IModbus.cs | 8 ++++---- NewLife.IoT/Controllers/ISerialPort.cs | 5 ++--- NewLife.IoT/Drivers/DriverAttribute.cs | 15 ++++----------- NewLife.IoT/Models/DeviceModel.cs | 6 +++--- NewLife.IoT/NewLife.IoT.csproj | 6 +++--- NewLife.IoT/ThingModels/ServiceModel.cs | 6 +++--- NewLife.IoT/ThingSpecification/ServiceSpec.cs | 2 +- 10 files changed, 49 insertions(+), 37 deletions(-) diff --git a/NewLife.IoT/Clients/IServiceHandler.cs b/NewLife.IoT/Clients/IServiceHandler.cs index 813767f..df0295f 100644 --- a/NewLife.IoT/Clients/IServiceHandler.cs +++ b/NewLife.IoT/Clients/IServiceHandler.cs @@ -100,7 +100,7 @@ public static async Task 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; @@ -131,9 +131,11 @@ public static async Task ExecuteService(this IServiceHandler private static async Task 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 func) return func(model.InputData); if (d is Func func2) return func2(model); diff --git a/NewLife.IoT/Controllers/DefaultSerialPort.cs b/NewLife.IoT/Controllers/DefaultSerialPort.cs index 859e600..b2e35ba 100644 --- a/NewLife.IoT/Controllers/DefaultSerialPort.cs +++ b/NewLife.IoT/Controllers/DefaultSerialPort.cs @@ -72,6 +72,9 @@ void OnReceiveSerial(Object sender, SerialDataReceivedEventArgs e) if (rs != null) { Received?.Invoke(this, new ReceivedEventArgs { Packet = rs }); + + // 回收内存池 + rs.TryDispose(); } } @@ -102,7 +105,7 @@ public virtual Int32 Read(Byte[] buffer, Int32 offset, Int32 count) /// 待发送数据 /// 等待响应数据的最小长度,默认1 /// - public virtual Packet Invoke(Packet? request, Int32 minLength) + public virtual IPacket Invoke(IPacket? request, Int32 minLength) { Open(); @@ -111,10 +114,10 @@ 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); } @@ -122,10 +125,11 @@ public virtual Packet Invoke(Packet? request, Int32 minLength) // 串口速度较慢,等待收完数据 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) diff --git a/NewLife.IoT/Controllers/IBoard.cs b/NewLife.IoT/Controllers/IBoard.cs index 88eda85..fb577b9 100644 --- a/NewLife.IoT/Controllers/IBoard.cs +++ b/NewLife.IoT/Controllers/IBoard.cs @@ -1,4 +1,6 @@ -namespace NewLife.IoT.Controllers; +using NewLife.Reflection; + +namespace NewLife.IoT.Controllers; /// 板卡接口。约定板卡所具备的一些基础功能 /// @@ -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(); diff --git a/NewLife.IoT/Controllers/IModbus.cs b/NewLife.IoT/Controllers/IModbus.cs index a13b875..4bfaccc 100644 --- a/NewLife.IoT/Controllers/IModbus.cs +++ b/NewLife.IoT/Controllers/IModbus.cs @@ -11,28 +11,28 @@ public interface IModbus /// 地址。例如0x0002 /// 线圈数量。一般要求8的倍数 /// 线圈状态字节数组 - Packet ReadCoil(Byte host, UInt16 address, UInt16 count); + IPacket ReadCoil(Byte host, UInt16 address, UInt16 count); /// 读离散量输入,0x02 /// 主机。一般是1 /// 地址。例如0x0002 /// 输入数量。一般要求8的倍数 /// 输入状态字节数组 - Packet ReadDiscrete(Byte host, UInt16 address, UInt16 count); + IPacket ReadDiscrete(Byte host, UInt16 address, UInt16 count); /// 读取保持寄存器,0x03 /// 主机。一般是1 /// 地址。例如0x0002 /// 寄存器数量。每个寄存器2个字节 /// 寄存器值数组 - Packet ReadRegister(Byte host, UInt16 address, UInt16 count); + IPacket ReadRegister(Byte host, UInt16 address, UInt16 count); /// 读取输入寄存器,0x04 /// 主机。一般是1 /// 地址。例如0x0002 /// 输入寄存器数量。每个寄存器2个字节 /// 输入寄存器值数组 - Packet ReadInput(Byte host, UInt16 address, UInt16 count); + IPacket ReadInput(Byte host, UInt16 address, UInt16 count); #endregion #region 写入 diff --git a/NewLife.IoT/Controllers/ISerialPort.cs b/NewLife.IoT/Controllers/ISerialPort.cs index c8aefc3..1bae9bf 100644 --- a/NewLife.IoT/Controllers/ISerialPort.cs +++ b/NewLife.IoT/Controllers/ISerialPort.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using NewLife.Data; +using NewLife.Data; using NewLife.Net; namespace NewLife.IoT.Controllers; @@ -42,5 +41,5 @@ public interface ISerialPort /// 待发送数据 /// 等待响应数据的最小长度,默认1 /// - Packet Invoke(Packet? request, Int32 minLength = 1); + IPacket Invoke(IPacket? request, Int32 minLength = 1); } diff --git a/NewLife.IoT/Drivers/DriverAttribute.cs b/NewLife.IoT/Drivers/DriverAttribute.cs index 92a0ed7..b6aaef5 100644 --- a/NewLife.IoT/Drivers/DriverAttribute.cs +++ b/NewLife.IoT/Drivers/DriverAttribute.cs @@ -1,16 +1,9 @@ namespace NewLife.IoT.Drivers; /// 驱动特性 -public class DriverAttribute : Attribute +/// 驱动名称 +public class DriverAttribute(String name) : Attribute { - /// - /// 名称 - /// - public String Name { get; set; } - - /// - /// 指定驱动名称 - /// - /// - public DriverAttribute(String name) => Name = name; + /// 名称 + public String Name { get; set; } = name; } \ No newline at end of file diff --git a/NewLife.IoT/Models/DeviceModel.cs b/NewLife.IoT/Models/DeviceModel.cs index aaa1566..b677f9f 100644 --- a/NewLife.IoT/Models/DeviceModel.cs +++ b/NewLife.IoT/Models/DeviceModel.cs @@ -27,9 +27,9 @@ public class DeviceModel : IDeviceInfo /// 设备参数。Xml/Json格式配置,根据协议驱动来解析 public String? Parameter { get; set; } + /// 是否启用 + public Boolean Enable { get; set; } + /// 更新时间。用于判断数据变化 public DateTime UpdateTime { get; set; } - - /// 是否启用 true 启用 false 停用 - public Boolean Enable { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/NewLife.IoT.csproj b/NewLife.IoT/NewLife.IoT.csproj index 8ae6fc3..948d82e 100644 --- a/NewLife.IoT/NewLife.IoT.csproj +++ b/NewLife.IoT/NewLife.IoT.csproj @@ -1,11 +1,11 @@  - netstandard2.1;netstandard2.0;net40;net45;net461;net6.0;net7.0;net8.0 + netstandard2.1;netstandard2.0;net45;net461;net6.0;net7.0;net8.0 IoT Standard 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平台建设,以及统一各种硬件驱动协议 新生命开发团队 ©2002-2024 新生命开发团队 - 2.2 + 2.3 $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) $(VersionPrefix).$(VersionSuffix) $(Version) @@ -28,7 +28,7 @@ https://github.com/NewLifeX/NewLife.IoT git 物联网;IoT;边缘计算;Edge;新生命团队;NewLife;$(AssemblyName) - 全面完善字节数组到数字类型的转换,支持各种字节序 + 使用IPacket MIT true true diff --git a/NewLife.IoT/ThingModels/ServiceModel.cs b/NewLife.IoT/ThingModels/ServiceModel.cs index a37ccd7..a818c71 100644 --- a/NewLife.IoT/ThingModels/ServiceModel.cs +++ b/NewLife.IoT/ThingModels/ServiceModel.cs @@ -21,9 +21,9 @@ public class ServiceModel /// 设备编码。服务调用请求发送给网关设备时,该参数指定子设备编码 public String? DeviceCode { get; set; } - /// 链路追踪 - public String? TraceId { get; set; } - /// 服务类型 public String? Type { get; set; } + + /// 链路追踪 + public String? TraceId { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingSpecification/ServiceSpec.cs b/NewLife.IoT/ThingSpecification/ServiceSpec.cs index 045d049..ade76f8 100644 --- a/NewLife.IoT/ThingSpecification/ServiceSpec.cs +++ b/NewLife.IoT/ThingSpecification/ServiceSpec.cs @@ -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 } };