Example #1
0
        /// <summary>
        /// 转换 LES 原始任务报文。
        /// </summary>
        /// <param name="xml">LES 原始任务报文。</param>
        /// <param name="dbContext">数据库上下文。</param>
        /// <returns>成功解析的实体。</returns>
        public static AST_LesTask ConvertRequest(string xml, GeelyPtlEntities dbContext)
        {
            XDocument       xDocument        = XDocument.Parse(xml);
            XElement        serviceElement   = xDocument.Descendants("Service").First();
            XElement        dataElement      = serviceElement.Descendants("Data").First();
            XElement        requestElement   = dataElement.Descendants("Request").First();
            XElement        taskElement      = requestElement.Descendants("ASSEMBLE").First();
            List <XElement> taskItemElements = requestElement.Descendants("ASSEMBLEITEM").ToList();

            if (taskItemElements.Count == 0)
            {
                throw new InterfaceDataException("没有明细。");
            }

            AST_LesTask astLesTask  = new AST_LesTask();
            string      projectCode = taskElement.Element("ProjectCode").Value;
            string      projectStep = taskElement.Element("StageCode").Value;

            string[] codes = projectCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] steps = projectStep.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (codes.Length > 1 && codes[0] == codes[1])
            {
                projectCode = codes[0];
            }
            if (steps.Length > 1 && steps[0] == steps[1])
            {
                projectStep = steps[0];
            }

            astLesTask.ProjectCode = projectCode;
            astLesTask.WbsId       = taskElement.Element("PS_POSID").Value;
            astLesTask.ProjectStep = projectStep;
            astLesTask.BillCode    = taskElement.Element("PickNO").Value;
            astLesTask.BillDate    = DateTime.Parse(taskElement.Element("Bill_Date").Value, CultureInfo.InvariantCulture);

            //工位随时会新增
            string          cfgWorkStationCode = taskElement.Element("STATIONCODE").Value;
            CFG_WorkStation cfgWorkStation     = dbContext.CFG_WorkStations
                                                 .FirstOrDefault(ws => ws.Code == cfgWorkStationCode);

            if (cfgWorkStation == null)
            {
                using (GeelyPtlEntities innerDbContext = new GeelyPtlEntities())
                {
                    cfgWorkStation      = new CFG_WorkStation();
                    cfgWorkStation.Code = cfgWorkStationCode;
                    cfgWorkStation.Name = cfgWorkStation.Code;

                    innerDbContext.CFG_WorkStations.Add(cfgWorkStation);

                    //每个工位有 8 个车位
                    for (int position = 1; position <= 8; position++)
                    {
                        CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = new CFG_WorkStationCurrentCart();
                        cfgWorkStationCurrentCart.CFG_WorkStation = cfgWorkStation;
                        cfgWorkStationCurrentCart.Position        = position;

                        innerDbContext.CFG_WorkStationCurrentCarts.Add(cfgWorkStationCurrentCart);
                    }

                    innerDbContext.SaveChanges();
                }
            }

            astLesTask.CFG_WorkStationId = cfgWorkStation.Id;
            astLesTask.GzzList           = taskElement.Element("GZZLIST").Value;
            astLesTask.BatchCode         = taskElement.Element("BatchCode").Value;

            string      cfgChannelCode = taskElement.Element("ChannelCode").Value;
            CFG_Channel cfgChannel     = dbContext.CFG_Channels
                                         .FirstOrDefault(c => c.Code == cfgChannelCode);

            if (cfgChannel == null)
            {
                throw new InterfaceDataException("无效的分拣巷道:" + cfgChannelCode);
            }

            astLesTask.CFG_ChannelId = cfgChannel.Id;

            //托盘随时会增加
            string     cfgPalletCode = taskElement.Element("PalletCode").Value;
            CFG_Pallet cfgPallet     = dbContext.CFG_Pallets
                                       .FirstOrDefault(p => p.Code == cfgPalletCode);

            if (cfgPallet == null)
            {
                using (GeelyPtlEntities innerDbContext = new GeelyPtlEntities())
                {
                    cfgPallet            = new CFG_Pallet();
                    cfgPallet.Code       = cfgPalletCode;
                    cfgPallet.PalletType = "01";

                    innerDbContext.CFG_Pallets.Add(cfgPallet);

                    innerDbContext.SaveChanges();
                }
            }

            astLesTask.CFG_PalletId       = cfgPallet.Id;
            astLesTask.BoxCode            = taskElement.Element("BoxCode").Value;
            astLesTask.FromPalletPosition = int.Parse(taskElement.Element("FromPalletPosition").Value, CultureInfo.InvariantCulture);
            if (astLesTask.FromPalletPosition < 1 || astLesTask.FromPalletPosition > 10)
            {
                astLesTask.FromPalletPosition = 1;
            }
            astLesTask.RequestTime = DateTime.Now;

            foreach (XElement taskItemElement in taskItemElements)
            {
                AST_LesTaskItem astLesTaskItem = new AST_LesTaskItem();
                astLesTaskItem.AST_LesTask     = astLesTask;
                astLesTaskItem.BillDetailId    = taskItemElement.Element("BillDtlID").Value;
                astLesTaskItem.MaterialCode    = taskItemElement.Element("MaterialCode").Value;
                astLesTaskItem.MaterialName    = taskItemElement.Element("MaterialName").Value;
                astLesTaskItem.MaterialBarcode = taskItemElement.Element("MaterialBarcode").Value;
                astLesTaskItem.ToPickQuantity  = (int)decimal.Parse(taskItemElement.Element("NEED_PICK_NUM").Value, CultureInfo.InvariantCulture);
                astLesTaskItem.MaxQuantityInSingleCartPosition = (int)decimal.Parse(taskItemElement.Element("MaxQuantityInSingleCartPosition").Value, CultureInfo.InvariantCulture);
                if (astLesTaskItem.MaxQuantityInSingleCartPosition <= 0)
                {
                    astLesTaskItem.MaxQuantityInSingleCartPosition = int.MaxValue;
                }
                astLesTaskItem.IsSpecial = taskItemElement.Element("IsSpecial").Value == "1";
                astLesTaskItem.IsBig     = taskItemElement.Element("STORETYPE").Value == "04";

                astLesTask.AST_LesTaskItems.Add(astLesTaskItem);
            }

            return(astLesTask);
        }
Example #2
0
        /// <summary>
        /// 发送生产线边配送任务
        /// </summary>
        /// <returns></returns>
        private bool SendProductAreaDistributeTask()
        {
            bool isSuccess = false;

            try
            {
                ExeConfigurationFileMap map = new ExeConfigurationFileMap {
                    ExeConfigFilename = @"服务控制台.exe.Config"
                };
                Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                string        StartMaterialMarketDistributeFlag = configuration.AppSettings.Settings["StartMaterialMarketDistribute"].Value;

                using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                {
                    //获取未响应的配送任务
                    List <DST_DistributeTask> distributeTasks = dbContext.DST_DistributeTasks.
                                                                Where(t => !t.isResponse &&
                                                                      (t.DistributeReqTypes == DistributeReqTypes.ProductCartSwitch ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.ProductNullCartBack ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.MaterialMarketDistribute ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.ProductOutToIn ||
                                                                       t.DistributeReqTypes == DistributeReqTypes.ProductInToOut
                                                                      ) && t.sendErrorCount < 5).
                                                                OrderBy(t => t.reqTime).ToList();

                    //List<DST_DistributeTask> distributeTasks = new List<DST_DistributeTask>();

                    if (distributeTasks.Count == 0)
                    {
                        return(false);
                    }

                    //已经设置成运输开始的寻车任务ID集合
                    //List<long> listNeedBlinkFndTask = new List<long>();

                    //发送信息
                    string sendInfo = "";
                    //AGV服务地址
                    string sURL = ptlToAgvServiceUrl + "/genAgvSchedulingTask";
                    //HTTP响应结果
                    string result = "";

                    foreach (DST_DistributeTask distributeTask in distributeTasks)
                    {
                        if (StartMaterialMarketDistributeFlag.Equals("no"))
                        {
                            if (distributeTask.DistributeReqTypes == DistributeReqTypes.MaterialMarketDistribute)
                            {
                                distributeTask.sendErrorCount = 5;
                                continue;
                            }
                        }

                        if (distributeTask.DistributeReqTypes == DistributeReqTypes.MaterialMarketDistribute)
                        {
                            Thread.Sleep(500);
                        }

                        //绑定配送任务
                        DST_DistributeTaskDto distributeTaskDto = new DST_DistributeTaskDto();
                        distributeTaskDto.reqCode          = distributeTask.reqCode;
                        distributeTaskDto.reqTime          = distributeTask.reqTime.ToString("yyyy-MM-dd HH:mm:ss");
                        distributeTaskDto.clientCode       = distributeTask.clientCode;
                        distributeTaskDto.tokenCode        = distributeTask.tokenCode;
                        distributeTaskDto.taskTyp          = distributeTask.taskTyp;
                        distributeTaskDto.userCallCode     = distributeTask.userCallCode;
                        distributeTaskDto.userCallCodePath = new List <string>();
                        distributeTaskDto.userCallCodePath.AddRange(new string[] { distributeTask.startPosition, distributeTask.endPosition });
                        distributeTaskDto.podCode   = distributeTask.podCode;
                        distributeTaskDto.robotCode = distributeTask.robotCode;
                        distributeTaskDto.taskCode  = distributeTask.taskCode;
                        distributeTaskDto.data      = distributeTask.data;

                        //发送信息
                        sendInfo = JsonConvert.SerializeObject(distributeTaskDto);
                        //发送HTTP请求,并返回响应结果
                        result = HttpService.HttpPost(sURL, sendInfo);

                        if (!string.IsNullOrEmpty(result))
                        {
                            //实例化HTTP响应结果
                            DST_DistributeTaskResultDto distributeTaskResultDto = JsonConvert.DeserializeObject <DST_DistributeTaskResultDto>(result);
                            if (distributeTaskResultDto != null)
                            {
                                //新增配送任务结果
                                DST_DistributeTaskResult distributeTaskResult = new DST_DistributeTaskResult();
                                distributeTaskResult.code        = distributeTaskResultDto.code;
                                distributeTaskResult.message     = distributeTaskResultDto.message;
                                distributeTaskResult.reqCode     = distributeTaskResultDto.reqCode;
                                distributeTaskResult.data        = distributeTaskResultDto.data;
                                distributeTaskResult.receiveTime = DateTime.Now;

                                dbContext.DST_DistributeTaskResults.Add(distributeTaskResult);

                                //更新配送任务响应值
                                if (distributeTaskResultDto.code.Equals("0"))
                                {
                                    distributeTask.isResponse = true;

                                    //if (distributeTask.DistributeReqTypes == DistributeReqTypes.MaterialMarketDistribute)
                                    //{
                                    //    string sWorkStationCode = distributeTask.startPosition;
                                    //    //string sWorkStationCode = distributeTask.endPosition.Split('-')[0];
                                    //    FND_Task fndTask = dbContext.FND_Tasks.Where(t => t.CFG_WorkStation.Code.Equals(sWorkStationCode)
                                    //        && (t.FindingStatus == FindingStatus.New || t.FindingStatus == FindingStatus.NeedDisplay || t.FindingStatus == FindingStatus.Displaying)
                                    //        && (t.CFG_Cart.CartStatus == CartStatus.NeedToWorkStation || t.CFG_Cart.CartStatus == CartStatus.WaitingToWorkStation)
                                    //        && !listNeedBlinkFndTask.Contains(t.Id)).
                                    //        OrderBy(t => t.BatchCode).ThenByDescending(t => t.FindingStatus).FirstOrDefault();
                                    //    if (fndTask != null)
                                    //    {
                                    //        if (fndTask.FindingStatus == FindingStatus.New)
                                    //        {
                                    //            fndTask.CFG_EmployeeId = 1;
                                    //            fndTask.DisplayTime = DateTime.Now;

                                    //            fndTask.CFG_Cart.CartStatus = CartStatus.WaitingToWorkStation;
                                    //        }
                                    //        fndTask.FindingStatus = FindingStatus.NeedBlink;
                                    //        fndTask.CFG_Cart.CartStatus = CartStatus.InCarriageToWorkStation;

                                    //        listNeedBlinkFndTask.Add(fndTask.Id);
                                    //    }
                                    //}
                                    if (distributeTask.DistributeReqTypes == DistributeReqTypes.ProductCartSwitch ||
                                        distributeTask.DistributeReqTypes == DistributeReqTypes.ProductNullCartBack ||
                                        distributeTask.DistributeReqTypes == DistributeReqTypes.ProductOutToIn ||
                                        distributeTask.DistributeReqTypes == DistributeReqTypes.ProductInToOut)
                                    {
                                        string[] arrStartPosition = distributeTask.startPosition.Split('-');
                                        string   sWorkStationCode = arrStartPosition[0];
                                        int      nPosition        = Convert.ToInt32(arrStartPosition[1]);

                                        //解除线边停靠
                                        CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = dbContext.CFG_WorkStationCurrentCarts
                                                                                               .FirstOrDefault(wscc => wscc.CFG_WorkStation.Code.Equals(sWorkStationCode) && wscc.Position == nPosition);
                                        if (cfgWorkStationCurrentCart != null)
                                        {
                                            if (distributeTask.DistributeReqTypes == DistributeReqTypes.ProductNullCartBack)
                                            {
                                                //清空小车上的物料
                                                CFG_Cart cfgCart = cfgWorkStationCurrentCart.CFG_Cart;
                                                if (cfgCart != null)
                                                {
                                                    List <CFG_CartCurrentMaterial> cfgCartCurrentMaterials = cfgCart.CFG_CartCurrentMaterials.Where(t => t.AST_CartTaskItemId != null).ToList();
                                                    foreach (CFG_CartCurrentMaterial cfgCartCurrentMaterial in cfgCartCurrentMaterials)
                                                    {
                                                        cfgCartCurrentMaterial.AST_CartTaskItemId = null;
                                                        cfgCartCurrentMaterial.ProjectCode        = null;
                                                        cfgCartCurrentMaterial.WbsId              = null;
                                                        cfgCartCurrentMaterial.ProjectStep        = null;
                                                        cfgCartCurrentMaterial.CFG_WorkStationId  = null;
                                                        cfgCartCurrentMaterial.BatchCode          = null;
                                                        cfgCartCurrentMaterial.CFG_ChannelId      = null;
                                                        cfgCartCurrentMaterial.CFG_PalletId       = null;
                                                        cfgCartCurrentMaterial.BoxCode            = null;
                                                        cfgCartCurrentMaterial.FromPalletPosition = null;
                                                        cfgCartCurrentMaterial.MaterialCode       = null;
                                                        cfgCartCurrentMaterial.MaterialName       = null;
                                                        cfgCartCurrentMaterial.MaterialBarcode    = null;
                                                        cfgCartCurrentMaterial.Quantity           = null;
                                                        cfgCartCurrentMaterial.AssortedTime       = null;
                                                        cfgCartCurrentMaterial.CFG_EmployeeId     = null;
                                                        if (cfgCartCurrentMaterial.Usability != CartPositionUsability.DisableByOffLineDevice)
                                                        {
                                                            cfgCartCurrentMaterial.Usability = CartPositionUsability.Enable;
                                                        }
                                                    }
                                                }
                                            }

                                            //解除线边位置停靠
                                            cfgWorkStationCurrentCart.CFG_CartId = null;
                                            cfgWorkStationCurrentCart.DockedTime = null;
                                        }
                                    }
                                }
                                else
                                {
                                    distributeTask.sendErrorCount = distributeTask.sendErrorCount + 1;
                                }
                            }
                        }
                    }

                    //更新数据库
                    isSuccess = dbContext.SaveChanges() > 0 ? true : false;
                }
            }
            catch (Exception)
            {
                isSuccess = false;
            }
            return(isSuccess);
        }
Example #3
0
        /// <summary>
        /// 显式或定时刷新。
        /// </summary>
        public void Refresh()
        {
            lock (this.refreshSyncRoot)
            {
                using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                {
                    List <FND_Task> needToDoFndTasks = dbContext.FND_Tasks
                                                       .Where(t => t.FindingStatus == FindingStatus.NeedDisplay ||
                                                              t.FindingStatus == FindingStatus.NeedBlink ||
                                                              t.FindingStatus == FindingStatus.NeedClear)
                                                       .ToList();

                    //获取是否开启AGV配送PTL料架
                    DST_AgvSwitch dstAgvSwitch = dbContext.DST_AgvSwitchs.FirstOrDefault(t => t.isOpen);

                    foreach (FND_Task fndTask in needToDoFndTasks)
                    {
                        CFG_WorkStation cfgWorkStation = fndTask.CFG_WorkStation;
                        CFG_Cart        cfgCart        = fndTask.CFG_Cart;

                        string gzzList = this.GetGzzListFromCfgCartMaterial(cfgCart);

                        CartPtl cartPtl          = CartPtlHost.Instance.GetCartPtl(fndTask.CFG_CartId);
                        Ptl900U ptl900UPublisher = cartPtl.GetPtl900UPublisher();
                        Ptl900U ptl900ULight     = cartPtl.GetPtl900ULight();

                        Display900UItem publisherDisplay900UItem = new Display900UItem();
                        publisherDisplay900UItem.Name        = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", cfgWorkStation.Name, gzzList);
                        publisherDisplay900UItem.Description = string.Format(CultureInfo.InvariantCulture, @"项目:{0},{1}
批次:{2}
最迟抵达:{3:HH:mm:ss}", fndTask.ProjectCode, fndTask.ProjectStep, fndTask.BatchCode, fndTask.MaxNeedArrivedTime);
                        publisherDisplay900UItem.Count       = (ushort)cfgCart.CFG_CartCurrentMaterials
                                                               .Where(ccm => ccm.Quantity != null)
                                                               .Select(ccm => ccm.Quantity.Value)
                                                               .Sum();
                        publisherDisplay900UItem.Unit = "个";

                        Display900UItem lightDisplay900UItem = new Display900UItem();

                        LightColor lightColor = (LightColor)fndTask.LightColor;

                        LightMode lightMode = new LightMode();
                        lightMode.Color  = lightColor;
                        lightMode.Period = LightOnOffPeriod.Period200;
                        lightMode.Ratio  = LightOnOffRatio.RatioP1V1;

                        if (fndTask.FindingStatus == FindingStatus.NeedDisplay)
                        {
                            //先保证数据库更新成功
                            fndTask.DisplayTime   = DateTime.Now;
                            fndTask.FindingStatus = FindingStatus.Displaying;

                            cfgCart.CartStatus = CartStatus.WaitingToWorkStation;

                            dbContext.SaveChanges();

                            //再控制设备
                            ptl900UPublisher.Clear(true);
                            ptl900UPublisher.Lock();
                            ptl900UPublisher.Display(publisherDisplay900UItem, LightColor.Off);

                            ptl900ULight.Clear();
                            ptl900ULight.Display(lightDisplay900UItem, lightColor);
                        }
                        else if (fndTask.FindingStatus == FindingStatus.NeedBlink)
                        {
                            if (fndTask.DisplayTime == null)
                            {
                                fndTask.CFG_EmployeeId = 1;
                                fndTask.DisplayTime    = DateTime.Now;
                            }
                            fndTask.DepartedTime  = DateTime.Now;
                            fndTask.FindingStatus = FindingStatus.Blinking;

                            if (dstAgvSwitch == null)
                            {
                                cfgCart.CartStatus = CartStatus.InCarriageToWorkStation;
                            }

                            //当前请求可以分小车提交
                            FND_DeliveryResult fndDeliveryResult = new FND_DeliveryResult();
                            fndDeliveryResult.FND_TaskId         = fndTask.Id;
                            fndDeliveryResult.ProjectCode        = fndTask.ProjectCode;
                            fndDeliveryResult.ProjectStep        = fndTask.ProjectStep;
                            fndDeliveryResult.CFG_WorkStationId  = fndTask.CFG_WorkStationId;
                            fndDeliveryResult.BatchCode          = fndTask.BatchCode;
                            fndDeliveryResult.MaxNeedArrivedTime = fndTask.MaxNeedArrivedTime;
                            fndDeliveryResult.CFG_CartId         = fndTask.CFG_CartId;
                            fndDeliveryResult.DepartedTime       = DateTime.Now;
                            fndDeliveryResult.CFG_EmployeeId     = fndTask.CFG_EmployeeId.Value;

                            dbContext.FND_DeliveryResults.Add(fndDeliveryResult);

                            FND_DeliveryResultMessage fndDeliveryResultMessage = new FND_DeliveryResultMessage();
                            fndDeliveryResultMessage.FND_DeliveryResult = fndDeliveryResult;
                            fndDeliveryResultMessage.SentSuccessful     = false;

                            dbContext.FND_DeliveryResultMessages.Add(fndDeliveryResultMessage);

                            List <CFG_CartCurrentMaterial> cfgCartCurrentMaterials = fndTask.CFG_Cart.CFG_CartCurrentMaterials
                                                                                     .Where(ccm => ccm.Quantity > 0)
                                                                                     .ToList();
                            foreach (CFG_CartCurrentMaterial cfgCartCurrentMaterial in cfgCartCurrentMaterials)
                            {
                                FND_DeliveryResultItem fndDeliveryResultItem = new FND_DeliveryResultItem();
                                fndDeliveryResultItem.FND_DeliveryResult = fndDeliveryResult;
                                fndDeliveryResultItem.CartPosition       = cfgCartCurrentMaterial.Position;
                                fndDeliveryResultItem.MaterialCode       = cfgCartCurrentMaterial.MaterialCode;
                                fndDeliveryResultItem.MaterialName       = cfgCartCurrentMaterial.MaterialName;
                                fndDeliveryResultItem.MaterialBarcode    = cfgCartCurrentMaterial.MaterialBarcode;
                                fndDeliveryResultItem.Quantity           = cfgCartCurrentMaterial.Quantity.Value;

                                dbContext.FND_DeliveryResultItems.Add(fndDeliveryResultItem);
                            }

                            dbContext.SaveChanges();

                            ptl900ULight.Clear();
                            ptl900ULight.Display(lightDisplay900UItem, lightMode, false);
                        }
                        else if (fndTask.FindingStatus == FindingStatus.NeedClear)
                        {
                            fndTask.FindingStatus = FindingStatus.Finished;

                            dbContext.SaveChanges();

                            ptl900ULight.Clear();
                        }
                    }

                    //运输一段时间后自动进入 NeedClear 状态
                    DateTime        maxBlinkingTime   = DateTime.Now.Subtract(this.autoClearPeriod);
                    List <FND_Task> autoClearFndTasks = dbContext.FND_Tasks
                                                        .Where(t => t.FindingStatus == FindingStatus.Blinking &&
                                                               t.DisplayTime < maxBlinkingTime)
                                                        .ToList();

                    foreach (FND_Task fndTask in autoClearFndTasks)
                    {
                        fndTask.FindingStatus = FindingStatus.NeedClear;
                        dbContext.SaveChanges();
                    }

                    if (dstAgvSwitch == null)
                    {
                        //运输一段时间后自动抵达生产线
                        DateTime        maxCarriageTime     = DateTime.Now.Subtract(this.autoReachedPeriod);
                        List <FND_Task> autoReachedFndTasks = dbContext.FND_Tasks
                                                              .Where(t => t.CFG_Cart.CartStatus == CartStatus.InCarriageToWorkStation &&
                                                                     t.FindingStatus == FindingStatus.Finished &&
                                                                     t.DepartedTime < maxCarriageTime)
                                                              .ToList();

                        foreach (FND_Task fndTask in autoReachedFndTasks)
                        {
                            CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = dbContext.CFG_WorkStationCurrentCarts
                                                                                   .Where(wscc => wscc.CFG_WorkStationId == fndTask.CFG_WorkStationId &&
                                                                                          wscc.CFG_CartId == null)
                                                                                   .OrderBy(wscc => wscc.Position)
                                                                                   .FirstOrDefault();
                            if (cfgWorkStationCurrentCart != null)
                            {
                                CFG_Cart cfgCart = fndTask.CFG_Cart;

                                cfgWorkStationCurrentCart.CFG_CartId = cfgCart.Id;
                                cfgWorkStationCurrentCart.DockedTime = DateTime.Now;

                                cfgCart.CartStatus = CartStatus.ArrivedAtWorkStation;
                            }

                            dbContext.SaveChanges();
                        }
                    }
                }
            }
        }
Example #4
0
        void ptl900U_Pressed(object sender, Ptl900UPressedEventArgs e, bool pickAll)
        {
            while (true)
            {
                try
                {
                    using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                    {
                        CFG_WorkStation cfgWorkStation = dbContext.CFG_WorkStations
                                                         .First(ws => ws.Id == this.CFG_WorkStationId);
                        ASM_TaskItem asmTaskItem = dbContext.ASM_TaskItems
                                                   .First(ti => ti.Id == this.CurrentAsmTaskItemId.Value);
                        ASM_AssembleIndicationItem asmAssembleIndicationItem = asmTaskItem.ASM_AssembleIndicationItem;
                        ASM_AssembleIndication     asmAssembleIndication     = asmAssembleIndicationItem.ASM_AssembleIndication;
                        List <ASM_TaskItem>        otherAsmTaskItems         = asmAssembleIndicationItem.ASM_TaskItems
                                                                               .Where(ti => ti.Id != asmTaskItem.Id)
                                                                               .ToList();
                        CFG_Cart cfgCart = asmTaskItem.CFG_Cart;
                        List <CFG_CartCurrentMaterial> cfgCartCurrentMaterials = cfgCart.CFG_CartCurrentMaterials
                                                                                 .OrderBy(ccm => ccm.Position)
                                                                                 .ToList();
                        CFG_CartCurrentMaterial    cfgCartCurrentMaterial    = cfgCartCurrentMaterials.First(ccm => ccm.Position == asmTaskItem.CartPosition);
                        CFG_WorkStationCurrentCart cfgWorkStationCurrentCart = dbContext.CFG_WorkStationCurrentCarts
                                                                               .FirstOrDefault(wscc => wscc.CFG_WorkStationId == this.CFG_WorkStationId && wscc.CFG_CartId == cfgCart.Id);

                        if (asmTaskItem.AssembledQuantity == null)
                        {
                            asmTaskItem.AssembledQuantity = 0;
                        }

                        int pickedCount = 1;
                        if (pickAll)
                        {
                            pickedCount = asmTaskItem.ToAssembleQuantity - asmTaskItem.AssembledQuantity.Value;
                        }

                        asmTaskItem.AssembledQuantity += pickedCount;
                        asmTaskItem.AssembledTime      = DateTime.Now;

                        if (asmAssembleIndicationItem.AssembledQuantity == null)
                        {
                            asmAssembleIndicationItem.AssembledQuantity = 0;
                        }
                        asmAssembleIndicationItem.AssembledQuantity += pickedCount;
                        asmAssembleIndicationItem.AssembledTime      = DateTime.Now;

                        bool currentItemFinished = asmTaskItem.AssembledQuantity == asmTaskItem.ToAssembleQuantity;
                        if (currentItemFinished)
                        {
                            asmTaskItem.AssembleStatus = AssembleStatus.Finished;
                            if (otherAsmTaskItems.All(ti => ti.AssembleStatus == AssembleStatus.Finished))
                            {
                                asmAssembleIndicationItem.AssembleStatus = AssembleStatus.Finished;
                            }
                        }

                        if (cfgCartCurrentMaterial.Quantity != null && cfgCartCurrentMaterial.Quantity > 0)
                        {
                            cfgCartCurrentMaterial.Quantity -= pickedCount;
                        }

                        //料车上物料消耗完则施放料车并通知 AGV 回收,再尝试补充一辆料车
                        if (cfgCartCurrentMaterials.All(ccm => ccm.Quantity == null || ccm.Quantity == 0))
                        {
                            cfgCart.CartStatus = CartStatus.Free;

                            foreach (CFG_CartCurrentMaterial innerCfgCartCurrentMaterial in cfgCartCurrentMaterials)
                            {
                                innerCfgCartCurrentMaterial.AST_CartTaskItemId = null;
                                innerCfgCartCurrentMaterial.ProjectCode        = null;
                                innerCfgCartCurrentMaterial.WbsId              = null;
                                innerCfgCartCurrentMaterial.ProjectStep        = null;
                                innerCfgCartCurrentMaterial.CFG_WorkStationId  = null;
                                innerCfgCartCurrentMaterial.BatchCode          = null;
                                innerCfgCartCurrentMaterial.CFG_ChannelId      = null;
                                innerCfgCartCurrentMaterial.CFG_PalletId       = null;
                                innerCfgCartCurrentMaterial.BoxCode            = null;
                                innerCfgCartCurrentMaterial.FromPalletPosition = null;
                                innerCfgCartCurrentMaterial.MaterialCode       = null;
                                innerCfgCartCurrentMaterial.MaterialName       = null;
                                innerCfgCartCurrentMaterial.MaterialBarcode    = null;
                                innerCfgCartCurrentMaterial.Quantity           = null;
                                innerCfgCartCurrentMaterial.AssortedTime       = null;
                                innerCfgCartCurrentMaterial.CFG_EmployeeId     = null;
                                if (innerCfgCartCurrentMaterial.Usability != CartPositionUsability.DisableByOffLineDevice)
                                {
                                    innerCfgCartCurrentMaterial.Usability = CartPositionUsability.Enable;
                                }
                            }

                            if (cfgWorkStationCurrentCart != null)
                            {
                                //通知 AGV 回收当前车,尝试空满切换下一辆车
                                DST_AgvSwitch dstAgvSwitch = dbContext.DST_AgvSwitchs.FirstOrDefault(t => t.isOpen);
                                if (dstAgvSwitch != null)
                                {
                                    int    nCurPosition     = cfgWorkStationCurrentCart.Position;
                                    string sWorkStationCode = cfgWorkStationCurrentCart.CFG_WorkStation.Code;
                                    if (nCurPosition <= 4) //内侧
                                    {
                                        //如果对应外侧没有正在执行的物料超市配送任务,才生成里侧的线边配送任务
                                        string             sOutPosition      = sWorkStationCode + "-" + (nCurPosition + 4);
                                        DST_DistributeTask outDistributeTask = dbContext.DST_DistributeTasks.FirstOrDefault(t => t.DistributeReqTypes == DistributeReqTypes.MaterialMarketDistribute &&
                                                                                                                            t.endPosition.Equals(sOutPosition) &&
                                                                                                                            t.sendErrorCount < 5 &&
                                                                                                                            t.arriveTime == null);
                                        if (outDistributeTask == null)
                                        {
                                            CFG_WorkStationCurrentCart cfgOutWorkStationCurrentCart = dbContext.CFG_WorkStationCurrentCarts
                                                                                                      .FirstOrDefault(wscc => wscc.CFG_WorkStationId == this.CFG_WorkStationId && wscc.Position == nCurPosition + 4 && wscc.CFG_CartId != null);
                                            if (cfgOutWorkStationCurrentCart != null)
                                            {
                                                //生成料架转换任务
                                                List <DST_DistributeTask> distributeTasks = DistributingTaskGenerator.Instance.GenerateProductCartSwitchTask(cfgCart);
                                                foreach (DST_DistributeTask distributeTask in distributeTasks)
                                                {
                                                    dbContext.DST_DistributeTasks.Add(distributeTask);
                                                }
                                            }
                                            else
                                            {
                                                //生成线边自动清线配送任务
                                                List <DST_DistributeTask> distributeTasks = DistributingTaskGenerator.Instance.GenerateProductAreaAutoClearTask(sWorkStationCode, nCurPosition.ToString(), cfgCart.Code);
                                                foreach (DST_DistributeTask distributeTask in distributeTasks)
                                                {
                                                    dbContext.DST_DistributeTasks.Add(distributeTask);
                                                }
                                            }
                                        }
                                    }
                                    else //外侧
                                    {
                                        //如果对应里侧没有正在执行的空满转换任务,才生成外侧的线边配送任务
                                        string             sInPosition      = sWorkStationCode + "-" + (nCurPosition - 4);
                                        DST_DistributeTask inDistributeTask = dbContext.DST_DistributeTasks.FirstOrDefault(t => t.DistributeReqTypes == DistributeReqTypes.ProductCartSwitch &&
                                                                                                                           t.startPosition.Equals(sInPosition) &&
                                                                                                                           t.sendErrorCount < 5 &&
                                                                                                                           t.arriveTime == null);
                                        if (inDistributeTask == null)
                                        {
                                            //生成线边配送任务
                                            string sTaskSendType = "自动";
                                            List <DST_DistributeTask> distributeTasks = DistributingTaskGenerator.Instance.GenerateProductAreaDistributeTask(sWorkStationCode, nCurPosition.ToString(), cfgCart.Code, sTaskSendType, true, dbContext);
                                            foreach (DST_DistributeTask distributeTask in distributeTasks)
                                            {
                                                dbContext.DST_DistributeTasks.Add(distributeTask);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //解除停靠
                                    cfgWorkStationCurrentCart.CFG_CartId = null;
                                    cfgWorkStationCurrentCart.DockedTime = null;
                                }
                            }

                            //尝试发起下一车的拉料任务
                            CFG_Cart nextCfgCart = dbContext.CFG_Carts
                                                   .FirstOrDefault(c => c.CartStatus == CartStatus.ArrivedAtBufferArea &&
                                                                   c.CFG_CartCurrentMaterials.Any(ccm => ccm.CFG_WorkStationId == this.CFG_WorkStationId &&
                                                                                                  ccm.Quantity > 0));
                            if (nextCfgCart != null)
                            {
                                nextCfgCart.CartStatus = CartStatus.NeedToWorkStation;

                                CFG_CartCurrentMaterial firstCfgCartFirstCartCurrentMaterial = nextCfgCart.CFG_CartCurrentMaterials
                                                                                               .First(ccm => ccm.Quantity > 0);

                                FND_Task fndTask = new FND_Task();
                                fndTask.ProjectCode        = firstCfgCartFirstCartCurrentMaterial.ProjectCode;
                                fndTask.ProjectStep        = firstCfgCartFirstCartCurrentMaterial.ProjectStep;
                                fndTask.BatchCode          = firstCfgCartFirstCartCurrentMaterial.BatchCode;
                                fndTask.MaxNeedArrivedTime = DateTime.Now.AddHours(1);
                                fndTask.RequestTime        = DateTime.Now;
                                fndTask.CFG_WorkStationId  = firstCfgCartFirstCartCurrentMaterial.CFG_WorkStationId.Value;
                                fndTask.CFG_CartId         = nextCfgCart.Id;
                                fndTask.LightColor         = (byte)LightColor.Off;
                                fndTask.FindingStatus      = FindingStatus.New;

                                dbContext.FND_Tasks.Add(fndTask);
                            }
                        }

                        dbContext.SaveChanges();

                        CartPtl cartPtl          = CartPtlHost.Instance.GetCartPtl(asmTaskItem.CFG_CartId);
                        Ptl900U ptl900UPublisher = cartPtl.GetPtl900UPublisher();
                        Ptl900U ptl900U          = cartPtl.GetPtl900UByPosition(asmTaskItem.CartPosition);
                        Ptl900U ptl900ULight     = cartPtl.GetPtl900ULight();

                        if (currentItemFinished)
                        {
                            Display900UItem cartPublisherDisplay900UItem = new Display900UItem();
                            cartPublisherDisplay900UItem.Name        = "抵达工位 " + cfgWorkStation.Name;
                            cartPublisherDisplay900UItem.Description = string.Format(CultureInfo.InvariantCulture, @"项目:{0}
阶段:{1}
批次:{2}", cfgCartCurrentMaterial.ProjectCode, cfgCartCurrentMaterial.ProjectStep, cfgCartCurrentMaterial.BatchCode);
                            if (cfgWorkStationCurrentCart != null)
                            {
                                cartPublisherDisplay900UItem.Count = (ushort)cfgWorkStationCurrentCart.Position;
                            }
                            cartPublisherDisplay900UItem.Unit = "位";

                            ptl900UPublisher.Pressed -= this.ptl900UPublisher_Pressed;
                            ptl900UPublisher.Clear(true);
                            ptl900UPublisher.Lock();
                            ptl900UPublisher.Display(cartPublisherDisplay900UItem, LightColor.Off);

                            ptl900U.Pressed -= this.ptl900U_Pressed;

                            ptl900ULight.Clear();

                            this.CurrentAsmTaskItemId = null;
                        }
                        else
                        {
                            Display900UItem publisherDisplay900UItem = new Display900UItem();
                            publisherDisplay900UItem.Name            = asmAssembleIndicationItem.MaterialName;
                            publisherDisplay900UItem.Description     = string.Format(CultureInfo.InvariantCulture, @"项目:{0},{1}
车号:{2}
量产工位:{3}", asmAssembleIndicationItem.ProjectCode, asmAssembleIndicationItem.ProjectStep, asmAssembleIndication.ProductSequence, asmAssembleIndicationItem.Gzz);
                            publisherDisplay900UItem.LongSubLocation = asmTaskItem.ToAssembleQuantity.ToString(CultureInfo.InvariantCulture);
                            publisherDisplay900UItem.Count           = (ushort)asmTaskItem.AssembledQuantity.Value;

                            Display900UItem display900UItem = new Display900UItem();
                            display900UItem.Count = (ushort)(asmTaskItem.ToAssembleQuantity - asmTaskItem.AssembledQuantity.Value);

                            LightMode lightMode = new LightMode();
                            lightMode.Color = LightColor.Green;
                            if (asmAssembleIndicationItem.Qtxbs == "1")
                            {
                                lightMode.Color  = LightColor.Magenta;
                                lightMode.Ratio  = LightOnOffRatio.RatioP1V1;
                                lightMode.Period = LightOnOffPeriod.Period500;
                            }

                            ptl900UPublisher.Clear(true);
                            //ptl900UPublisher.Unlock();
                            ptl900UPublisher.Lock();
                            ptl900UPublisher.Display(publisherDisplay900UItem, LightColor.Off, true);

                            ptl900U.Display(display900UItem, lightMode, true);

                            //如果长时间无法交互,则自动交互
                            this.AutoPressByDeviceErrorAsync(this.CurrentAsmTaskItemId.Value, ptl900U, display900UItem);
                        }

                        string logMessage = (pickAll ? " 拣选所有:" : " 拣选一个:")
                                            + asmAssembleIndication.ProductSequence + ", "
                                            + asmAssembleIndication.GzzList + ", "
                                            + asmAssembleIndicationItem.MaterialCode + ", "
                                            + asmAssembleIndicationItem.MaterialName + ", "
                                            + cfgCart.Name + ", "
                                            + asmTaskItem.CartPosition;
                        if (currentItemFinished)
                        {
                            logMessage += ", 明细完成";
                        }
                        Logger.Log(this.GetType().Name + "." + cfgWorkStation.Code, DateTime.Now.ToString("HH:mm:ss") + logMessage + Environment.NewLine);
                    }

                    break;
                }
                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("IndicatingExecutor.ptl900U_Pressed", DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine
                               + message + Environment.NewLine
                               + Environment.NewLine);

                    Thread.Sleep(1000);
                }
            }
        }