using HslCommunication;
|
using System.ComponentModel;
|
using System.Reflection;
|
using WIDESEAWCS_Communicator;
|
using WIDESEAWCS_QuartzJob.DeviceBase;
|
using WIDESEAWCS_QuartzJob.DTO;
|
using WIDESEAWCS_QuartzJob.StackerCrane;
|
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
|
|
namespace WIDESEAWCS_QuartzJob
|
{
|
/// <summary>
|
/// 一般堆垛机实现类,实现堆垛机接口层
|
/// </summary>
|
[Description("堆垛机")]
|
public class STK : IStackerCrane
|
{
|
#region Private Member
|
|
/// <summary>
|
/// 完成信号等待时间
|
/// </summary>
|
private const int WaitTimeout = 20 * 6000;
|
|
/// <summary>
|
/// 完成信号读取频率
|
/// </summary>
|
private const int ReadTimeout = 100;
|
|
/// <summary>
|
/// 堆垛机通讯对象
|
/// </summary>
|
private BaseCommunicator _communicator;
|
/// <summary>
|
/// 堆垛机协议信息
|
/// </summary>
|
private readonly List<DeviceProDTO> _deviceProDTOs;
|
/// <summary>
|
/// 堆垛机协议明细信息
|
/// </summary>
|
private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs;
|
/// <summary>
|
/// 设备编号
|
/// </summary>
|
public readonly string _deviceCode;
|
/// <summary>
|
/// 设备名称
|
/// </summary>
|
public readonly string _deviceName;
|
/// <summary>
|
/// 上一次任务号
|
/// </summary>
|
private int _lastTaskNum;
|
|
private bool _isChecked = false;
|
|
private bool _heartStatr = true;
|
|
private bool _isConnected = true;
|
#endregion Private Member
|
|
#region Public Member
|
/// <summary>
|
/// 堆垛机通讯对象
|
/// </summary>
|
public BaseCommunicator Communicator => _communicator;
|
|
/// <summary>
|
/// 堆垛机协议信息
|
/// </summary>
|
public List<DeviceProDTO> DeviceProDTOs => _deviceProDTOs;
|
|
/// <summary>
|
/// 堆垛机协议明细信息
|
/// </summary>
|
public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
|
|
/// <summary>
|
/// 上一次执行的任务号
|
/// </summary>
|
public int LastTaskNum => _lastTaskNum;
|
|
/// <summary>
|
/// 设备编号
|
/// </summary>
|
public string DeviceCode => _deviceCode;
|
|
/// <summary>
|
/// 设备名称
|
/// </summary>
|
public string DeviceName => _deviceName;
|
|
/// <summary>
|
/// 通讯是否已连接
|
/// </summary>
|
public bool IsConnected => Communicator.IsConnected && _isConnected;
|
|
/// <summary>
|
/// 堆垛机任务完成事件
|
/// </summary>
|
public event EventHandler<StackerCraneTaskCompletedEventArgs> StackerCraneTaskCompletedEventHandler;
|
|
/// <summary>
|
/// 堆垛机任务命令对象
|
/// </summary>
|
public object StackerCraneTaskCommand { get; set; }
|
|
/// <summary>
|
/// 堆垛机完成事件是否已订阅
|
/// </summary>
|
public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null;
|
|
/// <summary>
|
/// 堆垛机与MOM连接状态
|
/// </summary>
|
public bool StackerOnline { get; set; } = false;
|
|
public int? LastTaskType { get; set; } = null;
|
|
public int CurrentTaskNum => throw new NotImplementedException();
|
|
public bool IsFault => throw new NotImplementedException();
|
|
public DeviceStatus Status => throw new NotImplementedException();
|
#endregion
|
|
#region Constructor Function
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="communicator">堆垛机通讯对象</param>
|
/// <param name="deviceProDTOs">堆垛机协议信息</param>
|
/// <param name="deviceProtocolDetailDTOs">堆垛机协议明细信息</param>
|
/// <param name="deviceCode">设备编号</param>
|
/// <param name="deviceName">设备名称</param>
|
public STK(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName)
|
{
|
_communicator = communicator;
|
_deviceProDTOs = deviceProDTOs;
|
_deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
|
_deviceCode = deviceCode;
|
_deviceName = deviceName;
|
}
|
#endregion
|
|
#region Private Method
|
private void AnalysisData()
|
{
|
|
}
|
#endregion
|
|
#region Public Method
|
/// <summary>
|
/// 发送任务命令
|
/// </summary>
|
/// <param name="command">任务命令</param>
|
/// <returns></returns>
|
public bool SendCommand<T>(T command, string childCode) where T : IDataTransfer, new()
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == childCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
if (devicePro == null)
|
{
|
return false;
|
}
|
if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
|
{
|
StackerCraneTaskCommand = command;
|
return true;
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// 读取PLC数据,返回自定义对象
|
/// </summary>
|
/// <typeparam name="T">泛型</typeparam>
|
/// <param name="deviceChildCode">子设备编号</param>
|
/// <returns>返回自定义对象或抛出异常</returns>
|
/// <exception cref="Exception"></exception>
|
public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new()
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
|
if (devicePro == null)
|
{
|
throw new Exception("未找到协议信息");
|
}
|
else
|
{
|
return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
|
}
|
}
|
|
/// <summary>
|
/// 读取PLC数据,返回自定义对象
|
/// </summary>
|
/// <typeparam name="T">泛型</typeparam>
|
/// <param name="deviceChildCode">子设备编号</param>
|
/// <param name="typeName">参数类型</param>
|
/// <returns>返回自定义对象或抛出异常</returns>
|
/// <exception cref="Exception"></exception>
|
public T ReadCustomer<T>(string deviceChildCode, string typeName) where T : IDataTransfer, new()
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == typeName && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
|
|
if (devicePro == null)
|
{
|
throw new Exception("未找到协议信息");
|
}
|
else
|
{
|
return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
|
}
|
}
|
|
/// <summary>
|
/// 根据参数名称、设备子编号写入对应的数据。
|
/// </summary>
|
/// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
|
/// <typeparam name="TValue">要写入的数据类型。</typeparam>
|
/// <param name="enum">参数名称。</param>
|
/// <param name="value">要写入的数据。</param>
|
/// <param name="deviceChildCode">设备子编号写</param>
|
/// <returns>返回写入成功或失败</returns>
|
public bool SetValue<TEnum, TValue>(TEnum @enum, TValue value, string deviceChildCode)
|
where TEnum : Enum
|
where TValue : notnull
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
|
return devicePro == null ? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
|
}
|
|
|
/// <summary>
|
/// 根据参数名称、设备子编号写入对应的数据。
|
/// </summary>
|
/// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
|
/// <typeparam name="TValue">要写入的数据类型。</typeparam>
|
/// <param name="enum">参数名称。</param>
|
/// <param name="value">要写入的数据。</param>
|
/// <param name="deviceChildCode">设备子编号写</param>
|
/// <returns>返回写入成功或失败</returns>
|
public bool SetValue<TEnum, TValue>(TEnum @enum, TValue value, string deviceChildCode, string typeName)
|
where TEnum : Enum
|
where TValue : notnull
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamType == typeName && x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
|
return devicePro == null ? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
|
}
|
|
/// <summary>
|
/// 根据参数名称读取堆垛机对应的数据。
|
/// </summary>
|
/// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
|
/// <typeparam name="TRsult">读取结果的返回值类型。</typeparam>
|
/// <param name="value">参数名称。</param>
|
/// <returns>返回读取到的数据。</returns>
|
/// <exception cref="Exception"></exception>
|
public TRsult GetValue<TEnum, TRsult>(TEnum value, string childCode) where TEnum : Enum
|
{
|
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
|
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == childCode);
|
return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
|
}
|
|
/// <summary>
|
/// 与设备的心跳
|
/// </summary>
|
public void Heartbeat()
|
{
|
|
}
|
|
public void Dispose()
|
{
|
_heartStatr = false;
|
_communicator.Dispose();
|
GC.SuppressFinalize(this);
|
}
|
|
public bool SendCommand<T>(T command) where T : IDataTransfer, new()
|
{
|
throw new NotImplementedException();
|
}
|
#endregion
|
}
|
}
|