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 AllowGetVehicle(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return false;
var readResult = CommonTool.Siemens.ReadInt16WithName($"箱式线允许取货{point}");
return readResult.Success && readResult.Value == 1;
}
///
/// 箱式线允许卸货
///
/// 要取货的点位
///
public bool AllowSetVehicle(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return false;
var readResult = CommonTool.Siemens.ReadInt16WithName($"箱式线允许卸货{point}");
return readResult.Success && readResult.Value == 1;
}
///
/// 读取箱式线入库站台反馈的载具号
///
///
///
public List? ReadAreaConveyCode(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, string? code) ReadConveyCode(string? point)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return ("设备离线", default);
var readResult1 = CommonTool.Siemens.ReadStringWithName($"箱式线入库载具号{point}", 20, Encoding.ASCII);
if (!readResult1.Success!) return (readResult1.Message, default);
// 返回读取到的任务号
return (default, Regex .Replace(readResult1.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 返回任务状态
var str = Encoding.ASCII.GetString(readData, 2, 20);
string code = Regex.Replace(str, "\\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 ReadStandCode(string? standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return string.Empty;
var readResult = CommonTool.Siemens.ReadByteWithName($"站台条码读取{standId}", 22);
if (!readResult.Success || readResult.Value == default) return string.Empty;// 读取失败
try
{
var readData = readResult.Value;
short status = Convert.ToInt16(CommonTool.Siemens.Trans(readData, 0)); // PLC 返回任务状态
if (status == 0) return string.Empty;
string code = Regex.Replace(Encoding.ASCII.GetString(readData, 0, 20), "\\W", "");
return code;
}
catch (Exception ex)
{
_ = ex;
return string.Empty;
}
}
///
/// 清除站台扫码状态
///
///
///
public string ClearStandCodeStatus(string? standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return "PLC尚未连接。";
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"站台条码读取{standId}", (short)0);
if (!writeResult.Success) return writeResult.Message ?? "写入失败";
return string.Empty;
}
///
/// 写入箱式线任务
///
///
///
///
public string WriteTask(string? equipmentId, ConveyPLCTask task)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return "PLC尚未连接。";
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"箱式线写任务{equipmentId}",
task.PlcId,
task.Router);
return writeResult.Success ? string.Empty : writeResult.Message ?? "写入失败,未知原因";
}
///
/// 箱式线写任务
///
///
///
///
///
public string WriteTask(string? equipmentId, int plcId, short router) => WriteTask(equipmentId, new ConveyPLCTask(plcId, router));
///
/// 读取拣选站台释放的信号
///
///
///
public int GetAllowPickStandGo(string pickStandId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return 0;
var readResult = CommonTool.Siemens.ReadInt16WithName($"拣选确认按钮{pickStandId}");
if (!readResult.Success) return 0;// 读取失败
return readResult.Value;
}
///
/// 通知 plc 可以释放站台
///
///
///
///
public string AllowPickStandGo(string pickStandId, short router)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default) return "PLC尚未连接。";
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"拣选确认按钮{pickStandId}", router);
if (!writeResult.Success) return writeResult.Message ?? "写入失败"; // 读取失败
return string.Empty;
}
}