diff --git a/NewLife.IoT/Drivers/DriverBase.cs b/NewLife.IoT/Drivers/DriverBase.cs index 29a20a2..823ff28 100644 --- a/NewLife.IoT/Drivers/DriverBase.cs +++ b/NewLife.IoT/Drivers/DriverBase.cs @@ -26,7 +26,7 @@ public class DriverBase : DriverBase /// 逻辑设备 /// 参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter /// 节点对象,可存储站号等信息,仅驱动自己识别 - public override INode Open(IDevice device, IDriverParameter parameter) + public override INode Open(IDevice device, IDriverParameter? parameter) { var node = new TNode { @@ -55,7 +55,7 @@ public abstract class DriverBase : DisposeBase, IDriver, ILogFeature, ITracerFea /// /// Xml/Json参数配置 /// - public virtual IDriverParameter CreateParameter(String parameter) + public virtual IDriverParameter? CreateParameter(String? parameter) { var p = OnCreateParameter(); if (p == null) return null; @@ -76,7 +76,7 @@ public virtual IDriverParameter CreateParameter(String parameter) /// 创建驱动参数对象,分析参数配置或创建默认参数 /// - protected virtual IDriverParameter OnCreateParameter() => null; + protected virtual IDriverParameter? OnCreateParameter() => null; /// 获取产品物模型 /// @@ -87,7 +87,7 @@ public virtual IDriverParameter CreateParameter(String parameter) /// 获取后,按新版本覆盖旧版本。 /// /// - public virtual ThingSpec GetSpecification() + public virtual ThingSpec? GetSpecification() { var type = GetType(); var spec = new ThingSpec @@ -115,7 +115,7 @@ public virtual ThingSpec GetSpecification() /// 逻辑设备 /// 参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter /// 节点对象,可存储站号等信息,仅驱动自己识别 - public virtual INode Open(IDevice device, IDriverParameter parameter) + public virtual INode Open(IDevice device, IDriverParameter? parameter) { var node = new Node { @@ -140,7 +140,7 @@ public virtual void Close(INode node) { } /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 点位集合 /// - public virtual IDictionary Read(INode node, IPoint[] points) => throw new NotImplementedException(); + public virtual IDictionary Read(INode node, IPoint[] points) => throw new NotImplementedException(); /// 写入数据 /// @@ -150,7 +150,7 @@ public virtual void Close(INode node) { } /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 点位 /// 数值 - public virtual Object Write(INode node, IPoint point, Object value) => throw new NotImplementedException(); + public virtual Object? Write(INode node, IPoint point, Object? value) => throw new NotImplementedException(); /// 控制设备,特殊功能使用 /// @@ -159,15 +159,15 @@ public virtual void Close(INode node) { } /// /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 参数 - public virtual Object Control(INode node, IDictionary parameters) => throw new NotImplementedException(); + public virtual Object? Control(INode node, IDictionary parameters) => throw new NotImplementedException(); #endregion #region 日志 /// 日志 - public ILog Log { get; set; } + public ILog Log { get; set; } = Logger.Null; /// 性能追踪器 - public ITracer Tracer { get; set; } + public ITracer? Tracer { get; set; } /// 写日志 /// diff --git a/NewLife.IoT/Drivers/DriverFactory.cs b/NewLife.IoT/Drivers/DriverFactory.cs index f224d45..de572a5 100644 --- a/NewLife.IoT/Drivers/DriverFactory.cs +++ b/NewLife.IoT/Drivers/DriverFactory.cs @@ -2,7 +2,6 @@ using System.Reflection; using NewLife.Log; using NewLife.Reflection; -using NewLife.Xml; namespace NewLife.IoT.Drivers; @@ -15,12 +14,12 @@ public class DriverFactory public static IDictionary Map => _map; /// 驱动集合 - public static IList Drivers { get; private set; } + public static IList Drivers { get; private set; } = null!; /// 注册协议实现 /// /// - public static void Register(String name, Type type) + public static void Register(String? name, Type type) { if (type == null) throw new ArgumentNullException(nameof(type)); if (name.IsNullOrEmpty()) name = type.Name; @@ -31,14 +30,14 @@ public static void Register(String name, Type type) /// 注册协议实现 /// /// - public static void Register(String name = null) where T : IDriver => Register(name, typeof(T)); + public static void Register(String? name = null) where T : IDriver => Register(name, typeof(T)); - private static readonly ConcurrentDictionary _cache = new(); + private static readonly ConcurrentDictionary _cache = new(); /// 创建协议实例,根据地址确保唯一,多设备共用同一个串口 /// 驱动名称。一般由DriverAttribute特性确定 /// 唯一标识。没有传递标识时,每次返回新实例 /// - public static IDriver Create(String name, String identifier) + public static IDriver? Create(String name, String identifier) { if (!_map.TryGetValue(name, out var type) || type == null) return null; @@ -49,12 +48,12 @@ public static IDriver Create(String name, String identifier) return _cache.GetOrAdd(key, k => type.CreateInstance() as IDriver); } - private static readonly ConcurrentDictionary _defaults = new(); + private static readonly ConcurrentDictionary _defaults = new(); /// 创建驱动参数对象,分析参数配置或创建默认参数 /// 驱动名称。一般由DriverAttribute特性确定 /// Xml/Json参数配置 /// - public static IDriverParameter CreateParameter(String name, String parameter) + public static IDriverParameter? CreateParameter(String name, String parameter) { if (!_map.TryGetValue(name, out var type) || type == null) return null; diff --git a/NewLife.IoT/Drivers/DriverInfo.cs b/NewLife.IoT/Drivers/DriverInfo.cs index 509ba4f..ad12739 100644 --- a/NewLife.IoT/Drivers/DriverInfo.cs +++ b/NewLife.IoT/Drivers/DriverInfo.cs @@ -1,6 +1,4 @@ -using System.Runtime.Serialization; -using System.Xml.Serialization; -using NewLife.IoT.ThingSpecification; +using NewLife.IoT.ThingSpecification; namespace NewLife.IoT.Drivers; @@ -9,31 +7,31 @@ public class DriverInfo { #region 属性 /// 名称 - public String Name { get; set; } + public String Name { get; set; } = null!; /// 显示名 - public String DisplayName { get; set; } + public String? DisplayName { get; set; } /// 类型。编程语言等,例如.NET - public String Type { get; set; } + public String? Type { get; set; } /// 类型名 - public String ClassName { get; set; } + public String? ClassName { get; set; } /// 驱动版本 - public String Version { get; set; } + public String? Version { get; set; } /// 该驱动所依赖的IoT版本 - public String IoTVersion { get; set; } + public String? IoTVersion { get; set; } /// 描述 - public String Description { get; set; } + public String? Description { get; set; } /// 默认参数。可作为设备参数模版,Xml格式带注释 - public String DefaultParameter { get; set; } + public String? DefaultParameter { get; set; } /// 产品物模型。如果设备有固定点位属性、服务和事件,则直接返回,否则返回空 - public ThingSpec Specification { get; set; } + public ThingSpec? Specification { get; set; } #endregion /// 友好显示名称 diff --git a/NewLife.IoT/Drivers/IDriver.cs b/NewLife.IoT/Drivers/IDriver.cs index 1eb5367..d0c5a0a 100644 --- a/NewLife.IoT/Drivers/IDriver.cs +++ b/NewLife.IoT/Drivers/IDriver.cs @@ -21,7 +21,7 @@ public interface IDriver /// /// Xml/Json参数配置,为空时仅创建默认参数 /// - IDriverParameter CreateParameter(String parameter = null); + IDriverParameter? CreateParameter(String? parameter = null); /// 获取产品物模型 /// @@ -32,7 +32,7 @@ public interface IDriver /// 获取后,按新版本覆盖旧版本。 /// /// - ThingSpec GetSpecification(); + ThingSpec? GetSpecification(); #endregion #region 核心方法 @@ -42,7 +42,7 @@ public interface IDriver /// 逻辑设备 /// 参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter /// 节点对象,可存储站号等信息,仅驱动自己识别 - INode Open(IDevice device, IDriverParameter parameter); + INode Open(IDevice device, IDriverParameter? parameter); /// /// 关闭设备节点。多节点共用通信链路时,需等最后一个节点关闭才能断开 @@ -58,7 +58,7 @@ public interface IDriver /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 点位集合 /// - IDictionary Read(INode node, IPoint[] points); + IDictionary Read(INode node, IPoint[] points); /// 写入数据 /// @@ -68,7 +68,7 @@ public interface IDriver /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 点位 /// 数值 - Object Write(INode node, IPoint point, Object value); + Object? Write(INode node, IPoint point, Object? value); /// 控制设备,特殊功能使用 /// @@ -77,7 +77,7 @@ public interface IDriver /// /// 节点对象,可存储站号等信息,仅驱动自己识别 /// 参数 - Object Control(INode node, IDictionary parameters); + Object? Control(INode node, IDictionary parameters); #endregion } @@ -94,6 +94,7 @@ public static class DriverExtensions public static INode Open(this IDriver driver, IDevice device, IDictionary parameters) { var type = driver.CreateParameter()?.GetType(); + if (type == null) throw new InvalidOperationException(); var ps = JsonHelper.Default.Convert(parameters, type) as IDriverParameter; var node = driver.Open(device, ps); diff --git a/NewLife.IoT/Drivers/IDriverParameter.cs b/NewLife.IoT/Drivers/IDriverParameter.cs index 599b5d4..abdd6bb 100644 --- a/NewLife.IoT/Drivers/IDriverParameter.cs +++ b/NewLife.IoT/Drivers/IDriverParameter.cs @@ -56,20 +56,20 @@ public static String GetKey(this IDriverParameter parameter) /// 序列化参数对象为Xml /// /// - public static String EncodeParameter(this IDriverParameter parameter) => parameter?.ToXml(null, true).Trim((Char)0xFEFF); + public static String EncodeParameter(this IDriverParameter parameter) => parameter.ToXml(null, true).Trim((Char)0xFEFF); /// 序列化参数字典为Xml /// /// - public static String EncodeParameter(this IDictionary parameter) => parameter?.ToXml(null, true).Trim((Char)0xFEFF); + public static String EncodeParameter(this IDictionary parameter) => parameter.ToXml(null, true).Trim((Char)0xFEFF); /// 从Xml/Json反序列化为字典 /// /// - public static IDictionary DecodeParameter(this String parameter) + public static IDictionary? DecodeParameter(this String parameter) { - parameter = parameter?.Trim((Char)0xFEFF); - if (parameter.IsNullOrEmpty()) return null; + parameter = parameter.Trim((Char)0xFEFF); + if (parameter.IsNullOrEmpty()) throw new ArgumentNullException(nameof(parameter)); // 按Xml或Json解析参数成为字典 var ps = parameter.StartsWith("<") && parameter.EndsWith(">") ? diff --git a/NewLife.IoT/Drivers/INode.cs b/NewLife.IoT/Drivers/INode.cs index ff4711e..87c6df9 100644 --- a/NewLife.IoT/Drivers/INode.cs +++ b/NewLife.IoT/Drivers/INode.cs @@ -13,12 +13,12 @@ public interface INode /// /// 设备。业务逻辑设备 /// - IDevice Device { get; set; } + IDevice? Device { get; set; } /// /// 参数。设备使用的专用参数 /// - IDriverParameter Parameter { get; set; } + IDriverParameter? Parameter { get; set; } } /// @@ -29,15 +29,15 @@ public class Node : INode /// /// 驱动。设备节点使用的驱动对象,可能多设备共用 /// - public IDriver Driver { get; set; } + public IDriver Driver { get; set; } = null!; /// /// 设备。业务逻辑设备 /// - public IDevice Device { get; set; } + public IDevice? Device { get; set; } /// /// 参数。设备使用的专用参数 /// - public IDriverParameter Parameter { get; set; } + public IDriverParameter? Parameter { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/IDevice.cs b/NewLife.IoT/IDevice.cs index 5b79028..0d3676b 100644 --- a/NewLife.IoT/IDevice.cs +++ b/NewLife.IoT/IDevice.cs @@ -15,13 +15,13 @@ public interface IDevice public String Code { get; set; } /// 属性集合 - IDictionary Properties { get; } + IDictionary Properties { get; } /// 产品物模型。自定义客户端或驱动插件内部,可借助物模型去规范所需要采集的数据 - ThingSpec Specification { get; set; } + ThingSpec? Specification { get; set; } /// 点位集合 - IPoint[] Points { get; set; } + IPoint[]? Points { get; set; } /// 服务集合 IDictionary Services { get; } @@ -60,7 +60,7 @@ public interface IDevice /// 设置属性 /// /// - void SetProperty(String name, Object value); + void SetProperty(String name, Object? value); /// 添加自定义数据,批量上传 /// diff --git a/NewLife.IoT/Models/DeviceInfo.cs b/NewLife.IoT/Models/DeviceInfo.cs index c5a71aa..286170e 100644 --- a/NewLife.IoT/Models/DeviceInfo.cs +++ b/NewLife.IoT/Models/DeviceInfo.cs @@ -4,17 +4,17 @@ public class DeviceInfo : IDeviceInfo { /// 编码 - public String Code { get; set; } + public String Code { get; set; } = null!; /// 名称 - public String Name { get; set; } + public String? Name { get; set; } /// 产品编码 - public String ProductCode { get; set; } + public String? ProductCode { get; set; } /// 协议。选择使用哪一种驱动协议 - public String Protocol { get; set; } + public String? Protocol { get; set; } /// 设备参数。Xml/Json格式配置,根据协议驱动来解析 - public String Parameter { get; set; } + public String? Parameter { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/Models/DeviceModel.cs b/NewLife.IoT/Models/DeviceModel.cs index eb8568f..aaa1566 100644 --- a/NewLife.IoT/Models/DeviceModel.cs +++ b/NewLife.IoT/Models/DeviceModel.cs @@ -4,13 +4,13 @@ public class DeviceModel : IDeviceInfo { /// 编码 - public String Code { get; set; } + public String Code { get; set; } = null!; /// 名称 - public String Name { get; set; } + public String? Name { get; set; } /// 产品编码 - public String ProductCode { get; set; } + public String? ProductCode { get; set; } /// 上报间隔。属性上报间隔,采集后暂存数据,定时上报,默认60秒,-1表示不上报属性 public Int32 PostPeriod { get; set; } @@ -22,10 +22,10 @@ public class DeviceModel : IDeviceInfo public Int32 PollingTime { get; set; } /// 协议。选择使用哪一种驱动协议 - public String Protocol { get; set; } + public String? Protocol { get; set; } /// 设备参数。Xml/Json格式配置,根据协议驱动来解析 - public String Parameter { get; set; } + public String? Parameter { get; set; } /// 更新时间。用于判断数据变化 public DateTime UpdateTime { get; set; } diff --git a/NewLife.IoT/Models/IDeviceInfo.cs b/NewLife.IoT/Models/IDeviceInfo.cs index 1ef0440..b7041be 100644 --- a/NewLife.IoT/Models/IDeviceInfo.cs +++ b/NewLife.IoT/Models/IDeviceInfo.cs @@ -13,8 +13,8 @@ public interface IDeviceInfo String Code { get; set; } /// 名称 - String Name { get; set; } + String? Name { get; set; } /// 产品编码 - String ProductCode { get; set; } + String? ProductCode { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/Models/ServiceRequest.cs b/NewLife.IoT/Models/ServiceRequest.cs index 6244441..259aa93 100644 --- a/NewLife.IoT/Models/ServiceRequest.cs +++ b/NewLife.IoT/Models/ServiceRequest.cs @@ -7,13 +7,13 @@ public class ServiceRequest public Int32 DeviceId { get; set; } /// 设备编码。目标设备或其子设备 - public String DeviceCode { get; set; } + public String? DeviceCode { get; set; } /// 服务名 - public String ServiceName { get; set; } + public String ServiceName { get; set; } = null!; /// 入参。传递给该服务的参数,常见Json格式 - public String InputData { get; set; } + public String? InputData { get; set; } /// 开始执行时间。用于提前下发指令后延期执行 public DateTime StartTime { get;set; } diff --git a/NewLife.IoT/NewLife.IoT.csproj b/NewLife.IoT/NewLife.IoT.csproj index 37a7419..9d16a12 100644 --- a/NewLife.IoT/NewLife.IoT.csproj +++ b/NewLife.IoT/NewLife.IoT.csproj @@ -13,6 +13,7 @@ false ..\Bin True + enable enable latest True @@ -27,7 +28,7 @@ https://github.com/NewLifeX/NewLife.IoT git 物联网;IoT;边缘计算;Edge;新生命团队;NewLife;$(AssemblyName) - 增强IDriver.Open;恢复支持net45 + 启用可空特性 MIT true true @@ -49,10 +50,10 @@ - + - + diff --git a/NewLife.IoT/ThingModels/DataModel.cs b/NewLife.IoT/ThingModels/DataModel.cs index 57a4c86..a3fe764 100644 --- a/NewLife.IoT/ThingModels/DataModel.cs +++ b/NewLife.IoT/ThingModels/DataModel.cs @@ -7,8 +7,8 @@ public class DataModel public Int64 Time { get; set; } /// 名称 - public String Name { get; set; } + public String Name { get; set; } = null!; /// 数据 - public String Value { get; set; } + public String? Value { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/DataModels.cs b/NewLife.IoT/ThingModels/DataModels.cs index f73574f..f95fbbc 100644 --- a/NewLife.IoT/ThingModels/DataModels.cs +++ b/NewLife.IoT/ThingModels/DataModels.cs @@ -4,8 +4,8 @@ public class DataModels { /// 设备编码 - public String DeviceCode { get; set; } + public String DeviceCode { get; set; } = null!; /// 数据集合 - public DataModel[] Items { get; set; } + public DataModel[]? Items { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/DevicePropertyModel.cs b/NewLife.IoT/ThingModels/DevicePropertyModel.cs index 9b5cd85..3686058 100644 --- a/NewLife.IoT/ThingModels/DevicePropertyModel.cs +++ b/NewLife.IoT/ThingModels/DevicePropertyModel.cs @@ -4,5 +4,5 @@ public class DevicePropertyModel : PropertyModel { /// 设备编码 - public String DeviceCode { get; set; } + public String DeviceCode { get; set; } = null!; } diff --git a/NewLife.IoT/ThingModels/EventModel.cs b/NewLife.IoT/ThingModels/EventModel.cs index 7d3b97e..cb8b77c 100644 --- a/NewLife.IoT/ThingModels/EventModel.cs +++ b/NewLife.IoT/ThingModels/EventModel.cs @@ -7,11 +7,11 @@ public class EventModel public Int64 Time { get; set; } /// 事件类型。info/alert/error - public String Type { get; set; } + public String? Type { get; set; } /// 名称。事件名称,例如LightOpen - public String Name { get; set; } + public String? Name { get; set; } /// 内容。事件详情 - public String Remark { get; set; } + public String? Remark { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/EventModels.cs b/NewLife.IoT/ThingModels/EventModels.cs index 64edf38..deb7506 100644 --- a/NewLife.IoT/ThingModels/EventModels.cs +++ b/NewLife.IoT/ThingModels/EventModels.cs @@ -4,8 +4,8 @@ public class EventModels { /// 设备编码 - public String DeviceCode { get; set; } + public String DeviceCode { get; set; } = null!; /// 事件集合 - public EventModel[] Items { get; set; } + public EventModel[]? Items { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/IPoint.cs b/NewLife.IoT/ThingModels/IPoint.cs index 66324f6..7975594 100644 --- a/NewLife.IoT/ThingModels/IPoint.cs +++ b/NewLife.IoT/ThingModels/IPoint.cs @@ -7,14 +7,14 @@ namespace NewLife.IoT.ThingModels; public interface IPoint { /// 名称 - String Name { get; set; } + String? Name { get; set; } /// 地址。表示点位的地址,具体含义由设备驱动决定。例如常规地址6,字母地址DI07,Modbus地址4x0015,位域地址D012.05,比特位置0~15 /// 在某些场景中,特殊点位地址(如#)表示虚拟地址,该点位数值由ReadRule表达式动态计算得到,点位信息并不会传递给驱动层 - String Address { get; set; } + String? Address { get; set; } /// 数据类型。来自物模型 - String Type { get; set; } + String? Type { get; set; } /// 大小。数据字节数,或字符串长度,Modbus寄存器一般占2个字节 Int32 Length { get; set; } @@ -46,7 +46,7 @@ public static Object Convert(this IPoint point, Byte[] data) TypeCode.UInt16 => BitConverter.ToUInt16(data, 0), TypeCode.UInt32 => BitConverter.ToUInt32(data, 0), TypeCode.UInt64 => BitConverter.ToUInt64(data, 0), - _ => null, + _ => throw new NotSupportedException(), }; } @@ -56,24 +56,24 @@ public static Object Convert(this IPoint point, Byte[] data) /// 点位 /// 数据对象 /// - public static Byte[] GetBytes(this IPoint point, Object value) + public static Byte[]? GetBytes(this IPoint point, Object value) { var type = point.GetNetType(); var val = value.ChangeType(type); return type.GetTypeCode() switch { - TypeCode.Boolean => BitConverter.GetBytes((Boolean)val), - TypeCode.Byte => new[] { (Byte)val }, - TypeCode.Char => BitConverter.GetBytes((Char)val), - TypeCode.Double => BitConverter.GetBytes((Double)val), - TypeCode.Int16 => BitConverter.GetBytes((Int16)val), - TypeCode.Int32 => BitConverter.GetBytes((Int32)val), - TypeCode.Int64 => BitConverter.GetBytes((Int64)val), - TypeCode.Single => BitConverter.GetBytes((Single)val), - TypeCode.UInt16 => BitConverter.GetBytes((UInt16)val), - TypeCode.UInt32 => BitConverter.GetBytes((UInt32)val), - TypeCode.UInt64 => BitConverter.GetBytes((UInt64)val), + TypeCode.Boolean => BitConverter.GetBytes((Boolean)(val ?? false)), + TypeCode.Byte => new[] { (Byte)(val ?? 0) }, + TypeCode.Char => BitConverter.GetBytes((Char)(val ?? 0)), + TypeCode.Double => BitConverter.GetBytes((Double)(val ?? 0)), + TypeCode.Int16 => BitConverter.GetBytes((Int16)(val ?? 0)), + TypeCode.Int32 => BitConverter.GetBytes((Int32)(val ?? 0)), + TypeCode.Int64 => BitConverter.GetBytes((Int64)(val ?? 0)), + TypeCode.Single => BitConverter.GetBytes((Single)(val ?? 0)), + TypeCode.UInt16 => BitConverter.GetBytes((UInt16)(val ?? 0)), + TypeCode.UInt32 => BitConverter.GetBytes((UInt32)(val ?? 0)), + TypeCode.UInt64 => BitConverter.GetBytes((UInt64)(val ?? 0)), _ => null, }; } @@ -84,7 +84,7 @@ public static Byte[] GetBytes(this IPoint point, Object value) /// 原始数据,一般是字符串 /// 物模型 /// - public static UInt16[] ConvertToBit(this IPoint point, Object data, ThingSpec spec = null) + public static UInt16[]? ConvertToBit(this IPoint point, Object data, ThingSpec? spec = null) { var type = TypeHelper.GetNetType(point); if (type == null) @@ -120,7 +120,7 @@ public static UInt16[] ConvertToBit(this IPoint point, Object data, ThingSpec sp /// 原始数据,一般是字符串 /// 物模型 /// 返回短整型数组,有可能一个整数拆分为双字 - public static UInt16[] ConvertToWord(this IPoint point, Object data, ThingSpec spec = null) + public static UInt16[]? ConvertToWord(this IPoint point, Object data, ThingSpec? spec = null) { var type = TypeHelper.GetNetType(point); if (type == null) diff --git a/NewLife.IoT/ThingModels/PointModel.cs b/NewLife.IoT/ThingModels/PointModel.cs index 74eff07..5605b71 100644 --- a/NewLife.IoT/ThingModels/PointModel.cs +++ b/NewLife.IoT/ThingModels/PointModel.cs @@ -4,23 +4,23 @@ public class PointModel : IPoint { /// 名称 - public String Name { get; set; } + public String? Name { get; set; } /// 地址。表示点位的地址,具体含义由设备驱动决定。例如常规地址6,字母地址DI07,Modbus地址4x0015,位域地址D012.05,比特位置0~15 /// 在某些场景中,特殊点位地址(如#)表示虚拟地址,该点位数值由ReadRule表达式动态计算得到,点位信息并不会传递给驱动层 - public String Address { get; set; } + public String? Address { get; set; } /// 数据类型。来自物模型 - public String Type { get; set; } + public String? Type { get; set; } /// 大小。数据字节数,或字符串长度,Modbus寄存器一般占2个字节 public Int32 Length { get; set; } /// 读取规则。数据解析规则,表达式或脚本 - public String ReadRule { get; set; } + public String? ReadRule { get; set; } /// 写入规则。数据反解析规则,表达式或脚本 - public String WriteRule { get; set; } + public String? WriteRule { get; set; } /// 已重载。友好显示 /// diff --git a/NewLife.IoT/ThingModels/PropertyModel.cs b/NewLife.IoT/ThingModels/PropertyModel.cs index cf61ef0..5f6230d 100644 --- a/NewLife.IoT/ThingModels/PropertyModel.cs +++ b/NewLife.IoT/ThingModels/PropertyModel.cs @@ -4,8 +4,8 @@ public class PropertyModel { /// 名称 - public String Name { get; set; } + public String Name { get; set; } = null!; /// 数值 - public Object Value { get; set; } + public Object? Value { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/PropertyModels.cs b/NewLife.IoT/ThingModels/PropertyModels.cs index a2c231a..dbe265a 100644 --- a/NewLife.IoT/ThingModels/PropertyModels.cs +++ b/NewLife.IoT/ThingModels/PropertyModels.cs @@ -6,8 +6,8 @@ public class PropertyModels { /// 设备编码 - public String DeviceCode { get; set; } + public String DeviceCode { get; set; } = null!; /// 属性集合 - public PropertyModel[] Items { get; set; } + public PropertyModel[]? Items { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/ServiceEventArgs.cs b/NewLife.IoT/ThingModels/ServiceEventArgs.cs index d2fe1fb..10f5489 100644 --- a/NewLife.IoT/ThingModels/ServiceEventArgs.cs +++ b/NewLife.IoT/ThingModels/ServiceEventArgs.cs @@ -8,10 +8,10 @@ public class ServiceEventArgs : EventArgs /// /// 命令 /// - public ServiceModel Model { get; set; } + public ServiceModel? Model { get; set; } /// /// 响应 /// - public ServiceReplyModel Reply { get; set; } + public ServiceReplyModel? Reply { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/ServiceModel.cs b/NewLife.IoT/ThingModels/ServiceModel.cs index bb4276e..eac4213 100644 --- a/NewLife.IoT/ThingModels/ServiceModel.cs +++ b/NewLife.IoT/ThingModels/ServiceModel.cs @@ -7,10 +7,10 @@ public class ServiceModel public Int64 Id { get; set; } /// 服务名。如SetProperty - public String Name { get; set; } + public String Name { get; set; } = null!; /// 入参。传递给该服务的参数,常见Json格式 - public String InputData { get; set; } + public String? InputData { get; set; } /// 开始执行时间。用于提前下发指令后延期执行,暂时不支持取消 public DateTime StartTime { get; set; } @@ -19,8 +19,8 @@ public class ServiceModel public DateTime Expire { get; set; } /// 设备编码。服务调用请求发送给网关设备时,该参数指定子设备编码 - public String DeviceCode { get; set; } + public String? DeviceCode { get; set; } /// 链路追踪 - public String TraceId { get; set; } + public String? TraceId { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/ServiceReplyModel.cs b/NewLife.IoT/ThingModels/ServiceReplyModel.cs index f59a8ba..57290ec 100644 --- a/NewLife.IoT/ThingModels/ServiceReplyModel.cs +++ b/NewLife.IoT/ThingModels/ServiceReplyModel.cs @@ -10,5 +10,5 @@ public class ServiceReplyModel public ServiceStatus Status { get; set; } /// 返回数据 - public String Data { get; set; } + public String? Data { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingModels/ShadowModel.cs b/NewLife.IoT/ThingModels/ShadowModel.cs index 6054a49..3002e51 100644 --- a/NewLife.IoT/ThingModels/ShadowModel.cs +++ b/NewLife.IoT/ThingModels/ShadowModel.cs @@ -6,8 +6,8 @@ public class ShadowModel { /// 设备编码 - public String DeviceCode { get; set; } + public String DeviceCode { get; set; } = null!; /// 影子 - public Object Shadow { get; set; } + public Object? Shadow { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingSpecification/DataSpecs.cs b/NewLife.IoT/ThingSpecification/DataSpecs.cs index 38079b3..538b2a9 100644 --- a/NewLife.IoT/ThingSpecification/DataSpecs.cs +++ b/NewLife.IoT/ThingSpecification/DataSpecs.cs @@ -13,10 +13,10 @@ public class DataSpecs public Double Max { get; set; } /// 单位 - public String Unit { get; set; } + public String? Unit { get; set; } /// 单位名称 - public String UnitName { get; set; } + public String? UnitName { get; set; } /// 步进 public Double Step { get; set; } @@ -25,16 +25,16 @@ public class DataSpecs public Int32 Length { get; set; } /// 枚举映射。布尔型和数字型特有,例如“0=关,1=开”,又如“1=东,2=南,3=西,4=北” - public IDictionary Mapping { get; set; } + public IDictionary? Mapping { get; set; } #endregion #region 方法 /// 根据指定数据类型获取成员字典,不同类型所需要的字段不一样 /// 数据类型 /// - public IDictionary GetDictionary(String type) + public IDictionary GetDictionary(String type) { - var ds = new Dictionary(); + var ds = new Dictionary(); var t = TypeHelper.GetNetType(type); if (t == null) return this.ToDictionary(); diff --git a/NewLife.IoT/ThingSpecification/EventSpec.cs b/NewLife.IoT/ThingSpecification/EventSpec.cs index f955ebc..070efae 100644 --- a/NewLife.IoT/ThingSpecification/EventSpec.cs +++ b/NewLife.IoT/ThingSpecification/EventSpec.cs @@ -14,22 +14,22 @@ public class EventSpec : SpecBase /// /// 类型。info/warning/error /// - public String Type { get; set; } + public String? Type { get; set; } /// /// 描述 /// [DataMember(Name="desc")] - public String Description { get; set; } + public String? Description { get; set; } /// /// 方法 /// - public String Method { get; set; } + public String? Method { get; set; } /// /// 输出 /// - public PropertySpec[] OutputData { get; set; } + public PropertySpec[]? OutputData { get; set; } #endregion } \ No newline at end of file diff --git a/NewLife.IoT/ThingSpecification/Profile.cs b/NewLife.IoT/ThingSpecification/Profile.cs index 5ca70f3..4d0347f 100644 --- a/NewLife.IoT/ThingSpecification/Profile.cs +++ b/NewLife.IoT/ThingSpecification/Profile.cs @@ -13,5 +13,5 @@ public class Profile /// /// 产品编码 /// - public String ProductKey { get; set; } + public String? ProductKey { get; set; } } \ No newline at end of file diff --git a/NewLife.IoT/ThingSpecification/PropertyExtend.cs b/NewLife.IoT/ThingSpecification/PropertyExtend.cs index 0b93e9a..e502bae 100644 --- a/NewLife.IoT/ThingSpecification/PropertyExtend.cs +++ b/NewLife.IoT/ThingSpecification/PropertyExtend.cs @@ -13,10 +13,10 @@ public class PropertyExtend : IDictionarySource #region 属性 /// 唯一标识 [DataMember(Name = "identifier")] - public String Id { get; set; } + public String Id { get; set; } = null!; /// 采集点位置信息。常规地址6,Modbus地址 4x0023,位域地址D12.05,虚拟点位地址# - public String Address { get; set; } + public String? Address { get; set; } /// 交换16。字节交换,12转21,默认false大端 public Boolean Swap16 { get; set; } @@ -31,10 +31,10 @@ public class PropertyExtend : IDictionarySource public Single Constant { get; set; } /// 读取规则。数据解析规则,表达式或脚本 - public String ReadRule { get; set; } + public String? ReadRule { get; set; } /// 写入规则。数据反解析规则,表达式或脚本 - public String WriteRule { get; set; } + public String? WriteRule { get; set; } /// 事件模式。在客户端或服务端生成属性变更事件 public EventModes EventMode { get; set; } diff --git a/NewLife.IoT/ThingSpecification/PropertySpec.cs b/NewLife.IoT/ThingSpecification/PropertySpec.cs index 7dbd63d..a71d5e5 100644 --- a/NewLife.IoT/ThingSpecification/PropertySpec.cs +++ b/NewLife.IoT/ThingSpecification/PropertySpec.cs @@ -15,12 +15,12 @@ public class PropertySpec : SpecBase, IDictionarySource /// /// 访问模式 /// - public String AccessMode { get; set; } + public String? AccessMode { get; set; } /// /// 数据类型 /// - public TypeSpec DataType { get; set; } + public TypeSpec? DataType { get; set; } ///// ///// 采集点位置信息 @@ -60,7 +60,7 @@ public static PropertySpec Create(String id, String name, String type, Int32 len /// public static PropertySpec Create(MemberInfo member, Int32 length = 0) { - if (member == null) return null; + //if (member == null) return null; var ps = new PropertySpec { @@ -83,9 +83,9 @@ public static PropertySpec Create(MemberInfo member, Int32 length = 0) #region 方法 /// 转字典。根据不同类型,提供不一样的序列化能力 /// - public IDictionary ToDictionary() + public IDictionary ToDictionary() { - var dic = new Dictionary(); + var dic = new Dictionary(); if (!Id.IsNullOrEmpty()) dic.Add("identifier", Id); if (!Name.IsNullOrEmpty()) diff --git a/NewLife.IoT/ThingSpecification/ServiceSpec.cs b/NewLife.IoT/ThingSpecification/ServiceSpec.cs index dce1ecd..5386a0b 100644 --- a/NewLife.IoT/ThingSpecification/ServiceSpec.cs +++ b/NewLife.IoT/ThingSpecification/ServiceSpec.cs @@ -16,28 +16,28 @@ public class ServiceSpec : SpecBase /// 调用类型。sync/async /// [DataMember(Name = "callType")] - public String Type { get; set; } + public String? Type { get; set; } /// /// 描述 /// [DataMember(Name = "desc")] - public String Description { get; set; } + public String? Description { get; set; } /// /// 方法 /// - public String Method { get; set; } + public String? Method { get; set; } /// /// 输入 /// - public PropertySpec[] InputData { get; set; } + public PropertySpec[]? InputData { get; set; } /// /// 输出 /// - public PropertySpec[] OutputData { get; set; } + public PropertySpec[]? OutputData { get; set; } #endregion #region 方法 @@ -51,7 +51,7 @@ public class ServiceSpec : SpecBase /// public static ServiceSpec Create(MethodBase method) { - if (method == null) return null; + //if (method == null) return null; var ss = new ServiceSpec { @@ -79,7 +79,7 @@ public static ServiceSpec Create(MethodBase method) /// public static PropertySpec Create(ParameterInfo member) { - if (member == null) return null; + //if (member == null) return null; var ps = new PropertySpec { diff --git a/NewLife.IoT/ThingSpecification/SpecBase.cs b/NewLife.IoT/ThingSpecification/SpecBase.cs index 9a4bd29..9832354 100644 --- a/NewLife.IoT/ThingSpecification/SpecBase.cs +++ b/NewLife.IoT/ThingSpecification/SpecBase.cs @@ -12,12 +12,12 @@ public abstract class SpecBase /// 唯一标识 /// [DataMember(Name = "identifier")] - public String Id { get; set; } + public String Id { get; set; } = null!; /// /// 名称 /// - public String Name { get; set; } + public String? Name { get; set; } /// /// 是否必须 diff --git a/NewLife.IoT/ThingSpecification/ThingSpec.cs b/NewLife.IoT/ThingSpecification/ThingSpec.cs index 9d70e00..6ed5dbd 100644 --- a/NewLife.IoT/ThingSpecification/ThingSpec.cs +++ b/NewLife.IoT/ThingSpecification/ThingSpec.cs @@ -13,19 +13,19 @@ public class ThingSpec public String Schema { get; set; } = "http://iot.feifan.link/schema.json"; /// 简介 - public Profile Profile { get; set; } + public Profile? Profile { get; set; } /// 属性 - public PropertySpec[] Properties { get; set; } + public PropertySpec[]? Properties { get; set; } /// 事件 - public EventSpec[] Events { get; set; } + public EventSpec[]? Events { get; set; } /// 服务 - public ServiceSpec[] Services { get; set; } + public ServiceSpec[]? Services { get; set; } /// 属性扩展 - public PropertyExtend[] ExtendedProperties { get; set; } + public PropertyExtend[]? ExtendedProperties { get; set; } #endregion #region 方法 @@ -36,7 +36,7 @@ public class ThingSpec /// 长度 /// 点位地址 /// - public void AddProperty(String id, String name, String type, Int32 length = 0, String address = null) + public void AddProperty(String id, String name, String type, Int32 length = 0, String? address = null) { { var ps = PropertySpec.Create(id, name, type, length); diff --git a/NewLife.IoT/ThingSpecification/TypeSpec.cs b/NewLife.IoT/ThingSpecification/TypeSpec.cs index 6ab4f14..2b9e279 100644 --- a/NewLife.IoT/ThingSpecification/TypeSpec.cs +++ b/NewLife.IoT/ThingSpecification/TypeSpec.cs @@ -10,12 +10,12 @@ public class TypeSpec : IDictionarySource /// /// 类型。int/float/text /// - public String Type { get; set; } + public String? Type { get; set; } /// /// 数据规范 /// - public DataSpecs Specs { get; set; } + public DataSpecs? Specs { get; set; } /// 转字典。根据不同类型,提供不一样的序列化能力 /// diff --git a/NewLife.IoT/TypeHelper.cs b/NewLife.IoT/TypeHelper.cs index e749d94..d630d87 100644 --- a/NewLife.IoT/TypeHelper.cs +++ b/NewLife.IoT/TypeHelper.cs @@ -64,7 +64,7 @@ public static Int32 GetLength(Type type) /// /// /// - public static Type GetNetType(String type) + public static Type? GetNetType(String? type) { if (type.IsNullOrEmpty()) return null; diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index ca766f4..5244618 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -13,10 +13,10 @@ - + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive