using HH.WCS.Mobox3.KeKou.device;
|
using HH.WCS.Mobox3.KeKou.dispatch;
|
using HH.WCS.Mobox3.KeKou.util;
|
using HH.WCS.Mobox3.KeKou.wms;
|
using S7.Net;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.Sockets;
|
using System.Threading;
|
|
namespace HH.WCS.Mobox3.KeKou.process
|
{
|
/// <summary>
|
/// 设备信号处理,主要是tcp信号,我们做server被动接收信号来处理,根据项目定制的
|
/// </summary>
|
internal class DeviceProcess
|
{
|
internal static void Analysis(string data, string ip)
|
{
|
LogHelper.Info($"TCP TP--{ip} data--{data}");
|
if (data.Length >= 6)
|
{
|
//去掉消息头3F 00
|
if (data.Substring(0, 4) == "3f00")
|
{
|
data = data.Substring(4);
|
}
|
//Console.WriteLine($"{ip}-{data}");
|
var plc = Settings.deviceInfos.Where(a => a.address == ip && a.enable == 1).FirstOrDefault();
|
if (plc != null)
|
{
|
if (plc.deviceType == 1)
|
{
|
AnalysisBottleCapmolding(data, plc);
|
}
|
else if (plc.deviceType == 2)
|
{
|
AnalysisButton(data, plc);
|
}
|
}
|
else
|
{
|
Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
|
}
|
}
|
|
}
|
|
private static void AnalysisButton(string data, Settings.deviceInfo plc)
|
{
|
LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg={data}");
|
var db = new SqlHelper<object>().GetInstance();
|
if (data == "3a00020001057300020101b9")
|
{
|
if (plc.location.Length > 0)
|
{
|
var location = plc.location[0];
|
var locinfo = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
|
if (locinfo != null && locinfo.N_LOCK_STATE != 1 && locinfo.N_LOCK_STATE != 2)
|
{
|
LocationHelper.UnLockLoc(plc.location[0]);
|
}
|
PlcHelper.SendHex(plc.address, "2A00010002057300020101A9");
|
}
|
}
|
}
|
|
internal static void Traffic(string forkliftNo, string lockNo, int State)
|
{
|
if (State == 1023)
|
{
|
SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
|
SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
|
//PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
|
//PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
|
|
}
|
|
if (State == 1025)
|
{
|
SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
|
SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
|
//PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
|
//PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
|
}
|
}
|
|
|
private static string SendHexOnce(string ip, int port, string hex)
|
{
|
var res = string.Empty;
|
try
|
{
|
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
//client.Connect(ip, port);
|
IAsyncResult connectResult = client.BeginConnect(ip, port, null, null);
|
if (!connectResult.AsyncWaitHandle.WaitOne(2000))
|
{
|
client.Close();
|
return "";
|
}
|
client.ReceiveTimeout = 2000;
|
if (client.Connected)
|
{
|
client.Send(Hex2Bytes(hex));
|
byte[] buffer = new byte[1024];
|
|
var length = client.Receive(buffer, SocketFlags.None);
|
byte[] data = new byte[length];
|
Array.Copy(buffer, data, length);
|
res = BitConverter.ToString(data).Replace("-", "");
|
|
|
client.Disconnect(true);
|
client.Dispose();
|
}
|
client = null;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error(ex.Message, ex);
|
}
|
return res;
|
}
|
|
|
internal static byte[] Hex2Bytes(string hexString)
|
{
|
hexString = hexString.Replace(" ", "");
|
if ((hexString.Length % 2) != 0)
|
hexString += " ";
|
byte[] returnBytes = new byte[hexString.Length / 2];
|
for (int i = 0; i < returnBytes.Length; i++)
|
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
return returnBytes;
|
}
|
|
|
/// <summary>
|
/// 接收PLC注塑机信号
|
/// </summary>
|
/// <param name="data"></param>
|
/// <param name="plc"></param>
|
private static void AnalysisBottleCapmolding(string data, Settings.deviceInfo plc)
|
{
|
LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg={data}", "注塑机");
|
//下满框信号生成下满框任务 待取货完成解绑起点货位 生成送空任务
|
if (data.Substring(0, 2) == "11")
|
{
|
if (moldingFull(plc, plc.location[0]))
|
{
|
LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机A口生成取满任务", "注塑机");
|
}
|
}
|
if (data.Substring(2, 2) == "21")
|
{
|
if (moldingFull(plc, plc.location[1]))
|
{
|
LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机B口生成取满任务", "注塑机");
|
}
|
}
|
|
}
|
|
private static bool moldingFull(Settings.deviceInfo plc, string location)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
//通过站点编号查询工单
|
//通过工单获取物料编码、批号(前期富勒没上线批号自行编写)
|
var result = false;
|
|
|
//上空任务卸货完成之后,复位线体信号,并记录卸货完成时间,当卸货完成再五分钟之内的时候,不允许下满 避免造成下线信号异常导致的异常下满(舒哥杰作,酌情优化)
|
var canOff = true;
|
var cgInfo = db.Queryable<CGTTable>().Where(a => a.Bit == location).First();
|
if (cgInfo != null)
|
{
|
if (cgInfo.time.AddMinutes(5) > DateTime.Now)
|
{
|
canOff = false;
|
}
|
|
}
|
if (canOff)
|
{
|
if (location != "")
|
{
|
if (LocationHelper.CheckLocFree(location))
|
{
|
LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 查询工单", "瓶盖机");
|
var workOrder = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == plc.deviceName).First();
|
if (workOrder != null)
|
{
|
LogHelper.Info($"查到了工单{workOrder.S_WOB_CODE} 工单状态为: {workOrder.S_WOB_STATE} 即产即用={workOrder.S_USING_NOW} 连接区域={workOrder.S_LINKLINENO}", "瓶盖机");
|
if (workOrder.S_WOB_STATE.Trim() == "执行中")
|
{
|
LogHelper.Info($"查询当前货位 托盘数量 货位编码={location}");
|
var trayInfo = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == location).ToList();
|
if (trayInfo.Count() > 0)
|
{
|
LogHelper.Info($"当前货位托盘数量大于0");
|
var trayNo = trayInfo[0].S_CNTR_CODE;
|
ProduceTask(plc, location, db, workOrder, trayNo);
|
}
|
else
|
{
|
LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 该站点没有托盘无法取满", "瓶盖机");
|
}
|
}
|
}
|
else
|
{
|
LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 未找到工单", "瓶盖机");
|
}
|
}
|
else
|
{
|
LogHelper.Info($"瓶盖机:{plc.deviceName} 当前位置{location}有任务,不可触发满托下线", "瓶盖机");
|
}
|
}
|
}
|
|
|
return result;
|
}
|
|
public static void FeiLiaoTask(string location, SqlSugar.SqlSugarClient db, string trayNo)
|
{
|
Location endLoc = null;
|
Location startLoc = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
|
var endArea = "Flq";
|
|
LogHelper.Info($"库区:{endArea} ");
|
endLoc = db.Queryable<Location>()
|
.Where(a =>
|
a.S_AREA_CODE == endArea &&
|
a.N_CURRENT_NUM == 0 &&
|
a.N_LOCK_STATE == 0)
|
.First();
|
if (endLoc != null && startLoc != null)
|
{
|
//创建任务
|
var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
|
}
|
else
|
{
|
LogHelper.Info($"库区:{endArea}没有找到可用库位");
|
}
|
}
|
|
internal static void TaskState(WCSTask mst, int state)
|
{
|
//if (mst.S_OP_NAME == "注塑机下线" && state == 6)
|
//{
|
// LogHelper.Info($"车辆回报完后调用空框上线");
|
// KKoutTask(mst);
|
//}
|
if (state == 6)
|
{
|
var deviceInfo = Settings.deviceInfos.Where(a => a.location.Contains(mst.S_END_LOC) && a.deviceType == 1).FirstOrDefault();
|
if (deviceInfo != null)
|
{
|
//发送复位信号
|
if (deviceInfo.location[0] == mst.S_END_LOC)
|
{
|
//A口
|
PlcHelper.SendHex(deviceInfo.address, "3F00120D0A");
|
}
|
else
|
{
|
//B口
|
PlcHelper.SendHex(deviceInfo.address, "3F00220D0A");
|
}
|
}
|
}
|
}
|
|
private static void KKoutTask(WCSTask mst)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
try
|
{
|
//卸货完成生成上空任务
|
var deviceInfo = Settings.deviceInfos.Where(a => a.location.Contains(mst.S_START_LOC)).FirstOrDefault();
|
if (deviceInfo != null)
|
{
|
//查找对应产线的工单是否执行
|
var workInfo = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == deviceInfo.deviceName && a.S_WOB_STATE == "执行中").First();
|
if (workInfo != null)
|
{
|
//查找产线对应的空框库区配置
|
var prodectArea = Settings.connectAreas.Where(a => a.deviceName == deviceInfo.deviceName && a.locAttribute == "空").FirstOrDefault();
|
if (prodectArea != null)
|
{
|
//查询库区是否有空框使用
|
Location startLoc = null;
|
string trayNo = "";
|
for (int i = 0; i < prodectArea.connectAreaList.Length; i++)
|
{
|
LogHelper.Info($"伪长度{i}");
|
var startArea = prodectArea.connectAreaList[i];
|
startLoc = db.Queryable<Location>().Where(a => a.S_AREA_CODE == startArea && a.N_CURRENT_NUM > 0 && a.N_LOCK_STATE == 0).Includes(a => a.LocCntrRel).First();
|
if (startLoc != null)
|
{
|
LogHelper.Info($"S_CODE{startLoc.S_CODE}");
|
if (startLoc.LocCntrRel != null)
|
{
|
trayNo = startLoc.LocCntrRel.S_CNTR_CODE;
|
break;
|
}
|
}
|
LogHelper.Info($"trayNo{trayNo}");
|
|
}
|
LogHelper.Info($"进来了");
|
if (startLoc != null && !string.IsNullOrEmpty(trayNo))
|
{
|
var endLoc = db.Queryable<Location>().Where(a => a.S_CODE == mst.S_START_LOC).First();
|
WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机上线", 2);
|
}
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"注塑上线异常 任务号{mst.S_CODE} 异常信息{ex.Message}", ex);
|
}
|
}
|
|
private static void ProduceTask(Settings.deviceInfo plc, string location, SqlSugar.SqlSugarClient db, WorkOrder workOrder, string trayNo)
|
{
|
var startLoc = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
|
Location endLoc = null;
|
try
|
{
|
var endLocList = db.Queryable<KeKouSettings>().Where(a => a.deviceName == plc.deviceName).ToList();
|
if (endLocList.Count > 0)
|
{
|
foreach (var item in endLocList)
|
{
|
endLoc = db.Queryable<Location>()
|
.Where(a => a.S_CODE == item.location &&
|
a.N_CURRENT_NUM == 0 &&
|
a.N_LOCK_STATE == 0)
|
.First();
|
if (endLoc != null)
|
{
|
break;
|
}
|
}
|
|
if (startLoc != null && endLoc != null)
|
{
|
//创建任务
|
var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
|
if (res)
|
{
|
//将托盘绑定物料
|
var itemInfo = db.Queryable<CntrItemRel>()
|
.Where(a =>
|
a.S_ITEM_CODE == workOrder.S_ITEM_CODE &&
|
a.S_BP_CODE == workOrder.S_BP_CODE &&
|
a.S_BATCH_NO == workOrder.S_BATCH_NO &&
|
a.S_CNTR_CODE == trayNo)
|
.First();
|
if (itemInfo == null)
|
{
|
//新增
|
itemInfo = new CntrItemRel { S_ITEM_CODE = workOrder.S_ITEM_CODE, S_BP_CODE = workOrder.S_BP_CODE, S_CNTR_CODE = trayNo, S_BATCH_NO = workOrder.S_BATCH_NO };
|
db.Insertable(itemInfo).ExecuteCommand();
|
}
|
}
|
}
|
else
|
{
|
LogHelper.Info($"生成任务失败,没有找到可用库位");
|
}
|
}
|
//var connectArea = Settings.connectAreas.Where(a => a.deviceName == plc.deviceName && a.locAttribute == "满").FirstOrDefault();
|
//if (connectArea != null)
|
//{
|
// for (int i = 0; i < connectArea.connectAreaList.Length; i++)
|
// {
|
// var endArea = connectArea.connectAreaList[i];
|
//
|
// LogHelper.Info($"库区:{endArea} ");
|
// endLoc = db.Queryable<Location>()
|
// .Where(a =>
|
// a.S_AREA_CODE == endArea &&
|
// a.N_CURRENT_NUM == 0 &&
|
// a.N_LOCK_STATE == 0)
|
// .First();
|
// if (endLoc != null)
|
// {
|
// break;
|
// }
|
// else
|
// {
|
// LogHelper.Info($"库区:{endArea}没有找到可用库位");
|
// }
|
// }
|
//
|
// if (startLoc != null && endLoc != null)
|
// {
|
// //创建任务
|
// var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
|
// if (res)
|
// {
|
// //将托盘绑定物料
|
// var itemInfo = db.Queryable<CntrItemRel>()
|
// .Where(a =>
|
// a.S_ITEM_CODE == workOrder.S_ITEM_CODE &&
|
// a.S_BP_CODE == workOrder.S_BP_CODE &&
|
// a.S_BATCH_NO==workOrder.S_BATCH_NO &&
|
// a.S_CNTR_CODE == trayNo)
|
// .First();
|
// if (itemInfo == null)
|
// {
|
// //新增
|
// itemInfo = new CntrItemRel { S_ITEM_CODE = workOrder.S_ITEM_CODE, S_BP_CODE = workOrder.S_BP_CODE, S_CNTR_CODE = trayNo ,S_BATCH_NO=workOrder.S_BATCH_NO};
|
// db.Insertable(itemInfo).ExecuteCommand();
|
// }
|
// }
|
// }else
|
// LogHelper.Info($"生成任务失败,没有找到可用库位");
|
//}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"注塑机下线算取满框缓存位异常 异常信息{ex.Message}", ex);
|
}
|
}
|
}
|
}
|