优化OPCDA驱动

This commit is contained in:
iioter 2022-08-10 16:53:20 +08:00
parent 61422f89a2
commit 4076f65e85

View File

@ -1,30 +1,25 @@
using PluginInterface; using PluginInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automation.OPCClient; using Automation.OPCClient;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace DriverOPCDaClient namespace DriverOPCDaClient
{ {
[DriverSupported("OPCDaClient")]
[DriverInfo("OPCDaClient", "V1.0.0", "Copyright IoTGateway© 2022-08-10")]
internal class OPCDaClient : IDriver internal class OPCDaClient : IDriver
{ {
OPCClientWrapper opcDaClient = null; private OPCClientWrapper? opcDaClient;
public ILogger _logger { get; set; } public ILogger _logger { get; set; }
private readonly string _device; private readonly string _device;
#region #region
[ConfigParameter("设备Id")] public Guid DeviceId { get; set; } [ConfigParameter("设备Id")] public string DeviceId { get; set; }
[ConfigParameter("IP")] [ConfigParameter("IP")] public string Ip { get; set; } = "127.0.0.1";
public string IP{ get; set; } = "127.0.0.1";
[ConfigParameter("OpcServerName")] [ConfigParameter("OpcServerName")] public string OpcServerName { get; set; } = "ICONICS.SimulatorOPCDA.2";
public string OpcServerName { get; set; } = "ICONICS.SimulatorOPCDA.2";
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000; [ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
@ -51,7 +46,7 @@ namespace DriverOPCDaClient
try try
{ {
opcDaClient = new OPCClientWrapper(); opcDaClient = new OPCClientWrapper();
opcDaClient.Init(IP, OpcServerName); opcDaClient.Init(Ip, OpcServerName);
} }
catch (Exception) catch (Exception)
{ {
@ -70,7 +65,6 @@ namespace DriverOPCDaClient
} }
catch (Exception) catch (Exception)
{ {
return false; return false;
} }
} }
@ -83,7 +77,6 @@ namespace DriverOPCDaClient
} }
catch (Exception) catch (Exception)
{ {
} }
} }
@ -97,7 +90,7 @@ namespace DriverOPCDaClient
{ {
try try
{ {
var dataValue = opcDaClient.ReadNodeLabel(ioarg.Address); var dataValue = opcDaClient?.ReadNodeLabel(ioarg.Address);
switch (ioarg.ValueType) switch (ioarg.ValueType)
{ {
case DataTypeEnum.Bit: case DataTypeEnum.Bit:
@ -107,33 +100,37 @@ namespace DriverOPCDaClient
ret.Value = dataValue == "On"; ret.Value = dataValue == "On";
break; break;
case DataTypeEnum.Byte: case DataTypeEnum.Byte:
ret.Value = byte.Parse(dataValue); if (dataValue != null) ret.Value = byte.Parse(dataValue);
break; break;
case DataTypeEnum.UByte: case DataTypeEnum.UByte:
ret.Value = sbyte.Parse(dataValue); if (dataValue != null) ret.Value = sbyte.Parse(dataValue);
break; break;
case DataTypeEnum.Int16: case DataTypeEnum.Int16:
ret.Value = short.Parse(dataValue); if (dataValue != null) ret.Value = short.Parse(dataValue);
break; break;
case DataTypeEnum.Uint16: case DataTypeEnum.Uint16:
ret.Value = ushort.Parse(dataValue); if (dataValue != null) ret.Value = ushort.Parse(dataValue);
break; break;
case DataTypeEnum.Int32: case DataTypeEnum.Int32:
ret.Value = int.Parse(dataValue); if (dataValue != null) ret.Value = int.Parse(dataValue);
break; break;
case DataTypeEnum.Uint32: case DataTypeEnum.Uint32:
ret.Value = uint.Parse(dataValue); if (dataValue != null) ret.Value = uint.Parse(dataValue);
break; break;
case DataTypeEnum.Float: case DataTypeEnum.Float:
ret.Value = float.Parse(dataValue); if (dataValue != null) ret.Value = float.Parse(dataValue);
break; break;
case DataTypeEnum.Double: case DataTypeEnum.Double:
ret.Value = double.Parse(dataValue); if (dataValue != null) ret.Value = double.Parse(dataValue);
break; break;
case DataTypeEnum.AsciiString: case DataTypeEnum.AsciiString:
case DataTypeEnum.Utf8String: case DataTypeEnum.Utf8String:
ret.Value = dataValue; ret.Value = dataValue;
break; break;
default:
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,不支持的类型:{ioarg.ValueType}";
break;
} }
} }
catch (Exception ex) catch (Exception ex)
@ -167,7 +164,7 @@ namespace DriverOPCDaClient
return ret; return ret;
} }
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg) public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
{ {
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" }; RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
return rpcResponse; return rpcResponse;