ComnMethod.cs 105 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 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
using Infrastructure;
using RCS.Dal;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Service;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows;
using static RCS.Model.Comm.EnumMsg;
using LinqExpressions = System.Linq.Expressions;

namespace RCS.WinClient.Common
{
    public class ComnMethod
    {
        private static readonly object obj = new object();
        private static readonly object logLockObj = new object();
        /// <summary>
        /// 校验码验证
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="length">长度</param>
        /// <returns>校验结果</returns>
        public static byte CheckCode(byte[] data, int length)
        {
            byte crc = (byte)(data[0] ^ data[1]);
            for (int i = 2; i < length; i++)
            {
                crc = (byte)(crc ^ data[i]);
            }
            return crc;
        }


        /// <summary>
        /// 获取小车错误
        /// </summary>
        /// <param name="errorList">Bit数组,每一位的True表示报警,False表示正常</param>
        /// <returns></returns>
        public static string GetAgvError(BitArray errorArray)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < errorArray.Length; i++)
            {
                if (errorArray[i])
                {
                    sb.Append(App.Config_ErrMsgList[i]);
                }
            }
            return sb.ToString();
        }

        private static IEnumerable<AGVErrorCode> ErrorCodes = Enum.GetValues(typeof(AGVErrorCode)).Cast<AGVErrorCode>();
        /// <summary>
        /// 获取小车错误
        /// </summary>
        /// <param name="errorList">Bit数组,每一位的True表示报警,False表示正常</param>
        /// <returns></returns>
        public static string GetAgvError(AGVErrorCode errorList)
        {
            var sb = new StringBuilder();

            foreach (AGVErrorCode errorCode in ErrorCodes)
            {
                if (errorCode == AGVErrorCode.运行
                    || errorCode == AGVErrorCode.地面二维码对中
                    || errorCode == AGVErrorCode.目标停车
                    || errorCode == AGVErrorCode.货架二维码对中
                    ) continue;
                //由于方法调用频繁暂不使用HasFlag
                if ((errorList & errorCode) == errorCode)
                {
                    sb.Append(errorCode);
                    sb.Append(';');
                }
            }
            if (sb.Length > 1)
            {
                sb.Length--;
            }

            return sb.ToString();
        }

        /// <summary>
        /// 二进制的int数组转字符串
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetString(int[] num)
        {
            string str = "";
            if (num.Length != 8) return str;
            for (int i = 7; i >= 0; i--)
            {
                str += num[i];
            }
            return str;
        }

        /// <summary>
        /// 获取流水号
        /// </summary>
        /// <returns></returns>
        public static BllResult<string> GetTaskNo()
        {
            lock (obj)
            {
                string taskNo = "";
                var _tno = new BaseDal<Config_KeyValue>();
                var queryResult = _tno.Query(it => it.KeyVariable == "Tno");
                if (queryResult.IsSuccess && queryResult.Data != null)
                {
                    taskNo = queryResult.Data[0].Value;
                }
                else
                {
                    return BllResult<string>.Error("查询任务号失败,字典表没有任务号!");
                }
                //获取当前日期
                string dateNow = DateTime.Now.ToString("yyyyMMdd");
                if (string.IsNullOrEmpty(taskNo) || taskNo.Substring(0, 8) != dateNow)
                {
                    taskNo = DateTime.Now.ToString("yyyyMMdd") + "000001";
                }
                else
                {
                    int index = int.Parse(taskNo.Substring(8, 6)) + 1;
                    taskNo = taskNo.Substring(0, 8) + index.ToString().PadLeft(6, '0');
                }
                var tNo = new Config_KeyValue()
                {
                    ID = queryResult.Data[0].ID,
                    KeyVariable = queryResult.Data[0].KeyVariable,
                    KeyDescribe = queryResult.Data[0].KeyDescribe,
                    Value = taskNo
                };
                var updateResult = _tno.Update(tNo);
                if (updateResult.IsSuccess)
                {
                    return BllResult<string>.Success(taskNo, "");
                }
                else
                {
                    return BllResult<string>.Error($"更新任务号失败!原因:{updateResult.Message}");
                }
            }
        }

        /// <summary>
        /// 获取时间差(单位为秒,返回值带小数)
        /// </summary>
        /// <param name="dateTime">开始时间</param>
        /// <returns></returns>
        public static double DiffTime(DateTime dateTime)
        {
            double num = DateTime.Now.Subtract(dateTime).TotalSeconds;
            return num;
        }

        /// <summary>
        /// 获取小车停止原因
        /// </summary>
        /// <param name="oldstr"></param>
        /// <param name="newstr"></param>
        /// <returns></returns>
        public static string GetStopReason(string oldstr, string newstr)
        {
            if (oldstr == null) oldstr = "";
            if (newstr == null) newstr = "";

            if (newstr == "") return "";
            if (oldstr.Contains(newstr)) return oldstr;
            if (oldstr == "")
                return newstr;
            return oldstr + ";\n" + newstr;
        }

        /// <summary>
        /// 查找当前点的路径下一点
        /// </summary>
        /// <param name="lastAgv"></param>
        /// <param name="nowPoint"></param>
        /// <returns></returns>
        public static Base_PathPoint? GetNextPathPoint(Base_Agv agv, Base_PathPoint? pathPoint)
        {
            if (pathPoint == null || agv.PathPointList.Count == 0)
            {
                return null;
            }
            var nextPoint = agv.PathPointList.Find(a => a.Point.Barcode == pathPoint.Point.Barcode);
            if (nextPoint == null)
            {
                return null;
            }
            var nextPointNew = agv.PathPointList.Find(a => a.SerialNo == nextPoint.SerialNo + 1);
            if (nextPointNew == null)
            {
                return null;
            }
            return nextPointNew;
        }


        /// <summary>
        /// 查找当前点的路径下一点
        /// </summary>
        /// <param name="lastAgv"></param>
        /// <param name="nowPoint"></param>
        /// <returns></returns>
        public static Base_PathPoint? GetNextPathPoint(Base_Agv agv, string barcode)
        {
            var subTaskList = agv?.AgvTask?.SubTaskList;
            var nextAgvSonTask = subTaskList?.FirstOrDefault();
            var pathPointList = nextAgvSonTask?.PathPointList;

            if (pathPointList == null || pathPointList.Count == 0) return null;

            Base_PathPoint? currPoint = pathPointList.Find(a => a.Point.Barcode == barcode);
            if (currPoint == null) return null;
            Base_PathPoint? nextPointNew = pathPointList.Find(a => a.SerialNo == currPoint.SerialNo + 1);

            return nextPointNew;
        }

        /// <summary>
        /// 获取路径点
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Base_PathPoint GetPathPoint(Base_Point point)
        {
            var pathPoint = new Base_PathPoint
            {
                Point = point
            };
            return pathPoint;
        }

        /// <summary>
        /// 获取最近的AGV
        /// </summary>
        /// <param name="startPoint"></param>
        /// <returns></returns>
        public static Base_Agv? GetNearAgv(Base_Point startPoint, Base_Task task, List<Base_Agv>? agvs = null)
        {
            Base_Agv? nearAgv = null;
            //获取当前任务群组的所能派遣的小车任务群组
            var agvGroup = App.TaskAgvGroupList.FindAll(a => a.TaskType == task.TaskType);

            //查找所有状态满足的小车:启用、在线、无故障、无任务、自动空闲、无货、电量不是危险电量
            var agvList = agvs ?? App.AgvList;

            //获取群组中所有能执行任务的agv
            agvList = agvList.Where(a => agvGroup.Any(x => x.AgvGroup == a.Group) && a.IsCanExecTask()).ToList();


            if (agvList.Count == 0) return nearAgv;

            //查找距离起点最近agv
            var agvPoints = App.PointList.FindAll(_point => agvList.Any(_agv => _agv.Barcode == _point.Barcode));
            var closePoint = GetClosestPoint(agvList.First(), agvPoints, task.StartPoint);
            if (closePoint != null)
            {
                nearAgv = agvList.Find(x => x.Barcode == closePoint.Barcode);
            }
            return nearAgv;
        }

        /// <summary>
        /// 计算两点距离
        /// </summary>
        /// <param name="intX1"></param>
        /// <param name="intY1"></param>
        /// <param name="intX2"></param>
        /// <param name="intY2"></param>
        /// <returns></returns>
        public static int GetMinPoint(int intX1, int intY1, int intX2, int intY2)
        {
            int absXpos = Math.Abs(intX1 - intX2);
            int absYpos = Math.Abs(intY1 - intY2);
            return absXpos + absYpos;
        }

        /// <summary>
        /// 计算一条线上的两点距离
        /// </summary>
        /// <param name="oldBarcode">当前码值</param>
        /// <param name="newBarcode">下个码值</param>
        /// <param name="pathDirection">行驶方向</param>
        /// <returns></returns>
        public static int GetDistance(Base_Point oldPoint, Base_Point newPoint, EnumMsg.Direction pathDirection)
        {
            int distance = 0;
            if (oldPoint == null || newPoint == null) return distance;
            // 地图的X轴就是车的Y坐标
            if (pathDirection == EnumMsg.Direction.X正方向 || pathDirection == EnumMsg.Direction.X负方向)
            {
                distance = Math.Abs(newPoint.YLength - oldPoint.YLength);
            }
            // 地图的Y轴就是车的X坐标 
            else if (pathDirection == EnumMsg.Direction.Y正方向 || pathDirection == EnumMsg.Direction.Y负方向)
            {
                distance = Math.Abs(newPoint.XLength - oldPoint.XLength);
            }
            return distance;
        }

        /// <summary>
        /// 记录行驶前后两点距离
        /// </summary>
        /// <param name="oldBarcode">当前码值</param>
        /// <param name="newBarcode">下个码值</param>
        /// <returns></returns>
        public static int GetDistance(string oldBarcode, string newBarcode)
        {
            int distance = 0;
            var oldPoint = App.PointList.Find(a => a.Barcode == oldBarcode);
            var newPoint = App.PointList.Find(a => a.Barcode == newBarcode);
            if (oldPoint == null || newPoint == null) return distance;
            if (oldPoint.IntX == newPoint.IntX)
            {
                //往Y方向行驶距离
                distance = Math.Abs(newPoint.YLength - oldPoint.YLength);
            }
            else if (oldPoint.IntY == newPoint.IntY)
            {
                //往X方向行驶距离
                distance = Math.Abs(newPoint.XLength - oldPoint.XLength);
            }
            return distance;
        }

        /// <summary>
        /// 记录行驶前后两点距离
        /// </summary>
        /// <param name="oldBarcode">当前码值</param>
        /// <param name="newBarcode">下个码值</param>
        /// <param name="pathDirection">行驶方向</param>
        /// <returns></returns>
        public static int GetDistance(string oldBarcode, string newBarcode, EnumMsg.Direction pathDirection)
        {
            int distance = 0;
            var oldPoint = App.PointList.Find(a => a.Barcode == oldBarcode);
            var newPoint = App.PointList.Find(a => a.Barcode == newBarcode);
            if (oldPoint == null || newPoint == null) return distance;
            // 具体计算方式:X正方向行走的时候,比如 103 ==> 102,会IntX变大,IntY不变,XLength不变,YLength变小,也就是说小车把地图的Y坐标当X坐标来行走的,而且是反向。 
            if (pathDirection == EnumMsg.Direction.X正方向 || pathDirection == EnumMsg.Direction.X负方向)
            {
                //往X方向行驶距离
                distance = Math.Abs(newPoint.YLength - oldPoint.YLength);
            }
            // 具体计算方式:Y正方向行走的时候,比如 102 ==> 202,会IntX不变,IntY变大,XLength变大,YLength不变,也就是说小车把地图的X坐标当Y坐标来行走的,而且是同向。 
            else if (pathDirection == EnumMsg.Direction.Y正方向 || pathDirection == EnumMsg.Direction.Y负方向)
            {
                //往Y方向行驶距离
                distance = Math.Abs(newPoint.XLength - oldPoint.XLength);
            }
            return distance;
        }

        /// <summary>
        /// 计算点到线段的距离
        /// </summary>
        /// <param name="A">线段端点</param>
        /// <param name="B">线段端点</param>
        /// <param name="P">判断点</param>
        /// <returns></returns>
        public static double GetPointDistanceToLineSegment(Base_Point A, Base_Point B, Base_Point P)
        {

            double x0 = P.XLength;
            double y0 = P.YLength;
            double x1 = A.XLength;
            double y1 = A.YLength;
            double x2 = B.XLength;
            double y2 = B.YLength;
            double dx = x2 - x1;
            double dy = y2 - y1;

            double dotProduct = ((x0 - x1) * dx + (y0 - y1) * dy) / (dx * dx + dy * dy);

            double xp, yp;

            if (dotProduct < 0)
            {
                xp = x1;
                yp = y1;
            }
            else if (dotProduct > 1)
            {
                xp = x2;
                yp = y2;
            }
            else
            {
                xp = x1 + dotProduct * dx;
                yp = y1 + dotProduct * dy;
            }

            // 计算目标点到投影点的距离
            double distance = Math.Sqrt(Math.Pow(x0 - xp, 2) + Math.Pow(y0 - yp, 2));

            return distance;
        }


        /// <summary>
        /// 计算小车多久未发送心跳
        /// </summary>
        public static void HeartStop(Base_Agv agv)
        {
            var Sec = DiffTime(agv.Heart);
            if (Sec > 10)//大于30秒,判定离线
            {
                agv.IsOnline = false;
                //已经关机的车移除待关机车辆集合
                if (App.OffAgvNos.Contains(agv.AgvNo))
                {
                    App.OffAgvNos.Remove(agv.AgvNo);
                }
                if (agv.IsEnable)
                {
                    if (agv.AgvState != EnumMsg.AGVState.离线)
                    {
                        agv.AgvState = EnumMsg.AGVState.离线;
                        UpdateManage.UpdateAgv(agv);
                        UpdateManage.InsertAgvState(agv, 15);
                    }
                }
                else
                {
                    return;
                }
                var clientControl = App.ClientWork.FirstOrDefault(a => a.AgvNo == agv.AgvNo);
                if (clientControl == null) return;
                if (clientControl.IsDoing)
                {
                    clientControl.IsDoing = false;
                    App.ExFile.MessageError("HideClientIp", "==" + clientControl.AgvNo + "==" + "线程没正确释放此小车IP控制");
                }
            }
            else
            {
                agv.IsOnline = true;
            }
        }

        /// <summary>
        /// 判断心跳是否存在
        /// </summary>
        public static void CheckAgvOnline()
        {
            foreach (Base_Agv agv in App.AgvList.Where(a => a.IsEnable))
            {
                HeartStop(agv);
            }
            if (App.offAgv == 1)
            {
                var num = DiffTime(App.offTime);
                if (num > 300) App.offAgv = 0;
            }
        }

        /// <summary>
        /// 获取起点终点
        /// </summary>
        /// <param name="task"></param>
        /// <param name="fromToPoint"></param>
        /// <returns></returns>
        public static Base_Point? GetTaskPoint(Base_Task task, EnumMsg.ConfigToFromPoint fromToPoint)
        {
            Base_Point? point = null;
            switch (fromToPoint)
            {
                case EnumMsg.ConfigToFromPoint.等待分配:
                    point = new Base_Point();
                    break;
                case EnumMsg.ConfigToFromPoint.小车当前点:
                    var agv = App.AgvList.FirstOrDefault(x => x.AgvNo == task.TaskAgvNo);
                    point = App.PointList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                    break;
                case EnumMsg.ConfigToFromPoint.任务起点:
                    point = task.StartPoint;
                    break;
                case EnumMsg.ConfigToFromPoint.任务终点:
                    point = task.EndPoint;
                    break;
                case EnumMsg.ConfigToFromPoint.起点站台附属点:
                    var initialStation = App.StationList.FirstOrDefault(x => x.Name == task.Initial);
                    point = App.PointList.FirstOrDefault(x => x.Barcode == initialStation?.PreBarcode);
                    break;
                case EnumMsg.ConfigToFromPoint.终点站台附属点:
                    var targetStation = App.StationList.FirstOrDefault(x => x.Name == task.Target);
                    point = App.PointList.FirstOrDefault(x => x.Barcode == targetStation?.PreBarcode);
                    break;
                case EnumMsg.ConfigToFromPoint.起点站台附属点的前置点:
                    point = GetStationArcPoint(task.StartPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.终点站台附属点的前置点:
                    point = GetStationArcPoint(task.EndPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.起点的前置点:
                    point = GetPrePoint(task.StartPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.终点的前置点:
                    point = GetPrePoint(task.EndPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.出库区倒车点:
                    point = GetOutAreaBackPoint(task.EndPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.出库区放货前置点:
                    point = GetOutAreaPrePoint(task.EndPoint);
                    break;
                case EnumMsg.ConfigToFromPoint.充电桩姿态调整点:
                    agv = App.AgvList.FirstOrDefault(x => x.AgvNo == task.TaskAgvNo);
                    point = App.PointList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                    point = GetChargeStationAdjustPoint(point);
                    break;
                //由于小车在料框下不能进行转动,所以只能后退行走
                //add by liaoyu on 20240805
                case EnumMsg.ConfigToFromPoint.行走姿态调整点:
                    agv = App.AgvList.FirstOrDefault(x => x.AgvNo == task.TaskAgvNo);
                    point = App.PointList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                    point = GetStationPre(point);
                    break;
                case EnumMsg.ConfigToFromPoint.当前点附属点:
                    {
                        agv = App.AgvList.FirstOrDefault(x => x.AgvNo == task.TaskAgvNo);
                        var points = App.PointList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                        var station = App.StationList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                        if (station == null)
                        {
                            point = points;
                        }
                        else
                        {

                            point = App.PointList.Find(x => x.Barcode == station?.PreBarcode);

                        }
                        break;
                    }
                case EnumMsg.ConfigToFromPoint.等待点:
                    {
                        throw new NotImplementedException();
                        agv = App.AgvList.FirstOrDefault(x => x.AgvNo == task.TaskAgvNo);
                        var station = App.StationList.FirstOrDefault(x => x.Barcode == agv?.Barcode);
                        point = App.PointList.Find(x => x.Barcode == station?.PreBarcode);
                        break;
                    }

                default:
                    throw new Exception("未找到对应的点依据信息!!");
            }
            return point;
        }

        /// <summary>
        /// 获取当前点的附属点
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Base_Point? GetStationPre(Base_Point? point)
        {
            if (point == null) return null;

            if (point != null && point.PointType == EnumMsg.PointType.站台点)
            {
                var prePoint = App.PointList.Find(x => x.Barcode == point.PreBarcode);
                return prePoint;
            }
            else
            {
                return point;
            }
        }

        /// <summary>
        /// 获取站台附属点的前置点
        /// </summary>
        /// <param name="stationPoint">站台点</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static Base_Point? GetStationArcPoint(Base_Point? stationPoint)
        {
            Base_Point? point = null;
            var prePoint = App.PointList.FirstOrDefault(x => x.Barcode == stationPoint?.PreBarcode);
            if (prePoint == null)
            {
                return point;
            }
            //获取站台附属点的前置点
            if (stationPoint == prePoint.XPosPoint)
            {
                point = prePoint.XNegPoint;
            }
            else if (stationPoint == prePoint.XNegPoint)
            {
                point = prePoint.XPosPoint;
            }
            else if (stationPoint == prePoint.YPosPoint)
            {
                point = prePoint.YNegPoint;
            }
            else if (stationPoint == prePoint.YNegPoint)
            {
                point = prePoint.YPosPoint;
            }
            return point;
        }

        /// <summary>
        /// 获取点位的前置点
        /// </summary>
        /// <param name="stationPoint">站台点</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static Base_Point? GetPrePoint(Base_Point? point)
        {
            if (point == null)
            {
                return null;
            }
            //获取站台附属点的前置点
            if (point.XPosPoint != null)
            {
                return point.XPosPoint;
            }
            else if (point.XNegPoint != null)
            {
                return point.XNegPoint;
            }
            else if (point.YPosPoint != null)
            {
                return point.YPosPoint;
            }
            else if (point.YNegPoint != null)
            {
                return point.YNegPoint;
            }
            return null;
        }
        /// <summary>
        /// 获取前置点方向
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static EnumMsg.Direction? GetPrePointDirection(Base_Point? point)
        {
            if (point == null)
            {
                return null;
            }
            //获取站台附属点的前置点
            if (point.IsXPos && point.XPosPoint != null)
            {
                return EnumMsg.Direction.X正方向;
            }
            else if (point.IsXNeg && point.XNegPoint != null)
            {
                return EnumMsg.Direction.X负方向;
            }
            else if (point.IsYPos && point.YPosPoint != null)
            {
                return EnumMsg.Direction.Y正方向;
            }
            else if (point.IsYNeg && point.YNegPoint != null)
            {
                return EnumMsg.Direction.Y负方向;
            }
            else throw new Exception($"{point.Barcode}前置点未配置");
        }

        /// <summary>
        /// 获取待出库区倒车点
        /// </summary>
        /// <param name="endPoint">任务终点</param>
        /// <returns></returns>
        public static Base_Point? GetOutAreaBackPoint(Base_Point? endPoint)
        {
            if (endPoint == null)
            {
                return null;
            }
            //IntY大于等于995才是待出库地面区域
            if (endPoint.IntY < 995)
            {
                return null;
            }
            //根据地图,这里是倒退点
            if (endPoint.IntX <= 18)
            {
                return App.PointList.FirstOrDefault(x => x.IntY == endPoint.IntY && x.IntX == 22);
            }
            else
            {
                return App.PointList.FirstOrDefault(x => x.IntY == endPoint.IntY && x.IntX == 18);
            }
        }

        /// <summary>
        /// 获取待出库区放货点的前置点
        /// </summary>
        /// <param name="endPoint">任务终点</param>
        /// <returns></returns>
        public static Base_Point? GetOutAreaPrePoint(Base_Point? endPoint)
        {
            if (endPoint == null)
            {
                return null;
            }
            //IntY大于等于995才是待出库地面区域
            if (endPoint.IntY < 995)
            {
                return null;
            }
            //根据地图,这里是倒退点
            if (endPoint.IntX <= 18)
            {
                return App.PointList.Where(x => x.IntY == endPoint.IntY && x.IntX > endPoint.IntX).OrderBy(x => x.IntX).FirstOrDefault();
            }
            else
            {
                return App.PointList.Where(x => x.IntY == endPoint.IntY && x.IntX < endPoint.IntX).OrderByDescending(x => x.IntX).FirstOrDefault();
            }
        }

        /// <summary>
        /// 获取充电桩的调整点
        /// </summary>
        /// <param name="endPoint">任务终点</param>
        /// <returns></returns>
        public static Base_Point? GetChargeStationAdjustPoint(Base_Point? judgepoint)
        {
            if (judgepoint == null) return null;
            var val = App.GetKeyValue(judgepoint.Barcode);
            var barcode = val.ToString().Split(',')[0];
            var point = App.PointList.Find(x => x.Barcode == barcode);
            return point;

        }


        /// <summary>
        /// 清理无任务小车锁定的方向信息
        /// </summary>
        public static void ClearPointDirection()
        {
            try
            {
                //找出所有AGV正在执行任务的所有路径
                var pathList = App.AgvList.Where(x => x.PathPointList.Count > 0).SelectMany(x => x.PathPointList).ToList();
                var listPointDirectionLock = App.PointList.FindAll(x => x.TmpDirectionList.Count != 0);
                //找出所有包含方向锁的点位,如果点位的方向锁不包含在AGV任务的路径中,就是删除
                foreach (var point in listPointDirectionLock)
                {
                    for (int i = 0; i < point.TmpDirectionList.Count; i++)
                    {
                        var tmpDirection = point.TmpDirectionList[i];
                        bool isExists = pathList.Exists(x => x.Point == point && x.PathDirection == tmpDirection.PathDirection);
                        if (!isExists)
                        {
                            point.TmpDirectionList.Remove(tmpDirection);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("ClearPointDirection", ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 解除无AGV的锁定点
        /// </summary>
        public static void ClearPointOccupy()
        {
            try
            {
                var pointList = App.PointList.FindAll(x => string.IsNullOrEmpty(x.OccupyAgvNo));
                foreach (var point in pointList)
                {
                    var agv = App.AgvList.FirstOrDefault(x => x.AgvNo == point.OccupyAgvNo);
                    if (agv != null && agv.IsEnable && agv.Barcode == point.Barcode)
                    {
                        point.IsOccupy = true;
                        point.OccupyAgvNo = agv.AgvNo;
                    }
                    else
                    {
                        point.IsOccupy = false;
                        point.OccupyAgvNo = "";
                    }
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("SetPointOccupy", ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 解锁充电桩
        /// </summary>
        public static void UnLockStation()
        {
            try
            {
                foreach (var station in App.StationList)
                {
                    //不是锁定的充电桩就不处理。
                    if (station.StationType != EnumMsg.StationType.充电桩 || !station.IsLocked)
                    {
                        continue;
                    }

                    //已经有车选择了充电桩,也不处理。
                    var chargeAgv = App.AgvList.FirstOrDefault(a => a.LockStation?.Name == station.Name);
                    if (chargeAgv != null)
                    {
                        continue;
                    }

                    //如果AGV不在充电桩的点位,就解锁充电桩
                    var point = App.PointList.First(a => a.Barcode == station.Barcode);
                    var agv = App.AgvList.FirstOrDefault(a => a.Barcode == point.Barcode);
                    if (point.LockedAgv == null && agv == null)
                    {
                        station.IsLocked = false;
                        UpdateManage.UpdateStation(station);
                    }
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("UnLockStation", ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 获取最近的充电点
        /// </summary>
        /// <param name="agv"></param>
        public static Base_Station? GetMinChargeStation(Base_Agv agv)
        {
            try
            {
                Base_Station? minCs = null;
                //如果车配置了充电桩,就去对应的充电桩
                if (!string.IsNullOrEmpty(agv.ChargeStation))
                {
                    minCs = App.StationList.FirstOrDefault(x => x.Name == agv.ChargeStation);
                    return minCs;
                }
                int intMin = 0;
                var agvPoint = App.PointList.Find(a => a.Barcode == agv.Barcode);
                if (agvPoint!.PointType == EnumMsg.PointType.充电点)
                {
                    minCs = App.StationList.FirstOrDefault(a => a.IsEnable && a.Barcode == agvPoint.Barcode);
                    if (minCs != null)
                    {
                        return minCs;
                    }
                }
                //获取所有充电点,优先选择最靠近门的充电桩
                var listPoint = App.PointList
                    .Where(a => a.PointType == EnumMsg.PointType.充电点
                    && a.LockedAgv == null
                    //去除有任务占用的站台
                     && !App.TaskList.Any(x => x.EndBarcode == a.Barcode
                     || (x.StartBarcode == a.Barcode && x.TaskType == "充电")))
                     .OrderByDescending(b => b.StopLevel).ToList();
                if (listPoint.Count == 0)
                {
                    return minCs;
                }

                foreach (var point in listPoint)
                {
                    var Cs = App.StationList.FirstOrDefault(a => a.IsEnable && !a.IsLocked && a.Barcode == point.Barcode);
                    if (Cs == null)
                    {
                        continue;
                    }
                    else
                    {

                        if (point == null || agvPoint == null) continue;
                        int csMinNum = GetMinPoint(agvPoint.IntX, agvPoint.IntY, point.IntX, point.IntY);
                        if (intMin == 0) intMin = csMinNum;
                        if (csMinNum <= intMin)
                        {
                            minCs = Cs;
                            intMin = csMinNum;
                        }
                    }
                }
                return minCs;
            }
            catch (Exception ex)
            {
                throw new Exception($"GetMinChargeStation方法,异常信息:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }

        /// <summary>
        /// 获取最近的回家点
        /// </summary>
        /// <param name="agv"></param>
        /// <returns></returns>
        public static Base_Station GetMinHome(Base_Agv agv, int flag)
        {
            try
            {
                int intMin = 0;
                Base_Station? minHome = null;
                var startPoint = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
                if (startPoint == null)
                {
                    return minHome;
                }

                var listHome = App.StationList.FindAll(a => a.IsEnable && !a.IsLocked && a.StationType == EnumMsg.StationType.回家位).ToList();
                foreach (Base_Station home in listHome)
                {
                    if (startPoint.PointType == EnumMsg.PointType.回家点) continue;
                    if (App.TaskList.FirstOrDefault(a => a.EndPoint.Barcode == home.Barcode && a.IsSubmit) != null) continue;
                    if (App.PointList.Find(a => a.Barcode == home.Barcode).LockedAgv != null) continue;
                    if (App.AgvList.ToList().Exists(a => a.Barcode == home.Barcode)) continue;
                    if (App.AgvList.FirstOrDefault(a => a.LockStation?.Name == home.Name) != null) continue;
                    Base_Point homePoint = App.PointList.Find(a => a.Barcode == home.Barcode);
                    int csMinNum = GetMinPoint(startPoint.IntX, startPoint.IntY, homePoint.IntX, homePoint.IntY);
                    if (intMin == 0) intMin = csMinNum;
                    if (csMinNum <= intMin)
                    {
                        minHome = home;
                        intMin = csMinNum;
                    }
                }
                return minHome;
            }
            catch (Exception ex)
            {
                throw new Exception($"GetMinHome方法,异常信息:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }
        /// <summary>
        /// 寻找最近的站台点
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static Base_Point? GetMinStationPoint(Base_Agv agv)
        {
            Base_Point? minStationPoint = null;

            int intMin = 0;
            var startPoint = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
            if (startPoint == null)
            {
                return null;
            }

            var pointList = App.PointList.Where(a => a.IsEnable && a.LockedAgv == null //&& a.IsStop去除有任务占用的站台
                                                && startPoint.GetArea() == a.GetArea() && a.PointType == EnumMsg.PointType.站台点
                                                && !App.TaskList.Any(x => x.EndBarcode == a.Barcode || (x.StartBarcode == a.Barcode && x.TaskType == "充电")))
                                         .OrderByDescending(b => b.StopLevel).ToList();
            foreach (Base_Point point in pointList)
            {
                //剔除站台附属点有车的站台
                var perPoint = App.PointList.FirstOrDefault(a => a.IsEnable && a.Barcode == point.PreBarcode);
                if (perPoint != null)
                {
                    if (perPoint.LockedAgv != null) continue;
                }

                var taskList = App.TaskList.ToList();
                if (taskList.Count != 0)
                {
                    //当前停车点是否有任务
                    var result1 = taskList.Exists(a => a.SubTaskList.Count == 0
                                                       && (a.EndPoint == point || a.StartPoint == point));
                    if (result1) continue;
                    //获取所有提交的主任务的子任务
                    var result = taskList.Exists(a => a.SubTaskList.Count != 0
                                                      && a.SubTaskList.ToList().Exists(b => b.EndPoint == point));
                    if (result) continue;
                }
                int csMinNum = GetMinPoint(startPoint.IntX, startPoint.IntY, point.IntX, point.IntY);
                if (intMin == 0) intMin = csMinNum;
                if (csMinNum <= intMin)
                {
                    minStationPoint = point;
                    intMin = csMinNum;
                }
            }
            return minStationPoint;
        }

        /// <summary>
        /// 寻找最近的回家点
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static Base_Point? GetMinHomePoint(Base_Agv agv)
        {
            Base_Point? minHomePoint = null;
            //如果车配置了充电桩,待机就去对应的点位
            if (!string.IsNullOrEmpty(agv.ChargeStation)
                && App.StationList.FirstOrDefault(x => x.Name == agv.ChargeStation) is Base_Station chargeStation
                && App.PointList.FirstOrDefault(x => x.Barcode == chargeStation?.Barcode) is Base_Point chargePoint
                // && point?.Barcode != "3807"
                && agv.Barcode != chargeStation.Barcode
                )
            {
                return chargePoint;
            }

            int intMin = 0;
            var startPoint = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
            if (startPoint == null)
            {
                return null;
            }
            if (startPoint.IsStop || startPoint.Barcode == App.StationList.FirstOrDefault(x => x.Name == agv.ChargeStation)?.Barcode)
            {
                return startPoint;
            }
            var pointList = App.PointList
                .Where(a => a.IsEnable && a.LockedAgv == null
                    && a.IsHomePoint(agv)  //去除有任务占用的站台
                    && startPoint.GetArea() == a.GetArea()
                    && !App.TaskList.Any(x => x.EndBarcode == a.Barcode || x.StartBarcode == a.Barcode))
                .OrderByDescending(b => b.StopLevel)
                .ToList();
            foreach (Base_Point point in pointList)
            {
                if (minHomePoint != null && minHomePoint.StopLevel > point.StopLevel) continue;
                // 回家点仅仅为缓存区站台且不靠近充电桩
                var homeStation = App.StationList.Find(a => a.Barcode == point.Barcode);
                //if (homeStation != null && homeStation.StationType != EnumMsg.StationType.缓存区) continue;

                //回家点中是否存在比当前点停车等级高的点
                if (startPoint.IsStop)
                {
                    var stopList = pointList.FindAll(a => a.StopLevel > startPoint.StopLevel);
                    var isTrue = App.StationList.Exists(a => a.StationType == EnumMsg.StationType.缓存区 && stopList.Exists(u => u.Barcode == a.Barcode));
                    if (!isTrue) continue;
                }

                var taskList = App.TaskList.ToList();
                if (taskList.Count != 0)
                {
                    //当前停车点是否有任务
                    var result1 = taskList.Exists(a => a.SubTaskList.Count == 0
                                                       && (a.EndPoint == point || a.StartPoint == point));
                    if (result1) continue;
                    //获取所有提交的主任务的子任务
                    var result = taskList.Exists(a => a.SubTaskList.Count != 0
                                                      && a.SubTaskList.ToList().Exists(b => b.EndPoint == point));
                    if (result) continue;
                }
                int csMinNum = GetMinPoint(startPoint.IntX, startPoint.IntY, point.IntX, point.IntY);
                if (intMin == 0) intMin = csMinNum;
                if (csMinNum <= intMin)
                {
                    minHomePoint = point;
                    intMin = csMinNum;
                }
            }
            return minHomePoint;
        }
        /// <summary>
        /// 获取最近的电梯
        /// </summary>
        /// <param name="startPoint">起点信息</param>
        /// <returns></returns>
        public static Base_Point GetMinLiftPoint(Base_Point startPoint)
        {
            try
            {
                int intMin = 0;
                Base_Point minPoint = null;
                List<Base_Point> points = App.PointList.Where(a => a.PointType == EnumMsg.PointType.电梯点 && a.AreaType == startPoint.AreaType).ToList();
                foreach (Base_Point point in points)
                {
                    int csMinNum = GetMinPoint(startPoint.IntX, startPoint.IntY, point.IntX, point.IntY);
                    if (intMin == 0) intMin = csMinNum;
                    if (csMinNum <= intMin)
                    {
                        minPoint = point;
                        intMin = csMinNum;
                    }
                }
                return minPoint;

            }
            catch (Exception ex)
            {
                throw new Exception($"GetMinLiftPoint:" + $"原因:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }

        /// <summary>
        /// Demo获取最近的电梯
        /// </summary>
        /// <param name="startPoint">起点信息</param>
        /// <returns></returns>
        public static Base_Point GetMinLiftDemoPoint(Base_Point startPoint)
        {
            try
            {
                int intMin = 0;
                Base_Point minPoint = null;
                List<Base_Point> points = App.DemoPointList.Where(a => a.PointType == EnumMsg.PointType.电梯点 && a.AreaType == startPoint.AreaType).ToList();
                foreach (Base_Point point in points)
                {
                    int csMinNum = GetMinPoint(startPoint.IntX, startPoint.IntY, point.IntX, point.IntY);
                    if (intMin == 0) intMin = csMinNum;
                    if (csMinNum <= intMin)
                    {
                        minPoint = point;
                        intMin = csMinNum;
                    }
                }
                return minPoint;
            }
            catch (Exception ex)
            {
                throw new Exception("GetMinLiftDemoPoint:" + $"原因:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }


        /// <summary>
        /// 判断起点终点是否在同一层,如果不是将终点设置为过渡点
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static List<Base_Point> GetAreaPoint(Base_Point startPoint, Base_Point endPoint)
        {
            try
            {
                var points = new List<Base_Point>();
                var newStartPoint = App.PointList.Find(a => a.Barcode == startPoint.Barcode && a.AreaType == startPoint.AreaType);
                var newEndPoint = App.PointList.Find(a => a.Barcode == endPoint.Barcode && a.AreaType == endPoint.AreaType);
                if (newStartPoint == null || newEndPoint == null)
                {
                    return points;
                }
                //如果起点和终点不是一个区域类型,就获取同一个区域的起点和终点。这是为了避免过两个区域有相同的地标条码,导致点位错误。
                if (newStartPoint.AreaType != newEndPoint.AreaType)
                {
                    if (App.PointList.Exists(x => x.Barcode == newEndPoint.Barcode && x.AreaType == newStartPoint.AreaType))
                    {
                        newEndPoint = App.PointList.Find(x => x.Barcode == newEndPoint.Barcode && x.AreaType == newStartPoint.AreaType);
                    }
                    else
                    {
                        newStartPoint = App.PointList.Find(x => x.Barcode == newStartPoint.Barcode && x.AreaType == newEndPoint.AreaType);
                    }
                }
                if (newStartPoint == null || newEndPoint == null)
                {
                    return points;
                }
                //如果起点和终点都在电梯点,就找名字带2的站台的区域,再根据区域+条码找到点位
                if (newStartPoint.PointType == EnumMsg.PointType.电梯点 && newEndPoint.PointType == EnumMsg.PointType.电梯点)
                {
                    var startStation = App.StationList.Find(x => x.Barcode == newStartPoint.Barcode);
                    var endStation = App.StationList.Find(x => x.Barcode == newEndPoint.Barcode);
                    newStartPoint = App.PointList.Find(x => x.AreaType == startStation?.Area && x.Barcode == startPoint.Barcode);
                    newEndPoint = App.PointList.Find(x => x.AreaType == endStation?.Area && x.Barcode == endPoint.Barcode);
                }
                if (newStartPoint == null || newEndPoint == null)
                {
                    return points;
                }
                points.Add(newStartPoint);
                points.Add(newEndPoint);
                return points;
            }
            catch (Exception ex)
            {
                throw new Exception("GetAreaPoint:" + $"原因:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }


        public static List<Base_Point> GetAroundPoint(Base_Point currentPoint)
        {
            var listPoint = new List<Base_Point>();

            //找到当前点的X+方向点,判断是否在可用点里面,再判断X+的点是否可走

            if (currentPoint.IsXPos && currentPoint.XPosPoint != null)
            {
                listPoint.Add(currentPoint.XPosPoint);
            }
            if (currentPoint.IsXNeg && currentPoint.XNegPoint != null)
            {
                listPoint.Add(currentPoint.XNegPoint);
            }
            if (currentPoint.IsYPos && currentPoint.YPosPoint != null)
            {
                listPoint.Add(currentPoint.YPosPoint);
            }
            if (currentPoint.IsYNeg && currentPoint.YNegPoint != null)
            {
                listPoint.Add(currentPoint.YNegPoint);
            }

            return listPoint;
        }


        public static List<Base_Station> ByTaskTypeGetStation(string taskType)
        {

            return App.StationList.FindAll(x => x.StationType != EnumMsg.StationType.充电桩);


        }

        /// <summary>
        /// 滚筒编号及滚筒动作转换
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static int[] RollerStote(byte result)
        {
            int[] roller = new int[2];
            var boolsult = ByteToBoolArray(result);
            if (boolsult[0])
            {
                roller[0] = 1;
            }

            if (boolsult[1])
            {
                roller[0] = 2;
            }

            if (boolsult[0] && boolsult[1])
            {
                roller[0] = 3;
            }

            if (!boolsult[0] && !boolsult[1])
            {
                roller[0] = 0;
            }

            if (boolsult[2])
            {
                roller[1] = 1;
            }

            if (boolsult[3])
            {
                roller[1] = 2;
            }

            if (boolsult[2] && boolsult[3])
            {
                roller[1] = 3;
            }

            if (!boolsult[2] && !boolsult[3])
            {
                roller[1] = 0;
            }

            return roller;
        }

        /// <summary>
        /// 利用反射实现深拷贝
        /// </summary>
        /// <param name="_object"></param>
        /// <returns></returns>
        public static object[] DeepCopy(object[] objects)
        {
            object[] newObjects = new object[objects.Length];
            for (var i = 0; i < newObjects.Length; i++)
            {
                System.Type type = objects[i].GetType();
                if (type.IsValueType || type.Equals(typeof(string)))
                {
                    newObjects[i] = objects[i];
                    continue;
                }
                newObjects[i] = Activator.CreateInstance(type);
                PropertyInfo[] propertyInfos = type.GetProperties();
                foreach (var propertyInfo in propertyInfos)
                {
                    propertyInfo.SetValue(newObjects[i], propertyInfo.GetValue(objects[i]));
                }
            }

            return newObjects;
        }

        /// <summary>
        /// 比较两个数组对象的值是否相等
        /// </summary>
        /// <param name="objectArray1"></param>
        /// <param name="objectArray2"></param>
        /// <returns></returns>
        public static bool IsEquals(object[] objectArray1, object[] objectArray2)
        {
            if (objectArray1 == null || objectArray1.Length == 0)
            {
                if (objectArray2 == null || objectArray2.Length == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (objectArray2 == null || objectArray2.Length == 0)
            {
                return false;
            }
            if (objectArray1.Length != objectArray2.Length)
            {
                return false;
            }
            for (var i = 0; i < objectArray1.Length; i++)
            {
                if (!IsEquals(objectArray1[i], objectArray2[i]))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 比较两个对象的值是否相等
        /// </summary>
        /// <param name="objectArray1"></param>
        /// <param name="objectArray2"></param>
        /// <returns></returns>
        public static bool IsEquals(object object1, object object2)
        {
            if (object1 == null && object2 == null)
            {
                return true;
            }
            if (object1 == null && object2 != null)
            {
                return false;
            }
            if (object1 != null && object2 == null)
            {
                return false;
            }
            var object1Type = object1.GetType();
            var object2Type = object2.GetType();
            //如果类型不一样,那就不相等
            if (object1Type.FullName != object2Type.FullName)
            {
                return false;
            }
            //如果是值类型,直接比较
            if (object1Type.IsValueType || object1Type.Equals(typeof(string)))
            {
                if (!Equals(object1, object2))
                {
                    return false;
                }
            }
            else
            {
                //如果不是值类型,就用反射获取属性,比较属性的值
                PropertyInfo[] propertyInfos = object1Type.GetProperties();
                foreach (var propertyInfo in propertyInfos)
                {
                    var object1Property = propertyInfo.GetValue(object1);
                    var object2Property = propertyInfo.GetValue(object2);
                    //对属性的值比较,属性的值可能是引用类型,所以就递归比较
                    if (!IsEquals(object1Property, object2Property))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// ping指定的IP,判断网络是否连通
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public static BllResult Ping(string ip)
        {
            try
            {
                if (!IPAddress.TryParse(ip, out IPAddress iPAddress))
                {
                    return BllResult.Error($"PING失败,字符串【{ip}】不能转为IP地址!");
                }
                var ping = new System.Net.NetworkInformation.Ping();
                var pingStatus = ping.Send(IPAddress.Parse(ip), 100);
                if (pingStatus.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    return BllResult.Success();
                }
                return BllResult.Error($"PING地址【{ip}】失败,网络不通!");
            }
            catch (Exception ex)
            {
                return BllResult.Error($"PING地址【{ip}】出现异常: {ex.Message} \n");
            }
        }


        ///// <summary>
        ///// 全部无货或者有货但是agv在站台且处于低位就判定为无货
        ///// </summary>
        ///// <param name="agv"></param>
        ///// <returns></returns>
        //public static bool IsNotHaveGoods(Base_Agv agv)
        //{
        //    var flag = agv.InStock <= EnumMsg.InStock.全部无货
        //        || (App.StationList.Exists(station => station.Barcode == agv.Barcode)
        //            && agv.HeightState == EnumMsg.HeightState.低位);
        //    return flag;

        //}

        /// <summary>
        /// 死锁处理
        /// </summary>
        /// <param name="agv"></param>
        public static void DeadLockHandle(Base_Agv agv)
        {
            try
            {
                if (agv == null || agv.AgvTask == null || agv.AgvTask.SubTaskList == null || agv.AgvTask.SubTaskList.Count == 0) return;
                if (!IsCanForward(agv, null, 0))
                {
                    if (agv.AgvTask.TaskType == "回家" || agv.AgvTask.TaskType == "充电")
                    {
                        App.ExFile.MessageError("DeadLockHandle", $"agv:{agv.AgvNo}" + $"在执行{agv.AgvTask.TaskType}任务:{agv.AgvTask.TaskNo}" +
                                 $"的子任务:{agv.AgvTask.SubTaskList.Get(0)?.SubTaskNo}时触发死锁处理逻辑,删除{agv.AgvTask.TaskType}任务,{agv.Barcode}");
                        agv.AgvTask.TaskState = EnumMsg.TaskState.自动删除;
                        TaskManage.TaskDelete(agv.AgvTask, 140, "任务无法继续执行,删除任务");
                    }
                    else
                    {
                        //重置任务状态,重新分配agv
                        if (agv.AgvTask.SubTaskList.Any(x => x.TaskStage == TaskStage.去取货) && agv.IsNotHaveGoods())
                        {
                            App.ExFile.MessageError("DeadLockHandle", $"agv:{agv.AgvNo}在执行{agv.AgvTask.TaskType}任务:{agv.AgvTask.TaskNo}" +
                                     $"的子任务:{agv.AgvTask.SubTaskList.Get(0)?.SubTaskNo}时触发死锁处理逻辑,重置{agv.AgvTask.TaskType}任务,{agv.Barcode}");
                            TaskManage.TaskReset(agv.AgvTask);
                        }
                        //重新设置路线,并设置禁行点
                        else
                        {

                            var nextPoint = GetNextPathPoint(agv, agv.Barcode)?.Point;
                            var sonTask = agv.AgvTask.SubTaskList.First();
                            if (nextPoint != null)
                            {
                                sonTask.DisablePoints.Add(nextPoint);
                            }
                            UpdateManage.ResetPath(agv, sonTask);
                            App.ExFile.MessageError("DeadLockHandle", $"agv:{agv.AgvNo}" + $"在执行{agv.AgvTask.TaskType}任务:{agv.AgvTask.TaskNo}" +
                                                             $"的子任务:{agv.AgvTask.SubTaskList.Get(0)?.SubTaskNo}时触发死锁,重新规划路线,并设置禁行点{nextPoint?.Barcode}");
                        }
                    }

                }
            }
            catch (Exception e)
            {
                App.ExFile.MessageError("DeadLockHandle", $"agv:{agv.AgvNo}" + $"在执行{agv.AgvTask.TaskType}任务:{agv.AgvTask.TaskNo}" +
                          $"的子任务:{agv.AgvTask.SubTaskList.Get(0)?.SubTaskNo}时触发死锁处理逻辑报错:" + e.Message + e.StackTrace);
            }
        }
        /// <summary>
        /// 判断agv能否继续前进
        /// </summary>
        /// <param name="agv">需要判断的agv</param>
        /// <param name="lastAgv">上一层agv</param>
        /// <param name="deep">递归深度</param>
        /// <returns></returns>
        public static bool IsCanForward(Base_Agv? agv, Base_Agv? lastAgv, int deep)

        {
            if (agv == null) return true;
            if (deep > 4) return false;
            if (agv.AgvState != AGVState.自动忙碌 && agv.AgvState != AGVState.自动空闲)
                return false;

            //判断当前agv是否能继续前进否则判断不能前进原因
            var task = agv.AgvTask;
            //超过5s不回家表示回家任务生成出现限制
            if (task == null)
            {
                if ((DateTime.Now - agv.FreeTime).TotalSeconds > 5)
                    return false;
                else
                    return true;
            }
            var sonTask = task?.SubTaskList?.FirstOrDefault();
            //子任务为空时可能任务刚完成,暂时反回可继续前进
            if (sonTask == null) return true;
            Base_Agv? otherAgv;
            var flag = sonTask.SubTaskState == SubTaskState.建立路径失败 && sonTask.PathPointList.Count <= 0 || agv.AgvState == AGVState.自动空闲;
            //任务路线被挡
            if (flag)
            {
                otherAgv = GetOtherAgvInPath(agv);
                if (otherAgv == lastAgv && lastAgv != null) return false;
                return IsCanForward(otherAgv, agv, deep++);
            }
            //agv避让中
            if (IsAvoiding(agv, out otherAgv))
            {
                if (otherAgv == lastAgv && lastAgv != null) return false;

                return IsCanForward(otherAgv, agv, deep++);
            }

            return true;
        }

        /// <summary>
        /// 判断agv是否避让中
        /// </summary>
        /// <param name="agv"></param>
        /// <returns></returns>
        public static bool IsAvoiding(Base_Agv agv, out Base_Agv? otherAgv)
        {
            otherAgv = null;

            if (!IsAgvExistsTask(agv)) return false;

            var nextPath = GetNextPathPoint(agv, agv.Barcode);
            //互斥区域避让
            if (nextPath != null && nextPath.Point.RegionName != "")
            {
                var regionPoints = App.PointList.FindAll(x => x.RegionName == nextPath.Point.RegionName);
                otherAgv = regionPoints.Find(x => x.LockedAgv != null && x.LockedAgv != agv)?.LockedAgv;
                if (otherAgv != null)
                    return true;
            }
            if (nextPath == null) return false;
            var nextNextPathBarCode = GetNextPrePathPoint(agv, nextPath.Point.Barcode);
            //站台存在agv避让
            if (nextPath.Point.PointType == EnumMsg.PointType.站台附属点
                && nextNextPathBarCode != null
                && nextNextPathBarCode.Point().Data.PointType == EnumMsg.PointType.站台点
                && App.AgvList.Find(_agv => _agv.Barcode == nextNextPathBarCode && _agv.AgvNo != agv.AgvNo) is Base_Agv otherAgv1)
            {
                otherAgv = otherAgv1;
                return true;
            }

            return false;
        }
        public static bool IsAgvExistsTask(Base_Agv agv)
        {
            return agv?.AgvTask?.SubTaskList?.Get(0) != null;
        }

        /// <summary>
        /// 获取路径中的其他agv
        /// </summary>
        /// <param name="agv"></param>
        /// <returns></returns>
        public static Base_Agv? GetOtherAgvInPath(Base_Agv agv)
        {
            if (agv == null || agv.AgvTask == null) return null;
            Base_Agv? otherAgv = null;
            string barcode = "";
            foreach (var x in agv.AgvTask.PreLoadPath)
            {
                if (x.Barcode.Point().Data.LockedAgv is Base_Agv _agv
                    && _agv != null && _agv != agv)
                {
                    otherAgv = _agv;
                    barcode = x.Barcode;
                    App.ExFile.MessageBusinessErr("GetOtherAgvInPath", $"agv{agv.AgvNo}的行进路线中在点{barcode}存在其他agv{otherAgv?.AgvNo}挡道");
                    break;
                }
            }
            return otherAgv;
        }

        /// <summary>
        /// 获取下一个预加载路径
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static string GetNextPrePathPoint(Base_Agv agv, string barcode)
        {
            var task = agv?.AgvTask;

            var pathPointList = task?.PreLoadPath;

            if (task == null || pathPointList.Count == 0) return null;

            var currIndex = pathPointList.FirstOrDefault(a => a.Barcode == barcode).No;
            var currBarcode = pathPointList.FirstOrDefault(a => a.Barcode == barcode).Barcode;
            if (currIndex == 0 && currBarcode == null) return null;
            var nextPointNew = pathPointList.FirstOrDefault(a => a.No > currIndex && a.Barcode != currBarcode).Barcode;

            return nextPointNew;
        }
        /// <summary>
        /// 查找当前点的路径上一点
        /// </summary>
        /// <param name="lastAgv"></param>
        /// <param name="nowPoint"></param>
        /// <returns></returns>
        public static Base_PathPoint GetLastPathPoint(Base_Agv agv, Base_PathPoint pathPoint)
        {
            var subTaskList = agv?.AgvTask?.SubTaskList;

            var nextAgvSonTask = subTaskList?.Get(0);

            var pathPointList = nextAgvSonTask?.PathPointList;
            if (pathPointList == null || pathPointList.Count == 0) return null;

            Base_PathPoint currPoint = pathPointList.Find(a => a.Point.Barcode == pathPoint.Point.Barcode);
            if (currPoint == null)
                return null;

            Base_PathPoint lastPoint = pathPointList.Find(a => a.SerialNo == currPoint.SerialNo - 1);
            return lastPoint;
        }




        #region 门控制

        //public static bool[] ChangeDoor(int flag, int write, string liftNo)
        //{
        //    bool[] bo = new bool[20];
        //    try
        //    {
        //        ModbusTcpNet readModbus = null;
        //        ModbusTcpNet writeModbus = null;
        //        //门外检测门开传感器
        //        if (liftNo == "1")
        //        {
        //            readModbus = new ModbusTcpNet(App.DoorIp1, 502, 0x01);
        //            writeModbus = new ModbusTcpNet(App.DoorIp1, 502, 0x06);
        //        }
        //        else
        //        {
        //            readModbus = new ModbusTcpNet(App.DoorIp2, 502, 0x01);
        //            writeModbus = new ModbusTcpNet(App.DoorIp2, 502, 0x06);
        //        }
        //        OperateResult<bool[]> read1 = readModbus.ReadDiscrete("0", 20);

        //        //电梯外测传感器
        //        //OperateResult<bool[]> read1 = readModbus.ReadDiscrete("0", 20);
        //        if (read1.IsSuccess)
        //        {
        //            //门是否高位
        //            //bo[1]==true 门处在高位
        //            bo[0] = read1.Content[0];
        //            //门开
        //            bo[1] = read1.Content[16];
        //            //门关
        //            bo[2] = read1.Content[17];
        //        }
        //        else
        //        {
        //            App.ExFile.MessageError("ReadDoor", "风淋门模块读取失败!= =" + read1.ToMessageShowString() + "= =" + read1.Message);
        //            return bo = null;
        //        }
        //        //开门
        //        if (flag == 2)
        //        {
        //            writeModbus.WriteCoil("16", true);
        //            writeModbus.WriteCoil("17", false);
        //        }
        //        //关门
        //        else if (flag == 3)
        //        {
        //            writeModbus.WriteCoil("16", false);
        //            writeModbus.WriteCoil("17", false);
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        App.ExFile.MessageError("ReadDoor", ex.Message.Replace(Environment.NewLine, ""));
        //        return bo = null;
        //    }
        //    return bo;
        //}

        #endregion

        #region 电梯控制
        /// <summary>
        /// 问询电梯
        /// </summary>
        /// <returns></returns>
        public static byte[] liftAsk()
        {
            byte[] askDate = new byte[5];
            try
            {
                askDate[0] = 1;
                askDate[1] = 3;
                askDate[2] = 0;
                var result = CRC16(askDate, 3);
                askDate[3] = result[0];
                askDate[4] = result[1];
                return askDate;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 电梯控制
        /// </summary>
        /// <param name="flag">1、AGV叫电梯;2在电梯内选择去往的楼层,并且关门;3开门;4关门</param>
        /// <param name="liftFlood">楼层信息</param>
        /// <returns></returns>
        public static byte[] liftControl(int flag, int liftFlood)
        {
            byte[] date = new byte[9];
            try
            {
                date[0] = 1;
                date[1] = 6;
                date[2] = 4;
                if (flag == 1)
                {
                    date[3] = (byte)(liftFlood == 1 ? 65 : 2);
                    date[4] = 0;
                    date[5] = 0;
                }
                else if (flag == 2)
                {
                    //在电梯内选择去往的楼层
                    date[3] = 0;
                    date[4] = (byte)liftFlood;
                    date[5] = 32;
                }
                else if (flag == 3)
                {
                    //开门持续中
                    date[3] = 0;
                    date[4] = 0;
                    date[5] = 128;
                }
                else if (flag == 4)
                {
                    //关门持续中
                    date[3] = 0;
                    date[4] = 0;
                    date[5] = 32;
                }
                date[6] = 0;
                var result = CRC16(date, 7);
                date[7] = result[0];
                date[8] = result[1];
                return date;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 电梯校验
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] CRC16(byte[] data, int len)
        {
            //int len = data.Length;
            if (len > 0)
            {
                ushort crc = 0xFFFF;
                for (int i = 0; i < len; i++)
                {
                    crc = (ushort)(crc ^ (data[i]));
                    for (int j = 0; j < 8; j++)
                    {
                        crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
                    }
                }
                byte hi = (byte)((crc & 0xFF00) >> 8);  //高位置
                byte lo = (byte)(crc & 0x00FF);         //低位置

                return new byte[] { lo, hi };
            }
            return new byte[] { 0, 0 };
        }
        #endregion

        #region S7数组转换
        /// <summary>
        /// bool转Byte
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static byte BoolArrayToByte(bool[] array)
        {
            byte rarray = new byte();
            for (int i = 0; i < array.Length; i++)
            {
                int num3 = i % 8;
                byte b = 0;
                switch (num3)
                {
                    case 0:
                        b = 1;
                        break;

                    case 1:
                        b = 2;
                        break;

                    case 2:
                        b = 4;
                        break;

                    case 3:
                        b = 8;
                        break;

                    case 4:
                        b = 16;
                        break;

                    case 5:
                        b = 32;
                        break;

                    case 6:
                        b = 64;
                        break;

                    case 7:
                        b = 128;
                        break;
                }
                if (array[i])
                {
                    rarray += b;
                }
            }
            return rarray;
        }

        /// <summary>
        /// byte转bool
        /// </summary>
        /// <param name="InByte"></param>
        /// <returns></returns>
        public static bool[] ByteToBoolArray(byte InByte)
        {
            bool[] array = new bool[8];
            for (int i = 0; i < 8; i++)
            {
                int num = i / 8;
                int num2 = i % 8;
                byte b = 0;
                switch (num2)
                {
                    case 0:
                        b = 1;
                        break;
                    case 1:
                        b = 2;
                        break;
                    case 2:
                        b = 4;
                        break;
                    case 3:
                        b = 8;
                        break;
                    case 4:
                        b = 16;
                        break;
                    case 5:
                        b = 32;
                        break;
                    case 6:
                        b = 64;
                        break;
                    case 7:
                        b = 128;
                        break;
                }
                if ((InByte & b) == b)
                {
                    array[i] = true;
                }
            }
            return array;
        }

        #endregion

        #region  获取当前点小车转盘方向
        /// <summary>
        /// 获取当前点小车转盘方向
        /// </summary>
        /// <param name="currentPathPoint"></param>
        /// <param name="agv"></param>
        /// <returns></returns>
        public static int getAgvDial(Base_PathPoint currentPathPoint, Base_Agv agv)
        {
            try
            {
                int agvDial = 0;
                //给定当前点的车头和转盘方向
                if (currentPathPoint.IsCorner)
                {
                    //判断车头与行进方向是否相同
                    if (currentPathPoint.PathDirection == agv.AgvDirection)
                    {
                        //车头与行进方向相同,转盘方向不变
                        agvDial = (int)agv.DialDirection;
                    }
                    else if (Math.Abs((int)(currentPathPoint.PathDirection - agv.AgvDirection)) == 1)
                    {
                        //顺时针旋转90°,转盘顺时针90度
                        if (agv.DialDirection > EnumMsg.Direction.X正方向)
                        {
                            agvDial = (int)agv.DialDirection + 1;
                        }
                        else
                        {
                            agvDial = 4;
                        }
                    }
                    else if (Math.Abs((int)(currentPathPoint.PathDirection - agv.AgvDirection)) == 2)
                    {
                        //顺时针旋转180°,转盘顺时针180°
                        if (agv.DialDirection > EnumMsg.Direction.Y正方向)
                        {
                            agvDial = (int)agv.DialDirection - 2;
                        }
                        else
                        {
                            agvDial = (int)agv.DialDirection + 2;
                        }
                    }
                    else if (Math.Abs((int)(currentPathPoint.PathDirection - agv.AgvDirection)) == 3)
                    {
                        //顺时针旋转270°,转盘顺时针270°
                        if (agv.DialDirection < EnumMsg.Direction.Y负方向)
                        {
                            agvDial = (int)agv.DialDirection + 1;
                        }
                        else
                        {
                            agvDial = 1;
                        }
                    }
                }
                return agvDial;
            }
            catch (Exception ex)
            {
                throw new Exception($"getAgvDial方法,异常信息:{ex.Message},\n 堆栈:{ex.StackTrace}");
            }
        }
        #endregion

        #region 配送计算方向

        /// <summary>
        /// 配送计算方向
        /// </summary>
        /// <returns></returns>
        public static EnumMsg.Direction getDirection(int ori, int shelfOri, int stationOri)
        {
            int DialDirection = 0;
            if (ori == 0)
            {
                DialDirection = 5;
                return (EnumMsg.Direction)DialDirection;
            }
            else if (shelfOri - stationOri - ori + 5 != 0)
            {
                DialDirection = shelfOri - stationOri - ori + 5;
                if (DialDirection < 0)
                {
                    DialDirection = DialDirection + 4 + 5;
                    return (EnumMsg.Direction)DialDirection;
                }
                else if (DialDirection > 3)
                {
                    DialDirection = DialDirection - 4 + 5;
                    return (EnumMsg.Direction)DialDirection;
                }
                else
                {
                    DialDirection = DialDirection + 5;
                    return (EnumMsg.Direction)DialDirection;
                }
            }
            return (EnumMsg.Direction)DialDirection;
        }


        #endregion

        /// <summary>
        /// 在所有窗体程序最上层的提示框
        /// </summary>
        /// <param name="msg"></param>
        public static void Message(string msg)
        {
            MessageBox.Show(msg, "消息", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
        }
        /// <summary>
        /// 判断是否为地面库的点
        /// </summary>
        /// <returns></returns>

        public static bool IsGroundPoint(Base_Point point)
        {
            return point.IntY >= 995;
        }

        /// <summary>
        /// 获取回家或是避让点
        /// </summary>
        /// <param name="agvTask">执行中的任务</param>
        /// <param name="targetAGV">需要执行避让的车辆</param>
        public static string GetHomeBarcode(Base_Task agvTask, Base_Agv targetAGV)
        {
            string homeBarcode = null;
            ////执行中任务占用的最近距离= 任务最近点位 - 车长
            //var minXLength = 0;
            ////执行中任务占用的最远距离= 任务最远点位 + 车长
            //var maxXLength = 0;
            //if (agvTask.StartPoint.YLength < agvTask.EndPoint.YLength)
            //{
            //    minXLength = agvTask.StartPoint.XLength - targetAGV.Length;
            //    maxXLength = agvTask.EndPoint.XLength + targetAGV.Length;
            //}
            //else
            //{
            //    minXLength = agvTask.EndPoint.XLength - targetAGV.Length;
            //    maxXLength = agvTask.StartPoint.XLength + targetAGV.Length;
            //}
            //if (targetAGV.AgvNo == "A33_9002")
            //{
            //    if (targetAGV.XLength > minXLength)
            //        homeBarcode = App.PointList.Where(t => t.XLength < minXLength && t.PointType != EnumMsg.PointType.对接等待点).OrderByDescending(t => t.XLength).First().Barcode;
            //    else
            //        homeBarcode = targetAGV.Barcode;
            //}
            //if (targetAGV.AgvNo == "A33_9001")
            //{
            //    if (targetAGV.XLength < maxXLength)
            //        homeBarcode = App.PointList.Where(t => t.XLength > maxXLength && t.PointType != EnumMsg.PointType.对接等待点).OrderBy(t => t.XLength).First().Barcode;
            //    else
            //        homeBarcode = targetAGV.Barcode;
            //}
            return homeBarcode;
        }

        public static Base_Point GetStationPrePoint()
        {

            return new();
        }
        /// <summary>
        /// 获取货架高度
        /// </summary>
        /// <returns></returns>
        public static int GetShelvesLayer(string? shelvesCode)
        {

            if (shelvesCode.IsNullOrEmpty() || shelvesCode!.Length < 2 || shelvesCode.StartsWith("D")) return -1;
            var temp = shelvesCode[^2..];
            if (!int.TryParse(temp, out var value)) return -1;
            return value;
        }



        /// <summary>
        /// 获取最近的目标点,从起点到目标点
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="startPoint"></param>
        /// <param name="targetPoints"></param>
        public static Base_Point? GetClosestPoint(Base_Agv agv, Base_Point startPoint, List<Base_Point> targetPoints)
        {
            int minDistance = -1;
            Base_Point? minPoint = null;
            int distance;
            try
            {
                foreach (Base_Point point in targetPoints)
                {
                    var pointList = GetPath.GetPathList(agv, startPoint, point);
                    distance = GetPathPointListDistance(pointList);

                    // distance = GetPath.GetPathList(agv, startPoint, point).Count;
                    if (distance == -1) continue;
                    if (minDistance == -1 || distance < minDistance)
                    {
                        minDistance = distance;
                        minPoint = point;
                    }

                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("GetClosestPoint", ex.Message + ex.StackTrace);
            }
            return minPoint;
        }



        /// <summary>
        /// 获取最近的目标点,从目标点到起点
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="startPoint"></param>
        /// <param name="targetPoints"></param>
        public static Base_Point? GetClosestPoint(Base_Agv agv, List<Base_Point> targetPoints, Base_Point startPoint)
        {
            int minDistance = -1;
            Base_Point? minPoint = null;

            try
            {
                List<Base_PathPoint> pointList;
                foreach (Base_Point targetPoint in targetPoints)
                {
                    int distance = 0;
                    agv = App.AgvList.Find(x => x.Barcode == targetPoint.Barcode) ?? throw new Exception($"目标点{targetPoint.Barcode}不存在agv");
                    //回家任务中的agv,距离应该加上到下一个路径点的距离
                    if (agv.AgvTask != null && agv.AgvTask.TaskType.Contains("回家"))
                    {
                        distance += GetToNextPathPointDistance(agv);
                        var pathList = GetPathPoints(agv);
                        var nextPoint = GetNextPathPoint(agv, pathList?.Get(1))?.Point ?? targetPoint;
                        pointList = GetPath.GetPathList(agv, nextPoint, startPoint, true);
                    }
                    else
                    {
                        pointList = GetPath.GetPathList(agv, targetPoint, startPoint, true);
                    }

                    distance += GetPathPointListDistance(pointList);

                    // distance = GetPath.GetPathList(agv, startPoint, point).Count;
                    if (distance == -1) continue;
                    if (minDistance == -1 || distance < minDistance)
                    {
                        minDistance = distance;
                        minPoint = targetPoint;
                    }

                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("GetClosestPoint", ex.Message + ex.StackTrace);
            }
            return minPoint;
        }

        /// <summary>
        /// 获取agv到下一个路径点的距离
        /// </summary>
        /// <param name="agv"></param>
        /// <returns></returns>
        public static int GetToNextPathPointDistance(Base_Agv agv)
        {
            var nextPoint = agv.AgvTask?.SubTaskList?.Get(0)?.PathPointList?.Get(1)?.Point;
            if (nextPoint == null) return 0;
            var distance = 0d;
            distance += Math.Abs(nextPoint.XLength - agv.XLength);
            distance += Math.Abs(nextPoint.YLength - agv.YLength);
            return (int)distance;

        }


        public static List<Base_PathPoint>? GetPathPoints(Base_Agv agv)
        {
            var subTaskList = agv.AgvTask?.SubTaskList;
            var pathList = subTaskList?.Get(0)?.PathPointList;
            return pathList;
        }
        /// <summary>
        /// 获取一段路径的总距离,当路径集合为空时返回-1
        /// </summary>
        /// <param name="pointList"></param>
        /// <returns></returns>
        public static int GetPathPointListDistance(List<Base_PathPoint> pointList)
        {

            var distance = 0;
            if (pointList.Count == 0) return -1;
            //获取两点之间的距离
            for (int i = 0; i < pointList.Count; i++)
            {
                if (i > pointList.Count - 2)
                    break;
                var currPoint = pointList[i].Point;
                var nextPoint = pointList[i + 1].Point;

                distance += GetPathPointDistance(currPoint, nextPoint);

            }
            return distance;
        }

        public static Config_TaskSplit? GetFirstMoveSubTaskConfig(List<Config_TaskSplit> splitList)
        {
            foreach (var item in splitList)
            {
                if (item.AgvTaskType.In(EnumMsg.ActionType.倒车行走, EnumMsg.ActionType.普通行走)
                    && item.FromPoint != item.ToPoint)
                {
                    return item;
                }

            }
            return null;
        }
        /// <summary>
        /// 计算两个角度的差值
        /// </summary>
        /// <param name="ori1">方向1</param>
        /// <param name="ori2"></param>
        /// <param name="accuracy">精度</param>
        /// <returns></returns>
        public static double GetAngleDifference(double ori1, double ori2, int accuracy = 10)
        {
            //
            var diff = ori1 - ori2;
            var half = 180 * accuracy;
            var round = 360 * accuracy;
            //处理超过360度的特殊情况
            if (diff < -half)
            {
                diff = -(round + diff);
            }
            else if (diff > half)
            {
                diff = -(round - diff);
            }
            return diff;
        }

        /// <summary>
        /// 获取两点之间的距离
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        public static int GetPathPointDistance(Base_Point startPoint, Base_Point endPoint)
        {
            var distance = 0;
            distance += Math.Abs(endPoint.XLength - startPoint.XLength);
            distance += Math.Abs(endPoint.YLength - startPoint.YLength);
            return distance;
        }
        public static bool IsOnPoint(Base_Point point, double x, double y, int error = 25)
        {
            return point.XLength.IntBetween(x, error) && point.YLength.IntBetween(y, error);
        }
        public static bool IsOnPoint(Base_Point point, Base_Agv agv, int error = 25)
        {
            return point.XLength.IntBetween(agv.XLength, error) && point.YLength.IntBetween(agv.YLength, error);
        }


        public static bool ReadBit(byte @byte, int index)
        {
            return (@byte & (1 << index)) != 0;
        }

        #region 日志记录

        public static string GetLogInfo<T>(T obj)
        {
            try
            {

                var type = typeof(T);
                var typeName = type.Name;
                Dictionary<string, (string desc, Func<object, object> func)>? propDict;
                lock (logLockObj)
                {
                    var flag = App.LogObjectInfoCache.TryGetValue(typeName, out propDict);
                    if (propDict == null)
                    {

                        try
                        {
                            propDict = new Dictionary<string, (string desc, Func<object, object> func)>();

                            PropertyInfo[] properties = type.GetProperties();
                            // 遍历每个属性
                            foreach (PropertyInfo property in properties)
                            {
                                // 获取属性名称
                                string propertyName = property.Name;

                                // 获取属性的 DescriptionAttribute
                                DescriptionAttribute? descriptionAttribute = (DescriptionAttribute?)property.GetCustomAttribute(typeof(DescriptionAttribute));
                                if (descriptionAttribute == null) continue;
                                string propertyDescription = descriptionAttribute.Description;

                                // 如果属性有 DescriptionAttribute,获取描述
                                // string propertyDescription = descriptionAttribute != null ? descriptionAttribute.Description : propertyName;
                                var getter = CreateGetter(type, property);
                                if (getter != null && !propDict.ContainsKey(propertyName))
                                    propDict.Add(propertyName, (propertyDescription, getter));
                            }
                        }
                        catch (Exception ex)
                        {
                            propDict = null;
                            App.ExFile.MessageError("PrintLog", $"{ex}");
                            return "";
                        }
                        if (!flag)
                            App.LogObjectInfoCache.Add(typeName, propDict);
                        else
                            App.LogObjectInfoCache[typeName] = propDict;

                    }
                }

                var log = new StringBuilder();
                foreach (var item in propDict)
                {
                    var (desc, func) = item.Value;
                    log.Append(desc)
                        .Append(':')
                        .Append('【')
                        .Append(func(obj!))
                        .Append('】')
                        .Append(' ');
                }
                return log.ToString();
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("PrintLog", $"{ex}");
                return "";
            }


        }


        #endregion

        #region 获取缓存属性相关表达式树


        public static Action<T, TValue> CreateSetter<T, TValue>(string propertyName)
        {
            var target = LinqExpressions.Expression.Parameter(typeof(T), "target");
            var value = LinqExpressions.Expression.Parameter(typeof(TValue), "value");
            var setter = typeof(T).GetProperty(propertyName)?.GetSetMethod();

            if (setter == null)
            {
                throw new ArgumentException($"No setter found for property {propertyName} on type {typeof(T).Name}");
            }

            var body = LinqExpressions.Expression.Call(target, setter, value);
            return LinqExpressions.Expression.Lambda<Action<T, TValue>>(body, target, value).Compile();
        }

        public static Action<object, object>? CreateSetter(Type type, string propertyName)
        {

            var property = type.GetProperty(propertyName);
            return property == null
                ? throw new ArgumentException($"No setter found for property {propertyName} on type {type.Name}")
                : CreateSetter(property);
        }
        private static Action<object, object>? CreateSetter(PropertyInfo property)
        {
            var type = property.DeclaringType;
            if (type == null)
            {
                throw new ArgumentException($"The declaring type of the property {property.Name} is null.");
            }

            var target = LinqExpressions.Expression.Parameter(typeof(object), "target");
            var value = LinqExpressions.Expression.Parameter(typeof(object), "value");

            // 获取属性的 setter 方法
            var setter = property.GetSetMethod(true);
            if (setter == null)
            {
                App.ExFile.MessageLog("CreateSetter", $"No setter found for property {property.Name} on type {type.Name}");
                return null;
            }

            // 将 target 转换为属性的声明类型
            var targetCast = LinqExpressions.Expression.Convert(target, type);

            // 将 value 转换为属性的类型
            var convertedValue = LinqExpressions.Expression.Convert(value, property.PropertyType);

            // 创建 setter 方法调用的表达式
            var setterCall = LinqExpressions.Expression.Call(targetCast, setter, convertedValue);

            // 创建 lambda 表达式并编译为委托
            var lambda = LinqExpressions.Expression.Lambda<Action<object, object>>(setterCall, target, value);
            return lambda.Compile();
        }
        public static Func<object, object>? CreateGetter(Type type, PropertyInfo property)
        {
            var target = LinqExpressions.Expression.Parameter(typeof(object), "target");

            if (property == null)
            {
                return null;
            }

            var getter = property.GetGetMethod() ?? throw new ArgumentException($"No getter found for property {property.Name} on type {type.Name}");
            var targetConverted = LinqExpressions.Expression.Convert(target, type);
            var body = LinqExpressions.Expression.Convert(LinqExpressions.Expression.Call(targetConverted, getter), typeof(object));

            return LinqExpressions.Expression.Lambda<Func<object, object>>(body, target).Compile();
        }
        /// <summary>
        /// 获取类的属性信息
        /// </summary>
        public static Dictionary<string, PropCache> GetPropsCacheInfo(Type type)
        {
            PropertyInfo[] properties = type.GetProperties();
            Dictionary<string, PropCache> propDict = new();

            // 获取属性名称
            string propertyName;
            Func<object, object>? getter;
            Action<object, object>? setter;
            string? propertyDescription;
            PropCache propCache;
            // 遍历每个属性
            foreach (PropertyInfo property in properties)
            {
                // 获取属性名称
                propertyName = property.Name;

                // 获取属性的 DescriptionAttribute
                DescriptionAttribute? descriptionAttribute = (DescriptionAttribute?)property.GetCustomAttribute(typeof(DescriptionAttribute));

                propertyDescription = descriptionAttribute?.Description;


                getter = CreateGetter(type, property);
                setter = CreateSetter(property);

                propCache = new PropCache
                {
                    Name = propertyName,
                    Getter = getter,
                    Setter = setter,
                    ClassName = type.Name,
                    ClassFullName = type.FullName,
                    Desc = propertyDescription,
                };
                if (getter != null && !propDict.ContainsKey(propertyName))
                    propDict.Add(propertyName, propCache);
            }
            return propDict;
        }
        #endregion
        /// <summary>
        /// 协议解析
        /// </summary>
        /// <param name="protocolDetails"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static List<ProtocolDetail> ProtocolAnalysis(IEnumerable<ProtocolDetail> protocolDetails, byte[] bytes)
        {
            List<ProtocolDetail> details = [];
            foreach (var detail in protocolDetails.OrderBy(x => x.SerialNo))
            {
                //可以使用字段缓存类信息然后在此获取,目前只实现AGV
                // var classs = typeof(T).Name;
                object? value = null;
                ProtocolDetail? protocolDetail;
                switch (detail.DataType)
                {
                    case EnumMsg.DataType.BOOL:
                        var temp = detail.StartIndex.ToString().Split('.');
                        if (temp.Length < 2) throw new Exception($"属性{detail.Code}对应的数据起始索引{detail.StartIndex}与类型BOOL不匹配,必须为小数类型");
                        var bit = int.Parse(temp[1]);
                        var @byte = bytes[(int)detail.StartIndex];
                        value = ReadBit(@byte, bit);
                        break;
                    case EnumMsg.DataType.BYTE:
                        value = bytes[(int)detail.StartIndex];
                        break;
                    case EnumMsg.DataType.SHORT:
                        value = BitConverter.ToInt16(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.USHORT:
                        value = BitConverter.ToUInt16(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.INT:
                        value = BitConverter.ToInt32(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.UINT:
                        value = BitConverter.ToUInt32(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.LONG:
                        value = BitConverter.ToInt64(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.ULONG:
                        value = BitConverter.ToUInt64(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.FLOAT:
                        value = BitConverter.ToSingle(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.DOUBLE:
                        value = BitConverter.ToDouble(bytes, (int)detail.StartIndex);
                        break;
                    case EnumMsg.DataType.STRING:
                        value = BitConverter.ToString(bytes, (int)detail.StartIndex, (int)(detail.EndIndex - detail.StartIndex));
                        break;
                    default:
                        break;

                }
                protocolDetail = detail.Clone() as ProtocolDetail;
                if (detail.DestDataType != null)
                {
                    value = TypeChange(value, detail.DestDataType);
                }
                protocolDetail!.Value = value;
                details.Add(protocolDetail);
            }
            return details;

        }


        /// <summary>
        /// 协议模板数据赋值给对象的实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="protocolDetails"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static void ProtocolTempToInstance<T>(IEnumerable<ProtocolDetail> protocolDetails, T obj)
        {
            foreach (var detail in protocolDetails.OrderBy(x => x.SerialNo))
            {
                //可以使用字段缓存类信息然后在此获取,目前只实现AGV
                var classs = typeof(T).Name;
                if (App.AgvPropCache.TryGetValue(detail.Code, out var propCache))
                {
                    try
                    {
                        propCache.Setter!(obj!, detail.Value);
                    }
                    catch (Exception ex)
                    {
                        App.ExFile.MessageError("ProtocolTempToInstance", $"属性{propCache.Name}类型转换失败{ex}");
                    }
                }
            }

        }


        public static void ProtocolAnalysis<T>(IEnumerable<ProtocolDetail> protocolDetails, byte[] bytes, T obj)
        {

            foreach (var detail in protocolDetails.OrderBy(x => x.SerialNo))
            {
                //可以使用字段缓存类信息然后在此获取,目前只实现AGV
                var classs = typeof(T).Name;
                if (App.AgvPropCache.TryGetValue(detail.Name, out var propCache))
                {
                    object? value = null;
                    switch (detail.DataType)
                    {
                        case EnumMsg.DataType.BOOL:
                            var temp = detail.StartIndex.ToString().Split('.');
                            if (temp.Length < 2) throw new Exception($"属性{detail.Code}对应的数据起始索引{detail.StartIndex}与类型BOOL不匹配,必须为小数类型");
                            var bit = int.Parse(temp[1]);
                            var @byte = bytes[(int)detail.StartIndex];
                            value = ReadBit(@byte, bit);
                            break;
                        case EnumMsg.DataType.BYTE:
                            value = bytes[(int)detail.StartIndex];
                            break;
                        case EnumMsg.DataType.SHORT:
                            value = BitConverter.ToInt16(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.USHORT:
                            value = BitConverter.ToUInt16(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.INT:
                            value = BitConverter.ToInt32(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.UINT:
                            value = BitConverter.ToUInt32(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.LONG:
                            value = BitConverter.ToInt64(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.ULONG:
                            value = BitConverter.ToUInt64(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.FLOAT:
                            value = BitConverter.ToSingle(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.DOUBLE:
                            value = BitConverter.ToDouble(bytes, (int)detail.StartIndex);
                            break;
                        case EnumMsg.DataType.STRING:
                            value = BitConverter.ToString(bytes, (int)detail.StartIndex, (int)(detail.EndIndex - detail.StartIndex));
                            break;
                        default:
                            break;
                    }

                    propCache.Setter!(obj!, value);
                }


            }


        }



        public static object? TypeChange(object? value, EnumMsg.DataType? type)
        {

            switch (type)
            {
                case EnumMsg.DataType.BOOL:
                    value = Convert.ToBoolean(value);
                    break;
                case EnumMsg.DataType.BYTE:
                    value = Convert.ToByte(value);
                    break;
                case EnumMsg.DataType.SHORT:
                    value = Convert.ToInt16(value);
                    break;
                case EnumMsg.DataType.USHORT:
                    value = Convert.ToUInt16(value);
                    break;
                case EnumMsg.DataType.INT:
                    value = Convert.ToInt32(value);
                    break;
                case EnumMsg.DataType.UINT:
                    value = Convert.ToUInt32(value);
                    break;
                case EnumMsg.DataType.LONG:
                    value = Convert.ToInt64(value);
                    break;
                case EnumMsg.DataType.ULONG:
                    value = Convert.ToUInt64(value);
                    break;
                case EnumMsg.DataType.FLOAT:
                    value = Convert.ToSingle(value);
                    break;
                case EnumMsg.DataType.DOUBLE:
                    value = Convert.ToDouble(value);
                    break;
                case EnumMsg.DataType.STRING:
                    value = value?.ToString();
                    break;
                case EnumMsg.DataType.Direction:
                    var tempShort = Convert.ToInt16(value);
                    value = (EnumMsg.Direction)tempShort;// Convert.ChangeType(value!, typeof(Direction));
                    break;
                case EnumMsg.DataType.HeightState:
                    var temp = Convert.ToUInt32(value);
                    value = (EnumMsg.HeightState)temp;
                    //  value = Convert.ChangeType(value!, typeof(HeightState));
                    break;
                case EnumMsg.DataType.ActState:
                    temp = Convert.ToUInt32(value);
                    value = (EnumMsg.ActState)temp;
                    //value = Convert.ChangeType(value!, typeof(ActState));
                    break;
                case EnumMsg.DataType.AGVErrorCode:
                    var temp1 = Convert.ToUInt64(value);
                    value = (AGVErrorCode)temp1;
                    //value = Convert.ChangeType(value!, typeof(AGVErrorCode));
                    break;
                case EnumMsg.DataType.SensorStatus:
                    temp = Convert.ToUInt32(value);
                    value = (EnumMsg.SensorStatus)temp;
                    //value = Convert.ChangeType(value!, typeof(EnumMsg.SensorStatus));
                    break;
                case EnumMsg.DataType.RollerState:
                    temp = Convert.ToUInt32(value);
                    value = (EnumMsg.RollerState)temp;
                    //value = Convert.ChangeType(value!, typeof(EnumMsg.RollerState));
                    break;
                case EnumMsg.DataType.AGVState:
                    temp = Convert.ToUInt32(value);
                    value = (EnumMsg.AGVState)temp;
                    // value = Convert.ChangeType(value!, typeof(EnumMsg.AGVState));

                    break;
                case EnumMsg.DataType.ActionType:
                    temp = Convert.ToUInt32(value);
                    value = (EnumMsg.ActionType)temp;

                    break;
                default:
                    value = value?.ToString();
                    break;

            }
            return value;
        }

    }
}