OutTaskHelper.cs 40.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
using AGVLib;
using CtuDeviceLib;
using CtuDeviceLib.manager;
using Mushiny;
using OnlineStore.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeviceLibrary.CtuService
{
    public class OutTaskHelper
    {
        /// <summary>
        /// 待机点找任务
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        public static CtuTaskBase FindTasks(CTU ctu)
        {
            CtuTaskBase newTask = null;
            if (ctu.AllBasketIsEmpty())
            {
                newTask = GetTakeBoxFromLineTask(ctu);
                if (newTask == null)
                {
                    newTask = GetNearestTakeBoxFromShelf(ctu);
                }
            }
            else
            {
                newTask = GetPutBoxTask(ctu);
            }
            return newTask;
        }

        /// <summary>
        /// ctu将背篓中的空箱放入当前库位
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        public static CtuTaskBase GetPutEmptyBoxToCurShelf(CTU ctu)
        {
            return null;
            //if (ctu.CtuTask?.dstPosInfo?.IsShelf() ?? false)//当前位置是货架
            //{
            //    //如果当前货架为空,放空箱
            //    PutBoxToShelfTask putEmptyBoxToShelfTask = null;
            //    ctu.GetFullBaskets()?.ForEach(basket =>
            //    {
            //        var posinfo = PosInfos.GetPosInfoByName(basket.DstName);
            //        if (posinfo != null)
            //        {
            //            if (posinfo.IsShelf())//入库空箱
            //            {
            //                if (putEmptyBoxToShelfTask == null)
            //                {
            //                    ctu.CtuTask?.MoveInfo.Info($"背篓料箱【{basket.BoxCode}】由【{basket.DstName}】转换为直接放入当前库位【{ctu.CtuTask.DstName}】");
            //                    basket.DstName = ctu.CtuTask.DstName;
            //                    putEmptyBoxToShelfTask = new PutBoxToShelfTask(ctu, basket.DstName, basket.BoxCode);
            //                }
            //            }
            //        }
            //    });
            //    return putEmptyBoxToShelfTask;
            //}
            //else
            //{
            //    return null;
            //}
        }


        /// <summary>
        /// 其他ctu是否在使用单巷道的
        /// </summary>
        /// <param name="targetPointCode"></param>
        /// <returns></returns>
        static bool IsOtherCtuUsedLimitOneCtuLaneWay(CTU ctu, uint targetPointCode, out string occupiedCtu)
        {
            bool otherCtuUsedTheLaneway = false;
            occupiedCtu = "";
            string occupiedCtu1 = "";
            if (PathWays.IsPathwayLimitOneCtu(targetPointCode))
            {
                //目的地巷道限制一辆车,目的地有车或者已经有任务为该巷道
                CTUManager.GetEnabledCTUs().ForEach(c =>
                {
                    if (!ctu.IP.Equals(c.IP))
                    {
                        if (!otherCtuUsedTheLaneway)
                        {
                            //先判断当前点是否是该巷道,再判断任务是否为该巷道
                            otherCtuUsedTheLaneway = PathWays.IsInSamePath(targetPointCode, c.CurLandMark);
                            if (!otherCtuUsedTheLaneway)
                            {
                                var taskDstName = c.CtuTask?.DstName;
                                if (!string.IsNullOrEmpty(taskDstName))
                                {
                                    var taskDstPos = PosInfos.GetPosInfoByName(taskDstName);
                                    if (taskDstPos != null)
                                    {
                                        otherCtuUsedTheLaneway = PathWays.IsInSamePath(targetPointCode, taskDstPos.PointCode);
                                        if (otherCtuUsedTheLaneway)
                                        {
                                            occupiedCtu1 = c.Name;
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
            occupiedCtu = occupiedCtu1;
            return otherCtuUsedTheLaneway;
        }


        /// <summary>
        /// 查找最近的上架料箱任务--目的地检查限制一辆车
        /// </summary>
        /// <returns></returns>
        public static CtuTaskBase GetNearestPutBoxToShelfTask(CTU ctu, out string occupiedCtu)
        {
            occupiedCtu = "";
            string occupiedCtu1 = "";
            CtuTaskBase nearstPutEmptyBoxToShelfTask = null;
            ctu.GetFullBaskets().ForEach(basket =>
            {
                var boxPosInfo = PosInfos.GetPosInfoByName(basket.DstName);
                if (boxPosInfo?.IsShelf() ?? false)
                {
                    bool otherCtuUsedTheLaneway = IsOtherCtuUsedLimitOneCtuLaneWay(ctu, boxPosInfo.PointCode, out occupiedCtu1);
                    if (!otherCtuUsedTheLaneway)
                    {
                        var rtn = PathManger.PathCalc(ctu, boxPosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                        if (rtn)
                        {
                            if (nearstPutEmptyBoxToShelfTask == null || PathPointCodes.Count < nearstPutEmptyBoxToShelfTask.planedPoints.Count)
                            {
                                nearstPutEmptyBoxToShelfTask = new PutBoxToShelfTask(ctu, basket.DstName, basket.BoxCode);
                                nearstPutEmptyBoxToShelfTask.planedPoints = PathPointCodes;
                            }
                        }
                    }

                }


            });
            occupiedCtu = occupiedCtu1;
            return nearstPutEmptyBoxToShelfTask;

        }


        public static CtuTaskBase GetTakeBoxFromLineTask(CTU currentCtu)
        {
            CtuTaskBase takeBoxToShelf = null;
            var otherCtuHasTakeLineTask = CTUManager.GetOhterEnabledCTUs(currentCtu)?.Find(s => s.CtuTask != null && s.CtuTask is TakeBoxFromLineTask);
            if (otherCtuHasTakeLineTask != null)//有车去接入库任务,暂不接该类型任务
            {
                return takeBoxToShelf;
            }
            if (TaskManager.HasInstoreBoxReady(out RobotTask robotTask))
            {
                takeBoxToShelf = new TakeBoxFromLineTask(currentCtu);
                takeBoxToShelf.CurTask = robotTask;
            }
            else if (TaskManager.HasInstoreTask(out robotTask))
            {
                takeBoxToShelf = new TakeBoxFromLineTask(currentCtu);
            }
            return takeBoxToShelf;
        }
        /// <summary>
        /// 放料箱到线体/放料箱到货架
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        public static CtuTaskBase GetPutBoxTask(CTU ctu)
        {
            // 先尝试放出库料箱
            CtuTaskBase putFullTask = GetPutBoxToLineTask(ctu);
            //将空箱上架
            CtuTaskBase putEmptyTask = GetPutBoxToShelfTask(ctu, out Basket basketHasBoxToOneCtuLanewayShelf);
            if (putFullTask != null)
            {
                if (putEmptyTask != null)
                {
                    //近的
                    PathManger.PathCalc(ctu, putFullTask.destination, out List<PointCode> fullPathPointCodes, out _);
                    PathManger.PathCalc(ctu, putEmptyTask.destination, out List<PointCode> emptyPathPointCodes, out _);
                    if (fullPathPointCodes.Count < emptyPathPointCodes.Count)
                    {
                        return putFullTask;
                    }
                    else
                    {
                        return putEmptyTask;
                    }
                }
                return putFullTask;
            }
            return putEmptyTask;
        }

        /// <summary>
        /// check-获取最近的入库任务或出库的任务
        /// 如果任务为空,resultMsg不为空,那么说明任务是在死胡同中或者没有背篓无法取满箱
        /// </summary>
        /// <param name="ctu"></param>
        /// <param name="resultMsg"></param>
        /// <returns></returns>
        public static CtuTaskBase GetNearestTakeBoxFromShelfOrPutBoxToShelfTask(CTU ctu, out string resultMsg)
        {
            resultMsg = "";

            //先找取满箱
            CtuTaskBase task = GetNearestTakeBoxFromShelf(ctu);
            Basket basketHasBoxToOneCtuLanewayShelf = null;
            if (task == null)
            {
                //将空箱上架
                task = GetPutBoxToShelfTask(ctu, out basketHasBoxToOneCtuLanewayShelf);
            }

            if (task != null)
            {
                return task;
            }
            //未找到任务,返回原因
            if (basketHasBoxToOneCtuLanewayShelf != null)
            {
                resultMsg = $"有料箱{basketHasBoxToOneCtuLanewayShelf.BoxCode}要上架,但目的地[{basketHasBoxToOneCtuLanewayShelf.DstName}]为死胡同,且有CTU在里面,暂不前往";
            }
            return null;
        }

        /// <summary>
        /// 获取背篓里需要取的出库料箱的尺寸和目的地,
        /// 出库到缓存线
        /// </summary>
        /// <param name="ctu"></param>
        /// <param name="boxDst"></param>
        /// <param name="boxSize"></param>
        static void getNeedTakeSizeAndDstInBasket(CTU ctu, out string boxDst, out string boxSize)
        {
            boxDst = "";
            boxSize = "";
            //先判断背篓中是否有箱子,取相同尺寸和目的地的箱子
            var outBasket = ctu.GetFullBaskets()?.Find(basket =>
            {
                if (Setting_Init.Out_FeedingInlet.Equals(basket.DstName))
                {
                    return true;
                }
                if (Setting_Init.rawLine_in.Equals(basket.DstName))
                {
                    return true;
                }
                return false;
            });

            if (outBasket != null)
            {
                boxSize = outBasket.BoxCode.Substring(0, 3);
                boxDst = outBasket.DstName;
            }
        }
        // static CtuTaskBase
        /// <summary>
        /// check-获取最近的出库任务
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        static CtuTaskBase GetNearestTakeBoxFromShelf(CTU ctu)
        {
            if (ctu.HasEmptyBasket())
            {
                getNeedTakeSizeAndDstInBasket(ctu, out string boxDst, out string boxSize);
                //当前巷道是否有任务

                //第一轮,优先找没有其他CTU占用的巷道中最近的任务
                List<PathWay> ctuUsedPathways = GetAllCtuUsedPathways(ctu);
                CtuTaskBase takeBoxFromShelfTask = FindNearestTakeBoxFromShelfTaskNotAtPathway(ctu, boxSize, boxDst, ctuUsedPathways);

                // 第二轮,找其他无故障CTU占用,但通道不是限制一台CTU的最近的任务
                if (takeBoxFromShelfTask == null)
                {
                    List<PathWay> noErroCtuUsedPathways = GetNoErrorCtuUsedPathways(ctu);
                    var noErrorCtuUsedNoLimitPathways = noErroCtuUsedPathways?.FindAll(p => p != null && !p.IsLimitOneCtu);
                    takeBoxFromShelfTask = FindNearestTakeBoxFromShelfTaskNotAtPathway(ctu, boxSize, boxDst, ctuUsedPathways);
                }
                //第三轮,找有故障CTU占用,但通道不是限制一台CTU的最近的任务
                if (takeBoxFromShelfTask == null)
                {
                    List<PathWay> erroCtuUsedLimitOneCtuPathway = ctuUsedPathways?.FindAll(pathway => pathway != null && pathway != null && !pathway.IsLimitOneCtu);
                    takeBoxFromShelfTask = FindNearestTakeBoxFromShelfTaskNotAtPathway(ctu, boxSize, boxDst, ctuUsedPathways);
                }

                if (takeBoxFromShelfTask != null)
                {
                    return takeBoxFromShelfTask;
                }
            }
            return null;

        }
        /// <summary>
        /// 该料箱是否在小车背篓
        /// </summary>
        /// <param name="boxCode"></param>
        /// <returns></returns>
        public static bool boxIsInBasket(string boxCode)
        {
            if (string.IsNullOrEmpty(boxCode)) return false;
            var rtn = CTUManager.GetEnabledCTUs()?.Find(s =>
              {
                  var boxInBasket = s.GetAllBaskets()?.Find(bb => boxCode.Equals(bb.BoxCode));
                  if (boxInBasket == null)
                      return false;
                  else
                  {
                      return true;
                  }
              });
            if (rtn != null) return true;
            return false;
        }

        /// <summary>
        /// check-
        /// </summary>
        /// <param name="ctu"></param>
        /// <param name="boxSize"></param>
        /// <param name="boxDst"></param>
        /// <param name="ctuUsedPathWays"></param>
        /// <returns></returns>
        static CtuTaskBase FindNearestTakeBoxFromShelfTaskNotAtPathway(CTU ctu, string boxSize, string boxDst, List<PathWay> ctuUsedPathWays)
        {
            CtuTaskBase takeFullBoxFromShelfTask = null;
            var fullBoxList = TaskUtil.Outlet_FilterOutStores()?.FindAll(s => !boxIsInBasket(s.boxCode));
            // FinishedCtuTask boxToTake = null;
            fullBoxList?.ForEach(outBoxTask =>
            {
                PosInfo boxPosInfo = PosInfos.GetPosInfoByName(outBoxTask.fromLoc);
                if (boxPosInfo != null)
                {
                    bool canTake = true;
                    if (!string.IsNullOrEmpty(boxSize))
                    {
                        bool sameSize = outBoxTask.boxCode.StartsWith(boxSize);
                        if (!sameSize)
                        {
                            canTake = false;
                        }
                    }
                    if (!string.IsNullOrEmpty(boxDst))
                    {
                        bool sameDst = outBoxTask.toLoc.Equals(boxDst);
                        if (!sameDst)
                        {
                            canTake = false;
                        }
                    }
                    var otherCtuUsedPathWay = ctuUsedPathWays?.Find(pathWay => pathWay != null && pathWay.Paths.Contains(boxPosInfo.PointCode));
                    if (otherCtuUsedPathWay != null)
                    {
                        canTake = false;
                    }
                    //不是手动线ctu,不接手动线出库
                    if (CTUManager.ManualGroup.IsInGroup(ctu.Name))
                    {
                        //手动线ctu任务都接
                    }
                    else
                    {
                        //只接分拣机任务
                        if (Setting_Init.rawLine_in.Equals(outBoxTask.toLoc))
                        {
                            canTake = false;
                        }
                    }
                    if (canTake)
                    {
                        //同尺寸和相同目的地才可以取
                        var rtn = PathManger.PathCalc(ctu, boxPosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                        if (rtn)
                        {
                            if (takeFullBoxFromShelfTask == null || PathPointCodes.Count < takeFullBoxFromShelfTask.planedPoints.Count)
                            {
                                takeFullBoxFromShelfTask = new TakeBoxFromShelfTask(ctu, TaskManager.GenRobotTask(outBoxTask));
                                takeFullBoxFromShelfTask.planedPoints = PathPointCodes;
                            }
                        }
                    }
                }
                else
                {
                    Msg.add($"库位[{outBoxTask.fromLoc}]不存在,无法执行出满箱任务", MsgLevel.warning);
                }

            });
            return takeFullBoxFromShelfTask;
        }

        /// <summary>
        /// 从SMF获取出库缓存线上箱子数量
        /// </summary>
        /// <param name="ctu"></param>
        /// <param name="totalBoxCount"></param>
        /// <param name="emptyBoxCount"></param>
        public static void GetOutLineBoxCountFromSmf(string outLineName, out int totalBoxCount, out int emptyBoxCount, out int reelNumToOut)
        {
            GetBoxListAtOutLet(outLineName, out totalBoxCount, out emptyBoxCount, out reelNumToOut);
        }

        /// <summary>
        /// check-前提是ctu在待机点,将CTU上的满箱放到出库缓存线上,如果有任务,但返回空,那么只能是线体不允许放(线体满了或者线体程序出问题了)
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        public static CtuTaskBase GetPutBoxToLineTask(CTU ctu)
        {
            var basketToPutOnLine = ctu.GetFullBaskets()?.Find(s => Setting_Init.rawLine_in.Equals(s.DstName));
            if (basketToPutOnLine != null)
            {
                return new PutBoxToLineTask(ctu, Setting_Init.rawLine_in, basketToPutOnLine.BoxCode);
            }
            return null;
        }

        /// <summary>
        /// 查找当前巷道的任务
        /// </summary>
        /// <param name="ctu"></param>
        /// <returns></returns>
        public static CtuTaskBase FindCurLanePutEmptyBoxTask(CTU ctu)
        {
            CtuTaskBase putBoxToShelfTask = null;
            ctu.GetFullBaskets()?.ForEach(basket =>
            {
                if (!basket.Shield && !string.IsNullOrEmpty(basket.BoxCode))
                {
                    var boxPosInfo = PosInfos.GetPosInfoByName(basket.DstName);
                    if (boxPosInfo?.IsShelf() ?? false)
                    {
                        if (PathWays.IsInSamePath(ctu.CurLandMark, boxPosInfo.PointCode))
                        {
                            //找背篓中箱子目的地距离最近的
                            var rtn = PathManger.PathCalc(ctu, boxPosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                            if (rtn)
                            {
                                if (putBoxToShelfTask == null || PathPointCodes.Count < putBoxToShelfTask.planedPoints.Count)
                                {
                                    putBoxToShelfTask = new PutBoxToShelfTask(ctu, boxPosInfo.Name, basket.BoxCode);
                                    putBoxToShelfTask.planedPoints = PathPointCodes;
                                }
                            }
                        }
                    }
                }
            });
            return putBoxToShelfTask;
        }

        /// <summary>
        /// check-寻找最近背篓中的空箱上架任务,如果路径中有满箱要取,先取满箱, 
        /// 如果返回值为null,需要判断中是否还有空箱需要上架,如果有,那么表示此箱子的目的地在限制一台CTU的死胡同中
        /// </summary>
        /// <returns></returns>
        static CtuTaskBase GetPutBoxToShelfTask(CTU ctu, out Basket basketHasBoxToOneCtuLanewayShelf)
        {
            basketHasBoxToOneCtuLanewayShelf = null;
            //需要上架
            //找当前巷道任务
            CtuTaskBase putBoxToShelfTask = FindCurLanePutEmptyBoxTask(ctu);


            //第一轮,优先找没有其他CTU占用的巷道中最近的任务
            if (putBoxToShelfTask == null)
            {
                List<PathWay> ctuUsedPathways = GetAllCtuUsedPathways(ctu);
                putBoxToShelfTask = FindNearestPutEmptyBoxToShelfTaskNotAtPathway(ctu, ctuUsedPathways);

                //第二轮,找其他无故障CTU占用,但通道不是限制一台CTU的最近的任务
                if (putBoxToShelfTask == null)
                {
                    List<PathWay> noErroCtuUsedPathways = GetNoErrorCtuUsedPathways(ctu);
                    var noErrorCtuUsedNoLimitPathways = noErroCtuUsedPathways?.FindAll(p => p != null && !p.IsLimitOneCtu);
                    putBoxToShelfTask = FindNearestPutEmptyBoxToShelfTaskNotAtPathway(ctu, noErrorCtuUsedNoLimitPathways);
                }
                //第三轮,找有故障CTU占用,但通道不是限制一台CTU的最近的任务
                if (putBoxToShelfTask == null)
                {
                    List<PathWay> erroCtuUsedLimitOneCtuPathway = ctuUsedPathways?.FindAll(pathway => pathway != null && !pathway.IsLimitOneCtu);
                    putBoxToShelfTask = FindNearestPutEmptyBoxToShelfTaskNotAtPathway(ctu, ctuUsedPathways);
                }

            }
            if (putBoxToShelfTask != null)
            {
                if (ctu.HasEmptyBasket())
                {
                    //在路径上找是否有取箱子的任务
                    var takeFullBoxTaskInPath = FindTakeFullBoxTaskInPath(ctu, putBoxToShelfTask.planedPoints);
                    if (takeFullBoxTaskInPath != null)
                    {
                        return takeFullBoxTaskInPath;
                    }
                }

                //没有空背篓或者路上没有找到取箱子的任务,直接放箱子到架子
                return putBoxToShelfTask;
            }

            //判断背篓中有没有待上架的箱子, 如果有,那么此箱子的目的地肯定是在死胡同巷道
            basketHasBoxToOneCtuLanewayShelf = ctu.GetFullBaskets().Find(basket =>
            {
                if (!basket.Shield && !string.IsNullOrEmpty(basket.BoxCode))
                {
                    var boxPosInfo = PosInfos.GetPosInfoByName(basket.DstName);
                    if (boxPosInfo?.IsShelf() ?? false)
                    {
                        return true;
                    }
                }
                return false;
            });
            return null;
        }

        /// <summary>
        /// check-获取其他无故障CTU占用的巷道
        /// </summary>
        /// <returns></returns>
        static List<PathWay> GetNoErrorCtuUsedPathways(CTU ctu)
        {
            List<PathWay> ctuUsedPathways = new List<PathWay>();
            CTUManager.GetEnabledCTUs().ForEach(c =>
            {
                if (!c.Name.Equals(ctu.Name))
                {
                    if (!c.FaultInfo.HasFault)
                    {
                        var pathWay = PathWays.GetPathWay(c.CurLandMark);
                        if (pathWay != null && pathWay.IsLimitOneCtu)
                        {
                            ctuUsedPathways.Add(pathWay);
                        }
                        var taskDstName = c.CtuTask?.DstName;
                        if (!string.IsNullOrEmpty(taskDstName))
                        {
                            var taskDstPos = PosInfos.GetPosInfoByName(taskDstName);
                            if (taskDstPos != null)
                            {
                                var taskPathWay = PathWays.GetPathWay(taskDstPos.PointCode);
                                if (taskPathWay != null && taskPathWay.IsLimitOneCtu)
                                {
                                    ctuUsedPathways.Add(taskPathWay);
                                }
                            }
                        }
                    }
                }


            });
            return ctuUsedPathways?.FindAll(s => s != null && !s.Paths.Contains(ctu.CurLandMark));
        }

        /// <summary>
        /// check-获取所有CTU(有故障或无故障)当前在的和即将去(有任务)的巷道
        /// </summary>
        static List<PathWay> GetAllCtuUsedPathways(CTU ctu)
        {
            List<PathWay> ctuUsedPathways = new List<PathWay>();
            CTUManager.GetEnabledCTUs().ForEach(c =>
            {
                if (!c.Name.Equals(ctu.Name))
                {
                    var pathWay = PathWays.GetPathWay(c.CurLandMark);
                    if (pathWay != null)
                    {
                        ctuUsedPathways.Add(pathWay);
                    }
                    var taskDstName = c.CtuTask?.DstName;
                    if (!string.IsNullOrEmpty(taskDstName))
                    {
                        var taskDstPos = PosInfos.GetPosInfoByName(taskDstName);
                        if (taskDstPos != null)
                        {
                            var taskPathWay = PathWays.GetPathWay(taskDstPos.PointCode);
                            if (taskPathWay != null)
                            {
                                ctuUsedPathways.Add(taskPathWay);
                            }
                        }
                    }
                }

            });
            return ctuUsedPathways?.FindAll(s => s != null && !s.Paths.Contains(ctu.CurLandMark));
        }


        /// <summary>
        /// check-寻找没有在指定巷道中最近的空箱上架任务
        /// </summary>
        /// <param name="ctu"></param>
        /// <param name="ctuUsedPathWays"></param>
        /// <returns></returns>
        static CtuTaskBase FindNearestPutEmptyBoxToShelfTaskNotAtPathway(CTU ctu, List<PathWay> ctuUsedPathWays)
        {

            CtuTaskBase putBoxToShelfTask = null;
            bool putEmptyBoxToShelfWithEmptyPos = ConfigAppSettings.GetValue("Enable_OutletPutBoxToShelfUseEmptyPos", false, "出库分拣空箱入库位使用从smf获取的空库位直接入库");
            //从背篓里是否有入库料箱,有则获取最近不在库库位,排除屏蔽的巷道
            //排除其他ctu执行当前的库位
            if (putEmptyBoxToShelfWithEmptyPos)
            {
                var fullBasket = ctu.GetFirstFullBasket();
                if (fullBasket != null)//背篓有料箱
                {
                    var boxPosInfo = PosInfos.GetPosInfoByName(fullBasket.DstName);
                    if (boxPosInfo?.IsShelf() ?? false)//入库空箱
                    {
                        List<string> emptyPos = TaskUtil.EmptyPos?.ToList();
                        {
                            if (emptyPos != null && emptyPos.Count > 0)
                            {
                                emptyPos.ForEach(ePos =>
                                {
                                    var emptyPosInfo = PosInfos.GetPosInfoByName(ePos);
                                    if (emptyPosInfo != null)
                                    {
                                        if (!TaskUtil.disabledLancode.Contains(emptyPosInfo.Lanway))//排除屏蔽巷道
                                        {
                                            PathWay otherCtuUsedPathWay = ctuUsedPathWays?.Find(pathway => pathway != null && pathway.Paths.Contains(emptyPosInfo.PointCode));
                                            if (otherCtuUsedPathWay == null)//无其他ctu在该巷道
                                            {
                                                var otherCtus = CTUManager.GetOhterEnabledCTUs(ctu);
                                                var ctuTaskInThisPos = otherCtus?.Find(s => s.CtuTask != null && ePos.Equals(s.CtuTask.DstName));
                                                if (ctuTaskInThisPos == null)//无ctu执行该库位的任务
                                                {
                                                    //巷道未被其他CTU使用,可以去这个巷道放箱子,找空库位中距离当前位置距离最近的
                                                    var rtn = PathManger.PathCalc(ctu, emptyPosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                                                    if (rtn)
                                                    {
                                                        if (putBoxToShelfTask == null || PathPointCodes.Count < putBoxToShelfTask.planedPoints.Count)
                                                        {
                                                            putBoxToShelfTask = new PutBoxToShelfTask(ctu, emptyPosInfo.Name, fullBasket.BoxCode);
                                                            putBoxToShelfTask.planedPoints = PathPointCodes;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                        }

                    }
                    if (putBoxToShelfTask != null)
                    {
                        LogUtil.info($"空箱入库使用最近空库位:【{fullBasket.BoxCode}】由【{fullBasket.DstName}】改为【{putBoxToShelfTask.DstName}】");
                        fullBasket.DstName = putBoxToShelfTask.DstName;
                    }
                }

            }
            else
            {
                ctu.GetFullBaskets().ForEach(basket =>
                {
                    if (!basket.Shield && !string.IsNullOrEmpty(basket.BoxCode))
                    {
                        var boxPosInfo = PosInfos.GetPosInfoByName(basket.DstName);
                        if (boxPosInfo?.IsShelf() ?? false)
                        {
                            PathWay otherCtuUsedPathWay = ctuUsedPathWays?.Find(pathway => pathway != null && pathway.Paths.Contains(boxPosInfo.PointCode));
                            if (otherCtuUsedPathWay == null)
                            {
                                //巷道未被其他CTU使用,可以去这个巷道放箱子,找背篓中箱子目的地距离最近的
                                var rtn = PathManger.PathCalc(ctu, boxPosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                                if (rtn)
                                {
                                    if (putBoxToShelfTask == null || PathPointCodes.Count < putBoxToShelfTask.planedPoints.Count)
                                    {
                                        putBoxToShelfTask = new PutBoxToShelfTask(ctu, boxPosInfo.Name, basket.BoxCode);
                                        putBoxToShelfTask.planedPoints = PathPointCodes;
                                    }
                                }
                            }
                        }
                    }
                });
            }

            return putBoxToShelfTask;
        }

        /// <summary>
        /// check-在路径上查找取满箱任务
        /// </summary>
        /// <param name="PathPointCodes"></param>
        /// <returns></returns>
        static CtuTaskBase FindTakeFullBoxTaskInPath(CTU ctu, List<PointCode> PathPointCodes)
        {
            CtuTaskBase takeFullBoxTask = null;
            var emptyBasketCount = ctu.GetEnabledBaskets().Count(b => !b.Shield && string.IsNullOrEmpty(b.BoxCode));
            //必须有空背篓才可以取, 不然不生成取满箱出库任务
            if (emptyBasketCount > 0)
            {
                getNeedTakeSizeAndDstInBasket(ctu, out string boxDst, out string boxSize);
                var fullBoxList = TaskUtil.Outlet_FilterOutStores()?.FindAll(s => !boxIsInBasket(s.boxCode));
                FinishedCtuTask boxToTake = null;
                fullBoxList?.ForEach(b =>
                {
                    var canTake = true;
                    //不是手动线ctu,不接手动线出库
                    if (CTUManager.ManualGroup.IsInGroup(ctu.Name))
                    {
                        //手动线ctu任务都接
                    }
                    else
                    {
                        //只接分拣机任务
                        if (Setting_Init.rawLine_in.Equals(b.toLoc))
                        {
                            canTake = false;
                        }
                    }
                    if (canTake)
                    {
                        var boxPos = PosInfos.GetPosInfoByName(b.fromLoc);
                        if (boxPos != null)
                        {
                            var index = PathPointCodes.FindIndex(s => s.Id == boxPos.PointCode);
                            if (index != -1)
                            {
                                if (string.IsNullOrEmpty(boxSize))//背篓无料箱
                                {
                                    //该箱子在路径上,先取这个箱子
                                    PathPointCodes = PathPointCodes.Take(index + 1).ToList();
                                    boxToTake = b;
                                }
                                else if (b.boxCode.StartsWith(boxSize) && b.toLoc.Equals(boxDst))
                                {
                                    PathPointCodes = PathPointCodes.Take(index + 1).ToList();
                                    boxToTake = b;
                                }
                            }
                        }
                        else
                        {
                            Msg.add($"库位[{b.fromLoc}]不存在,无法执行出满箱任务", MsgLevel.warning);
                        }
                    }


                });
                if (boxToTake != null)
                {
                    takeFullBoxTask = new TakeBoxFromShelfTask(ctu, TaskManager.GenRobotTask(boxToTake));
                }
            }
            return takeFullBoxTask;
        }
        static List<ClientNode> filterRawLineOut(List<ClientNode> clientNodes)
        {
            return clientNodes.FindAll(s =>
                        ((s.name.StartsWith(Setting_Init.outLine) && s.name.EndsWith(Setting_Init.dir_out)) ||
                        (Setting_Init.rawLine_out.Equals(s.name))));
        }
        /// <summary>
        /// 获取当前CTU可取空料箱出料线体的最近节点任务-v1
        /// </summary>
        /// <returns></returns>
        public static CtuTaskBase GetNearstTakeBoxFromLineTask(CTU ctu)
        {
            CtuTaskBase currentCtuNearestTakeFullBoxTask = null;

            //距离当前节点最近的CTU任务
            CtuTaskBase nearestNodeTakeEmptyBoxTask = null;
            var nodePosInfo = PosInfos.GetPosInfoByName(Setting_Init.rawLine_out);
            if (nodePosInfo != null)
            {
                var ctuAtThisPoint = false;
                var otherCtus = CTUManager.GetOhterEnabledCTUs(ctu);
                if (otherCtus != null && otherCtus.Count > 0)
                {
                    var find = otherCtus.Find(s => s.CurLandMark == nodePosInfo.PointCode || (s.CtuTask != null && s.CtuTask.destination == nodePosInfo.PointCode));
                    ctuAtThisPoint = find != null;
                }
                if (!ctuAtThisPoint)
                {
                    CTUManager.GetEnabledCTUs().ForEach(c =>
                    {
                        if (c.Enabled)
                        {
                            var hasNeedLeave = true;
                            if (TaskManager.HasInstoreTask(out RobotTask inTask))
                            {
                                if (inTask != null)
                                {
                                    hasNeedLeave = true;
                                }
                                else
                                {
                                    hasNeedLeave = false;
                                }
                            }
                            if (hasNeedLeave)
                            {
                                if (c.HasEmptyBasket())
                                {
                                    var rtn = PathManger.PathCalc(c, nodePosInfo.PointCode, out List<PointCode> PathPointCodes, out string errmsg);
                                    if (rtn)
                                    {
                                        if (nearestNodeTakeEmptyBoxTask == null || PathPointCodes.Count < nearestNodeTakeEmptyBoxTask.planedPoints.Count)
                                        {
                                            nearestNodeTakeEmptyBoxTask = new TakeBoxFromLineTask(c);
                                            nearestNodeTakeEmptyBoxTask.planedPoints = PathPointCodes;
                                        }
                                    }
                                }
                            }
                            else //没有需要离开的空箱任务
                            {

                            }

                        }

                    });
                }
            }
            if (nearestNodeTakeEmptyBoxTask != null && nearestNodeTakeEmptyBoxTask.ctu.IP.Equals(ctu.IP))
            {
                //可取这个节点满箱的CTU就是当前的CTU,与已有任务进行比对,看是否比之前的任务更近
                if (currentCtuNearestTakeFullBoxTask == null || nearestNodeTakeEmptyBoxTask.planedPoints.Count < currentCtuNearestTakeFullBoxTask.planedPoints.Count)
                {
                    currentCtuNearestTakeFullBoxTask = nearestNodeTakeEmptyBoxTask;
                }
            }
            return currentCtuNearestTakeFullBoxTask;
        }

        /// <summary>
        /// 出库缓存线是否允许放满箱
        /// </summary>
        public static bool canPutBoxToLine(string outletLineName, string boxCode, out string msg)
        {
            msg = "";
            if (string.IsNullOrEmpty(outletLineName)) return true;
            if (!outletLineName.StartsWith(Setting_Init.outLine)) return true;
            string outLineServerBase = ConfigAppSettings.GetValue($"{outletLineName}_ServerBase", "http://127.0.0.1");
            string lineNo = outletLineName.Replace("_out", "").Replace("_in", "");//线体name
            var rtn = HttpManager.CanPutBoxToLine(outLineServerBase, lineNo, boxCode, out msg);
            return rtn;
        }

        /// <summary>
        /// 出库缓存线是否允许放空箱
        /// </summary>
        public static bool canTakeBoxFromLine(string outletLineName, string boxCode)
        {
            //location= outLine01
            if (string.IsNullOrEmpty(outletLineName)) return true;
            if (!outletLineName.StartsWith(Setting_Init.outLine)) return true;
            string outLineServerBase = ConfigAppSettings.GetValue($"{outletLineName}_ServerBase", "http://127.0.0.1");
            string lineNo = outletLineName.Replace("_out", "").Replace("_in", "");//线体name
            var rtn = HttpManager.CanTakeBoxFromLine(outLineServerBase, lineNo, boxCode, out string msg);
            return rtn;
        }



        /// <summary>
        /// 出库缓存线上有多少个箱子
        /// </summary>
        /// <returns></returns>
        private static bool GetBoxListAtOutLet(string outLinename, out int totalBoxCount, out int emptyBoxCount, out int reelNumToOut)
        {
            totalBoxCount = 0;
            emptyBoxCount = 0;
            reelNumToOut = 0;
            var rawoutBoxes = new List<RawOutBoxInfo>();
            if (outLinename.StartsWith(Setting_Init.outLine))
            {
                outLinename = outLinename.Replace("_out", "_in");
                if (RobotManager.BoxListAtOutLetBuff.ContainsKey(outLinename))
                {
                    totalBoxCount = RobotManager.BoxListAtOutLetBuff[outLinename].TotalCnt;
                    emptyBoxCount = RobotManager.BoxListAtOutLetBuff[outLinename].EmptyCnt;
                    reelNumToOut = RobotManager.BoxListAtOutLetBuff[outLinename].WaitOutReelCnt;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}