using System.Text;
using System.Text.RegularExpressions;
using WcsMain.Common;
using WcsMain.EquipOperation.Entity;
using WcsMain.WcsAttribute.AutoFacAttribute;
namespace WcsMain.EquipOperation.Convey;
///
/// 输送机操作 ---- 箱式线,不包含于立库库前设备,库前请参考堆垛机操作
///
[Component]
public class ConveyOperation
{
///
/// 写入输送机心跳
///
///
public string WriteHeartBeat()
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return "PLC尚未连接。";
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"箱式线写心跳", (short)0);
return writeResult.Success ? string.Empty : writeResult.Message ?? "写入失败,未知原因";
}
///
/// 箱式线允许取货
///
/// 要取货的点位
///
public bool AllGetVehicle(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return false;
var readResult = CommonTool.Siemens.ReadInt16WithName($"箱式线允许取货{point}");
return readResult.Success && readResult.Value == 1;
}
///
/// 箱式线允许卸货
///
/// 要取货的点位
///
public bool AllSetVehicle(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return false;
var readResult = CommonTool.Siemens.ReadInt16WithName($"箱式线允许卸货{point}");
return readResult.Success && readResult.Value == 1;
}
///
/// 读取箱式线入库站台反馈的载具号
///
///
///
public List? ReadConveyCode(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return default;
var readResult1 = CommonTool.Siemens.ReadStringWithName($"箱式线入库载具号{point}-1", 20, Encoding.ASCII);
var readResult2 = CommonTool.Siemens.ReadStringWithName($"箱式线入库载具号{point}-2", 20, Encoding.ASCII);
if (!readResult1.Success! || !readResult2.Success!) return default;
// 返回读取到的任务号
return [Regex.Replace(readResult1.Value ?? "", "\\W", ""), Regex.Replace(readResult2.Value ?? "", "\\W", "")];
}
///
/// 读取扫码点信息
///
///
///
public (string? errText, short scanOk, string code) ReadScanInfo(string? scanId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return ("PLC尚未连接。", 0, "");
var readResult = CommonTool.Siemens.ReadByteWithName($"扫码读取{scanId}", 22);
if (!readResult.Success || readResult.Value == default) return (readResult.Message, 0, "");// 读取失败
try
{
var readData = readResult.Value;
short status = Convert.ToInt16(CommonTool.Siemens.Trans(readData, 0)); // PLC 返回任务状态
string code = Regex.Replace(Encoding.ASCII.GetString(readData, 2, 20), "\\W", "");
return (string.Empty, status, code);
}
catch (Exception ex)
{
return (ex.Message, 0, "");
}
}
///
/// 清除扫码状态
///
///
///
public string ClearScanStatus(string? scanId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return "PLC尚未连接。";
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"扫码读取{scanId}", (short)0);
if (!writeResult.Success) return writeResult.Message ?? "写入失败";
return string.Empty;
}
///
/// 写入箱式线任务
///
///
///
///
public string WriteTask(string? equipmentId, ConveyPLCTask task)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return "PLC尚未连接。";
}
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"箱式线写任务{equipmentId}",
task.PlcId,
task.Router);
if (writeResult.Success)
{
return string.Empty;
}
return writeResult.Message ?? "写入失败,未知原因";
}
///
/// 获取箱式线出库站台状态
///
///
///
public bool GetOutConveyStatus(string? standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return false;
}
var readGoodsExistResult = CommonTool.Siemens.ReadBoolWithName($"站台货物检测{standId}");
if (!readGoodsExistResult.Success || readGoodsExistResult.Value) // 读取失败,或者有货 就不允许出库
{
return false;
}
var readAGVExistResult = CommonTool.Siemens.ReadBoolWithName($"站台AGV检测{standId}");
if (!readAGVExistResult.Success || readAGVExistResult.Value) // 读取失败,或者有AGV 就不允许出库
{
return false;
}
if (!readGoodsExistResult.Value && !readAGVExistResult.Value) // 无货并且没有AGV才能出库
{
return true;
}
return false;
}
// 10.40.200.210
///
/// 获取入库站台状态 --- 是否可以入库
///
///
///
public bool GetInConveyStatus(string? standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return false;
}
if (string.IsNullOrEmpty(standId)) { return false; }
if (standId.StartsWith('P'))
{
var readGoodsNeedIn = CommonTool.Siemens.ReadBoolWithName($"站台请求入库{standId}");
if (!readGoodsNeedIn.Success || !readGoodsNeedIn.Value) // 读取失败,或者没有货 就不允许入库
{
return false;
}
return true;
}
var readGoodsExistResult = CommonTool.Siemens.ReadBoolWithName($"站台货物检测{standId}");
if (!readGoodsExistResult.Success || !readGoodsExistResult.Value) // 读取失败,或者没有货 就不允许入库
{
return false;
}
var readAGVExistResult = CommonTool.Siemens.ReadBoolWithName($"站台AGV检测{standId}");
if (!readAGVExistResult.Success || readAGVExistResult.Value) // 读取失败,或者有AGV 就不允许入库
{
return false;
}
if (readGoodsExistResult.Value && !readAGVExistResult.Value) // 有货并且没有AGV才能入库
{
return true;
}
return false;
}
///
/// 获取箱式线拣选站台状态
///
///
///
public bool GetPickConveyStatus(string? standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return false;
}
var readGoodsExistResult = CommonTool.Siemens.ReadBoolWithName($"站台货物检测{standId}");
if (!readGoodsExistResult.Success) // 读取失败,或者有货 就不允许出库
{
return false;
}
return !readGoodsExistResult.Value;
}
///
/// 获取输送机箱子号
///
///
///
public string GetStandCode(int stackerId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return string.Empty;
}
var readResult1 = CommonTool.Siemens.ReadStringWithName($"箱式线入库载具号{stackerId}", 10, Encoding.ASCII);
if (!readResult1.Success)
{
// 读取失败
return string.Empty;
}
// 返回读取到的任务号
return Regex.Replace(readResult1.Value ?? "", "\\W", "");
}
}