using WIDESEAWCS_QuartzJob;
namespace WIDESEAWCS_Tasks
{
///
/// 输送线设备请求处理器:处理拘束机/插拔钉机上下层请求。
///
public class ConveyorLineTargetAddressSelector
{
private const string ConstraintMachineName = "拘束机";
private const string PinMachineName = "插拔钉机";
public void HandleInboundNextAddress(CommonConveyorLine conveyorLine, string nextAddress, string childDeviceCode)
{
HandleDeviceRequest(conveyorLine, nextAddress, childDeviceCode, isUpper: true);
}
public void HandleOutboundNextAddress(CommonConveyorLine conveyorLine, string nextAddress, string childDeviceCode)
{
HandleDeviceRequest(conveyorLine, nextAddress, childDeviceCode, isUpper: false);
}
private void HandleDeviceRequest(CommonConveyorLine conveyorLine, string nextAddress, string childDeviceCode, bool isUpper)
{
var devices = Storage.Devices;
if (string.Equals(nextAddress, ConstraintMachineName, StringComparison.Ordinal))
{
ConstraintMachine? constraint = devices.OfType().FirstOrDefault(d => d.DeviceName == ConstraintMachineName);
if (constraint == null)
{
return;
}
ProcessDeviceRequest(
conveyorLine,
childDeviceCode,
getMaterialRequest: () => isUpper
? constraint.GetValue(ConstraintMachineDBName.MaterialRequestUpper)
: constraint.GetValue(ConstraintMachineDBName.MaterialRequestLower),
getOutputRequest: () => isUpper
? constraint.GetValue(ConstraintMachineDBName.OutputRequestUpper)
: constraint.GetValue(ConstraintMachineDBName.OutputRequestLower),
setOutputReady: outputReq =>
{
if (isUpper)
{
constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyUpper, outputReq ? 1 : 0);
}
else
{
constraint.SetValue(ConstraintMachineDBName.ConstraintTrayOutputReadyLower, outputReq ? 1 : 0);
}
});
}
else if (string.Equals(nextAddress, PinMachineName, StringComparison.Ordinal))
{
PinMachine? pinMachine = devices.OfType().FirstOrDefault(d => d.DeviceName == PinMachineName);
if (pinMachine == null)
{
return;
}
ProcessDeviceRequest(
conveyorLine,
childDeviceCode,
getMaterialRequest: () => isUpper
? pinMachine.GetValue(PinMachineDBName.MaterialRequestUpper)
: pinMachine.GetValue(PinMachineDBName.MaterialRequestLower),
getOutputRequest: () => isUpper
? pinMachine.GetValue(PinMachineDBName.OutputRequestUpper)
: pinMachine.GetValue(PinMachineDBName.OutputRequestLower),
setOutputReady: outputReq =>
{
if (isUpper)
{
pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyUpper, outputReq ? 1 : 0);
}
else
{
pinMachine.SetValue(PinMachineDBName.PlugPinTrayOutputReadyLower, outputReq ? 1 : 0);
}
});
}
}
private static void ProcessDeviceRequest(
CommonConveyorLine conveyorLine,
string childDeviceCode,
Func getMaterialRequest,
Func getOutputRequest,
Action setOutputReady)
{
bool materialReq = getMaterialRequest();
bool outputReq = getOutputRequest();
if (materialReq)
{
conveyorLine.SetValue(ConveyorLineDBNameNew.Target, 1, childDeviceCode);
conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 1, childDeviceCode);
}
else
{
setOutputReady(outputReq);
}
}
}
}