using HslCommunication;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_QuartzJob
{
///
/// 自定义堆垛机
///
[Description("堆垛机2")]
public class SpeStackerCrane : IStackerCrane
{
#region Private Member
///
/// 堆垛机通讯对象
///
private BaseCommunicator _communicator;
///
/// 堆垛机协议信息
///
private readonly List _deviceProDTOs;
///
/// 堆垛机协议明细信息
///
private readonly List _deviceProtocolDetailDTOs;
///
/// 设备编号
///
public readonly string _deviceCode;
///
/// 设备名称
///
public readonly string _deviceName;
///
/// 上一次任务号
///
private int _lastTaskNum;
private bool _isChecked = false;
private bool _heartStatr = true;
private bool _isConnected = true;
#endregion Private Member
#region Public Member
public BaseCommunicator Communicator => _communicator;
public List DeviceProDTOs => _deviceProDTOs;
public List DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
public int LastTaskNum => _lastTaskNum;
public int CurrentTaskNum => GetCurrentTaskNum();
public int? LastTaskType { get; set; } = null;
public string DeviceCode => _deviceCode;
public string DeviceName => _deviceName;
public bool IsConnected => Communicator.IsConnected && _isConnected;
public object StackerCraneTaskCommand { get; set; }
#endregion
#region Constructor Function
///
/// 构造函数
///
/// 堆垛机通讯对象
/// 堆垛机协议信息
/// 堆垛机协议明细信息
/// 设备编号
/// 设备名称
public SpeStackerCrane(BaseCommunicator communicator, List deviceProDTOs, List deviceProtocolDetailDTOs, string deviceCode, string deviceName)
{
_communicator = communicator;
_deviceProDTOs = deviceProDTOs;
_deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
_deviceCode = deviceCode;
_deviceName = deviceName;
CheckConnect();
}
#endregion
#region Private Method
private object GetStatus(string protocolParamType)
{
if (Communicator.IsConnected)
{
List devicePros = _deviceProDTOs.Where(x => x.DeviceProParamType == protocolParamType).ToList();
if (devicePros.Count == 0)
{
throw new Exception("未获取到协议信息");
}
for (int i = 0; i < devicePros.Count; i++)
{
object readStatus = Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
//todo 协议明细信息未获取到时抛出异常
DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamType && x.ProtocalDetailValue.Equals(readStatus.ToString()));
if (deviceProtocolDetail != null)
{
return Convert.ToInt32(readStatus);
}
return -1;
}
}
//todo 通讯未连接时抛出异常
return -1;
}
private int GetCurrentTaskNum()
{
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CurrentTaskNum));
return devicePro == null ? throw new Exception($"读取当前任务号错误,未获取到协议信息,请检查配置参数名称是否配置,且配置为{nameof(CurrentTaskNum)}") : (int)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
}
private void CheckConnect()
{
Task.Run(() =>
{
while (_heartStatr)
{
try
{
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault();
if (devicePro == null)
_isConnected = false;
else
Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
_isConnected = true;
}
catch (Exception ex)
{
_isConnected = false;
}
Thread.Sleep(500);
}
});
}
#endregion
#region Public Method
public T GetStackerCraneStatus() where T : notnull, Enum
{
return (T)GetStatus(typeof(T).Name);
}
public void Dispose()
{
_heartStatr = false;
_communicator.Dispose();
GC.SuppressFinalize(this);
}
public TResult GetValue(TEnum value) where TEnum : Enum
{
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString());
return devicePro == null ? throw new Exception($"读取数据错误,未在协议信息里面找到参数{value.ToString()}") : (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
}
public void Heartbeat()
{
}
public bool SendCommand(T command) where T : IDataTransfer, new()
{
if (Communicator is SiemensS7)
{
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
if (devicePro == null)
{
return false;
}
if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
{
StackerCraneTaskCommand = command;
//CheckStackerCraneTaskCompleted();
return true;
}
return false;
}
else
{
throw new Exception("暂不支持除西门子之外的PLC");
}
}
public bool SetValue(TEnum @enum, TValue value)
where TEnum : Enum
where TValue : notnull
{
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString());
return devicePro == null ? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
}
#endregion
}
}