MsgManage.cs 37.5 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using System.Threading;
using XingYe_ACS.WebApi;
using System.Data;

namespace XingYe_ACS.Business
{
    public class MsgManage
    {
        /// <summary>
        /// 处理客户端指令
        /// </summary>
        /// <param name="clientMsg">客户端数据</param>
        /// <param name="socketOpt">客户端对象</param>
        public static void TcpManage(ClientMsg clientMsg, SocketOpt socketOpt)
        {
            try
            {
                AgvHeartManage(clientMsg, socketOpt);
                #region 注释功能码
                //switch (clientMsg.intFuncCode)
                //{
                //    case 0:
                //        AgvHeartManage(clientMsg, socketOpt);
                //        break;
                //}
                #endregion
                if (clientMsg.clientErr != 0)
                {
                    if (clientMsg.clientErr != EnumMsg.ErrMsg.不作处理)
                    {
                        App.ExFile.MessageBusinessErr("BusinessException", clientMsg.msgAgv.strAgvNo + "业务处理异常:" + ";任务号:" + clientMsg.intTaskNo + ":" + clientMsg.clientErr.ToString() + "="
                            + clientMsg.msgAgv.strStopReason);
                    }
                    if (clientMsg.clientErr == EnumMsg.ErrMsg.任务不存在)
                    {
                        List<ClientMotion> listMotion = new List<ClientMotion>();
                        socketOpt.sendData = GetSendData.GetNewActionData(clientMsg.intTaskNo, listMotion);
                    }
                    else if (App.offAgvNo.Count > 0 && App.offAgvNo.Contains(clientMsg.strAddress))
                    {
                        //无任务且不在充电的AGV关机
                        Agv offAgv = App.AgvList.Find(a => a.strAgvNo == clientMsg.strAddress);
                        Point point = App.PointList.Find(a => a.strBarcode == offAgv.strBarcode);
                        if (offAgv.agvTask == null && point != null && offAgv.agvState != EnumMsg.State.充电中
                            && (point.pointType == EnumMsg.PointType.回家点 || point.pointType == EnumMsg.PointType.充电点))
                        {
                            socketOpt.sendData = GetSendData.GetNewActionData(21, null);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("TcpManage:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 处理Agv心跳指令
        /// </summary>
        /// <param name="clientMsg">客户端数据</param>
        /// <param name="socketOpt">客户端对象</param>
        public static void AgvHeartManage(ClientMsg clientMsg, SocketOpt socketOpt)
        {
            try
            {
                //检查基础数据
                //刚开机,没扫到码,给的是0
                if (clientMsg.strBarcode == "0")
                    clientMsg.strBarcode = clientMsg.msgAgv.strBarcode;
                Point currentPoint = App.PointList.FirstOrDefault(a => a.strBarcode == clientMsg.strBarcode);
                if (currentPoint == null)
                {
                    Agv agv = clientMsg.msgAgv;
                    clientMsg.clientErr = EnumMsg.ErrMsg.当前点不在地图中;
                    clientMsg.msgAgv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, clientMsg.clientErr.ToString() + "新码值:" + clientMsg.strBarcode + ":旧码值:" + clientMsg.msgAgv.strBarcode);
                    agv.strAgvError = clientMsg.msgAgv.strStopReason;
                    AcsPushBuild.PushAgv(agv);
                    return;
                }
                currentPoint.lockedAgv = clientMsg.msgAgv;
                string oldBarcode = clientMsg.msgAgv.strBarcode;
                string newBarcode = clientMsg.strBarcode;

                //运行状态和充电状态记录状态表中
                // 运行状态/充电状态/空闲状态
                if ((int)clientMsg.msgAgv.agvState != clientMsg.intState)
                {
                    //更新AGV状态变更表
                    UpdateManage.InsertAgvState(clientMsg.msgAgv,clientMsg.intState);
                }
                //更新AGV和货架信息
                bool isUpdateResult = UpdateManage.UpdateClientMsg(clientMsg);

                if (!isUpdateResult)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.更新AGV信息失败;
                    clientMsg.msgAgv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, clientMsg.clientErr.ToString());
                    return;
                }

                if (oldBarcode != newBarcode)
                {
                    //更新界面小车位置
                    App.Sharp.AgvChange(clientMsg.msgAgv);
                    //码值发生变化计算距离
                    clientMsg.msgAgv.agvMilage += ComnMethod.GetDistance(oldBarcode, newBarcode, "");
                    UpdateManage.UpdateAgvMsg(clientMsg.msgAgv);
                }

                App.Sharp.AgvStateChange(clientMsg.msgAgv);
                if (clientMsg.msgAgv.agvState != EnumMsg.State.忙碌)
                {
                    List<Point> lockedPoint = new List<Point>();
                    lockedPoint.Add(currentPoint);
                    UpdateManage.relieveLocked(lockedPoint, clientMsg.msgAgv);
                }
                bool isDealResult = CheckAgvState(clientMsg, socketOpt);
                //界面显示异常AGV信息
                Agv erroAgv = clientMsg.msgAgv;
                if (erroAgv.strAgvError == "")
                    erroAgv.strAgvError = erroAgv.strStopReason;
                if (!isDealResult)
                {
                    AcsPushBuild.PushAgv(erroAgv);
                    return;
                }
                AgvDoWork(clientMsg, socketOpt);
            }
            catch (Exception Ex)
            {
                throw new Exception("AgvHeartManage:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 判断AGV状态是否可以执行任务
        /// </summary>
        /// <param name="clientMsg"></param>
        /// <param name="socketOpt"></param>
        /// <returns></returns>
        public static bool CheckAgvState(ClientMsg clientMsg, SocketOpt socketOpt)
        {
            try
            {
                //得到当前小车信息
                Agv agv = clientMsg.msgAgv;
               
                if ((agv.agvTask == null || agv.agvTask.sonTaskList.Count == 0) && clientMsg.intTaskNo != 0 && agv.strAgvError == "")
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.任务不存在;
                    return false;
                }
                //对于没有任务、或者有异常的小车只用回复重发
                if (agv.strStopReason != "" || (agv.strAgvError != "" && agv.strAgvError != "充电异常-" && !agv.strAgvError.Contains("@")) || agv.agvTask == null || agv.agvTask.sonTaskList.Count == 0 || agv.agvTask.sonTaskList[0].sonTaskState < EnumMsg.SonTaskState.已建立路径)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                    return false;
                }
                //得到当前小车的第一条子任务
                SonTask sonTask = agv.agvTask.sonTaskList[0];
                if (agv.agvState == 0 || agv.agvState == EnumMsg.State.手动无码 || agv.agvState == EnumMsg.State.手动有码 || agv.agvState == EnumMsg.State.自动无码 || agv.agvState == EnumMsg.State.自动歪码)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                    agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "小车状态不正确;");
                    return false;
                }
                //对于当前有任务的小车
                if (clientMsg.intTaskNo != 0)
                {
                    if (clientMsg.intTaskNo != sonTask.intSonTaskNo)
                    {
                        clientMsg.clientErr = EnumMsg.ErrMsg.任务不存在;
                        return false;
                    }
                    if (agv.agvState == EnumMsg.State.忙碌 && clientMsg.actState == EnumMsg.ActState.未完成)
                    {
                        if (App.PointList.FirstOrDefault(a => a.strBarcode == agv.strBarcode) == sonTask.endPoint)
                        {
                            List<Point> lockedPoint = new List<Point>();
                            lockedPoint.Add(sonTask.endPoint);
                            UpdateManage.relieveLocked(lockedPoint, clientMsg.msgAgv);
                        }
                        clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                        return false;
                    }
                }

                //等待点小车要等待时间
                if (sonTask.sonTaskType == EnumMsg.AgvTaskType.等待)
                {
                    int intWaitTime = ComnMethod.DiffTime(DateTime.Parse(DateTime.Now.ToString()));
                    //if (intWaitTime < sonTask.waitTime) return false;
                }

                if (sonTask.sonTaskType != EnumMsg.AgvTaskType.行走 && sonTask.sonTaskType != EnumMsg.AgvTaskType.弧线行走)
                {
                    //顶升、旋转、充电等,小车位置必须在终点位置
                    if (agv.strBarcode != sonTask.endPoint.strBarcode)
                    {
                        clientMsg.clientErr = EnumMsg.ErrMsg.任务不合理;
                        agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "做顶升任务或是旋转时小车与终点不在同一地点;");
                        return false;
                    }
                }
                //else
                //{
                //    if (!agv.agvTask.strTaskID.Contains("手动"))
                //    {
                //        if (sonTask.isCarry && !agv.isCarry)
                //        {
                //            clientMsg.clientErr = EnumMsg.ErrMsg.小车状态不合理;
                //            agv.strStopReason =
                //                ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "确认小车转盘是否有货;");
                //            return false;
                //        }
                //        else if (!sonTask.isCarry && agv.isCarry)
                //        {
                //            clientMsg.clientErr = EnumMsg.ErrMsg.小车状态不合理;
                //            agv.strStopReason =
                //                ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "确认小车转盘是否无货;");
                //            return false;
                //        }
                //    }
                //}

                PathPoint pathPoint = sonTask.pathPointList.FirstOrDefault(a => a.point.strBarcode == agv.strBarcode);
                if (pathPoint == null)
                {
                    UpdateManage.ResetPath(sonTask);
                    clientMsg.clientErr = EnumMsg.ErrMsg.当前点不在路径中;
                    agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "当前点不在路径中");
                    return false;
                }
                //var hoister1 = App.HoisterList.First();
                //if (agv.strBarcode == "743" && agv.currentCharge < 90 && !hoister1.read_isHoisterManual && !App.TaskList.ToList().Exists(a => a.strTaskID == EnumMsg.TaskType.充电.ToString()))
                //{
                //    Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);


                //    //判断小车有没有收到允许求的信号

                //    bool result = !hoister1.read_isGet;// && 02 hoister1.write_isGet && 
                //    if (result)
                //    {


                //        //如果没有完成信号,则开始判断小车等待时间
                //        //等待标志为0则代表第一次进入时间判断
                //        if (agv.agvTask.taskStopSign == 0)
                //        {
                //            agv.agvTask.taskStopSign = 1;
                //            agv.agvTask.taskStopTime = DateTime.Now;
                //            clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                //            agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "AGV等待完成信号");
                //            return false;
                //        }
                //        else
                //        {
                //            //时间判断
                //            int intTaskTime = 0;
                //            if (agv.agvTask.taskStopTime != DateTime.MinValue)
                //            {
                //                TimeSpan ts1 = new TimeSpan(agv.agvTask.taskStopTime.Ticks);
                //                TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                //                TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                //                intTaskTime = ts3.Minutes * 60 + ts3.Seconds;
                //            }
                //            //等待时间超过半小时就清楚任务
                //            if (intTaskTime < App.TaskWaitTime)
                //            {
                //                UpdateManage.DeleteTask();
                //                ComnMethod.DeleteTask(agv.agvTask);
                //                Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                //                Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                //                TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                //                return false;
                //            }

                //        }

                //    }

                //}
                //agv.agvTask.taskStopSign = 0;

                #region 提升机附属点判断是否能进入提升机
                var currentPoint = pathPoint.point;
                if (currentPoint.pointType == EnumMsg.PointType.对接等待点 && sonTask.endPoint.pointType == EnumMsg.PointType.对接点 && agv.agvTask.strTaskID != "手动行走" && agv.agvTask.strTaskID !="充电")
                {
                    //获取请求提升机站台
                    Station station = App.StationList.Find(a => a.strBarcode == sonTask.endPoint.strBarcode
                                                             && a.strPreBarcode == currentPoint.strBarcode
                                                             && a.stationType == EnumMsg.StationType.料台);
                    if (station != null)
                    {
                        Hoister hoister = App.HoisterList.First();
                        if (!sonTask.isCarry && !hoister.read_isGet)
                        {
                            //取货请求
                            hoister.write_isGet = true;
                            hoister.write_isAgvGetFinish = false;
                            hoister.write_isAgvPutFinish = false;
                            hoister.write_isHoisterGetAck = false;
                            hoister.write_isHoisterPutAck = false;
                            hoister.write_isGetAck = false;
                            hoister.write_isPutAck = false;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "请求取货清除提升机信号其他信号");
                            return false;
                        }
                        else if (sonTask.isCarry && !hoister.read_isPut)
                        {
                            //放货请求
                            hoister.write_isPut = true;
                            hoister.write_isAgvGetFinish = false;
                            hoister.write_isAgvPutFinish = false;
                            hoister.write_isHoisterGetAck = false;
                            hoister.write_isHoisterPutAck = false;
                            hoister.write_isGetAck = false;
                            hoister.write_isPutAck = false;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "请求放货清除提升机信号其他信号");
                            return false;
                        }

                    }
                }
                #endregion

                #region 离开提升机告知AGV离开完成
                if (currentPoint.pointType == EnumMsg.PointType.对接等待点 && sonTask.startPoint.pointType == EnumMsg.PointType.对接点&&agv.agvTask.strTaskID!="手动行走"&&agv.agvTask.strTaskID!="充电")
                {

                    //获取请求提升机站台
                    Station station = App.StationList.Find(a => a.strBarcode == sonTask.startPoint.strBarcode
                                                             && a.strPreBarcode == currentPoint.strBarcode
                                                             && a.stationType == EnumMsg.StationType.料台);
                    if (station != null)
                    {
                        Hoister hoister = App.HoisterList.First();
                        if (!sonTask.isCarry && !hoister.read_isAgvPutAck && agv.flag == 2)
                        {
                            //AGV放货完成离开
                            hoister.write_isAgvPutFinish = true;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV放货完成离开true");
                            return false;
                        }
                        else if (sonTask.isCarry && !hoister.read_isAgvGetAck && agv.flag == 2)
                        {
                            //AGV取货完成离开
                            hoister.write_isAgvGetFinish = true;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV取货完成离开true");
                            //写入完成
                            return false;
                        }
                        if (!sonTask.isCarry && hoister.read_isAgvPutAck)
                        {
                            hoister.write_isAgvPutFinish = false;
                            hoister.write_isHoisterGetAck = false;
                            agv.flag = 3;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV放货完成离开false;提升机取货完成确认false");
                            Console.WriteLine("3");
                        }
                        else if ( sonTask.isCarry && hoister.read_isAgvGetAck)
                        {
                            hoister.write_isAgvGetFinish = false;
                            hoister.write_isHoisterPutAck = false;
                            agv.flag = 3;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV取货完成离开false;提升机放货完成确认false");
                            Console.WriteLine("4");
                        }
                        else { return false; }
                    }
                }
                #endregion

                //记录日志
                App.ExFile.MessageLog("GetMotion" + agv.strAgvNo, string.Format("AgvNo:{0}【动作完成】 当前点:{1} 任务类型:{2} 任务号:{3} 动作状态:{4} 自身状态:{5} 当前电量:{6}"
                    , agv.strAgvNo, agv.strBarcode, sonTask.sonTaskType, clientMsg.intTaskNo, clientMsg.actState, agv.agvState, agv.currentCharge));
                agv.strStopReason = "";
                return true;
            }
            catch (Exception Ex)
            {
                throw new Exception("CheckAgvState:" + Ex.StackTrace);
            }
        }

        /// <summary>
        /// 小车执行任务
        /// </summary>
        /// <param name="clientMsg"></param>
        /// <param name="socketOpt"></param>
        private static void AgvDoWork(ClientMsg clientMsg, SocketOpt socketOpt)
        {
            try
            {
                string strFinish = "";
                Agv agv = clientMsg.msgAgv;
                SonTask sonTask = agv.agvTask.sonTaskList[0];
                List<ClientMotion> listMotion = new List<ClientMotion>();
                //对于在终点的小车,判断任务状态,设置完成。
                if  (clientMsg.intTaskNo == sonTask.intSonTaskNo && sonTask.endPoint.strBarcode == agv.strBarcode && clientMsg.actState == EnumMsg.ActState.已完成)
                
                {
                    //if (sonTask.endPoint.oriAgv != 0 && sonTask.endPoint.oriAgv != agv.agvOri) return;
                    Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);


                    ////判断小车有没有收到允许求的信号
                    //var hoister1 = App.HoisterList.First();
                    //bool result = hoister1.write_isGet && !hoister1.read_isGet && !hoister1.read_isHoisterManual;
                    //if (result)
                    //{


                    //    //如果没有完成信号,则开始判断小车等待时间
                    //    //等待标志为0则代表第一次进入时间判断
                    //    if (agv.agvTask.taskStopSign == 0)
                    //    {
                    //        agv.agvTask.taskStopSign = 1;
                    //        agv.agvTask.taskStopTime = DateTime.Now;
                    //        clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                    //        agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "AGV等待完成信号");
                    //        return;
                    //    }
                    //    else
                    //    {
                    //        //时间判断
                    //        int intTaskTime = 0;
                    //        if (agv.agvTask.taskStopTime != DateTime.MinValue)
                    //        {
                    //            TimeSpan ts1 = new TimeSpan(agv.agvTask.taskStopTime.Ticks);
                    //            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    //            TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                    //            intTaskTime = ts3.Minutes * 60 + ts3.Seconds;
                    //        }
                    //        //等待时间超过半小时就清楚任务
                    //        if (intTaskTime < App.TaskWaitTime)
                    //        {
                    //            UpdateManage.DeleteTask();
                    //            ComnMethod.DeleteTask(agv.agvTask);
                    //            Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                    //            Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                    //            TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                    //            return;
                    //        }

                    //    }

                    //}

                    //agv.agvTask.taskStopSign = 0;

                    #region AGV在提升机内取货或者放货完成
                    if (sonTask.sonTaskType == EnumMsg.AgvTaskType.顶升||sonTask.sonTaskType == EnumMsg.AgvTaskType.直接顶升)
                    {
                        //AGV发送取货到位
                        Station station = App.StationList.Find(a => a.strBarcode == point.strBarcode && a.stationType == EnumMsg.StationType.料台);
                        if (station != null)
                        {
                            Hoister hoister = App.HoisterList.First();
                            agv.flag = 2;
                            if (!hoister.read_isHoisterPutFinish)
                            {
                                hoister.write_isGetAck = true;
                                App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV取货到位确认true");
                                return;
                            }
                            else if (hoister.read_isHoisterPutFinish)
                            {
                                hoister.write_isGetAck = false;
                                hoister.write_isHoisterPutAck = true;
                                App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV取货到位确认false;提升机放货完成取认true");
                            }
                            else { return; }
                        }
                    }
                    else if (sonTask.sonTaskType == EnumMsg.AgvTaskType.下降||sonTask.sonTaskType==EnumMsg.AgvTaskType.直接下降)
                    {
                        Station station = App.StationList.Find(a => a.strBarcode == agv.strBarcode && a.stationType == EnumMsg.StationType.料台);
                        if (station != null)
                        {
                            Hoister hoister = App.HoisterList.First();
                            agv.flag = 2;
                            if (!hoister.read_isHoisterGetFinish)
                            {
                                hoister.write_isPutAck = true;
                                App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "AGV放货到位确认true");
                                return;
                            }
                            else if (hoister.read_isHoisterGetFinish)
                            {
                                hoister.write_isPutAck = false;
                                hoister.write_isHoisterGetAck = true;
                            }
                            else { return; }
                        }
                    }
                    #endregion

                    sonTask.taskRequestType = EnumMsg.TaskRequestType.信号发送完成;
                    bool sonTaskUpdateResult = TaskManage.SonTaskFinish(clientMsg);
                    if (!sonTaskUpdateResult)
                    {
                        clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                        agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "AGV任务完成更新失败");
                        return;
                    }
                    //运行至终点时,解除其他占用的点
                    List<Point> lockedPoint = new List<Point>();
                    lockedPoint.Add(sonTask.endPoint);
                    UpdateManage.relieveLocked(lockedPoint, agv);
                    //获取下个子任务
                    if (agv.agvTask == null)
                    {
                        socketOpt.sendData = GetSendData.GetNewActionData(sonTask.intSonTaskNo, listMotion);
                        strFinish = string.Format("AgvNo:{0}【回复动作】 主任务号:{1} 已完成", agv.strAgvNo, sonTask.strTaskNo);
                        App.ExFile.MessageLog("GetMotion" + agv.strAgvNo, strFinish);
                        return;
                    }
                    else
                    {
                        sonTask = agv.agvTask.sonTaskList[0];
                        strFinish += string.Format("AgvNo:{0}【切换下个子任务】 任务号:{1} ", agv.strAgvNo, sonTask.intSonTaskNo);
                    }
                    App.ExFile.MessageLog("GetMotion" + agv.strAgvNo, strFinish);
                    //待更改如果建立路径失败,如果判断
                    Thread.Sleep(200);
                    if (sonTask.sonTaskState != EnumMsg.SonTaskState.已建立路径)
                    {
                        return;
                    }
                }

                #region 对于前往工位是新任务的情况
                //if (sonTask.endPoint.pointType == EnumMsg.PointType.站台点 && sonTask.endPoint.lockedAgv != null && sonTask.endPoint.lockedAgv != agv)
                //{
                //    clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                //    agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "工位点有小车,等待执行;");
                //    return;
                //}
                #endregion

                #region 假装没有发
                //if (agv.strBarcode == sonTask.endPoint.strBarcode && sonTask.sonTaskType == EnumMsg.AgvTaskType.取消充电 && agv.agvState == EnumMsg.State.自动空闲)
                //{
                //    bool sonTaskUpdateResult = TaskManage.SonTaskFinish(clientMsg);
                //    agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "AGV任务完成更新失败;");
                //    return;
                //}
                #endregion

                #region 准备顶升或下降时,清除取放货信号

                if (sonTask.sonTaskType == EnumMsg.AgvTaskType.顶升 || sonTask.sonTaskType == EnumMsg.AgvTaskType.下降 
                    || sonTask.sonTaskType == EnumMsg.AgvTaskType.直接顶升 || sonTask.sonTaskType == EnumMsg.AgvTaskType.直接下降)
                {
                    //AGV发送取货到位
                    Station station = App.StationList.Find(a => a.strBarcode == agv.strBarcode && a.stationType == EnumMsg.StationType.料台);
                    if (station != null)
                    {
                        Hoister hoister = App.HoisterList.First();
                        if (sonTask.sonTaskType == EnumMsg.AgvTaskType.顶升 || sonTask.sonTaskType == EnumMsg.AgvTaskType.直接顶升)
                        {
                            hoister.write_isGet = false;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "请求取货false;AGV进入提升机false");
                        }
                        else if (sonTask.sonTaskType == EnumMsg.AgvTaskType.下降 || sonTask.sonTaskType == EnumMsg.AgvTaskType.直接下降)
                        {
                            hoister.write_isPut = false;
                            App.ExFile.AgvVoltageLog(station.strAddress + clientMsg.msgAgv.strAgvNo, "请求放货false;AGV进入提升机false");
                        }
                    }
                }

                #endregion
                
                //发送后续动作点
                listMotion = GetNextPoint.Motions(sonTask, agv);
                if (listMotion.Count == 0)
                {
                    clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                    agv.strStopReason = ComnMethod.GetStopReason(agv.strStopReason, "当前点未被lock;");
                    Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                    point.lockedAgv = agv;
                    return;
                }
                else if (listMotion.Count > 1)
                {
                    agv.strStopReason = "";
                }
                else
                {
                    agv.strStopReason = ComnMethod.GetStopReason(agv.strStopReason, "避让中lock;");
                }
                if (listMotion[0].strBarcode == sonTask.startPoint.strBarcode)
                {
                    sonTask.strBeginTime = DateTime.Now.ToString();
                    sonTask.sonTaskState = EnumMsg.SonTaskState.已执行;
                    bool isUpdateSonTaskResult = UpdateManage.UpdateSonTask(sonTask);
                    if (!isUpdateSonTaskResult)
                    {
                        clientMsg.clientErr = EnumMsg.ErrMsg.更新子任务失败;
                        agv.strStopReason = ComnMethod.GetStopReason(agv.strStopReason, clientMsg.clientErr.ToString());
                        return;
                    }
                }
                socketOpt.sendData = GetSendData.GetNewActionData(sonTask.intSonTaskNo, listMotion);
                string strTaskNo = BitConverter.ToUInt32(socketOpt.sendData, 1).ToString();
                if (string.IsNullOrEmpty(strTaskNo))
                    strTaskNo = "0";
                string strMotion = string.Format("AgvNo:{0}【回复动作】 任务号:{1}  车头方向:{2}  当前电量:{3}", agv.strAgvNo, strTaskNo, agv.agvOri, agv.currentCharge);
                foreach (ClientMotion clientMotion in listMotion)
                {
                    strMotion += "\r\n";
                    strMotion += string.Format(@"动作类型:{0} 动作码值:{1} X:{2} Y:{3} X间距:{4} Y间距:{5} 路径点属性:{6} 车头方向:{7} 转盘方向:{8} 防撞区域:{9};"
                                                , (EnumMsg.AgvTaskType)clientMotion.intTaskType, clientMotion.strBarcode, clientMotion.intX, clientMotion.intY, clientMotion.intXLength, clientMotion.intYLength
                                                , (EnumMsg.PointType)clientMotion.intPathPointType, (EnumMsg.OriType)clientMotion.intOriAgv, (EnumMsg.OriType)clientMotion.intOriDial, clientMotion.intAntiCollision);
                }
                strMotion += "\r\n";
                App.ExFile.MessageLog("GetMotion" + agv.strAgvNo, strMotion);
            }
            catch (Exception Ex)
            {
                throw new Exception("AgvDoWork:" + Ex.StackTrace);
            }
        }
        //判断输送线手自动状态决定小车去充电
        public static void IsCharage()
        {
            try
            {
                //得到当前小车信息
                Agv agv = App.AgvList.Find(a => a.strAgvNo == "A11_3108");
                var hoister1 = App.HoisterList.First();
                bool result1 = (agv.strBarcode == "746" && agv.currentCharge < 90 && !hoister1.read_isHoisterManual && !App.TaskList.ToList().Exists(a => a.strTaskID == EnumMsg.TaskType.充电.ToString()));
               // bool result2 = hoister1.
               
                if (result1)
                {
                    Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);


                    //判断小车有没有收到允许求的信号

                    bool result = !hoister1.read_isGet;// && 02  hoister1.write_isGet &&
                    if (result)
                    {


                        //如果没有完成信号,则开始判断小车等待时间
                        //等待标志为0则代表第一次进入时间判断
                        //if (agv.agvTask.taskStopSign == 0)
                        //{
                        //    agv.agvTask.taskStopSign = 1;
                        //    agv.agvTask.taskStopTime = DateTime.Now;
                        //    //clientMsg.clientErr = EnumMsg.ErrMsg.不作处理;
                        //    // agv.strStopReason = ComnMethod.GetStopReason(clientMsg.msgAgv.strStopReason, "AGV等待完成信号");
                        //    return;
                        //}
                        //else
                        //{
                        //    //时间判断
                        //    int intTaskTime = 0;
                        //    if (agv.agvTask.taskStopTime != DateTime.MinValue)
                        //    {
                        //        TimeSpan ts1 = new TimeSpan(agv.agvTask.taskStopTime.Ticks);
                        //        TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                        //        TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                        //        intTaskTime = ts3.Minutes * 60 + ts3.Seconds;
                        //    }
                        //    //等待时间超过半小时就清楚任务
                        //    if (intTaskTime < App.TaskWaitTime)
                        //    {
                        if ( App.TaskList.Count>0)
                        {
                            UpdateManage.DeleteTask();
                            ComnMethod.DeleteTask(agv.agvTask);
                            Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                            Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                            TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                        }else
                        {

                            Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                            Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                            TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                        }
                                //UpdateManage.DeleteTask();
                                //ComnMethod.DeleteTask(agv.agvTask);
                                //Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                                //Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                                //TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                                return;
                            //}

                        }

                    }
                    //agv.agvTask.taskStopSign = 0;
                
                
            }
            catch (Exception)
            {

                throw new Exception("IsCharage:" + "手自动出错"); ;
            }
        }
       
    }
}