void threadStart(object unused) { while (!this.threadNeedQuit) { try { if (this.holdingDays > 0 && this.holdingDays < TimeSpan.MaxValue.TotalDays) { DateTime minTime = DateTime.Today.Subtract(TimeSpan.FromDays(this.holdingDays)); using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { //装配指引部分 List <ASM_AssembleIndication> asmAssembleIndications = dbContext.ASM_AssembleIndications .Where(ai => ai.CarArrivedTime < minTime && ai.AssembleStatus == AssembleStatus.Finished && ai.ASM_AssembleResults.All(ar => ar.ASM_AssembleResultMessage.SentSuccessful)) .ToList(); foreach (ASM_AssembleIndication asmAssembleIndication in asmAssembleIndications) { ASM_AssembleIndicationMessage asmAssembleIndicationMessage = asmAssembleIndication.ASM_AssembleIndicationMessage; List <ASM_AssembleIndicationItem> asmAssembleIndicationItems = asmAssembleIndication.ASM_AssembleIndicationItems.ToList(); List <ASM_Task> asmTasks = asmAssembleIndication.ASM_Tasks.ToList(); List <ASM_AssembleResult> asmAssembleResults = asmAssembleIndication.ASM_AssembleResults.ToList(); foreach (ASM_AssembleResult asmAssembleResult in asmAssembleResults) { ASM_AssembleResultMessage asmAssembleResultMessage = asmAssembleResult.ASM_AssembleResultMessage; List <ASM_AssembleResultItem> asmAssembleResultItems = asmAssembleResult.ASM_AssembleResultItems.ToList(); dbContext.ASM_AssembleResultItems.RemoveRange(asmAssembleResultItems); dbContext.ASM_AssembleResultMessages.Remove(asmAssembleResultMessage); dbContext.ASM_AssembleResults.Remove(asmAssembleResult); } foreach (ASM_Task asmTask in asmTasks) { List <ASM_TaskItem> asmTaskItems = asmTask.ASM_TaskItems.ToList(); dbContext.ASM_TaskItems.RemoveRange(asmTaskItems); dbContext.ASM_Tasks.Remove(asmTask); } dbContext.ASM_AssembleIndicationItems.RemoveRange(asmAssembleIndicationItems); dbContext.ASM_AssembleIndicationMessages.Remove(asmAssembleIndicationMessage); dbContext.ASM_AssembleIndications.Remove(asmAssembleIndication); } //分拣部分 List <AST_PalletArrived> astPalletArriveds = dbContext.AST_PalletArriveds .Where(pa => pa.ArrivedTime < minTime) .ToList(); foreach (AST_PalletArrived astPalletArrived in astPalletArriveds) { AST_PalletArrivedMessage astPalletArrivedMessage = astPalletArrived.AST_PalletArrivedMessage; dbContext.AST_PalletArrivedMessages.Remove(astPalletArrivedMessage); dbContext.AST_PalletArriveds.Remove(astPalletArrived); } List <AST_PalletTask> astPalletTasks = dbContext.AST_PalletTasks .Where(pt => pt.CreateTime < minTime && pt.PickStatus == PickStatus.Finished) .ToList(); foreach (AST_PalletTask astPalletTask in astPalletTasks) { List <AST_PalletTaskItem> astPalletTaskItems = astPalletTask.AST_PalletTaskItems.ToList(); foreach (AST_PalletTaskItem astPalletTaskItem in astPalletTaskItems) { List <AST_LesTaskItem> astLesTaskItems = astPalletTaskItem.AST_LesTaskItems.ToList(); List <AST_CartTaskItem> astCartTaskItems = astPalletTaskItem.AST_CartTaskItems.ToList(); List <AST_LesTask> astLesTasks = new List <AST_LesTask>(); List <AST_LesTaskMessage> astLesTaskMessages = new List <AST_LesTaskMessage>(); foreach (AST_LesTaskItem astLesTaskItem in astLesTaskItems) { AST_LesTask astLesTask = astLesTaskItem.AST_LesTask; AST_LesTaskMessage astLesTaskMessage = astLesTask.AST_LesTaskMessage; if (astLesTask != null && !astLesTasks.Contains(astLesTask)) { astLesTasks.Add(astLesTask); } if (astLesTaskMessage != null && !astLesTaskMessages.Contains(astLesTaskMessage)) { astLesTaskMessages.Add(astLesTaskMessage); } dbContext.AST_LesTaskItems.Remove(astLesTaskItem); } dbContext.AST_LesTaskMessages.RemoveRange(astLesTaskMessages); dbContext.AST_LesTasks.RemoveRange(astLesTasks); List <AST_CartTask> astCartTasks = new List <AST_CartTask>(); foreach (AST_CartTaskItem astCartTaskItem in astCartTaskItems) { AST_CartTask astCartTask = astCartTaskItem.AST_CartTask; if (astCartTask != null && !astCartTasks.Contains(astCartTask)) { astCartTasks.Add(astCartTask); } dbContext.AST_CartTaskItems.Remove(astCartTaskItem); } dbContext.AST_CartTasks.RemoveRange(astCartTasks); dbContext.AST_PalletTaskItems.Remove(astPalletTaskItem); } dbContext.AST_PalletTasks.Remove(astPalletTask); } List <AST_PalletResult> astPalletResults = dbContext.AST_PalletResults .Where(pr => pr.EndPickTime < minTime && pr.AST_PalletResultMessage.SentSuccessful) .ToList(); foreach (AST_PalletResult astPalletResult in astPalletResults) { AST_PalletResultMessage astPalletResultMessage = astPalletResult.AST_PalletResultMessage; List <AST_PalletResultItem> astPalletResultItems = astPalletResult.AST_PalletResultItems.ToList(); dbContext.AST_PalletResultItems.RemoveRange(astPalletResultItems); dbContext.AST_PalletResultMessages.Remove(astPalletResultMessage); dbContext.AST_PalletResults.Remove(astPalletResult); } List <AST_CartResult> astCartResults = dbContext.AST_CartResults .Where(cr => cr.EndPickTime < minTime && cr.AST_CartResultMessage.SentSuccessful) .ToList(); foreach (AST_CartResult astCartResult in astCartResults) { AST_CartResultMessage astCartResultMessage = astCartResult.AST_CartResultMessage; List <AST_CartResultItem> astCartResultItems = astCartResult.AST_CartResultItems.ToList(); dbContext.AST_CartResultItems.RemoveRange(astCartResultItems); dbContext.AST_CartResultMessages.Remove(astCartResultMessage); dbContext.AST_CartResults.Remove(astCartResult); } //手持机分拣部分 List <AST_PalletArrived_PDA> astPalletArrived_PDAs = dbContext.AST_PalletArrived_PDAs .Where(pa => pa.ArrivedTime < minTime) .ToList(); foreach (AST_PalletArrived_PDA astPalletArrived_PDA in astPalletArrived_PDAs) { AST_PalletArrivedMessage_PDA astPalletArrivedMessage_PDA = astPalletArrived_PDA.AST_PalletArrivedMessage_PDA; dbContext.AST_PalletArrivedMessage_PDAs.Remove(astPalletArrivedMessage_PDA); dbContext.AST_PalletArrived_PDAs.Remove(astPalletArrived_PDA); } List <AST_PalletTask_PDA> astPalletTask_PDAs = dbContext.AST_PalletTask_PDAs .Where(pt => pt.CreateTime < minTime && pt.PickStatus == PickStatus.Finished) .ToList(); foreach (AST_PalletTask_PDA astPalletTask_PDA in astPalletTask_PDAs) { List <AST_PalletTaskItem_PDA> astPalletTaskItem_PDAs = astPalletTask_PDA.AST_PalletTaskItem_PDAs.ToList(); foreach (AST_PalletTaskItem_PDA astPalletTaskItem_PDA in astPalletTaskItem_PDAs) { List <AST_LesTaskItem_PDA> astLesTaskItem_PDAs = astPalletTaskItem_PDA.AST_LesTaskItem_PDAs.ToList(); List <AST_LesTask_PDA> astLesTask_PDAs = new List <AST_LesTask_PDA>(); List <AST_LesTaskMessage_PDA> astLesTaskMessage_PDAs = new List <AST_LesTaskMessage_PDA>(); foreach (AST_LesTaskItem_PDA astLesTaskItem_PDA in astLesTaskItem_PDAs) { // AST_LesTaskItem_PDA -> AST_LesTask_PDA FK ERROR AST_LesTask_PDA astLesTask_PDA = astLesTaskItem_PDA.AST_LesTask_PDA; AST_LesTaskMessage_PDA astLesTaskMessage_PDA = null; if (astLesTask_PDA != null) { astLesTaskMessage_PDA = astLesTask_PDA.AST_LesTaskMessage_PDA; } if (astLesTask_PDA != null && !astLesTask_PDAs.Contains(astLesTask_PDA)) { astLesTask_PDAs.Add(astLesTask_PDA); } if (astLesTaskMessage_PDA != null && !astLesTaskMessage_PDAs.Contains(astLesTaskMessage_PDA)) { astLesTaskMessage_PDAs.Add(astLesTaskMessage_PDA); } dbContext.AST_LesTaskItem_PDAs.Remove(astLesTaskItem_PDA); } dbContext.AST_LesTaskMessage_PDAs.RemoveRange(astLesTaskMessage_PDAs); dbContext.AST_LesTask_PDAs.RemoveRange(astLesTask_PDAs); dbContext.AST_PalletTaskItem_PDAs.Remove(astPalletTaskItem_PDA); } dbContext.AST_PalletTask_PDAs.Remove(astPalletTask_PDA); } List <AST_PalletPickResult_PDA> astPalletPickResults = dbContext.AST_PalletPickResult_PDAs .Where(pa => pa.ReceivedTime < minTime) .ToList(); foreach (AST_PalletPickResult_PDA astPalletPickResult in astPalletPickResults) { dbContext.AST_PalletPickResult_PDAs.Remove(astPalletPickResult); } List <AST_PalletPickResultMessage_PDA> astPalletPickResultMessages = dbContext.AST_PalletPickResultMessage_PDAs .Where(t => t.ReceivedTime < minTime).ToList(); foreach (AST_PalletPickResultMessage_PDA astPalletPickResultMessage in astPalletPickResultMessages) { dbContext.AST_PalletPickResultMessage_PDAs.Remove(astPalletPickResultMessage); } //AGV搬运部分 List <DST_DistributeTask> dstDistributeTasks = dbContext.DST_DistributeTasks.Where(t => t.reqTime < minTime).ToList(); foreach (DST_DistributeTask dstDistributeTask in dstDistributeTasks) { dbContext.DST_DistributeTasks.Remove(dstDistributeTask); } List <DST_DistributeTaskResult> dstDistributeTaskResults = dbContext.DST_DistributeTaskResults.Where(t => t.receiveTime < minTime).ToList(); foreach (DST_DistributeTaskResult dstDistributeTaskResult in dstDistributeTaskResults) { dbContext.DST_DistributeTaskResults.Remove(dstDistributeTaskResult); } List <DST_DistributeArriveTask> dstDistributeArriveTasks = dbContext.DST_DistributeArriveTasks.Where(t => t.receiveTime < minTime).ToList(); foreach (DST_DistributeArriveTask dstDistributeArriveTask in dstDistributeArriveTasks) { dbContext.DST_DistributeArriveTasks.Remove(dstDistributeArriveTask); } List <DST_DistributeArriveTaskResult> dstDistributeArriveTaskResults = dbContext.DST_DistributeArriveTaskResults.Where(t => t.sendTime < minTime).ToList(); foreach (DST_DistributeArriveTaskResult dstDistributeArriveTaskResult in dstDistributeArriveTaskResults) { dbContext.DST_DistributeArriveTaskResults.Remove(dstDistributeArriveTaskResult); } List <DST_DistributeArriveResult> dstDistributeArriveResults = dbContext.DST_DistributeArriveResults.Where(t => t.arriveTime < minTime).ToList(); foreach (DST_DistributeArriveResult dstDistributeArriveResult in dstDistributeArriveResults) { dbContext.DST_DistributeArriveResults.Remove(dstDistributeArriveResult); } dbContext.SaveChanges(); } } } catch (Exception ex) { string message = ex.Message; DbEntityValidationException dbEntityValidationException = ex as DbEntityValidationException; if (dbEntityValidationException != null) { foreach (DbEntityValidationResult validationResult in dbEntityValidationException.EntityValidationErrors) { foreach (DbValidationError validationError in validationResult.ValidationErrors) { message += Environment.NewLine + validationError.ErrorMessage; } } } message += Environment.NewLine + ex.StackTrace; Logger.Log(this.GetType().Name, DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine + message + Environment.NewLine + Environment.NewLine); } finally { Thread.Sleep(this.threadPeriod); } } }
/// <summary> /// 确保基础数据已经初始化。 /// </summary> public static void EnsureInitialized() { using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { if (!dbContext.CFG_Employees.Any()) { //操作员 CFG_Employee cfgEmployee = new CFG_Employee(); cfgEmployee.Code = "Administrator"; cfgEmployee.Name = "管理员"; cfgEmployee.LoginName = "admin"; cfgEmployee.Password = string.Empty; cfgEmployee.IsEnabled = true; dbContext.CFG_Employees.Add(cfgEmployee); //巷道 //转台上的标签,托盘有 5 个库位,但只在单侧使用 3 个标签 //7 个巷道共用 1 个 4 口 XGate,按现场走线,总线 1 对应 1、2 巷道,总线 2 对应 3 巷道,总线 3 对应 4、5 巷道,总线 4 对应 6、7 巷道 //标签地址分别为:1、2、3,4、5、6,51、52、53,101、102、103,104、105、106,151、152、153,154、155、156。 //托盘库位定义为:1、2 在远端需旋转,3、4、5 在近段 string channelXGateIP = "10.34.36.17"; Dictionary <int, byte[]> channelPtl900UAddressesByChannelNumber = new Dictionary <int, byte[]>(); channelPtl900UAddressesByChannelNumber.Add(1, new byte[] { 3, 1, 1, 2, 3 }); channelPtl900UAddressesByChannelNumber.Add(2, new byte[] { 6, 4, 4, 5, 6 }); channelPtl900UAddressesByChannelNumber.Add(3, new byte[] { 53, 51, 51, 52, 53 }); channelPtl900UAddressesByChannelNumber.Add(4, new byte[] { 103, 101, 101, 102, 103 }); channelPtl900UAddressesByChannelNumber.Add(5, new byte[] { 106, 104, 104, 105, 106 }); channelPtl900UAddressesByChannelNumber.Add(6, new byte[] { 153, 151, 151, 152, 153 }); channelPtl900UAddressesByChannelNumber.Add(7, new byte[] { 156, 154, 154, 155, 156 }); for (int channelNumber = 1; channelNumber <= channelPtl900UAddressesByChannelNumber.Count; channelNumber++) { CFG_Channel cfgChannel = new CFG_Channel(); cfgChannel.Code = string.Format(CultureInfo.InvariantCulture, "{0}", channelNumber); cfgChannel.Name = string.Format(CultureInfo.InvariantCulture, "巷道 {0}", channelNumber); dbContext.CFG_Channels.Add(cfgChannel); //转台上的标签 byte[] deviceAddresses = channelPtl900UAddressesByChannelNumber[channelNumber]; for (int position = 1; position <= 5; position++) { byte deviceAddress = deviceAddresses[position - 1]; byte busIndex = (byte)(deviceAddress / 50); CFG_ChannelPtlDevice cfgChannelPtlDevice = new CFG_ChannelPtlDevice(); cfgChannelPtlDevice.CFG_Channel = cfgChannel; cfgChannelPtlDevice.Position = position; cfgChannelPtlDevice.XGateIP = channelXGateIP; cfgChannelPtlDevice.RS485BusIndex = busIndex; cfgChannelPtlDevice.Ptl900UAddress = deviceAddress; dbContext.CFG_ChannelPtlDevices.Add(cfgChannelPtlDevice); } //巷道边的 4 个车位,多加两个备用车位 for (int position = 1; position <= 6; position++) { CFG_ChannelCurrentCart cfgChannelCurrentCart = new CFG_ChannelCurrentCart(); cfgChannelCurrentCart.CFG_Channel = cfgChannel; cfgChannelCurrentCart.Position = position; dbContext.CFG_ChannelCurrentCarts.Add(cfgChannelCurrentCart); } //巷道上的 1 个托盘 CFG_ChannelCurrentPallet cfgChannelCurrentPallet = new CFG_ChannelCurrentPallet(); cfgChannelCurrentPallet.CFG_Channel = cfgChannel; dbContext.CFG_ChannelCurrentPallets.Add(cfgChannelCurrentPallet); } //小车 for (int cartNumber = 1; cartNumber <= 100; cartNumber++) { CFG_Cart cfgCart = new CFG_Cart(); cfgCart.Code = string.Format(CultureInfo.InvariantCulture, "{0:000000}", 100000 + cartNumber); cfgCart.Name = string.Format(CultureInfo.InvariantCulture, "料车 {0}", cartNumber); cfgCart.Rfid1 = "AABBCCDDEEFF"; cfgCart.XGateIP = "192.168.0.10"; cfgCart.CartStatus = CartStatus.Free; dbContext.CFG_Carts.Add(cfgCart); //小车上的 8 个库位 for (int position = 1; position <= 8; position++) { CFG_CartCurrentMaterial cfgCartCurrentMaterial = new CFG_CartCurrentMaterial(); cfgCartCurrentMaterial.CFG_Cart = cfgCart; cfgCartCurrentMaterial.Position = position; cfgCartCurrentMaterial.Usability = CartPositionUsability.Enable; dbContext.CFG_CartCurrentMaterials.Add(cfgCartCurrentMaterial); } //小车上的 10 个标签 for (byte deviceAddress = 1; deviceAddress <= 10; deviceAddress++) { CFG_CartPtlDevice cfgCartPtlDevice = new CFG_CartPtlDevice(); cfgCartPtlDevice.CFG_Cart = cfgCart; cfgCartPtlDevice.DeviceAddress = deviceAddress; dbContext.CFG_CartPtlDevices.Add(cfgCartPtlDevice); } } //dbContext.SaveChanges(); } ////线边工位的8个车位 //List<CFG_WorkStation> cfgWorkStations = dbContext.CFG_WorkStations.ToList(); //foreach (CFG_WorkStation cfgWorkStation in cfgWorkStations) //{ // if (cfgWorkStation.CFG_WorkStationCurrentCarts != null && cfgWorkStation.CFG_WorkStationCurrentCarts.Count > 0) // { // continue; // } // for (int position = 1; position <= 8; position++) // { // CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = new CFG_WorkStationCurrentCart(); // cfgWorkStationCurrentCart.CFG_WorkStation = cfgWorkStation; // cfgWorkStationCurrentCart.Position = position; // dbContext.CFG_WorkStationCurrentCarts.Add(cfgWorkStationCurrentCart); // } //} //AGV开关信息 if (!dbContext.DST_AgvSwitchs.Any()) { DST_AgvSwitch dstAgvSwitch = new DST_AgvSwitch(); dstAgvSwitch.isOpen = false; dstAgvSwitch.lastCloseTime = DateTime.Now; dbContext.DST_AgvSwitchs.Add(dstAgvSwitch); } dbContext.SaveChanges(); } }