iotgateway/Plugins/Drivers/DriverMTConnect/MTConnectClient.cs

108 lines
2.8 KiB
C#
Raw Normal View History

2022-01-02 15:24:11 +00:00
using PluginInterface;
using System;
using OpenNETCF.MTConnect;
namespace DriverMTConnect
{
internal class MTConnectClient : IDriver
{
#region
[ConfigParameter("设备Id")]
public Guid DeviceId { get; set; }
[ConfigParameter("uri")]
public string Uri { get; set; }
[ConfigParameter("超时时间ms")]
public int Timeout { get; set; } = 3000;
2022-01-02 15:24:11 +00:00
[ConfigParameter("最小通讯周期ms")]
public uint MinPeriod { get; set; } = 3000;
public bool IsConnected { get; set; }
#endregion
EntityClient m_client = null;
public MTConnectClient(Guid deviceId)
{
DeviceId = deviceId;
}
public bool Close()
{
try
{
m_client = null;
IsConnected = false;
return true;
}
catch (Exception)
{
return false;
}
}
public bool Connect()
{
try
{
m_client = new EntityClient(Uri);
m_client.RequestTimeout = Timeout;
2022-01-02 15:24:11 +00:00
IsConnected = true;
}
catch (Exception)
{
IsConnected = false;
}
return IsConnected;
}
public void Dispose()
{
return;
}
[Method("读MTConnect", description: "读MTConnect ID")]
public DriverReturnValueModel ReadById(DriverAddressIoArgModel Ioarg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (IsConnected)
{
try
{
var dataValue = m_client.GetDataItemById(Ioarg.Address).Value;
ret.Value = dataValue;
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
return ret;
}
public DriverReturnValueModel Read(DriverAddressIoArgModel Ioarg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
return ret;
}
2022-04-13 09:01:24 +00:00
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg)
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
return rpcResponse;
}
2022-01-02 15:24:11 +00:00
}
}