/// <summary>
        /// 转换装配结果到报文。
        /// </summary>
        /// <param name="asmAssembleResultInDbContext">处于数据库上下文中的实体。</param>
        /// <returns>报文。</returns>
        public static string ConvertRequest(ASM_AssembleResult asmAssembleResultInDbContext)
        {
            XDocument xDocument       = new XDocument();
            XElement  serviceElement  = new XElement("Service");
            XElement  dataElement     = new XElement("Data");
            XElement  requestElement  = new XElement("Request");
            XElement  responseElement = new XElement("Response");

            xDocument.Add(serviceElement);
            serviceElement.Add(dataElement);
            dataElement.Add(requestElement);
            dataElement.Add(responseElement);

            XElement resultElement = new XElement("ASSEMBLEDRESULT");

            resultElement.SetAttributeValue("p_type", "G");
            resultElement.SetAttributeValue("loop_num", "1");
            resultElement.Add(new XElement("ID", asmAssembleResultInDbContext.Id));
            resultElement.Add(new XElement("FCCODE", asmAssembleResultInDbContext.FactoryCode));
            resultElement.Add(new XElement("PLCODE", asmAssembleResultInDbContext.ProductionLineCode));
            resultElement.Add(new XElement("STCODE", asmAssembleResultInDbContext.CFG_WorkStation.Code));
            resultElement.Add(new XElement("GZZLIST", asmAssembleResultInDbContext.GzzList));
            resultElement.Add(new XElement("MONUM", asmAssembleResultInDbContext.MONumber));
            resultElement.Add(new XElement("PRDSEQ", asmAssembleResultInDbContext.ProductSequence));
            resultElement.Add(new XElement("BEGINTIME", asmAssembleResultInDbContext.BeginTime.ToString("yyyy-MM-dd HH:mm:ss")));
            resultElement.Add(new XElement("ENDTIME", asmAssembleResultInDbContext.EndTime.ToString("yyyy-MM-dd HH:mm:ss")));
            resultElement.Add(new XElement("TOTALNUM", asmAssembleResultInDbContext.ASM_AssembleResultItems.Count));

            requestElement.Add(resultElement);

            int loopNumber = 1;

            foreach (ASM_AssembleResultItem asmAssembleResultItem in asmAssembleResultInDbContext.ASM_AssembleResultItems)
            {
                XElement resultItemElement = new XElement("RESULTITEM");
                resultItemElement.SetAttributeValue("p_type", "G");
                resultItemElement.SetAttributeValue("loop_num", loopNumber++);
                resultItemElement.Add(new XElement("CARTCODE", asmAssembleResultItem.CFG_Cart.Code));
                resultItemElement.Add(new XElement("CARTPOSITION", asmAssembleResultItem.CartPosition));
                resultItemElement.Add(new XElement("GZZ", asmAssembleResultItem.Gzz));
                resultItemElement.Add(new XElement("MTCODE", asmAssembleResultItem.MaterialCode));
                resultItemElement.Add(new XElement("ASSSEQ", asmAssembleResultItem.AssembleSequence));
                resultItemElement.Add(new XElement("CPQTY", asmAssembleResultItem.ToAssembleQuantity));
                resultItemElement.Add(new XElement("ASSQTY", asmAssembleResultItem.AssembledQuantity));
                resultItemElement.Add(new XElement("PICKEDTIME", asmAssembleResultItem.PickedTime.ToString("yyyy-MM-dd HH:mm:ss")));
                resultItemElement.Add(new XElement("PRJCODE", asmAssembleResultItem.ProjectCode));
                resultItemElement.Add(new XElement("PRJSTEP", asmAssembleResultItem.ProjectStep));

                requestElement.Add(resultItemElement);
            }

            return(Converter.ToXml(xDocument));
        }
Beispiel #2
0
        void threadStart(object notUsed)
        {
            while (true)
            {
                //加载线程推动新明细,按钮交互推进明细的进度
                if (this.CurrentAsmTaskId != null && this.CurrentAsmTaskItemId == null)
                {
                    try
                    {
                        using (GeelyPtlEntities dbContext = new GeelyPtlEntities())
                        {
                            CFG_WorkStation cfgWorkStation = dbContext.CFG_WorkStations
                                                             .First(ws => ws.Id == this.CFG_WorkStationId);
                            ASM_Task asmTask = dbContext.ASM_Tasks
                                               .First(t => t.Id == this.CurrentAsmTaskId.Value);

                            //因为 IndicatingExecutorLoader 加载和 IndicatingExecutor 是不同线程,所以再次核对状态
                            if (asmTask.AssembleStatus == AssembleStatus.Finished)
                            {
                                this.CurrentAsmTaskId = null;

                                continue;
                            }

                            ASM_AssembleIndication asmAssembleIndication = asmTask.ASM_AssembleIndication;
                            ASM_TaskItem           asmTaskItem           = dbContext.ASM_TaskItems
                                                                           .OrderBy(ti => ti.Gzz)
                                                                           .ThenBy(ti => ti.AssembleSequence)
                                                                           .FirstOrDefault(ti => ti.ASM_TaskId == asmTask.Id &&
                                                                                           ti.AssembleStatus != AssembleStatus.Finished);
                            if (asmTaskItem == null)
                            {
                                asmTask.AssembleStatus = AssembleStatus.Finished;
                                asmAssembleIndication.AssembleStatus = asmTask.AssembleStatus;

                                #region 当前装配可以提交

                                List <ASM_AssembleIndicationItem> asmAssembleIndicatonItems = asmAssembleIndication.ASM_AssembleIndicationItems
                                                                                              .ToList();

                                ASM_AssembleResult asmAssembleResult = new ASM_AssembleResult();
                                asmAssembleResult.ASM_AssembleIndicationId = asmAssembleIndication.Id;
                                asmAssembleResult.FactoryCode        = asmAssembleIndication.FactoryCode;
                                asmAssembleResult.ProductionLineCode = asmAssembleIndication.ProductionLineCode;
                                asmAssembleResult.CFG_WorkStationId  = asmAssembleIndication.CFG_WorkStationId;
                                asmAssembleResult.GzzList            = asmAssembleIndication.GzzList;
                                asmAssembleResult.MONumber           = asmAssembleIndication.MONumber;
                                asmAssembleResult.ProductSequence    = asmAssembleIndication.ProductSequence;
                                asmAssembleResult.BeginTime          = asmTask.ASM_TaskItems.OrderBy(ati => ati.AssembledTime).First().AssembledTime.Value;
                                asmAssembleResult.EndTime            = asmTask.ASM_TaskItems.OrderBy(ati => ati.AssembledTime).Last().AssembledTime.Value;

                                dbContext.ASM_AssembleResults.Add(asmAssembleResult);

                                ASM_AssembleResultMessage asmAssembleResultMessage = new ASM_AssembleResultMessage();
                                asmAssembleResultMessage.ASM_AssembleResult = asmAssembleResult;
                                asmAssembleResultMessage.SentSuccessful     = false;

                                dbContext.ASM_AssembleResultMessages.Add(asmAssembleResultMessage);

                                foreach (ASM_AssembleIndicationItem asmAssembleIndicationItem in asmAssembleIndicatonItems)
                                {
                                    List <ASM_TaskItem> asmTaskItems = asmAssembleIndicationItem.ASM_TaskItems
                                                                       .ToList();
                                    ASM_TaskItem lastAsmTaskItem = asmTaskItems.Last();

                                    ASM_AssembleResultItem asmAssembleResultItem = new ASM_AssembleResultItem();
                                    asmAssembleResultItem.ASM_AssembleResult = asmAssembleResult;
                                    asmAssembleResultItem.CFG_CartId         = lastAsmTaskItem.CFG_CartId;
                                    asmAssembleResultItem.CartPosition       = lastAsmTaskItem.CartPosition;
                                    asmAssembleResultItem.Gzz                = asmAssembleIndicationItem.Gzz;
                                    asmAssembleResultItem.MaterialCode       = asmAssembleIndicationItem.MaterialCode;
                                    asmAssembleResultItem.MaterialName       = asmAssembleIndicationItem.MaterialName;
                                    asmAssembleResultItem.AssembleSequence   = asmAssembleIndicationItem.AssembleSequence;
                                    asmAssembleResultItem.ToAssembleQuantity = asmAssembleIndicationItem.ToAssembleQuantity;
                                    asmAssembleResultItem.AssembledQuantity  = asmTaskItems.Sum(ti => ti.AssembledQuantity.Value);
                                    asmAssembleResultItem.PickedTime         = lastAsmTaskItem.AssembledTime.Value;
                                    asmAssembleResultItem.ProjectCode        = asmAssembleIndicationItem.ProjectCode;
                                    asmAssembleResultItem.ProjectStep        = asmAssembleIndicationItem.ProjectStep;

                                    dbContext.ASM_AssembleResultItems.Add(asmAssembleResultItem);
                                }

                                #endregion

                                dbContext.SaveChanges();

                                this.CurrentAsmTaskId = null;

                                Logger.Log(this.GetType().Name + "." + cfgWorkStation.Code, DateTime.Now.ToString("HH:mm:ss") + " 装配任务完成:"
                                           + asmAssembleIndication.ProductSequence + ", "
                                           + asmAssembleIndication.GzzList + Environment.NewLine);
                            }
                            else
                            {
                                ASM_AssembleIndicationItem asmAssembleIndicationItem = asmTaskItem.ASM_AssembleIndicationItem;
                                CFG_Cart cfgCart = asmTaskItem.CFG_Cart;

                                //先更新数据库
                                asmTask.AssembleStatus                   = AssembleStatus.Assembling;
                                asmTaskItem.AssembleStatus               = AssembleStatus.Assembling;
                                asmAssembleIndication.AssembleStatus     = AssembleStatus.Assembling;
                                asmAssembleIndicationItem.AssembleStatus = AssembleStatus.Assembling;
                                cfgCart.CartStatus = CartStatus.Indicating;

                                dbContext.SaveChanges();

                                //再控制设备
                                CartPtl cartPtl          = CartPtlHost.Instance.GetCartPtl(asmTaskItem.CFG_CartId);
                                Ptl900U ptl900UPublisher = cartPtl.GetPtl900UPublisher();
                                Ptl900U ptl900U          = cartPtl.GetPtl900UByPosition(asmTaskItem.CartPosition);
                                Ptl900U ptl900ULight     = cartPtl.GetPtl900ULight();

                                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           = 0;

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

                                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.Pressed += this.ptl900UPublisher_Pressed;
                                ptl900UPublisher.Clear(true);
                                //ptl900UPublisher.Unlock();
                                ptl900UPublisher.Lock();
                                ptl900UPublisher.Display(publisherDisplay900UItem, LightColor.Off, true);

                                ptl900U.Pressed += this.ptl900U_Pressed;
                                ptl900U.Unlock();
                                ptl900U.Display(display900UItem, lightMode, true);

                                ptl900ULight.Clear();
                                ptl900ULight.Display(new Display900UItem(), lightMode, false);

                                this.CurrentAsmTaskItemId = asmTaskItem.Id;

                                Logger.Log(this.GetType().Name + "." + cfgWorkStation.Code, DateTime.Now.ToString("HH:mm:ss") + " 点亮装配明细:"
                                           + asmAssembleIndication.ProductSequence + ", "
                                           + asmAssembleIndication.GzzList + ", "
                                           + asmAssembleIndicationItem.MaterialCode + ", "
                                           + asmAssembleIndicationItem.MaterialName + ", "
                                           + cfgCart.Name + ", "
                                           + asmTaskItem.CartPosition + Environment.NewLine);

                                //如果新底盘抵达,则自动完成之前的
                                this.AutoPressByNewCarArrivedAsync(this.CurrentAsmTaskId.Value, this.CurrentAsmTaskItemId.Value, ptl900U, display900UItem);
                                //如果长时间无法交互,则自动交互
                                this.AutoPressByDeviceErrorAsync(this.CurrentAsmTaskItemId.Value, ptl900U, display900UItem);
                            }
                        }
                    }
                    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.threadStart", DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine
                                   + message + Environment.NewLine
                                   + Environment.NewLine);

                        Thread.Sleep(1000);
                    }
                }

                Thread.Sleep(1);
            }
        }