using Logs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WCS.Service.Entity;
namespace WCS.Service
{
public class WMS
{
private const string Url = "http://192.168.249.150:8026";
//private static string Url = "http://127.0.0.1:8026";
private const string WareHouseId = "opphouse";
///
/// 向WMS获取入库任务 一次单卷
///
/// 产品条码
/// 设备条码
///
///
///
public static List I_WCS_GetInTask(string barcode, string devCode, bool getTunnel = false)
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetInTask", new List()
{
new(){
ContainerBarCode = barcode,
WareHouseId = WareHouseId,
EquipmentNo = devCode,
Memo1 = getTunnel ? "1" : "" //1:分巷道 2:分货位
}
});
if (res.ResType) return res.TaskList;
TaskException(devCode, res.ResMessage);
throw new WarnException(res.ResMessage);
}
///
/// 向WMS获取入库任务 一次双卷
///
/// 产品1条码
/// 设备1编号
/// 产品1是否直接分配巷道
/// 产品2条码
/// 设备2编号
/// 产品2是否直接分配巷道
///
///
public static List I_WCS_GetInTask(string barcode1, string devCode1, string barcode2, string devCode2, bool getTunnel1 = false, bool getTunnel2 = false)
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetInTask", new List()
{
new(){
ContainerBarCode = barcode1,
WareHouseId = WareHouseId,
EquipmentNo = devCode1,
Memo1 = getTunnel1 ? "1" : "" //1:分巷道 2:分货位
},
new(){
ContainerBarCode = barcode2,
WareHouseId = WareHouseId,
EquipmentNo = devCode2,
Memo1 = getTunnel2 ? "1" : "" //1:分巷道 2:分货位
},
});
if (res.ResType) return res.TaskList;
TaskException($"G{devCode1}", res.ResMessage);
throw new WarnException(res.ResMessage);
}
///
/// 分配货位
///
/// WMS任务ID
/// 巷道
/// 设备号
/// 申请任务对应的货叉
///
///
public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device, WareCellForkNum ForkNum)
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetWareCell", new I_WCS_GetWareCellRequest
{
PickUpEquipmentNo = device,
TunnelNum = tunnel.Last().ToString(),
WMSTaskNum = wmstaskid.ToString(),
ForkNum = ForkNum
});
if (res.ResType) return res;
TaskException(device, res.ResMessage);
throw new WarnException(res.ResMessage);
}
public static void UpdateTask(string POSIDNEXT, int wmstaskid, int status, int size = -1)
{
var res = APICaller.CallApi>(Url + "/api/Task/I_WCS_PutTaskStep", new List
{
new()
{
TASK_WKSTATUS = status,
TASK_WMSNO = wmstaskid.ToString(),
TASK_POSIDNEXT=POSIDNEXT,
Size=size
}
});
if (res == null || res.Count == 0)
{
throw new WarnException("I_WCS_PutTaskStep调用失败");
}
if (!res.First().ResType)
throw new WarnException(res.First().ResMessage);
}
public static GetProductInfoResponse GetProductInfo(string barcode, string dev)
{
var res = APICaller.CallApi(Url + "/api/Task/GetProductInfo", new GetProductInfoRequest
{
BarCode = barcode
});
try
{
if (!res.ResType)
throw new WarnException(res.ResMessage);
if (res.ChildContainerType is <= 0 or > 9)
throw new WarnException("托盘类型错误");
if (res.LoadCount <= 0)
throw new WarnException("最大组盘数量错误");
if (res.DocId == 0)
throw new WarnException("单据ID错误");
if (res.ProLine is < 1 or > 9)
throw new WarnException("产线编号" + res.ProLine + "错误");
}
catch (Exception ex)
{
TaskException(dev, ex.Message);
throw;
}
return res;
}
///
/// 获取出库任务
///
/// 出库位置
/// 调用方法的设备号
///
///
public static List GetOutTask(string position, string devCode)
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest
{
OutType = 1,
WareHouseId = WareHouseId,
OutEndPostion = position
});
if (!res.ResType)
{
TaskException(position, res.ResMessage);
throw new WarnException(res.ResMessage);
}
res.WMSTasks.ForEach(v =>
{
var tcode = "TY" + v.TunnelNum;
var tunnel = Device.Find(tcode);
//var next = tunnel.GetPath(devCode);
//v.Memo1 = next.CODE;
v.TunnelNum = tunnel.CODE;
});
return res.WMSTasks;
}
public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
{
var res = APICaller.CallApi(Url + "/api/Task/AutoBuildUpGroupStock", new
{
ContainerCode = containerCode,
BarCode = barcode
});
if (res.ResType) return res;
TaskException(dev, res.ResMessage);
throw new WarnException(res.ResMessage);
}
///
/// 获取巷道
///
/// WMS任务号集合
/// 请求设备号
///
///
public static I_WCS_GetTunnelListResponse GetTunnelList(List WMSTaskId, string dev)
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetTunnelList", new I_WCS_GetTunnelListRequest
{
WMSTaskNum = WMSTaskId
});
if (res.ResType) return res;
TaskException(dev, res.ResMessage);
throw new WarnException(res.ResMessage);
}
public static void TaskException(string device, string exMsg)
{
if (exMsg == "接口调用中")
return;
Task.Run(() =>
{
try
{
Console.WriteLine(device + ":" + exMsg);
var res = APICaller.CallApi2(Url + "/api/Task/I_WCS_GetExcTask", new I_WCS_GetExcTaskRequest
{
ExcCode = exMsg,
EquipmentNo = device,
ExcMessage = exMsg,
});
InfoLog.INFO_I_WCS_GetExcTask(device + ":" + exMsg);
if (!res.ResType) throw new WarnException(res.ResMessage);
}
catch (Exception)
{
}
});
}
private static List DevInfoList = new();
public static void DevInfo(string device, string exMsg)
{
DevInfoList.Add(new I_WCS_PutDevInfoRequest
{
STA_EQUIPMENTNO = device,
STA_ALARMSMSG = exMsg
});
}
public static void UploadDevInfo()
{
return;
try
{
var res = APICaller.CallApi(Url + "/api/Task/I_WCS_PutDevInfo", DevInfoList);
if (!res.ResType)
throw new WarnException(res.ResMessage);
}
catch (Exception)
{
Console.WriteLine("I_WCS_PutDevInfo" + "接口调用失败");
}
finally
{
DevInfoList.Clear();
}
}
public static void UnBound(string barcode)
{
try
{
var res = APICaller.CallApi(Url + "/api/PDA/UnboundMwGroupStock", new
{
containerCode = barcode
});
}
catch { }
}
}
}