2022-07-16 15:35:16 +00:00
|
|
|
|
using PluginInterface;
|
|
|
|
|
using Automation.OPCClient;
|
2022-08-06 10:21:00 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
namespace OPC.DaClient
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2022-08-10 08:53:20 +00:00
|
|
|
|
[DriverSupported("OPCDaClient")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
[DriverInfo("OPCDaClient", "V1.0.0", "Copyright IoTGateway.net 20230220")]
|
|
|
|
|
internal class DeviceDaClient : IDriver
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
private OPCClientWrapper? _opcDaClient;
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2022-08-06 10:21:00 +00:00
|
|
|
|
public ILogger _logger { get; set; }
|
2022-08-08 07:15:09 +00:00
|
|
|
|
private readonly string _device;
|
2022-08-06 10:21:00 +00:00
|
|
|
|
|
2022-07-16 15:35:16 +00:00
|
|
|
|
#region 配置参数
|
|
|
|
|
|
2022-08-10 08:53:20 +00:00
|
|
|
|
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2022-08-10 08:53:20 +00:00
|
|
|
|
[ConfigParameter("IP")] public string Ip { get; set; } = "127.0.0.1";
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2022-08-10 08:53:20 +00:00
|
|
|
|
[ConfigParameter("OpcServerName")] public string OpcServerName { get; set; } = "ICONICS.SimulatorOPCDA.2";
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
|
|
|
|
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
#region 生命周期
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 反射构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="device"></param>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
public DeviceDaClient(string device, ILogger logger)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2022-08-06 10:21:00 +00:00
|
|
|
|
_device = device;
|
|
|
|
|
_logger = logger;
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2022-08-08 07:15:09 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Create()");
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsConnected => _opcDaClient != null && _opcDaClient.IsOPCServerConnected();
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-07-16 15:35:16 +00:00
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Connect()");
|
|
|
|
|
|
|
|
|
|
_opcDaClient = new OPCClientWrapper();
|
|
|
|
|
_opcDaClient.Init(Ip, OpcServerName);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Connect()");
|
2022-07-16 15:35:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IsConnected;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 断开
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-07-16 15:35:16 +00:00
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Close()");
|
|
|
|
|
|
|
|
|
|
_opcDaClient?.Disconnect();
|
2022-07-16 15:35:16 +00:00
|
|
|
|
return !IsConnected;
|
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogError(ex, $"Device:[{_device}],Close(),Error");
|
2022-07-16 15:35:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 释放
|
|
|
|
|
/// </summary>
|
2022-07-16 15:35:16 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Dispose()");
|
|
|
|
|
|
|
|
|
|
_opcDaClient = null;
|
|
|
|
|
|
|
|
|
|
// Suppress finalization.
|
|
|
|
|
GC.SuppressFinalize(this);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogError(ex, $"Device:[{_device}],Dispose(),Error");
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 读写方法
|
|
|
|
|
|
2022-07-16 15:35:16 +00:00
|
|
|
|
|
|
|
|
|
[Method("读OPCDa", description: "读OPCDa节点")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
public DriverReturnValueModel ReadNode(DriverAddressIoArgModel ioArg)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
|
|
|
|
if (IsConnected)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var dataValue = _opcDaClient?.ReadNodeLabel(ioArg.Address);
|
|
|
|
|
switch (ioArg.ValueType)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
|
|
|
|
case DataTypeEnum.Bit:
|
|
|
|
|
ret.Value = dataValue == "On" ? 1 : 0;
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Bool:
|
|
|
|
|
ret.Value = dataValue == "On";
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Byte:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = byte.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.UByte:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = sbyte.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int16:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = short.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Uint16:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = ushort.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int32:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = int.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Uint32:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = uint.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Float:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = float.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Double:
|
2022-08-10 08:53:20 +00:00
|
|
|
|
if (dataValue != null) ret.Value = double.Parse(dataValue);
|
2022-07-16 15:35:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.AsciiString:
|
|
|
|
|
case DataTypeEnum.Utf8String:
|
2024-05-31 14:20:57 +00:00
|
|
|
|
case DataTypeEnum.Any:
|
2022-07-16 15:35:16 +00:00
|
|
|
|
ret.Value = dataValue;
|
|
|
|
|
break;
|
2022-08-10 08:53:20 +00:00
|
|
|
|
default:
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
2023-04-12 15:40:00 +00:00
|
|
|
|
ret.Message = $"读取失败,不支持的类型:{ioArg.ValueType}";
|
2022-08-10 08:53:20 +00:00
|
|
|
|
break;
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = $"读取失败,{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Method("测试方法", description: "测试方法,返回当前时间")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
|
|
|
|
if (IsConnected)
|
|
|
|
|
ret.Value = DateTime.Now;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg)
|
2022-07-16 15:35:16 +00:00
|
|
|
|
{
|
|
|
|
|
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
|
2023-04-12 15:40:00 +00:00
|
|
|
|
await Task.CompletedTask;
|
2022-07-16 15:35:16 +00:00
|
|
|
|
return rpcResponse;
|
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-07-16 15:35:16 +00:00
|
|
|
|
}
|
2022-08-10 08:53:20 +00:00
|
|
|
|
}
|