iotgateway/Plugins/Drivers/CNC.MTConnect/DeviceMTClient.cs

123 lines
3.2 KiB
C#
Raw Normal View History

2022-01-02 15:24:11 +00:00
using PluginInterface;
using OpenNETCF.MTConnect;
using Microsoft.Extensions.Logging;
namespace CNC.MTConnect
2022-01-02 15:24:11 +00:00
{
[DriverSupported("MTConnectClient")]
[DriverInfo("MTConnectClient", "V1.0.0", "Copyright IoTGateway.net 20230220")]
public class DeviceMTClient : IDriver
2022-01-02 15:24:11 +00:00
{
2022-08-10 08:52:53 +00:00
private EntityClient? _mClient;
public ILogger _logger { get; set; }
private readonly string _device;
2022-01-02 15:24:11 +00:00
#region
2022-08-10 08:52:53 +00:00
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
2022-01-02 15:24:11 +00:00
2022-08-10 08:52:53 +00:00
[ConfigParameter("uri")] public string Uri { get; set; }
2022-01-02 15:24:11 +00:00
2022-08-10 08:52:53 +00:00
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
2022-01-02 15:24:11 +00:00
2022-08-10 08:52:53 +00:00
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
2022-01-02 15:24:11 +00:00
public bool IsConnected { get; set; }
#endregion
#region
public DeviceMTClient(string device, ILogger logger)
2022-01-02 15:24:11 +00:00
{
_device = device;
_logger = logger;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Create()");
2022-01-02 15:24:11 +00:00
}
public bool Close()
{
try
{
2022-08-10 08:52:53 +00:00
_mClient = null;
2022-01-02 15:24:11 +00:00
IsConnected = false;
return true;
}
catch (Exception)
{
return false;
}
}
public bool Connect()
{
try
{
2022-08-10 08:52:53 +00:00
_mClient = new EntityClient(Uri);
_mClient.RequestTimeout = Timeout;
2022-01-02 15:24:11 +00:00
IsConnected = true;
}
catch (Exception)
{
IsConnected = false;
}
2022-08-10 08:52:53 +00:00
2022-01-02 15:24:11 +00:00
return IsConnected;
}
public void Dispose()
{
// Suppress finalization.
GC.SuppressFinalize(this);
2022-01-02 15:24:11 +00:00
}
#endregion
#region
2022-01-02 15:24:11 +00:00
[Method("读MTConnect", description: "读MTConnect ID")]
2022-08-10 08:52:53 +00:00
public DriverReturnValueModel ReadById(DriverAddressIoArgModel ioarg)
2022-01-02 15:24:11 +00:00
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (IsConnected)
{
try
{
2022-08-10 08:52:53 +00:00
var dataValue = _mClient?.GetDataItemById(ioarg.Address).Value;
2022-01-02 15:24:11 +00:00
ret.Value = dataValue;
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
2022-08-10 08:52:53 +00:00
2022-01-02 15:24:11 +00:00
return ret;
}
2022-08-10 08:52:53 +00:00
public DriverReturnValueModel Read(DriverAddressIoArgModel ioarg)
2022-01-02 15:24:11 +00:00
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
return ret;
}
2022-08-10 08:52:53 +00:00
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
2022-04-13 09:01:24 +00:00
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
return rpcResponse;
}
#endregion
2022-01-02 15:24:11 +00:00
}
}