using HPSocketCS; using iWareCc.Cache.Entity; using iWareCc.Conveyor.Datagram; using iWareCc.Conveyor.Service; using iWareCc.Conveyor.Socket; using iWareCc.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 { /// /// 库位 /// public PlaceViewEntity Place { get; private set; } #region Client /// /// 用于收发报文的Socket服务(客户端) /// public SocketClient Socket { get; set; } /// /// Client内存日志 /// public List ClientReceiveLogger { get; set; } /// /// Client内存发送日志 /// public List ClientSendLogger { get; set; } #endregion /// /// 用于发送报文的Socket服务(服务端) /// public SocketServer SocketServer { get; set; } /// /// Server接收端内存接收日志 /// public List ServerReceiveLogger { get; set; } /// /// Server接收端内存发送日志 /// public List ServerSendLogger { get; set; } /// /// 缓存接收报文的字符串 /// public string DatagramReceivePool { get; set; } /// /// 缓存接收报文的字符串 /// public string DatagramReceiveAlermPool { get; set; } /// /// 缓存接收报文的字符串 /// public string DatagramReceiveSCPool { get; set; } /// /// 缓存发送报文的字符串 /// public string DatagramSendPool { get; set; } /// /// 发送标志 /// public bool SendFlag { get; set; } /// /// 未捕获心跳次数 /// public int DisconnectTimes = 5; /// /// 上传获得的心跳数 /// public string LastHandShakeCount = "0"; /// /// 报警 /// public string AlarmCode = "0"; /// /// 是否在线 /// public bool IsOnline { get; set; } /// /// 任务号 /// public string TaskId { get; set; } /// /// 取消 /// public bool Canceled { get; set; } /// /// 到位 /// public bool ROccupied { get; set; } /// /// 是否为空 /// public bool RIsEmpty { get; set; } /// /// 扫描到的物料号 /// public string RCheckMaterialCode { get; set; } /// /// 放货完成(开始任务) /// public bool WPutFinishSymbol { get; set; } /// /// 取货完成(开始任务) /// public bool WPickFinishSymbol { get; set; } /// /// 滚动开始 /// public bool WStartRotation { get; set; } /// /// 是否入库 /// public bool WIsInStorage { get; set; } /// /// 错误数量 /// public int ErrorCount { get; set; } /// /// WCS需要写入的OPC地址 /// public OpcReadItem OpcReadItems { get; set; } /// /// WCS需要读取的OPC地址 /// public OpcWriteItem OpcWriteItems { get; set; } /// /// 构造函数 /// /// 调用该货口的输送机 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(); //this.ClientSendLogger = new List(); //this.Socket = new GateEntitySocket(this, new TcpClient(), place.Ip, place.Point); //Server this.ServerReceiveLogger = new List(); this.ServerSendLogger = new List(); 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; } } /// /// 发送任务 /// /// 是否发送成功 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; } } /// /// 发送任务 /// /// 是否发送成功 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; } } /// /// 发送任务号 /// /// 任务号 /// 是否发送成功 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; } } /// /// 发送取货完成 /// /// 是否发送成功 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; } } /// /// 重置流转卡 /// /// 流转卡号 /// 是否重置成功 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; } } /// /// 发送手动开始 /// /// 是否发送成功 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; } } } /// /// 发送开始 /// /// 是否发送成功 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通讯发送信息 /// /// 发送入库滚动的任务 /// /// 任务号 /// 是否发送成功 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; } } /// /// 发送物料宽度 /// /// 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; } } /// /// 发送报警信息 /// /// 任务号 /// 是否发送成功 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; } } /// /// 发送不入库直接滚动的任务 /// /// 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 } }