ServiceImpl.cs 46.4 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
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Business;
using XingYe_ACS.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace XingYe_ACS.WebApi
{
    public class ServiceImpl : IService
    {
        /// <summary>
        /// 增删改RFID值
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage ChangePoint(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(ChangePoint)," + s + "\r");
                Point point = JsonConvert.DeserializeObject<Point>(s);
                string strBarcode = point.strBarcode;
                string areaType = point.AreaType;
                string type = point.type;
                switch (type)
                {
                    case "insert":
                        if (!App.PointList.Exists(a => a.strBarcode == strBarcode && a.AreaType == areaType))
                        {
                            bool insertSql = UpdateManage.SqlPointMsg(point, type);
                            if (!insertSql) return ReMessageFactory.Error(1, "插入RFID失败!");
                        }
                        break;
                    case "delete":
                        bool delPointSql = UpdateManage.SqlPointMsg(point, type);
                        if (!delPointSql) return ReMessageFactory.Error(1, "删除RFID失败!");
                        App.PointList.RemoveAll(a => a.AreaType == point.AreaType);
                        App.StationList.RemoveAll(a => a.stationArea == point.AreaType && (a.stationType != EnumMsg.StationType.料台));
                        //App.Config_PointList.RemoveAll(a => a.areaType == point.AreaType);
                        break;
                    case "save":
                        string ErrMsg = "";
                        Init.DownPoint(ref ErrMsg);
                        if (ErrMsg != "") return ReMessageFactory.Error(1, ErrMsg);
                        //bool sqlConfigPoint = UpdateManage.SqlConfigPointMsg(App.PointList.FindAll(a => a.AreaType == areaType));
                        //if (!sqlConfigPoint) return ReMessageFactory.Error(1, "旋转点绑定失败!");
                        //获取所旋转点,将旋转点周围点放到一个集合中,插入数据库中
                        //Init.DownConfigPoint(ref ErrMsg);
                        //if(ErrMsg != "") return ReMessageFactory.Error(1, ErrMsg);
                        break;
                    case "simInsert":
                        if (App.DemoPointList.Exists(a => a.strBarcode == strBarcode && a.AreaType == areaType))
                            return ReMessageFactory.Error(1, "当前" + areaType + "区域已存在相同RFID!");
                        bool simInsertSql = UpdateManage.SqlDemoPointMsg(point, type);
                        if (!simInsertSql) return ReMessageFactory.Error(1, "插入RFID失败!");
                        break;
                    case "simDelete":
                        bool simDeleteSql = UpdateManage.SqlDemoPointMsg(point, type);
                        if (!simDeleteSql) return ReMessageFactory.Error(1, "删除RFID失败!");
                        App.DemoPointList.RemoveAll(a => a.AreaType == point.AreaType);
                        break;
                    case "simSave":
                        string DemoErrMsg = "";
                        Init.DownDemoPoint(ref DemoErrMsg);
                        if (DemoErrMsg != "") return ReMessageFactory.Error(1, DemoErrMsg);
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(ChangePoint)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess1("建立成功");
        }

        /// <summary>
        /// 增删改AGV信息
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage ChangeAGV(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(ChangeAGV)," + s + "\r");
                Agv agv = JsonConvert.DeserializeObject<Agv>(s);
                string strAgvNo = agv.strAgvNo;
                string agvGroup = agv.agvGroup;
                agv.agvState = 0;
                agv.heightState = 0;
                agv.currentCharge = 0;
                agv.isOnline = false;
                agv.strStopReason = "";
                string type = agv.type;
                if (strAgvNo == "" || agvGroup == "") return ReMessageFactory.Error(1, "AGV输入有误或群组为空!");
                Agv getAgv = App.AgvList.FirstOrDefault(a => a.strAgvNo == strAgvNo);
                switch (type)
                {
                    case "insert":
                        if (getAgv != null) return ReMessageFactory.Error(1, "已经存在当前AGV!");
                        bool insertSql = UpdateManage.SqlAgvMsg(agv, type);
                        if (!insertSql) return ReMessageFactory.Error(1, "插入AGV失败!");
                        //插入数据库的车,插到缓存表中
                        App.AgvList.Add(agv);
                        break;
                    case "update":
                        if (getAgv == null) return ReMessageFactory.Error(1, "不存在更改的AGV!");
                        if (!agv.isEnable && getAgv.agvTask != null)
                        {
                            if (getAgv.agvTask.strTaskID == "回家" || getAgv.agvTask.strTaskID == "回家-1"
                                || getAgv.agvTask.strTaskID == "充电" || getAgv.agvTask.strTaskID == "取消充电")
                            {
                                getAgv.agvTask.taskState = "Cancel";
                                ComnMethod.DeleteTask(getAgv.agvTask);
                            }
                            else
                            {
                                return ReMessageFactory.Error(1, "当前AGV存在任务,请删除任务后,再下线!");
                            }
                        }
                        getAgv.isEnable = agv.isEnable;
                        getAgv.strChooseStation = agv.strChooseStation;
                        getAgv.agvGroup = agv.agvGroup;
                        getAgv.agvAreaType = agv.agvAreaType;
                        getAgv.agvMap = agv.agvMap;
                        if (!getAgv.isEnable)
                        {
                            getAgv.strBarcode = "0";
                            getAgv.isOnline = false;
                            getAgv.agvState = 0;
                            List<Point> agvLockedList = App.PointList.Where(a => a.lockedAgv == getAgv).ToList();
                            foreach (Point point in agvLockedList)
                            {
                                point.lockedAgv = null;
                            }
                        }
                        UpdateManage.UpdateAgvMsg(getAgv);
                        break;
                    case "delete":
                        if (getAgv == null) return ReMessageFactory.Error(1, "不存在当前AGV!");
                        bool delSql = UpdateManage.SqlAgvMsg(agv, type);
                        App.AgvList.Remove(getAgv);
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(ChangeAGV)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 增删改站台信息
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage ChangeStation(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(ChangeStation)," + s + "\r");
                Station station = JsonConvert.DeserializeObject<Station>(s);
                int stationType = (int)station.stationType;
                //string stationGroup = station.stationState.ToString();
                string stationArea = station.stationArea;
                bool isEnable = station.isEnable;
                bool isLocked = station.isLocked;
                string strBarcode = station.strBarcode;
                string strStationNo = station.strStationNo;
                station.strPreBarcode = "0";
                string type = station.type;
                switch (type)
                {
                    case "insert":
                        bool insertSql = UpdateManage.SqlStationMsg(station, type);
                        if (!insertSql) return ReMessageFactory.Error(1, "插入站台失败!");
                        App.StationList.Add(station);
                        break;
                    case "update":
                        Station cs = App.StationList.Find(a => a.strStationNo == strStationNo);
                        cs.isEnable = isEnable;
                        cs.isLocked = isLocked;
                        UpdateManage.UpdateStation(cs);
                        break;
                    case "delete":
                        bool delSql = UpdateManage.SqlStationMsg(station, type);
                        App.StationList.RemoveAll(a => a.strStationNo == strStationNo);
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(ChangeStation)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 关机
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage CloseAgv(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(CloseAgv)," + s + "\r");
                offAgv offAgv = JsonConvert.DeserializeObject<offAgv>(s);
                App.offAgv = 1;
                App.offTime = DateTime.Now;
                App.offAgvNo.Clear();
                if (offAgv.offType == 2)
                {
                    Agv getAgv = App.AgvList.FirstOrDefault(a => a.strAgvNo == offAgv.agvNo);
                    if (getAgv != null)
                    {
                        App.offAgvNo.Add(offAgv.agvNo);
                    }
                }
                else if (offAgv.offType == 1)
                {
                    foreach (Agv agv in App.AgvList.FindAll(a => a.isEnable && a.isOnline))
                    {
                        App.offAgvNo.Add(agv.strAgvNo);
                        if (agv.agvState == EnumMsg.State.充电中 && agv.agvTask == null)
                        {
                            Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                            if (point != null)
                                TaskManage.TaskBuild("取消充电", "", "", 1, agv.strAgvNo, "", point, point, "");
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 连接需要单步控制的AGV
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage LinkAgv(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(LinkAgv)," + s + "\r");
                LinkAgv linkAgv = JsonConvert.DeserializeObject<LinkAgv>(s);
                string agvNo = linkAgv.agvNo;
                bool link = linkAgv.link;
                Agv agv = App.AgvList.FirstOrDefault(a => a.strAgvNo == agvNo);
                if (agv == null || !agv.isOnline || !agv.isEnable) return ReMessageFactory.Error(1, "AGV编号输入错误!");
                if (link)
                {
                    if (App.SingleControlList.Exists(a => a.agvNo == agvNo)) return ReMessageFactory.Error(1, "当前AGV已被操作!");
                    //连接agv
                    SingleControl singleAgv = new SingleControl();
                    singleAgv.agvNo = agvNo;
                    singleAgv.agvAction = 200;
                    App.SingleControlList.Add(singleAgv);
                }
                else
                {
                    //断开AGV
                    SingleControl singleAgv = App.SingleControlList.Find(a => a.agvNo == agvNo);
                    if (singleAgv == null) return ReMessageFactory.Sucess("建立成功");
                    singleAgv.agvAction = 209;
                }
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 单步控制AGV
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage ControlAgv(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(ControlAgv)," + s + "\r");
                LinkAgv linkAgv = JsonConvert.DeserializeObject<LinkAgv>(s);
                SingleControl singleAgv = App.SingleControlList.Find(a => a.agvNo == linkAgv.agvNo);
                if (singleAgv == null) return ReMessageFactory.Error(1, "请断开连接,并重新连接AGV!");
                singleAgv.agvAction = linkAgv.action;
                Console.WriteLine(singleAgv.agvAction);
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 满电,安全,危险电量更改
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage KeyValue(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(KeyValue)," + s + "\r");
                KeyValue keyValue = JsonConvert.DeserializeObject<KeyValue>(s);
                switch (keyValue.keyVariable)
                {
                    case "Cfull":
                        App.FullCharge = keyValue.value;
                        break;
                    case "Savety":
                        App.SafeCharge = keyValue.value;
                        break;
                    case "Danger":
                        App.DangerCharge = keyValue.value;
                        break;
                    case "MinCharges":
                        App.MinCharges = keyValue.value;
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
                string sqlInsert = string.Format(@"UPDATE  T_Config_KeyValue SET Value = '{1}' WHERE KeyVariable = '{0}';"
                                                , keyValue.keyVariable, keyValue.value);
                int intSqlResult = DbHelperSQL.ExecuteSql(sqlInsert);
                if (intSqlResult < 1) return ReMessageFactory.Error(1, "插入RFID失败!");
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(KeyValue)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 对未执行的任务状态更改,暂停/删除
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskChange(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(TaskChange)," + s + "\r");
                FTask fTask = JsonConvert.DeserializeObject<FTask>(s);
                string strTaskNum = fTask.taskNum;
                string strTaskState = fTask.taskState;
                FTask getFTask = App.TaskList.FirstOrDefault(a => a.strTaskNo == strTaskNum);
                if (getFTask == null) return ReMessageFactory.Error(1, "当前任务已被删除,无法操作");
                switch (strTaskState)
                {
                    case "Stop":
                    case "Cancel":
                        getFTask.taskState = fTask.taskState;
                        string strResult = ComnMethod.DeleteTask(getFTask);
                        if (strResult != "") return ReMessageFactory.Error(1, strResult);
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(TaskChange)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 任务分解
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskSplit(Stream stream)
        {
            try
            {
                //2取3放9停
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "WEB发送数据(TaskSplit)," + s + "\r");
                int intSqlResult = 0;
                Web_TaskSplit config_TaskSplit = JsonConvert.DeserializeObject<Web_TaskSplit>(s);
                string taskID = config_TaskSplit.taskID;
                int intSerialNo = config_TaskSplit.intSeriaNo;
                int agvTaskType = (int)config_TaskSplit.agvTaskType;
                Point fromPoint = App.PointList.FirstOrDefault(a => a.strBarcode == config_TaskSplit.fromPoint);
                Point toPoint = App.PointList.FirstOrDefault(a => a.strBarcode == config_TaskSplit.toPoint);
                int waitTime = config_TaskSplit.waitTime;
                string type = config_TaskSplit.type;
                //任务ID重复,删除已经存在的任务ID,添加新的ID
                if (App.Config_TaskSplitList.Exists(a => a.taskID == taskID && intSerialNo == 1) || type == "delete")
                {
                    string sqlDelete = string.Format(@"DELETE FROM  T_Config_TaskSplit WHERE taskID='{0}';", taskID);
                    intSqlResult = DbHelperSQL.ExecuteSql(sqlDelete);
                    if (intSqlResult < 1) return ReMessageFactory.Error(1, "删除任务分解失败!");
                    //插入数据库的车,插到缓存表中
                    App.Config_TaskSplitList.RemoveAll(a => a.taskID == config_TaskSplit.taskID);
                }
                if (type == "insert")
                {
                    //当前任务ID最大编号
                    int oldSerialNo = 0;
                    if (intSerialNo > 1)
                    {
                        oldSerialNo = App.Config_TaskSplitList.Where(a => a.taskID == taskID).Max(b => b.intSerialNo);
                    }
                    List<Config_TaskSplit> listSplits = new List<Config_TaskSplit>();
                    int index = 0;
                    if (fromPoint != null && fromPoint.AreaType != toPoint.AreaType)
                    {
                        index = 3;
                    }
                    else
                    {
                        index = 2;
                        if (fromPoint != null && fromPoint.AreaType == toPoint.AreaType && fromPoint.AreaType == "P5_C区_1F" && intSerialNo != 3)
                        {
                            index = 4;
                        }
                    }
                    for (int i = 1; i <= index; i++)
                    {
                        Config_TaskSplit taskSplit = new Config_TaskSplit();
                        taskSplit.taskID = taskID;
                        taskSplit.intSerialNo = oldSerialNo + i;
                        taskSplit.taskRequestType = 0;
                        if (i % index == 1)
                        {
                            taskSplit.agvTaskType = EnumMsg.AgvTaskType.行走;
                            if (fromPoint == null)
                            {
                                taskSplit.fromBarcode = EnumMsg.ConfigToFromPoint.小车当前点.GetIndexString();
                                taskSplit.toBarcode = toPoint.strBarcode;
                            }
                            else
                            {
                                taskSplit.fromBarcode = fromPoint.strBarcode;
                                if (index == 2)
                                {
                                    taskSplit.toBarcode = toPoint.strBarcode;
                                }
                                else if (index == 3)
                                {
                                    //跨區域獲取終點最近电梯点
                                    taskSplit.toBarcode = ComnMethod.GetMinLiftPoint(toPoint).strBarcode;
                                }
                                else
                                {
                                    //同區域跨兩個電梯獲取起點最近電梯點
                                    taskSplit.toBarcode = ComnMethod.GetMinLiftPoint(fromPoint).strBarcode;
                                }
                            }
                        }
                        else if (i % index == 2 || i % index == 3)
                        {
                            taskSplit.agvTaskType = EnumMsg.AgvTaskType.行走;
                            if (index == 4)
                            {
                                if (i % index == 2)
                                {
                                    taskSplit.fromBarcode = ComnMethod.GetMinLiftPoint(fromPoint).strBarcode;
                                    taskSplit.toBarcode = ComnMethod.GetMinLiftPoint(toPoint).strBarcode;
                                }
                                else
                                {
                                    taskSplit.fromBarcode = ComnMethod.GetMinLiftPoint(toPoint).strBarcode;
                                    taskSplit.toBarcode = toPoint.strBarcode;
                                }
                            }
                            else if (index == 3)
                            {
                                taskSplit.fromBarcode = ComnMethod.GetMinLiftPoint(toPoint).strBarcode;
                                taskSplit.toBarcode = toPoint.strBarcode;
                            }
                        }
                        else
                        {
                            taskSplit.agvTaskType = (EnumMsg.AgvTaskType)agvTaskType;
                            taskSplit.fromBarcode = toPoint.strBarcode;
                            taskSplit.toBarcode = toPoint.strBarcode;
                            taskSplit.waitTime = waitTime;
                        }
                        if (i % index == 2 || i % index == 1 || i % index == 3)
                        {
                            Config_TaskSplit beforeSplit = App.Config_TaskSplitList.Find(a => a.intSerialNo == taskSplit.intSerialNo - 1
                                                     && a.taskID == taskID);
                            if (intSerialNo > 1 && ((beforeSplit != null && beforeSplit.agvTaskType == EnumMsg.AgvTaskType.顶升)
                                || ((listSplits.Count == 1 || listSplits.Count == 2) && listSplits.Exists(a => a.isCarry))))
                            {
                                taskSplit.isCarry = true;
                            }
                            else
                            {
                                taskSplit.isCarry = false;
                            }
                        }
                        listSplits.Add(taskSplit);
                    }
                    foreach (Config_TaskSplit Split in listSplits)
                    {
                        //更改taskRequestType
                        //if (Split.intSerialNo == 1) Split.taskRequestType = EnumMsg.TaskRequestType.取货请求;
                        //if (Split.intSerialNo == 2) Split.taskRequestType = EnumMsg.TaskRequestType.取货完成请求;
                        string sqlInsert = string.Format(@"INSERT INTO  T_Config_TaskSplit(taskID,intSerialNo,agvTaskType,fromPoint,toPoint,waitTime,isCarry,taskRequestType)VALUES
                                                            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');"
                                        , Split.taskID, Split.intSerialNo, (int)Split.agvTaskType, Split.fromBarcode, Split.toBarcode, Split.waitTime, Split.isCarry == false ? "0" : "1", (int)Split.taskRequestType);
                        intSqlResult = DbHelperSQL.ExecuteSql(sqlInsert);
                        if (intSqlResult < 1) return ReMessageFactory.Error(1, "插入任务分解失败!");
                        //插入数据库的车,插到缓存表中
                        App.Config_TaskSplitList.Add(Split);
                    }
                }
                else if (type != "delete")
                {
                    return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(TaskSplit)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 任务AGV群组绑定
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskAgvGroup(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "WEB发送数据(TaskAgvGroup)," + s + "\r");
                TaskAgvGroup taskAgv = JsonConvert.DeserializeObject<TaskAgvGroup>(s);
                switch (taskAgv.type)
                {
                    case "insert":
                        bool insertSql = UpdateManage.SqlTaskAgvGroup(taskAgv, taskAgv.type);
                        if (!insertSql) return ReMessageFactory.Error(1, "插入任务AGV群组绑定失败");
                        App.TaskAgvGroupList.Add(taskAgv);
                        break;
                    case "delete":
                        bool deleteSql = UpdateManage.SqlTaskAgvGroup(taskAgv, taskAgv.type);
                        if (!deleteSql) return ReMessageFactory.Error(1, "删除任务AGV群组绑定失败");
                        App.TaskAgvGroupList.Remove(taskAgv);
                        break;
                    default:
                        return ReMessageFactory.Error(1, "类型错误");
                }
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 任务预览
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskPreview(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "WEB发送数据(TaskPreview)," + s + "\r");
                List<Web_TaskSplit> webTaskSplitList = JsonConvert.DeserializeObject<List<Web_TaskSplit>>(s);
                List<Web_TaskSplit> acsTaskSplits = new List<Web_TaskSplit>();
                //返回任务路径
                List<TaskPath> pathList = new List<TaskPath>();
                int index = 1;
                for (int i = 0; i < webTaskSplitList.Count; i++)
                {
                    PathAStar pathAStar = new PathAStar();
                    List<Point> listApoint = App.PointList.FindAll(a => a.pointType != EnumMsg.PointType.充电点);
                    Point startPoint = App.PointList.Find(a => a.strBarcode == webTaskSplitList[i].fromPoint);
                    if (webTaskSplitList[i].fromArea != null)
                    {
                        startPoint = App.PointList.Find(a => a.strBarcode == startPoint.strBarcode && a.AreaType == webTaskSplitList[i].fromArea);
                    }
                    Point endPoint = App.PointList.Find(a => a.strBarcode == webTaskSplitList[i].toPoint);
                    if (webTaskSplitList[i].toArea != null)
                    {
                        endPoint = App.PointList.Find(a => a.strBarcode == endPoint.strBarcode && a.AreaType == webTaskSplitList[i].toArea);
                    }
                    //不再同一区域
                    if (startPoint.AreaType != endPoint.AreaType)
                    {
                        //找到同一区域电梯点
                        endPoint = App.PointList.Find(a => a.AreaType == startPoint.AreaType && a.strBarcode == ComnMethod.GetMinLiftPoint(endPoint).strBarcode);
                        Web_TaskSplit taskSplit = new Web_TaskSplit();
                        //获取当前不同区域终点
                        Point toPoint = App.PointList.Find(a => a.strBarcode == webTaskSplitList[i].toPoint);
                        taskSplit.fromPoint = App.PointList.Find(a => a.AreaType == toPoint.AreaType && a.pointType == EnumMsg.PointType.电梯点).strBarcode;
                        taskSplit.toPoint = toPoint.strBarcode;
                        taskSplit.fromArea = toPoint.AreaType;
                        webTaskSplitList.Insert(i + 1, taskSplit);
                    }
                    listApoint.Add(endPoint);
                    GetPath.ClearParentPoint();
                    Point Parent = pathAStar.GetPathPoint(listApoint, startPoint, endPoint, null, 0);
                    if (Parent == null || (Parent != null && Parent != endPoint))
                    {
                        //找不到路径判断是否有电梯
                        if (startPoint.AreaType == endPoint.AreaType && startPoint.pointType != EnumMsg.PointType.电梯点 && endPoint.pointType != EnumMsg.PointType.电梯点)
                        {
                            Point startLiftPoint = ComnMethod.GetMinLiftPoint(startPoint);
                            Point endLiftPoint = ComnMethod.GetMinLiftPoint(endPoint);
                            if (startLiftPoint != null && endLiftPoint != null && startLiftPoint != endLiftPoint)
                            {
                                for (int j = 1; j < 4; j++)
                                {
                                    //获取当前区域电梯电点
                                    Web_TaskSplit taskSplit = new Web_TaskSplit();
                                    //获取当前不同区域终点
                                    switch (j)
                                    {
                                        case 1:
                                            taskSplit.fromPoint = startPoint.strBarcode;
                                            taskSplit.toPoint = startLiftPoint.strBarcode;
                                            taskSplit.toArea = startPoint.AreaType;
                                            break;
                                        case 2:
                                            taskSplit.fromPoint = startLiftPoint.strBarcode;
                                            taskSplit.toPoint = endLiftPoint.strBarcode;
                                            taskSplit.fromArea = App.PointList.Find(a => a.AreaType != startLiftPoint.AreaType && a.strBarcode == taskSplit.fromPoint).AreaType;
                                            taskSplit.toArea = App.PointList.Find(a => a.AreaType != startLiftPoint.AreaType && a.strBarcode == taskSplit.toPoint).AreaType;
                                            break;
                                        case 3:
                                            taskSplit.fromPoint = endLiftPoint.strBarcode;
                                            taskSplit.toPoint = endPoint.strBarcode;
                                            taskSplit.fromArea = endLiftPoint.AreaType;
                                            break;
                                        default:
                                            break;

                                    }
                                    webTaskSplitList.Insert(i + j, taskSplit);
                                }
                                continue;
                            }
                        }
                        //else if () { }
                        return ReMessageFactory.Error(1, "未找到路径;请查看坐标(" + (Parent.intX - 1) * 30 + "," + (Parent.intY - 1) * 30 + ")附近是否配置错误");
                    }
                    List<PathPoint> listPathPoint = GetPath.GetPathPointList(Parent);
                    //获取路径方点的方向
                    GetPath.GetPathDirection(listPathPoint);
                    for (int j = 0; j < listPathPoint.Count; j++)
                    {
                        TaskPath taskPath = new TaskPath();
                        taskPath.areaType = listPathPoint[j].point.AreaType;
                        taskPath.intSerialNo = index;
                        taskPath.strBarcode = listPathPoint[j].point.strBarcode;
                        taskPath.intX = listPathPoint[j].point.intX;
                        taskPath.intY = listPathPoint[j].point.intY;
                        taskPath.pathDriection = (int)listPathPoint[j].pathDirection;
                        if (taskPath.pathDriection == 0 && i + 1 < webTaskSplitList.Count) continue;
                        pathList.Add(taskPath);
                        index++;
                    }
                }
                //转换为JSON形式,返回WEB界面
                string json = JsonConvert.SerializeObject(pathList);
                return ReMessageFactory.Sucess(json);
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
        }

        /// <summary>
        /// Demo任务预览
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage DemoTaskPreview(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "WEB发送数据(DemoTaskPreview)," + s + "\r");
                DemoWeb_TaskSplit webTaskSplitList = JsonConvert.DeserializeObject<DemoWeb_TaskSplit>(s);
                List<Web_TaskSplit> acsTaskSplits = new List<Web_TaskSplit>();
                //返回任务路径
                List<TaskPath> pathList = new List<TaskPath>();
                int index = 1;
                for (int i = 0; i < webTaskSplitList.taskPreviewModels.Count; i++)
                {
                    PathAStar pathAStar = new PathAStar();
                    List<Point> listApoint = App.DemoPointList.FindAll(a => a.pointType != EnumMsg.PointType.充电点
                                                    && a.AreaType == webTaskSplitList.areaType);
                    Point startPoint = App.DemoPointList.Find(a => a.strBarcode == webTaskSplitList.taskPreviewModels[i].fromPoint && a.AreaType == webTaskSplitList.areaType);
                    Point endPoint = App.DemoPointList.Find(a => a.strBarcode == webTaskSplitList.taskPreviewModels[i].toPoint && a.AreaType == webTaskSplitList.areaType);
                    if (startPoint == null && endPoint == null) continue;
                    if (startPoint == null)
                    {
                        startPoint = ComnMethod.GetMinLiftDemoPoint(endPoint);
                    }
                    else if (endPoint == null)
                    {
                        endPoint = ComnMethod.GetMinLiftDemoPoint(startPoint);
                    }
                    listApoint.Add(endPoint);
                    GetPath.ClearDemoParentPoint();
                    Point Parent = pathAStar.GetPathPoint(listApoint, startPoint, endPoint, null, 0);
                    if (Parent == null || (Parent != null && Parent.strBarcode != endPoint.strBarcode))
                    {
                        if (startPoint.AreaType == endPoint.AreaType && startPoint.pointType != EnumMsg.PointType.电梯点
                                && endPoint.pointType != EnumMsg.PointType.电梯点)
                        {
                            Point startLiftPoint = ComnMethod.GetMinLiftDemoPoint(startPoint);
                            Point endLiftPoint = ComnMethod.GetMinLiftDemoPoint(endPoint);
                            for (int j = 1; j < 3; j++)
                            {
                                //获取当前区域电梯电点
                                Web_TaskSplit taskSplit = new Web_TaskSplit();
                                //获取当前不同区域终点
                                switch (j)
                                {
                                    case 1:
                                        taskSplit.fromPoint = startPoint.strBarcode;
                                        taskSplit.toPoint = startLiftPoint.strBarcode;
                                        taskSplit.toArea = startPoint.AreaType;
                                        break;
                                    case 2:
                                        taskSplit.fromPoint = endLiftPoint.strBarcode;
                                        taskSplit.toPoint = endPoint.strBarcode;
                                        taskSplit.fromArea = endLiftPoint.AreaType;
                                        break;
                                    default:
                                        break;

                                }
                                webTaskSplitList.taskPreviewModels.Insert(i + j, taskSplit);
                            }
                            continue;
                        }
                        return ReMessageFactory.Error(1, "未找到路径;请查看坐标(" + (Parent.intX - 1) * 30 + "," + (Parent.intY - 1) * 30 + ")附近是否配置错误");
                    }
                    List<PathPoint> listPathPoint = GetPath.GetPathPointList(Parent);
                    //获取路径方点的方向
                    GetPath.GetPathDirection(listPathPoint);
                    for (int j = 0; j < listPathPoint.Count; j++)
                    {
                        TaskPath taskPath = new TaskPath();
                        taskPath.areaType = listPathPoint[j].point.AreaType;
                        taskPath.intSerialNo = index;
                        taskPath.strBarcode = listPathPoint[j].point.strBarcode;
                        taskPath.intX = listPathPoint[j].point.intX;
                        taskPath.intY = listPathPoint[j].point.intY;
                        taskPath.pathDriection = (int)listPathPoint[j].pathDirection;
                        if (taskPath.pathDriection == 0 && i + 1 < webTaskSplitList.taskPreviewModels.Count) continue;
                        pathList.Add(taskPath);
                        index++;
                    }
                }
                //转换为JSON形式,返回WEB界面
                string json = JsonConvert.SerializeObject(pathList);
                return ReMessageFactory.Sucess(json);
            }
            catch (Exception ex)
            {
                return ReMessageFactory.Error(1, ex.ToString());
            }
        }

        /// <summary>
        /// wms确认任务完成
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskFinish(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(ChangePoint)," + s + "\r");
                stationNo stationNo = JsonConvert.DeserializeObject<stationNo>(s);
                string station = stationNo.cuStNo;
                int state = stationNo.cuStState;
                //判断站台是否有车,有车给任务类型
                Station st = App.StationList.FirstOrDefault(a => a.strStationNo == station);
                if (st == null) return ReMessageFactory.Error(1, "没有当前站台");
                Point point = App.PointList.FirstOrDefault(a => a.strBarcode == st.strBarcode);
                Agv agv = App.AgvList.FirstOrDefault(a => a.strBarcode == point.strBarcode);
                if (agv == null || agv.agvTask == null || agv.agvTask.sonTaskList.Count == 0) return ReMessageFactory.Error(1, "当前站台没有AGV");
                var agvSonTask = agv.agvTask.sonTaskList[0];
                if (agvSonTask.taskRequestType == EnumMsg.TaskRequestType.取货完成请求) 
                    agvSonTask.taskRequestType = EnumMsg.TaskRequestType.信号发送完成;
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(ChangePoint)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }

        /// <summary>
        /// 任务下发
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ReMessage TaskAssign(Stream stream)
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                StreamReader sr = new StreamReader(stream);
                string s = sr.ReadToEnd();
                App.ExFile.MessageWebApi("WebApiMsg", "接口(TaskAssign)," + s + "\r");
                WebApiTask fTask = JsonConvert.DeserializeObject<WebApiTask>(s);
                /**
                 * 
                 * {"TaskNo":"DC202104210010","TaskGroup":"","AgvNo":"","BeginBarcode":"1","EndBarcode":"21","Status":"0"}
                 **/
                string taskNo = fTask.TaskNo;
                string taskType = fTask.TaskType;
                string BeginBarcode = fTask.BeginBarcode;
                string EndBarcode = fTask.EndBarcode;
                //起点
                if (taskType == "配送")
                {
                    BeginBarcode = "750";
                    EndBarcode = "745";
                }
                Point startPoint = App.PointList.Find(a => a.strBarcode == BeginBarcode);
                //终点
                Point endPoint = App.PointList.Find(a => a.strBarcode == EndBarcode);
                if (App.TaskList.FirstOrDefault(a => a.endPoint == endPoint) != null && endPoint.lockedAgv != null)
                {
                    return ReMessageFactory.Error(1, "存在当前站台的任务,请稍后下发");
                }
                bool result = TaskManage.TaskBuild(taskType, "配送", taskNo, 1, "", "", startPoint, endPoint, taskNo);
                if (!result) return ReMessageFactory.Error(1, "创建" + taskNo + "任务失败");
            }
            catch (Exception ex)
            {
                App.ExFile.MessageWebApi("WebApiError", "接口(TaskAssign)," + ex.ToString() + "\r");
                return ReMessageFactory.Error(1, ex.ToString());
            }
            return ReMessageFactory.Sucess("建立成功");
        }
    }
}