|
using HPSocketCS;
|
using iWareCc.Cache.Entity;
|
using iWareCc.Conveyor.Datagram;
|
using iWareCc.Conveyor.Service;
|
using iWareCc.Conveyor.Socket;
|
using iWareCcTest.Properties;
|
using iWareCc.Util;
|
using iWareCommon.Utils;
|
using iWareDataCore.BASE.Entity;
|
using iWareDataCore.ORM;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace iWareCc.Conveyor.Entity
|
{
|
public class GateEntity
|
{
|
/// <summary>
|
/// 库位
|
/// </summary>
|
public PlaceViewEntity Place { get; private set; }
|
|
|
|
#region Client
|
/// <summary>
|
/// 用于收发报文的Socket服务(客户端)
|
/// </summary>
|
public SocketClient Socket { get; set; }
|
|
/// <summary>
|
/// Client内存日志
|
/// </summary>
|
public List<string> ClientReceiveLogger { get; set; }
|
|
/// <summary>
|
/// Client内存发送日志
|
/// </summary>
|
public List<string> ClientSendLogger { get; set; }
|
#endregion
|
|
/// <summary>
|
/// 用于发送报文的Socket服务(服务端)
|
/// </summary>
|
public SocketServer SocketServer { get; set; }
|
|
/// <summary>
|
/// Server接收端内存接收日志
|
/// </summary>
|
public List<string> ServerReceiveLogger { get; set; }
|
|
/// <summary>
|
/// Server接收端内存发送日志
|
/// </summary>
|
public List<string> ServerSendLogger { get; set; }
|
|
/// <summary>
|
/// 缓存接收报文的字符串
|
/// </summary>
|
public string DatagramReceivePool { get; set; }
|
|
/// <summary>
|
/// 缓存接收报文的字符串
|
/// </summary>
|
public string DatagramReceiveAlermPool { get; set; }
|
|
/// <summary>
|
/// 缓存接收报文的字符串
|
/// </summary>
|
public string DatagramReceiveSCPool { get; set; }
|
|
/// <summary>
|
/// 缓存发送报文的字符串
|
/// </summary>
|
public string DatagramSendPool { get; set; }
|
|
/// <summary>
|
/// 发送标志
|
/// </summary>
|
public bool SendFlag { get; set; }
|
|
|
|
/// <summary>
|
/// 未捕获心跳次数
|
/// </summary>
|
public int DisconnectTimes = 5;
|
|
/// <summary>
|
/// 上传获得的心跳数
|
/// </summary>
|
public string LastHandShakeCount = "0";
|
|
/// <summary>
|
/// 报警
|
/// </summary>
|
public string AlarmCode = "0";
|
|
/// <summary>
|
/// 是否在线
|
/// </summary>
|
public bool IsOnline { get; set; }
|
|
|
/// <summary>
|
/// 任务号
|
/// </summary>
|
public string TaskId { get; set; }
|
|
/// <summary>
|
/// 取消
|
/// </summary>
|
public bool Canceled { get; set; }
|
|
|
/// <summary>
|
/// 到位
|
/// </summary>
|
public bool ROccupied { get; set; }
|
|
/// <summary>
|
/// 是否为空
|
/// </summary>
|
public bool RIsEmpty { get; set; }
|
|
/// <summary>
|
/// 扫描到的物料号
|
/// </summary>
|
public string RCheckMaterialCode { get; set; }
|
|
/// <summary>
|
/// 放货完成(开始任务)
|
/// </summary>
|
public bool WPutFinishSymbol { get; set; }
|
|
/// <summary>
|
/// 取货完成(开始任务)
|
/// </summary>
|
public bool WPickFinishSymbol { get; set; }
|
|
/// <summary>
|
/// 滚动开始
|
/// </summary>
|
public bool WStartRotation { get; set; }
|
|
/// <summary>
|
/// 是否入库
|
/// </summary>
|
public bool WIsInStorage { get; set; }
|
|
/// <summary>
|
/// 错误数量
|
/// </summary>
|
public int ErrorCount { get; set; }
|
|
/// <summary>
|
/// WCS需要写入的OPC地址
|
/// </summary>
|
public OpcReadItem OpcReadItems { get; set; }
|
|
/// <summary>
|
/// WCS需要读取的OPC地址
|
/// </summary>
|
public OpcWriteItem OpcWriteItems { get; set; }
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="conveyor">调用该货口的输送机</param>
|
public GateEntity(PlaceViewEntity place)
|
{
|
this.Place = place;
|
this.DatagramReceivePool = "";
|
this.DatagramSendPool = "";
|
this.DatagramReceiveAlermPool = "";
|
this.DatagramReceiveSCPool = "";
|
this.SendFlag = false;
|
this.ErrorCount = 0;
|
//Client
|
//this.ClientReceiveLogger = new List<string>();
|
//this.ClientSendLogger = new List<string>();
|
//this.Socket = new GateEntitySocket(this, new TcpClient(), place.Ip, place.Point);
|
//Server
|
this.ServerReceiveLogger = new List<string>();
|
this.ServerSendLogger = new List<string>();
|
this.SocketServer = new GateSocketServer( this,new TcpServer(),place.Ip, place.Point);
|
switch (place.PlaceTypeName)
|
{
|
//入库输送机(Conveyor1)
|
//起始点
|
case "gate11":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0",//滚动开始
|
WIsInStorage= "S7:[S7_Connection_1]DB600,X2366.0"//是否入库
|
} : new OpcWriteItem
|
{
|
WStartRotation = "S7:[@LOCALSERVER]DB1,X682.2",//滚动开始
|
WIsInStorage= "S7:[S7_Connection_1]DB600,X2366.0"//是否入库
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
|
RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
|
|
} : new OpcReadItem
|
{
|
ROccupied = "S7:[@LOCALSERVER]DB1,X688.0",//允许开始
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
|
};
|
break;
|
//目标点
|
case "gate12":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
|
} : new OpcWriteItem
|
{
|
|
WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
} : new OpcReadItem
|
{
|
|
ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
|
};
|
break;
|
case "gate13":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
|
} : new OpcWriteItem
|
{
|
|
WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
} : new OpcReadItem
|
{
|
|
ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1",//是否为空端口
|
|
};
|
break;
|
case "gate14":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
|
} : new OpcWriteItem
|
{
|
|
WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
} : new OpcReadItem
|
{
|
|
ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
|
};
|
break;
|
case "gate15":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WPickFinishSymbol = "S7:[S7_Connection_1]DB601,X516.4"//取货完成
|
} : new OpcWriteItem
|
{
|
|
WPickFinishSymbol = "S7:[@LOCALSERVER]DB1,X952.1"//取货完成
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
} : new OpcReadItem
|
{
|
|
ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
|
RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
|
};
|
break;
|
|
//出库输送机conveyor2
|
//起始点
|
case "gate21":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
|
} : new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
|
RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
|
} : new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
|
RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
|
};
|
break;
|
//目标点
|
case "gate22":
|
OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
|
{
|
WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
|
} : new OpcWriteItem
|
{
|
|
WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
|
};
|
OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
|
{
|
ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
|
RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2",//是否为空端口
|
} : new OpcReadItem
|
{
|
ROccupied = "S7:[@LOCALSERVER]DB1,X3018.0",//是否到位
|
RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2",//是否为空端口
|
};
|
break;
|
|
|
}
|
}
|
|
|
/// <summary>
|
/// 发送任务
|
/// </summary>
|
/// <returns>是否发送成功</returns>
|
public bool SendOk()
|
{
|
try
|
{
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.WStartRotation, true);
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送自动开始标志={0},结果={1}", true, flag);
|
return flag;
|
|
}
|
catch (Exception)
|
{
|
return false;
|
}
|
|
}
|
|
/// <summary>
|
/// 发送任务
|
/// </summary>
|
/// <returns>是否发送成功</returns>
|
public bool SendProcessCard(string processCardNumber, int roadwayNo, int taskType)
|
{
|
try
|
{
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { }, new object[] { roadwayNo, taskType, processCardNumber });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送流转卡--> processCardNumber={0},roadwayNo={1},taskType={2},结果={3}", processCardNumber, roadwayNo, taskType, flag);
|
return flag;
|
|
}
|
catch (Exception)
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 发送任务号
|
/// </summary>
|
/// <param name="taskid">任务号</param>
|
/// <returns>是否发送成功</returns>
|
public bool SendTaskId(string taskid)
|
{
|
try
|
{
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WTaskId }, new object[] { taskid });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送任务号--> taskId={0},结果={1}", taskid, flag);
|
return flag;
|
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
|
|
/// <summary>
|
/// 发送取货完成
|
/// </summary>
|
/// <returns>是否发送成功</returns>
|
public bool SendPickSymbol()
|
{
|
try
|
{
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WPutFinishSymbol }, new object[] { true });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送取货完成={0},结果={1}", true, flag);
|
return flag;
|
|
}
|
catch (Exception)
|
{
|
return false;
|
}
|
|
}
|
|
|
/// <summary>
|
/// 重置流转卡
|
/// </summary>
|
/// <param name="processCardNumber">流转卡号</param>
|
/// <returns>是否重置成功</returns>
|
public bool ResetProcessCard(string processCardNumber)
|
{
|
try
|
{
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] {}, new object[] { processCardNumber });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "重置流转卡--> processCardNumber={0},结果={1}", processCardNumber, flag);
|
return flag;
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
|
/// <summary>
|
/// 发送手动开始
|
/// </summary>
|
/// <returns>是否发送成功</returns>
|
public bool SendStartSymbolManual(out string msg)
|
{
|
using (var dbModel = new DbModelCore())
|
{
|
try
|
{
|
msg = "";
|
if (!this.ROccupied)
|
{
|
msg = string.Format("端口{0}上没有检测到物料", this.Place.PlaceTypeName);
|
return false;
|
|
}
|
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WStartRotation }, new object[] { true });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送手动放货完成={0},结果={1}", true, flag);
|
|
if (flag)
|
{
|
var pvc = dbModel.BASEPlaceMaterialViews.FirstOrDefault(x => x.placetypename == this.Place.PlaceTypeName);
|
if (pvc != null)
|
{
|
dbModel.BASEPlaceMaterialViews.Remove(pvc);
|
dbModel.SaveChanges();
|
}
|
}
|
|
return flag;
|
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
return false;
|
}
|
|
|
}
|
|
|
}
|
|
/// <summary>
|
/// 发送开始
|
/// </summary>
|
/// <returns>是否发送成功</returns>
|
public bool SendStartSymbol(out string msg)
|
{
|
using (var dbModel = new DbModelCore())
|
{
|
try
|
{
|
msg = "";
|
if (!this.ROccupied)
|
{
|
msg = string.Format("端口{0}上没有检测到物料", this.Place.PlaceTypeName);
|
return false;
|
|
}
|
|
var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WPutFinishSymbol }, new object[] { true });
|
LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送放货完成={0},结果={1}", true, flag);
|
|
if (flag)
|
{
|
var pvc = dbModel.BASEPlaceMaterialViews.FirstOrDefault(x => x.placetypename == this.Place.PlaceTypeName );
|
if (pvc != null)
|
{
|
dbModel.BASEPlaceMaterialViews.Remove(pvc);
|
dbModel.SaveChanges();
|
}
|
}
|
|
return flag;
|
|
}
|
catch (Exception ex)
|
{
|
msg = ex.Message;
|
return false;
|
}
|
}
|
}
|
|
#region 使用Socket通讯发送信息
|
/// <summary>
|
/// 发送入库滚动的任务
|
/// </summary>
|
/// <param name="taskid">任务号</param>
|
/// <returns>是否发送成功</returns>
|
public bool SendGateTask()
|
{
|
try
|
{
|
if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
|
{
|
Name = this.Place.PlaceTypeName,
|
Roll = "T",
|
InOut = "T"
|
}).ParseByteDatagram()))
|
{
|
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
|
return false;
|
}
|
return true;
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
/// <summary>
|
/// 发送物料宽度
|
/// </summary>
|
/// <returns></returns>
|
public bool SendMaterialWindTask(string materialwind,string thick)
|
{
|
try
|
{
|
int wind = int.Parse(materialwind);
|
string mwind = "";
|
if (wind < 1000 && wind >= 100)
|
{
|
mwind = wind.ToString();
|
}
|
else if (wind < 100 && wind >= 10)
|
{
|
mwind = "0" + wind.ToString();
|
}
|
else if (wind < 10 && wind > 0)
|
{
|
mwind = "00" + wind.ToString();
|
}
|
else
|
{
|
mwind = "999";
|
}
|
int thic = int.Parse(thick);
|
string wthik = "";
|
if (thic < 10)
|
{
|
wthik = "0" + thic.ToString();
|
}
|
else if (thic >= 10 && thic < 100)
|
{
|
wthik = thic.ToString();
|
}
|
else
|
{
|
wthik = "99";
|
}
|
|
|
if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
|
{
|
Name = this.Place.PlaceTypeName,
|
Wind= mwind,
|
Thick= wthik
|
}).ParseByteDatagram()))
|
{
|
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
|
return false;
|
}
|
return true;
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
/// <summary>
|
/// 发送报警信息
|
/// </summary>
|
/// <param name="taskid">任务号</param>
|
/// <returns>是否发送成功</returns>
|
public bool SendGateAlarmTask(int msg)
|
{
|
try
|
{
|
if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
|
{
|
Name = this.Place.PlaceTypeName,
|
Alarmcode =("0"+(msg+1).ToString())
|
}).ParseByteDatagram()))
|
{
|
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
|
return false;
|
}
|
return true;
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
/// <summary>
|
/// 发送不入库直接滚动的任务
|
/// </summary>
|
/// <returns></returns>
|
public bool SendNotIngGateTask()
|
{
|
try
|
{
|
if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
|
{
|
Name = this.Place.PlaceTypeName,
|
Roll = "F",
|
InOut = "F"
|
}).ParseByteDatagram()))
|
{
|
LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
|
return false;
|
}
|
return true;
|
}
|
catch (Exception)
|
{
|
return false;
|
|
}
|
}
|
#endregion
|
|
}
|
}
|