using HslCommunication;
using System.ComponentModel;
using System.Reflection;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_Tasks.StackerCraneJob
{
public class GetStackerObject
{
private SpeStackerCrane _speStackerCrane;
public GetStackerObject(SpeStackerCrane speStackerCrane)
{
_speStackerCrane = speStackerCrane;
}
///
/// 设备状态(有无货状态)
///
///
///
public RgvEquipmentStatus RgvCraneStatusValue => GetRgvCraneStatus();
///
/// 工作模式(1联机 0脱机)
///
///
///
public RgvCraneAutoStatus RgvCraneAutoStatusValue => GetRgvCraneAutoStatus();
///
/// 任务状态(0空闲、1执行中、2已完成、3异常完成)
///
///
///
public RgvJobCraneStatus StaclerkJobJobStatusValue => GetRgvJobCraneWorkStatus();
///
/// 步进状态(0无动作、1取货行走、2取货、3放货行走、4放货、5仅行走中)
///
///
///
public RGVStepprocess RgvCraneWorkStatusValue => GetRgvCraneWorkStatus();
///
/// 当前,子母车的地址
///
public int RGVCurrentlocation => GetRGVCurrentlocation();
///
/// 当前rgv任务
///
public int CurrentRgvtaskid => GetCurrentRgvtaskid();
///
/// 当前兰剑任务
///
public int CurrentLanjiantaskid => GetLanjiantaskid();
///
/// 获取RGV设备状态
///
///
private RgvEquipmentStatus GetRgvCraneStatus()
{
return Enum.Parse(GetStatus(nameof(RgvEquipmentStatus)));
}
///
/// 获取rgv手自动状态
///
///
private RgvCraneAutoStatus GetRgvCraneAutoStatus()
{
return Enum.Parse(GetStatus(nameof(RgvCraneAutoStatus)));
}
///
/// 获取rgv工作状态
///
///
private RGVStepprocess GetRgvCraneWorkStatus()
{
return Enum.Parse(GetStatus(nameof(RGVStepprocess)));
}
///
/// 获取rgv作业状态
///
///
private RgvJobCraneStatus GetRgvJobCraneWorkStatus()
{
return Enum.Parse(GetStatus(nameof(RgvJobCraneStatus)));
}
///
/// 获取当前地址
///
///
private int GetRGVCurrentlocation()
{
DeviceProDTO deviceProDTO = _speStackerCrane.DeviceProDTOs.FirstOrDefault((DeviceProDTO x) => x.DeviceProParamName == "RGVCurrentlocation");
if (deviceProDTO != null)
{
return (int)_speStackerCrane.Communicator.ReadAsObj(deviceProDTO.DeviceProAddress, deviceProDTO.DeviceDataType, 1);
}
return 0;
}
///
/// 获取rgv任务id
///
///
private int GetCurrentRgvtaskid()
{
DeviceProDTO deviceProDTO = _speStackerCrane.DeviceProDTOs.FirstOrDefault((DeviceProDTO x) => x.DeviceProParamName == "RGV_Rgvtaskid");
if (deviceProDTO != null)
{
return (int)_speStackerCrane.Communicator.ReadAsObj(deviceProDTO.DeviceProAddress, deviceProDTO.DeviceDataType, 1);
}
return 0;
}
///
/// 获取当前兰剑任务id
///
///
private int GetLanjiantaskid()
{
DeviceProDTO deviceProDTO = _speStackerCrane.DeviceProDTOs.FirstOrDefault((DeviceProDTO x) => x.DeviceProParamName == "RGV_Lanjiantaskid");
if (deviceProDTO != null)
{
return (int)_speStackerCrane.Communicator.ReadAsObj(deviceProDTO.DeviceProAddress, deviceProDTO.DeviceDataType, 1);
}
return 0;
}
private string GetEnumDes(T value) where T : Enum
{
FieldInfo? fieldInfo = typeof(T).GetField(value.ToString());
if (fieldInfo != null)
{
DescriptionAttribute? descriptionAttribute = fieldInfo.GetCustomAttribute();
if (descriptionAttribute != null)
{
return descriptionAttribute.Description;
}
return "未定义";
}
return "未知";
}
private string GetStatus(string protocolParamType)
{
try
{
List devicePros = _speStackerCrane.DeviceProDTOs.Where(x => x.DeviceProParamType == protocolParamType).ToList();
if (devicePros.Count == 0)
{
throw new Exception("未获取到协议信息");
}
for (int i = 0; i < devicePros.Count; i++)
{
object readStatus = _speStackerCrane.Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
DeviceProtocolDetailDTO? deviceProtocolDetail = _speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
deviceProtocolDetail = _speStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamType && x.ProtocalDetailValue.Equals(readStatus.ToString()));
if (deviceProtocolDetail != null)
{
return deviceProtocolDetail.ProtocolDetailType;
}
return StackerCraneStatus.Unkonw.ToString();
}
return StackerCraneStatus.Unkonw.ToString();
}
catch (Exception ex)
{
throw;
}
}
public bool SendCommand(RgvCraneTaskCommand command)
{
try
{
if(_speStackerCrane.IsConnected)
{
_speStackerCrane.SetValue(RgvCraneDBName.RGV_RGVTasklocation, command.RGV_RGVTasklocation);
_speStackerCrane.SetValue(RgvCraneDBName.RGV_RGVtasktype, command.RGV_RGVtasktype);
_speStackerCrane.SetValue(RgvCraneDBName.RGV_Rgvtaskid, command.RGV_Rgvtaskid);
_speStackerCrane.SetValue(RgvCraneDBName.RGV_Lanjiantaskid, command.RGV_Lanjiantaskid);
return true;
}
return false;
}
catch (Exception ex)
{
return false;
}
}
}
}