Skip to content

Commit

Permalink
v1.9.2023.1001 启用可空特性
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 1, 2023
1 parent 34eca54 commit 8e7e3a9
Show file tree
Hide file tree
Showing 36 changed files with 153 additions and 154 deletions.
20 changes: 10 additions & 10 deletions NewLife.IoT/Drivers/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DriverBase<TNode, TParameter> : DriverBase
/// <param name="device">逻辑设备</param>
/// <param name="parameter">参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter</param>
/// <returns>节点对象,可存储站号等信息,仅驱动自己识别</returns>
public override INode Open(IDevice device, IDriverParameter parameter)
public override INode Open(IDevice device, IDriverParameter? parameter)
{
var node = new TNode
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public abstract class DriverBase : DisposeBase, IDriver, ILogFeature, ITracerFea
/// </remarks>
/// <param name="parameter">Xml/Json参数配置</param>
/// <returns></returns>
public virtual IDriverParameter CreateParameter(String parameter)
public virtual IDriverParameter? CreateParameter(String? parameter)
{
var p = OnCreateParameter();
if (p == null) return null;
Expand All @@ -76,7 +76,7 @@ public virtual IDriverParameter CreateParameter(String parameter)

/// <summary>创建驱动参数对象,分析参数配置或创建默认参数</summary>
/// <returns></returns>
protected virtual IDriverParameter OnCreateParameter() => null;
protected virtual IDriverParameter? OnCreateParameter() => null;

/// <summary>获取产品物模型</summary>
/// <remarks>
Expand All @@ -87,7 +87,7 @@ public virtual IDriverParameter CreateParameter(String parameter)
/// 获取后,按新版本覆盖旧版本。
/// </remarks>
/// <returns></returns>
public virtual ThingSpec GetSpecification()
public virtual ThingSpec? GetSpecification()
{
var type = GetType();
var spec = new ThingSpec
Expand Down Expand Up @@ -115,7 +115,7 @@ public virtual ThingSpec GetSpecification()
/// <param name="device">逻辑设备</param>
/// <param name="parameter">参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter</param>
/// <returns>节点对象,可存储站号等信息,仅驱动自己识别</returns>
public virtual INode Open(IDevice device, IDriverParameter parameter)
public virtual INode Open(IDevice device, IDriverParameter? parameter)
{
var node = new Node
{
Expand All @@ -140,7 +140,7 @@ public virtual void Close(INode node) { }
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="points">点位集合</param>
/// <returns></returns>
public virtual IDictionary<String, Object> Read(INode node, IPoint[] points) => throw new NotImplementedException();
public virtual IDictionary<String, Object?> Read(INode node, IPoint[] points) => throw new NotImplementedException();

/// <summary>写入数据</summary>
/// <remarks>
Expand All @@ -150,7 +150,7 @@ public virtual void Close(INode node) { }
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="point">点位</param>
/// <param name="value">数值</param>
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();

/// <summary>控制设备,特殊功能使用</summary>
/// <remarks>
Expand All @@ -159,15 +159,15 @@ public virtual void Close(INode node) { }
/// </remarks>
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="parameters">参数</param>
public virtual Object Control(INode node, IDictionary<String, Object> parameters) => throw new NotImplementedException();
public virtual Object? Control(INode node, IDictionary<String, Object?> parameters) => throw new NotImplementedException();
#endregion

#region 日志
/// <summary>日志</summary>
public ILog Log { get; set; }
public ILog Log { get; set; } = Logger.Null;

/// <summary>性能追踪器</summary>
public ITracer Tracer { get; set; }
public ITracer? Tracer { get; set; }

Check warning on line 170 in NewLife.IoT/Drivers/DriverBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type of 'ITracer? DriverBase.Tracer.get' doesn't match implicitly implemented member 'ITracer ITracerFeature.Tracer.get' (possibly because of nullability attributes).

Check warning on line 170 in NewLife.IoT/Drivers/DriverBase.cs

View workflow job for this annotation

GitHub Actions / build-test

Nullability of reference types in return type of 'ITracer? DriverBase.Tracer.get' doesn't match implicitly implemented member 'ITracer ITracerFeature.Tracer.get' (possibly because of nullability attributes).

Check warning on line 170 in NewLife.IoT/Drivers/DriverBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type of 'ITracer? DriverBase.Tracer.get' doesn't match implicitly implemented member 'ITracer ITracerFeature.Tracer.get' (possibly because of nullability attributes).

/// <summary>写日志</summary>
/// <param name="format"></param>
Expand Down
15 changes: 7 additions & 8 deletions NewLife.IoT/Drivers/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using NewLife.Log;
using NewLife.Reflection;
using NewLife.Xml;

namespace NewLife.IoT.Drivers;

Expand All @@ -15,12 +14,12 @@ public class DriverFactory
public static IDictionary<String, Type> Map => _map;

/// <summary>驱动集合</summary>
public static IList<DriverInfo> Drivers { get; private set; }
public static IList<DriverInfo> Drivers { get; private set; } = null!;

/// <summary>注册协议实现</summary>
/// <param name="name"></param>
/// <param name="type"></param>
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;
Expand All @@ -31,14 +30,14 @@ public static void Register(String name, Type type)
/// <summary>注册协议实现</summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
public static void Register<T>(String name = null) where T : IDriver => Register(name, typeof(T));
public static void Register<T>(String? name = null) where T : IDriver => Register(name, typeof(T));

private static readonly ConcurrentDictionary<String, IDriver> _cache = new();
private static readonly ConcurrentDictionary<String, IDriver?> _cache = new();
/// <summary>创建协议实例,根据地址确保唯一,多设备共用同一个串口</summary>
/// <param name="name">驱动名称。一般由DriverAttribute特性确定</param>
/// <param name="identifier">唯一标识。没有传递标识时,每次返回新实例</param>
/// <returns></returns>
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;

Expand All @@ -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<String, IDriver> _defaults = new();
private static readonly ConcurrentDictionary<String, IDriver?> _defaults = new();
/// <summary>创建驱动参数对象,分析参数配置或创建默认参数</summary>
/// <param name="name">驱动名称。一般由DriverAttribute特性确定</param>
/// <param name="parameter">Xml/Json参数配置</param>
/// <returns></returns>
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;

Expand Down
22 changes: 10 additions & 12 deletions NewLife.IoT/Drivers/DriverInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.Serialization;
using System.Xml.Serialization;
using NewLife.IoT.ThingSpecification;
using NewLife.IoT.ThingSpecification;

namespace NewLife.IoT.Drivers;

Expand All @@ -9,31 +7,31 @@ public class DriverInfo
{
#region 属性
/// <summary>名称</summary>
public String Name { get; set; }
public String Name { get; set; } = null!;

/// <summary>显示名</summary>
public String DisplayName { get; set; }
public String? DisplayName { get; set; }

/// <summary>类型。编程语言等,例如.NET</summary>
public String Type { get; set; }
public String? Type { get; set; }

/// <summary>类型名</summary>
public String ClassName { get; set; }
public String? ClassName { get; set; }

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

/// <summary>该驱动所依赖的IoT版本</summary>
public String IoTVersion { get; set; }
public String? IoTVersion { get; set; }

/// <summary>描述</summary>
public String Description { get; set; }
public String? Description { get; set; }

/// <summary>默认参数。可作为设备参数模版,Xml格式带注释</summary>
public String DefaultParameter { get; set; }
public String? DefaultParameter { get; set; }

/// <summary>产品物模型。如果设备有固定点位属性、服务和事件,则直接返回,否则返回空</summary>
public ThingSpec Specification { get; set; }
public ThingSpec? Specification { get; set; }
#endregion

/// <summary>友好显示名称</summary>
Expand Down
13 changes: 7 additions & 6 deletions NewLife.IoT/Drivers/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IDriver
/// </remarks>
/// <param name="parameter">Xml/Json参数配置,为空时仅创建默认参数</param>
/// <returns></returns>
IDriverParameter CreateParameter(String parameter = null);
IDriverParameter? CreateParameter(String? parameter = null);

/// <summary>获取产品物模型</summary>
/// <remarks>
Expand All @@ -32,7 +32,7 @@ public interface IDriver
/// 获取后,按新版本覆盖旧版本。
/// </remarks>
/// <returns></returns>
ThingSpec GetSpecification();
ThingSpec? GetSpecification();
#endregion

#region 核心方法
Expand All @@ -42,7 +42,7 @@ public interface IDriver
/// <param name="device">逻辑设备</param>
/// <param name="parameter">参数。不同驱动的参数设置相差较大,对象字典具有较好灵活性,其对应IDriverParameter</param>
/// <returns>节点对象,可存储站号等信息,仅驱动自己识别</returns>
INode Open(IDevice device, IDriverParameter parameter);
INode Open(IDevice device, IDriverParameter? parameter);

/// <summary>
/// 关闭设备节点。多节点共用通信链路时,需等最后一个节点关闭才能断开
Expand All @@ -58,7 +58,7 @@ public interface IDriver
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="points">点位集合</param>
/// <returns></returns>
IDictionary<String, Object> Read(INode node, IPoint[] points);
IDictionary<String, Object?> Read(INode node, IPoint[] points);

/// <summary>写入数据</summary>
/// <remarks>
Expand All @@ -68,7 +68,7 @@ public interface IDriver
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="point">点位</param>
/// <param name="value">数值</param>
Object Write(INode node, IPoint point, Object value);
Object? Write(INode node, IPoint point, Object? value);

/// <summary>控制设备,特殊功能使用</summary>
/// <remarks>
Expand All @@ -77,7 +77,7 @@ public interface IDriver
/// </remarks>
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param>
/// <param name="parameters">参数</param>
Object Control(INode node, IDictionary<String, Object> parameters);
Object? Control(INode node, IDictionary<String, Object?> parameters);
#endregion
}

Expand All @@ -94,6 +94,7 @@ public static class DriverExtensions
public static INode Open(this IDriver driver, IDevice device, IDictionary<String, Object> 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);
Expand Down
10 changes: 5 additions & 5 deletions NewLife.IoT/Drivers/IDriverParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ public static String GetKey(this IDriverParameter parameter)
/// <summary>序列化参数对象为Xml</summary>
/// <param name="parameter"></param>
/// <returns></returns>
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);

/// <summary>序列化参数字典为Xml</summary>
/// <param name="parameter"></param>
/// <returns></returns>
public static String EncodeParameter(this IDictionary<String, Object> parameter) => parameter?.ToXml(null, true).Trim((Char)0xFEFF);
public static String EncodeParameter(this IDictionary<String, Object> parameter) => parameter.ToXml(null, true).Trim((Char)0xFEFF);

/// <summary>从Xml/Json反序列化为字典</summary>
/// <param name="parameter"></param>
/// <returns></returns>
public static IDictionary<String, Object> DecodeParameter(this String parameter)
public static IDictionary<String, Object?>? 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(">") ?
Expand Down
10 changes: 5 additions & 5 deletions NewLife.IoT/Drivers/INode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public interface INode
/// <summary>
/// 设备。业务逻辑设备
/// </summary>
IDevice Device { get; set; }
IDevice? Device { get; set; }

/// <summary>
/// 参数。设备使用的专用参数
/// </summary>
IDriverParameter Parameter { get; set; }
IDriverParameter? Parameter { get; set; }
}

/// <summary>
Expand All @@ -29,15 +29,15 @@ public class Node : INode
/// <summary>
/// 驱动。设备节点使用的驱动对象,可能多设备共用
/// </summary>
public IDriver Driver { get; set; }
public IDriver Driver { get; set; } = null!;

/// <summary>
/// 设备。业务逻辑设备
/// </summary>
public IDevice Device { get; set; }
public IDevice? Device { get; set; }

/// <summary>
/// 参数。设备使用的专用参数
/// </summary>
public IDriverParameter Parameter { get; set; }
public IDriverParameter? Parameter { get; set; }
}
8 changes: 4 additions & 4 deletions NewLife.IoT/IDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public interface IDevice
public String Code { get; set; }

/// <summary>属性集合</summary>
IDictionary<String, Object> Properties { get; }
IDictionary<String, Object?> Properties { get; }

/// <summary>产品物模型。自定义客户端或驱动插件内部,可借助物模型去规范所需要采集的数据</summary>
ThingSpec Specification { get; set; }
ThingSpec? Specification { get; set; }

/// <summary>点位集合</summary>
IPoint[] Points { get; set; }
IPoint[]? Points { get; set; }

/// <summary>服务集合</summary>
IDictionary<String, Delegate> Services { get; }
Expand Down Expand Up @@ -60,7 +60,7 @@ public interface IDevice
/// <summary>设置属性</summary>
/// <param name="name"></param>
/// <param name="value"></param>
void SetProperty(String name, Object value);
void SetProperty(String name, Object? value);

/// <summary>添加自定义数据,批量上传</summary>
/// <param name="name"></param>
Expand Down
10 changes: 5 additions & 5 deletions NewLife.IoT/Models/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
public class DeviceInfo : IDeviceInfo
{
/// <summary>编码</summary>
public String Code { get; set; }
public String Code { get; set; } = null!;

/// <summary>名称</summary>
public String Name { get; set; }
public String? Name { get; set; }

/// <summary>产品编码</summary>
public String ProductCode { get; set; }
public String? ProductCode { get; set; }

/// <summary>协议。选择使用哪一种驱动协议</summary>
public String Protocol { get; set; }
public String? Protocol { get; set; }

/// <summary>设备参数。Xml/Json格式配置,根据协议驱动来解析</summary>
public String Parameter { get; set; }
public String? Parameter { get; set; }
}
10 changes: 5 additions & 5 deletions NewLife.IoT/Models/DeviceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
public class DeviceModel : IDeviceInfo
{
/// <summary>编码</summary>
public String Code { get; set; }
public String Code { get; set; } = null!;

/// <summary>名称</summary>
public String Name { get; set; }
public String? Name { get; set; }

/// <summary>产品编码</summary>
public String ProductCode { get; set; }
public String? ProductCode { get; set; }

/// <summary>上报间隔。属性上报间隔,采集后暂存数据,定时上报,默认60秒,-1表示不上报属性</summary>
public Int32 PostPeriod { get; set; }
Expand All @@ -22,10 +22,10 @@ public class DeviceModel : IDeviceInfo
public Int32 PollingTime { get; set; }

/// <summary>协议。选择使用哪一种驱动协议</summary>
public String Protocol { get; set; }
public String? Protocol { get; set; }

/// <summary>设备参数。Xml/Json格式配置,根据协议驱动来解析</summary>
public String Parameter { get; set; }
public String? Parameter { get; set; }

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

0 comments on commit 8e7e3a9

Please sign in to comment.