using Elastic.Clients.Elasticsearch.Tasks;
using Furion.Logging;
using Microsoft.AspNetCore.SignalR;
using RazorEngine.Compilation.ImpromptuInterface.Dynamic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WCS.Application;
public static class PLCCommon
{
private static readonly ISqlSugarClient _db = SqlSugarSetup.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
///
/// 获取任务相关信息
///
/// 目标位置
/// 1:出库任务 2:移库任务
/// 起始巷道
/// 目标巷道
///
public static PlcTaskInfo GetCTaskInfo(string endLocat,string taskType,string roadWay,string endRoadWay)
{
// 各层输送线跺机放货工位集合
var conveyList1 = new List() { "033", "039", "044", "051", "059" };
var conveyList2 = new List() { "205", "234" };
var conveyList3 = new List() { "440", "443" };
PlcTaskInfo taskInfo = new PlcTaskInfo();
taskInfo.EndCeng = "3";
taskInfo.EndLie = "65";
taskInfo.EndPai = int.Parse(roadWay.Substring(1,2)).ToString();
// 移库任务
if (taskType == "2")
{
if (roadWay == endRoadWay) // 同巷道移库,获取目标地址
{
taskInfo = GetEndPai(endRoadWay.Substring(1, 2),endLocat.Substring(2, 2), endLocat.Substring(0, 2), endLocat.Substring(4, 2), endLocat.Substring(6, 2));
}
else // 跨巷道移库,获取三楼出库口
{
taskInfo.Ip = "10.18.51.130";
switch (roadWay)
{
case "R01":
taskInfo.EndStation = "402";
break;
case "R02":
taskInfo.EndStation = "406";
break;
case "R03":
taskInfo.EndStation = "410";
break;
case "R04":
taskInfo.EndStation = "414";
break;
case "R05":
taskInfo.EndStation = "418";
break;
case "R06":
taskInfo.EndStation = "422";
break;
case "R07":
taskInfo.EndStation = "426";
break;
case "R08":
taskInfo.EndStation = "430";
break;
case "R09":
taskInfo.EndStation = "434";
break;
case "R10":
taskInfo.EndStation = "438";
break;
}
}
}
// 出库任务
if (taskType == "1")
{
if (conveyList1.Contains(endLocat))
{
taskInfo.Ip = "10.18.51.110";
taskInfo.EndCeng = "1";
#region 一层输送线
switch (roadWay)
{
case "R01":
taskInfo.EndStation = "147";
break;
case "R02":
taskInfo.EndStation = "139";
break;
case "R03":
taskInfo.EndStation = "129";
break;
case "R04":
taskInfo.EndStation = "121";
break;
case "R05":
taskInfo.EndStation = "111";
break;
case "R06":
taskInfo.EndStation = "103";
break;
case "R07":
taskInfo.EndStation = "093";
break;
case "R08":
taskInfo.EndStation = "85";
break;
case "R09":
taskInfo.EndStation = "75";
break;
case "R10":
taskInfo.EndStation = "67";
break;
}
#endregion
}
else if (conveyList2.Contains(endLocat))
{
#region 二层输送线
taskInfo.Ip = "10.18.51.120";
taskInfo.EndCeng = "2";
switch (roadWay)
{
case "R01":
taskInfo.EndStation = "254";
break;
case "R02":
taskInfo.EndStation = "262";
break;
case "R03":
taskInfo.EndStation = "272";
break;
case "R04":
taskInfo.EndStation = "280";
break;
case "R05":
taskInfo.EndStation = "290";
break;
case "R06":
taskInfo.EndStation = "294";
break;
case "R07":
taskInfo.EndStation = "309";
break;
case "R08":
taskInfo.EndStation = "317";
break;
case "R09":
taskInfo.EndStation = "327";
break;
case "R10":
taskInfo.EndStation = "331";
break;
}
#endregion
}
else if (conveyList3.Contains(endLocat))
{
#region 三层输送线
taskInfo.Ip = "10.18.51.130";
taskInfo.EndCeng = "3";
switch (roadWay)
{
case "R01":
taskInfo.EndStation = "402";
break;
case "R02":
taskInfo.EndStation = "406";
break;
case "R03":
taskInfo.EndStation = "410";
break;
case "R04":
taskInfo.EndStation = "414";
break;
case "R05":
taskInfo.EndStation = "418";
break;
case "R06":
taskInfo.EndStation = "422";
break;
case "R07":
taskInfo.EndStation = "426";
break;
case "R08":
taskInfo.EndStation = "430";
break;
case "R09":
taskInfo.EndStation = "434";
break;
case "R10":
taskInfo.EndStation = "438";
break;
}
#endregion
}
else
{
throw new Exception("出库口工位未查询到");
}
}
//根据巷道口和层数确认出库工位排
switch (roadWay)
{
case "R01":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R02":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R03":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R04":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R05":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R06":
if (taskInfo.EndCeng == "1" || taskInfo.EndCeng == "2" )
{
taskInfo.EndPai = "2";
}
if ( taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R07":
if (taskInfo.EndCeng == "1" )
{
taskInfo.EndPai = "2";
}
if ( taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R08":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R09":
if (taskInfo.EndCeng == "1")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "2" || taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
case "R10":
if (taskInfo.EndCeng == "1" || taskInfo.EndCeng == "2")
{
taskInfo.EndPai = "2";
}
if (taskInfo.EndCeng == "3")
{
taskInfo.EndPai = "3";
}
break;
}
return taskInfo;
}
///
/// 根据工位号获取对应的输送线Ip
///
///
///
public static PLCUtil GetPlcIp(string startStation)
{
startStation = startStation.PadLeft(3, '0');
string strIp = "";
var conveyList1 = new List() { "147", "145", "139", "137", "129", "127", "121", "119", "111", "109", "103", "101", "093", "091", "085", "083", "075", "073", "067", "065","033","039","044" ,"051","059"};
var conveyList2 = new List() { "205", "234", "252", "254", "260", "262", "270", "272", "278", "280", "288", "290", "294", "301", "307", "309", "315", "317", "325", "327", "331", "337" };
var conveyList3 = new List() { "401", "402", "405", "406", "409", "410", "413", "414", "417", "418", "421", "422", "425", "426", "429", "430", "433", "434", "437", "438" };
if (conveyList1.Contains(startStation))
{
strIp = "10.18.51.110";
}
else if (conveyList2.Contains(startStation))
{
strIp = "10.18.51.120";
}
else if (conveyList3.Contains(startStation))
{
strIp = "10.18.51.130";
}
else
{
throw Oops.Bah("工位未查询到");
}
var plcConveyorConn = PLCTaskAction.listPlcConn.First(m => m.PlcIP == strIp);
return plcConveyorConn;
}
///
/// 根据wms目标地址转换为Plc目标地址 03010301
///
/// 巷道02
/// 列01
/// 排03
/// 层
/// 深度01
///
public static PlcTaskInfo GetEndPai(string road,string lie, string pai,string ceng, string dept)
{
var roadNum = int.Parse(road);
var lieNum = int.Parse(lie);
var paiNum = int.Parse(pai);
var cengNum = int.Parse(ceng);
var deptNum = int.Parse(dept);
var paiVal = 0;
// 判断巷道奇偶数
//if (roadNum % 2 == 0)// 偶数
//{
// // 判断排奇偶数
// if (paiNum % 2 == 0)
// {
// if (deptNum == 1)
// {
// paiVal = 3;
// }
// else
// {
// paiVal = 4;
// }
// }
// else
// {
// if (deptNum == 1)
// {
// paiVal = 2;
// }
// else
// {
// paiVal = 1;
// }
// }
//}
//else // 奇数
//{
// 判断排奇偶数
if (paiNum % 2 == 0)
{
if (deptNum == 1)
{
paiVal = 3;
}
else
{
paiVal = 4;
}
}
else
{
if (deptNum == 1)
{
paiVal = 2;
}
else
{
paiVal = 1;
}
}
//}
PlcTaskInfo taskInfo = new PlcTaskInfo();
taskInfo.EndPai = paiVal.ToString();
taskInfo.EndCeng = cengNum.ToString();
taskInfo.EndLie = lieNum.ToString() ;
if (taskInfo.EndPai == "0" || taskInfo.EndCeng == "0" || taskInfo.EndLie == "0")
{
throw new Exception("排列层转换失败");
}
return taskInfo;
}
///
/// 根据巷道和层数确定巷道口入库工位
///
///
///
///
public static string RoadwayToStationNum(string endRoadway, string ceng)
{
string stationNum = "";
switch (endRoadway)
{
case "R01":
if (ceng == "1")
{
stationNum = "145";
}
if (ceng == "2")
{
stationNum = "252";
}
if (ceng == "3")
{
stationNum = "401";
}
break;
case "R02":
if (ceng == "1")
{
stationNum = "137";
}
if (ceng == "2")
{
stationNum = "260";
}
if (ceng == "3")
{
stationNum = "405";
}
break;
case "R03":
if (ceng == "1")
{
stationNum = "127";
}
if (ceng == "2")
{
stationNum = "270";
}
if (ceng == "3")
{
stationNum = "409";
}
break;
case "R04":
if (ceng == "1")
{
stationNum = "119";
}
if (ceng == "2")
{
stationNum = "278";
}
if (ceng == "3")
{
stationNum = "413";
}
break;
case "R05":
if (ceng == "1")
{
stationNum = "109";
}
if (ceng == "2")
{
stationNum = "288";
}
if (ceng == "3")
{
stationNum = "417";
}
break;
case "R06":
if (ceng == "1")
{
stationNum = "101";
}
if (ceng == "2")
{
stationNum = "301";
}
if (ceng == "3")
{
stationNum = "421";
}
break;
case "R07":
if (ceng == "1")
{
stationNum = "091";
}
if (ceng == "2")
{
stationNum = "307";
}
if (ceng == "3")
{
stationNum = "425";
}
break;
case "R08":
if (ceng == "1")
{
stationNum = "83";
}
if (ceng == "2")
{
stationNum = "315";
}
if (ceng == "3")
{
stationNum = "429";
}
break;
case "R09":
if (ceng == "1")
{
stationNum = "73";
}
if (ceng == "2")
{
stationNum = "325";
}
if (ceng == "3")
{
stationNum = "433";
}
break;
case "R10":
if (ceng == "1")
{
stationNum = "65";
}
if (ceng == "2")
{
stationNum = "337";
}
if (ceng == "3")
{
stationNum = "437";
}
break;
}
return stationNum;
}
///
/// 根据工位号获取对应巷道
///
/// 入出工位号
/// 跺机联机
public static string GetRoadwayByStation(string StationNum)
{
StationNum = StationNum.PadLeft(3, '0');
string value = "";
var conveyList1 = new List() { "147", "145", "252", "254", "401", "402", };
var conveyList2 = new List() { "139", "137", "260", "262", "405", "406", };
var conveyList3 = new List() { "129", "127", "272", "270", "409", "410", };
var conveyList4 = new List() { "121", "119", "278", "280", "413", "414", };
var conveyList5 = new List() { "111", "109", "288", "290", "417", "418", };
var conveyList6 = new List() { "103", "101", "294", "301", "421", "422", };
var conveyList7 = new List() { "093", "091", "307", "309", "425", "426", };
var conveyList8 = new List() { "085", "083", "315", "317", "429", "430", };
var conveyList9 = new List() { "075", "073", "325", "327", "433", "434", };
var conveyList10 = new List() { "067", "065", "331", "337", "437", "438", };
if (conveyList1.Contains(StationNum))
{
value = "R01";
}
else if (conveyList2.Contains(StationNum))
{
value = "R02";
}
else if (conveyList3.Contains(StationNum))
{
value = "R03";
}
else if (conveyList4.Contains(StationNum))
{
value = "R04";
}
else if (conveyList5.Contains(StationNum))
{
value = "R05";
}
else if (conveyList6.Contains(StationNum))
{
value = "R06";
}
else if (conveyList7.Contains(StationNum))
{
value = "R07";
}
else if (conveyList8.Contains(StationNum))
{
value = "R08";
}
else if (conveyList9.Contains(StationNum))
{
value = "R09";
}
else if (conveyList10.Contains(StationNum))
{
value = "R10";
}
return value;
}
///
/// 根据巷道口出入库工位号获取对应楼层
///
/// 入出工位号
/// 跺机联机
public static string GetRoadwayByStationNew(string StationNum)
{
string value = "";
//var conveyList1 = new List() { "147", "145", "139", "137", "129", "127", "121", "119", "111", "109", "103", "101", "093", "091", "085", "083", "075", "073", "067", "065" };
//var conveyList2 = new List() { "252", "254", "260", "262", "272", "270", "278", "280", "288", "290", "294", "301", "307", "309", "315", "317", "325", "327", "331", "337" };
//var conveyList3 = new List() { "401", "402", "405", "406", "409", "410", "413", "414", "417", "418", "421", "422", "425", "426", "429", "430", "433", "434", "437", "438" };
var ipInfo = _db.Queryable().Where(s => s.Type == PLCTypeEnum.ConveyorLine && s.IsDelete == false).ToList(); //s.Type == PLCTypeEnum.StackingMachine ||
var ip1 = ipInfo.First(m => m.Text == "1层托盘输送线");
var ip2 = ipInfo.First(m => m.Text == "2层托盘输送线");
var ip3 = ipInfo.First(m => m.Text == "3层托盘输送线");
if (ip1 != null)
{
var dev1 = PLCTaskAction.plcDevices.Where(m => m.IsDelete == false && m.PlcId == ip1.Id).Select(m => m.StationNum).ToList();
if (dev1.Contains(StationNum))
{
value = "1";
return value;
}
}
if (ip2 != null)
{
var dev2 = PLCTaskAction.plcDevices.Where(m => m.IsDelete == false && m.PlcId == ip2.Id).Select(m => m.StationNum).ToList();
if (dev2.Contains(StationNum))
{
value = "2";
return value;
}
}
if (ip3 != null)
{
var dev3 = PLCTaskAction.plcDevices.Where(m => m.IsDelete == false && m.PlcId == ip3.Id).Select(m => m.StationNum).ToList();
if (dev3.Contains(StationNum))
{
value = "3";
return value;
}
}
return "";
}
///
/// 根据巷道号、层数 获取对应的跺机IP及入库工位排列层信息
///
/// 巷道号
/// 连接集合
///
public static StokInfo GetStokePlc(string roadway, string ceng)
{
StokInfo sInfo = new StokInfo();
sInfo.Pai = "2";
switch (roadway)
{
case "R01":
sInfo.Ip = "10.18.51.10";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R02":
sInfo.Ip = "10.18.51.20";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R03":
sInfo.Ip = "10.18.51.30";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R04":
sInfo.Ip = "10.18.51.40";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R05":
sInfo.Ip = "10.18.51.50";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R06":
sInfo.Ip = "10.18.51.60";
if (ceng == "2" || ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R07":
sInfo.Ip = "10.18.51.70";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R08":
sInfo.Ip = "10.18.51.80";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R09":
sInfo.Ip = "10.18.51.90";
if (ceng == "1")
{
sInfo.Pai = "3";
}
break;
case "R10":
sInfo.Ip = "10.18.51.100";
if (ceng == "2" || ceng == "1")
{
sInfo.Pai = "3";
}
break;
}
sInfo.Lie = "65";
sInfo.Ceng = "1";
if (ceng == "2")
{
sInfo.Ceng = "2";
}
if (ceng == "3")
{
sInfo.Ceng = "3";
}
return sInfo;
}
///
/// 根据储位排获取跺机对应的地址排03010302
///
/// 排02
/// 排03
/// 深度02
///
public static int GetDjAdress(string road, string pai, string dept)
{
var roadNum = int.Parse(road);
var paiNum = int.Parse(pai);
var deptNum = int.Parse(dept);
var paiVal = 0;
//// 判断巷道奇偶数
//if (roadNum % 2 == 0)// 偶数
//{
// // 判断排奇偶数
// if (paiNum % 2 == 0)
// {
// if (deptNum == 1)
// {
// paiVal = 3;
// }
// else
// {
// paiVal = 4;
// }
// }
// else
// {
// if (deptNum == 1)
// {
// paiVal = 2;
// }
// else
// {
// paiVal = 1;
// }
// }
//}
//else // 奇数
//{
// 判断排奇偶数
if (paiNum % 2 == 0)
{
if (deptNum == 1)
{
paiVal = 3;
}
else
{
paiVal = 4;
}
}
else
{
if (deptNum == 1)
{
paiVal = 2;
}
else
{
paiVal = 1;
}
}
//}
if (paiVal == 0)
{
Log.Error("排转换失败!");
throw new Exception("排转换失败");
}
return paiVal;
}
}