using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using WIDESEAWCS_Communicator;
|
using WIDESEAWCS_QuartzJob.DTO;
|
|
namespace WIDESEAWCS_QuartzJob.StackerCrane.Common
|
{
|
public class StackerCraneBase : IStackerCraneBase
|
{
|
public bool IsCanSendTask(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs)
|
{
|
List<string> paramNames = ResolveDefaultParamNames(DeviceProDTOs, DeviceProtocolDetailDTOs);
|
if (paramNames.Count == 0)
|
{
|
return false;
|
}
|
|
return IsCanSendTaskByParamNames(communicator, DeviceProDTOs, DeviceProtocolDetailDTOs, paramNames);
|
}
|
|
public bool IsCanSendTask(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs, params string[] suffixes)
|
{
|
if (suffixes == null || suffixes.Length == 0)
|
{
|
return false;
|
}
|
|
List<string> paramNames = new List<string>(suffixes.Length);
|
foreach (string suffix in suffixes)
|
{
|
string? paramName = ResolveParamName(DeviceProDTOs, DeviceProtocolDetailDTOs, suffix);
|
if (paramName == null)
|
{
|
return false;
|
}
|
|
paramNames.Add(paramName);
|
}
|
|
return IsCanSendTaskByParamNames(communicator, DeviceProDTOs, DeviceProtocolDetailDTOs, paramNames);
|
}
|
|
private static List<string> ResolveDefaultParamNames(List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs)
|
{
|
List<string> paramNames = new List<string>();
|
foreach (var devicePro in deviceProDTOs)
|
{
|
if (!devicePro.DeviceProParamName.Contains("StackerCrane", StringComparison.Ordinal))
|
{
|
continue;
|
}
|
|
if (!devicePro.DeviceProParamName.EndsWith("Status", StringComparison.Ordinal))
|
{
|
continue;
|
}
|
|
if (deviceProtocolDetailDTOs.Any(x => x.DeviceProParamName == devicePro.DeviceProParamName))
|
{
|
paramNames.Add(devicePro.DeviceProParamName);
|
}
|
}
|
|
return paramNames;
|
}
|
|
private bool IsCanSendTaskByParamNames(BaseCommunicator communicator, List<DeviceProDTO> DeviceProDTOs, List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs, List<string> paramNames)
|
{
|
for (int i = 0; i < paramNames.Count; i++)
|
{
|
DeviceProDTO? devicePro = DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == paramNames[i]);
|
if (devicePro == null)
|
{
|
throw new Exception();
|
}
|
|
DeviceProtocolDetailDTO? deviceProtocolDetail = DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == paramNames[i]);
|
if (deviceProtocolDetail == null)
|
{
|
throw new Exception();
|
}
|
|
object obj = communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
|
|
if (!(obj?.ToString() ?? "").Equals(deviceProtocolDetail.ProtocalDetailValue))
|
{
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
private static string? ResolveParamName(List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string suffix)
|
{
|
foreach (var devicePro in deviceProDTOs)
|
{
|
if (!devicePro.DeviceProParamName.EndsWith(suffix, StringComparison.Ordinal))
|
{
|
continue;
|
}
|
|
if (deviceProtocolDetailDTOs.Any(x => x.DeviceProParamName == devicePro.DeviceProParamName))
|
{
|
return devicePro.DeviceProParamName;
|
}
|
}
|
|
return null;
|
}
|
}
|
}
|