TaskManage.cs 36.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 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
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using XingYe_ACS.WebApi;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XingYe_ACS.Business
{
    
    public class TaskManage
    {
        
        /// <summary>
        /// 任务创建
        /// </summary>
        /// <param name="strTaskID">任务ID</param>
        /// <param name="strTaskGroup">任务群组</param>
        /// <param name="strTaskBarcode">任务条码</param>
        /// <param name="intTaskLevel">任务等级</param>
        /// <param name="strAgvNo">执行AGV</param>
        /// <param name="strStationNo">目标站台</param>
        /// <param name="startPoint">起点</param>
        /// <param name="endPoint">终点信息</param>
        /// <param name="direction">转盘方向</param>
        /// <returns></returns>
        public static bool TaskBuild(string strTaskID, string strTaskGroup, string strTaskBarcode, int intTaskLevel, string strAgvNo, string strStationNo, Point startPoint, Point endPoint, string strTaskNo)
        {
            try
            {
                Agv agv = App.AgvList.FirstOrDefault(a => a.strAgvNo == strAgvNo);
                if (strAgvNo != "" && agv != null && agv.agvTask != null) return false;

                FTask task = new FTask();
                if (strTaskNo == "")
                {
                    task.strTaskNo = ComnMethod.GetTaskNo();
                }
                else
                {
                    task.strTaskNo = strTaskNo;
                }
                task.strTaskID = strTaskID;
                task.strTaskGroup = strTaskGroup;
                task.intTaskLevel = intTaskLevel;
                task.intTaskSplitStep = 1;
                if (strAgvNo != "")
                {
                    task.isTaskSubmit = true;
                    task.strTaskAgv = strAgvNo;
                }
                else
                {
                    task.isTaskSubmit = false;
                    task.strTaskAgv = "";
                }
                
                task.strTaskBarcode = strTaskBarcode;
                if (App.StationList.FirstOrDefault(a => a.strBarcode == startPoint.strBarcode) != null)
                {
                    task.strInitial = App.StationList.FirstOrDefault(a => a.strBarcode == startPoint.strBarcode).strStationNo;
                }
                task.strTarget = strStationNo;
                task.startPoint = startPoint;
                task.endPoint = endPoint;
                task.strAcceptTime = DateTime.Now;
                task.sonTaskList = new ObservableCollection<SonTask>();
                task.strBeginTime = "";
                task.strEndTime = "";
                task.taskErrMsg = "";
                task.direction = 0;
                task.taskStopSign = 0;
                task.taskStopTime = DateTime.Now;
                //更新数据库
                string sqlInsert = string.Format(@"INSERT INTO  T_Task (strTaskNo ,strTaskID ,strTaskGroup ,intTaskLevel ,strTaskBarcode ,strInitial,strTarget
                        ,startPoint ,endPoint ,strAcceptTime ,isTaskSubmit ,strTaskAgv ,strBeginTime 
                        ,strEndTime ,taskErrMsg , direction)
                        VALUES ('{0}' ,'{1}' ,'{2}' ,'{3}'  ,'{4}' ,'{5}' 
                                ,'{6}' ,'{7}' ,'{8}' ,'{9}' ,'{10}' ,'{11}' 
                                ,'{12}' ,'{13}','{14}','{15} ');"
                        , task.strTaskNo, task.strTaskID, task.strTaskGroup, task.intTaskLevel, task.strTaskBarcode, task.strInitial, task.strTarget
                        , task.startPoint.strBarcode, task.endPoint.strBarcode, task.strAcceptTime, task.isTaskSubmit == false ? "0" : "1", task.strTaskAgv, task.strBeginTime == "" ? "null" : task.strBeginTime
                        , task.strEndTime == "" ? "null" : task.strEndTime, task.taskErrMsg.ToString(), (int)task.direction);
                int intSqlResult = DbHelperSQL.ExecuteSql(sqlInsert);
                if (intSqlResult < 1) return false;
                App.AppDispatcher.Invoke(new Action(() =>
                {
                    App.TaskList.Add(task);
                }));
                if (agv != null)
                    agv.agvTask = task;
                return true;
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("TaskBuild", strTaskID.ToString() + "任务建立失败" + Ex.Message.Replace(Environment.NewLine, ""));
                return false;
            }
        }

        /// <summary>
        /// 任务获取小车
        /// </summary>
        /// <returns></returns>·    
        public static int AgvGetTask()
        {
            int taskNum = 0;
            try
            {
                for (int index = 0; index < App.TaskList.Count; index++)
                {
                    FTask task = App.TaskList[index];
                    if (task.isTaskSubmit) continue;
                    if (task.strTaskAgv == "") continue;
                    Agv agv = App.AgvList.FirstOrDefault(a => a.strAgvNo == task.strTaskAgv);
                    if (!agv.isEnable || !agv.isOnline)
                    {
                        task.strTaskAgv = "";
                        continue;
                    }
                    if (agv.agvState != EnumMsg.State.自动空闲 || agv.agvTask != null) continue;
                    TaskSubmit(task, agv);
                }
                List<FTask> tasks = App.TaskList.Where(b => b.taskState != "Stop").OrderByDescending(a => a.intTaskLevel).ToList();
                for (int index = 0; index < tasks.Count; index++)
                {
                    FTask task = tasks[index];
                    if (task.isTaskSubmit) continue;
                    if (task.strTaskAgv != "") continue;
                    Agv agv = App.AgvList.FirstOrDefault(a => a.strBarcode == task.startPoint.strBarcode 
                                            || a.strBarcode == task.endPoint.strBarcode);
                    if (agv != null)
                    {
                        if (agv.agvState != EnumMsg.State.自动空闲 || agv.agvTask != null) continue;
                        if (agv.heightState == EnumMsg.HeightState.低位 && agv.strBarcode == task.startPoint.strBarcode)
                        {
                            task.intTaskSplitStep = 2;
                        }
                        TaskSubmit(task, agv);
                        continue;
                    }
                    //获取当前执行任务的AGV的终点是否为当前任务起点
                    var taskList = App.TaskList.Where(a => a.isTaskSubmit).ToList();
                    var result = taskList.Exists(a => a.endPoint == task.startPoint);
                    if (result) continue;
                    //查找AGV,找最近
                    agv = ComnMethod.GetNearAgv(task.startPoint, task);
                    if (agv == null)
                    {
                        taskNum++;
                        continue;
                    }
                    TaskSubmit(task, agv);
                }
                return taskNum;
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("AgvGetTask", Ex.Message.Replace(Environment.NewLine, ""));
                return taskNum;
            }
        }

        /// <summary>
        /// 任务的提交
        /// </summary>
        /// <param name="task"></param>
        /// <param name="agv"></param>
        public static void TaskSubmit(FTask task, Agv agv)
        {
            try
            {
                bool ToWeb = false;
                task.taskErrMsg = "";
                task.isTaskSubmit = true;
                task.strTaskAgv = agv.strAgvNo;
                bool fTaskUpdateResult = UpdateManage.UpdateFTask(task);
                if (!fTaskUpdateResult)
                {
                    task.isTaskSubmit = false;
                    task.strTaskAgv = "";
                }
                else
                {
                    if (task.strTaskID == "PeiSong")
                    {
                        Hoister hoister = App.HoisterList.Find(a => a.hoisterAddress == task.startPoint.strBarcode);
                        hoister.write_isGet = true;
                    }
                }
                ToWeb = CServiceImpl.ToWebTaskRefresh(task, 1);
                agv.agvTask = task;
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("TaskSubmit", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 任务的分解
        /// </summary>
        public static void TaskSplit()
        {
            try
            {
                List<FTask> taskList = App.TaskList.Where(a => a.isTaskSubmit && a.strBeginTime == "" && a.sonTaskList.Count == 0).ToList();
                Random random = new Random();
                foreach (FTask task in taskList)
                {
                    try
                    {
                        ObservableCollection<SonTask> sonTaskList = new ObservableCollection<SonTask>();
                        string sqlInsert = "";
                        List<Config_TaskSplit> splitList = App.Config_TaskSplitList.FindAll(a => a.taskID == task.strTaskID);
                        int intNum = random.Next(99999);
                        bool flag = false;
                        foreach (Config_TaskSplit config in splitList)
                        {
                            if (config.intSerialNo < task.intTaskSplitStep) continue;
                            SonTask sonTask = new SonTask();
                            sonTask.sonTaskType = config.agvTaskType;
                            if (Enum.IsDefined(typeof(EnumMsg.ConfigToFromPoint), config.fromPoint))
                            {
                                sonTask.startPoint = ComnMethod.GetTaskPoint(task, config.fromPoint);
                                if (config.agvTaskType == EnumMsg.AgvTaskType.充电) { sonTask.startPoint = task.endPoint; }
                            }
                            else
                            {
                                sonTask.startPoint = App.PointList.Find(a => a.strBarcode == config.fromPoint.GetIndexString());
                            }
                            if (Enum.IsDefined(typeof(EnumMsg.ConfigToFromPoint), config.toPoint))
                            {
                                sonTask.endPoint = ComnMethod.GetTaskPoint(task, config.toPoint);
                            }
                            else
                            {
                                sonTask.endPoint = App.PointList.Find(a => a.strBarcode == config.toPoint.GetIndexString());
                            }
                            if (sonTask.startPoint == null || sonTask.endPoint == null)
                            {
                                flag = true;
                                task.taskErrMsg = EnumMsg.ErrMsg.分解任务未找到起点或终点.ToString();
                                break;
                            }
                            //跨区域重新分配起点终点属性
                            List<Point> listPoints = ComnMethod.GetAreaPoint(sonTask.startPoint, sonTask.endPoint);
                            if (listPoints == null)
                            {
                                flag = true;
                                task.taskErrMsg = EnumMsg.ErrMsg.分解任务未找到起点或终点.ToString();
                                break;
                            }
                            sonTask.startPoint = listPoints.First();
                            sonTask.endPoint = listPoints.LastOrDefault();
                            if (sonTask.startPoint.strBarcode == sonTask.endPoint.strBarcode && sonTask.sonTaskType == EnumMsg.AgvTaskType.行走)
                            {
                                continue;
                            }
                            sonTask.intSerialNo = config.intSerialNo;
                            sonTask.intSonTaskNo = intNum + config.intSerialNo;
                            sonTask.strTaskNo = task.strTaskNo;
                            sonTask.waitTime = config.waitTime;
                            sonTask.isCarry = config.isCarry;
                            sonTask.taskRequestType = config.taskRequestType;
                            //20180820
                            //获取子任务方向
                            if (sonTask.sonTaskType == EnumMsg.AgvTaskType.原地旋转)
                            {
                                sonTask.dialDirection = task.direction;
                            }
                            if (sonTask.sonTaskType == EnumMsg.AgvTaskType.充电 || sonTask.sonTaskType == EnumMsg.AgvTaskType.取消充电)
                            {
                                var endSt = App.StationList.Find(a => a.strBarcode == sonTask.endPoint.strBarcode);
                                if (endSt != null)
                                {
                                    sonTask.agvDirection = endSt.stationOri;
                                    sonTask.chargeLength = endSt.stationLength;
                                }
                            }
                            else
                            {
                                sonTask.agvDirection = sonTask.endPoint.oriAgv;
                            }
                            sonTask.pathPointList = new List<PathPoint>();
                            sonTask.sonTaskState = EnumMsg.SonTaskState.初始化;
                            sonTask.strBeginTime = "";
                            sonTask.strEndTime = "";

                            sonTaskList.Add(sonTask);
                            sqlInsert += string.Format(@"INSERT INTO T_SonTask (intSerialNo ,intSonTaskNo ,strTaskNo ,sonTaskType ,startPoint ,endPoint ,waitTime
                                ,isCarry,taskRequestType ,dialDirection ,agvDirection ,sonTaskState 
                                ,strBeginTime ,strEndTime,chargeLength)
                                VALUES ('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}'
                                ,'{5}' ,'{6}' ,'{7}' ,'{8}' ,'{9}' ,'{10}','{11}','{12}','{13}','{14}');"
                                    , sonTask.intSerialNo, sonTask.intSonTaskNo, sonTask.strTaskNo, (int)sonTask.sonTaskType, sonTask.startPoint.strBarcode, sonTask.endPoint.strBarcode, sonTask.waitTime
                                    , sonTask.isCarry == false ? "0" : "1", (int)sonTask.taskRequestType, (int)sonTask.dialDirection, (int)sonTask.agvDirection, (int)sonTask.sonTaskState
                                    , sonTask.strBeginTime == "" ? "null" : sonTask.strBeginTime, sonTask.strEndTime == "" ? "null" : sonTask.strEndTime, sonTask.chargeLength);
                        }
                        if (flag) continue;
                        task.strBeginTime = DateTime.Now.ToString();
                        Agv agv = App.AgvList.FirstOrDefault(a => a.strAgvNo == task.strTaskAgv);
                        agv.taskTime = DateTime.Now;

                        bool isUpdateFTaskResult = UpdateManage.UpdateFTask(task);
                        if (!isUpdateFTaskResult) continue;
                        bool isSqlResult = DbHelperSQL.SqlTransOperator(sqlInsert);
                        if (isSqlResult)
                        {
                            App.AppDispatcher.Invoke(new Action(() =>
                            {
                                foreach (SonTask getSonTask in sonTaskList)
                                {
                                    if (task.sonTaskList == null) task.sonTaskList = new ObservableCollection<SonTask>();
                                    task.sonTaskList.Add(getSonTask);
                                }
                            }));
                        }
                    }
                    catch (Exception Ex)
                    {
                        App.ExFile.MessageError("TaskSplit", Ex.StackTrace);
                    }
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("TaskSplit", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 子任务任务完成
        /// </summary>
        /// <param name="clientMsg"></param>
        /// <returns></returns>
        public static bool SonTaskFinish(ClientMsg clientMsg)
        {
            try
            {
                Agv agv = clientMsg.msgAgv;
                FTask task = agv.agvTask;
                SonTask sonTask = task.sonTaskList[0];
                //更新子任务,删除路径
                sonTask.strEndTime = DateTime.Now.ToString();
                sonTask.sonTaskState = EnumMsg.SonTaskState.已完成;
                bool isUpdateSonTaskResult = UpdateManage.UpdateSonTask(sonTask);
                if (!isUpdateSonTaskResult) return false;
                App.AppDispatcher.Invoke(new Action(() =>
                {
                    task.sonTaskList.RemoveAt(0);
                }));
                if (task.sonTaskList.Count == 0)
                {
                    task.strEndTime = DateTime.Now.ToString();
                    bool isUpdateFTaskResult = UpdateManage.UpdateFTask(task);
                    if (!isUpdateFTaskResult) return false;
                    //告知WEB界面任务完成
                    bool ToWeb = CServiceImpl.ToWebTaskRefresh(task, 99);
                    //更新任务里程
                    UpdateManage.InsertAgvMilage(task);
                    //20180925
                    //充电任务解锁strChooseStation
                    if (agv.agvTask.strTaskID == "充电")
                    {
                        agv.startCharges = agv.currentCharge;
                        //充电延时赋值
                        agv.chargeTime = DateTime.Now;
                        //充电任务结束,记录开始充电电量
                        UpdateManage.InsertAgvCharge(task);
                    }
                    if (agv.agvTask.strTaskID == "取消充电")
                    {
                        //取消充电任务结束,记录结束充电电量
                        UpdateManage.InsertAgvCharge(task);
                    }
                    App.AppDispatcher.Invoke(new Action(() =>
                    {
                        App.TaskList.Remove(task);
                    }));
                    agv.agvTask = null;
                    agv.strChooseStation = "";
                    agv.agvMilage = 0;
                    UpdateManage.UpdateAgvMsg(agv);
                }
                return true;
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("SonTaskFinish", Ex.Message.Replace(Environment.NewLine, ""));
                return false;
            }
        }

        /// <summary>
        /// 返回AGV基础信息
        /// </summary>
        /// <param name="read"></param>
        /// <returns></returns>
        public static byte[] ReturnAGVState()
        {
            HslCommunication.Core.IByteTransform ByteTransform = new HslCommunication.Core.ReverseBytesTransform();
            var singleState = new byte[69];
            try
            {
                /** AGV信息反馈S7
                *iStatus 状态
                *iAlarm 错误
                *iAlarmMsg 错误信息
                *iCurrentPose 当前坐标
                *iBeforePose 前一坐标
                *iAfterPose 后一坐标
                *iTaskStatus 状态[0:空闲1: 有任务2: 充电]
                *rSpeed 车速[mm / min]
                *rBatVoltage 电压
                *rBatCurrent 电流
                *iBatteryCapacity 电池电量
                *iBatPowerSupplyStatus 供能状态[0:功能状态未知1: 充电中2: 前往充电点3: 没在充电4: 电池满电量]
                *iBatPowerSupplyHealth 电池健康状态[0:健康状态未知1: 良好2: 过热3: DEAD4: 超压5: 发生未指定错误6: COLD7: 看门狗定时器超时8: 安全定时器超时]
                *iActProductState 当前小车托盘状态[0:无托盘1: 下料托盘2: 上料托盘]
                **/
                int index = 0;
                foreach (var agv in App.AgvList.OrderBy(a => a.strAgvNo))
                {
                    if (agv.strAgvNo == "A02_0024") { index = 0; } else { index = 34; }
                    s7AGV s7Agv = new s7AGV();
                    if (agv.agvTask != null)
                    {
                        s7Agv.iStatus = 1;
                        s7Agv.iTaskStatus = 1;
                        if (agv.agvTask.strTaskID == "充电")
                        {
                            s7Agv.iBatPowerSupplyStatus = 2;
                        }
                        //判断任务
                        if (agv.heightState != EnumMsg.HeightState.低位)
                        {
                            if (agv.agvTask.sonTaskList[0].intSerialNo < 4)
                            {
                                s7Agv.iActProductState = 1;
                            }
                            else
                            {
                                s7Agv.iActProductState = 2;
                            }
                        }

                    }
                    else if (agv.agvTask == null)
                    {
                        s7Agv.iStatus = 2;
                        s7Agv.iTaskStatus = 0;
                        s7Agv.iBatPowerSupplyStatus = 3;
                    }
                    if (agv.strAgvError != "")
                    {
                        s7Agv.iStatus = 3;
                    }
                    else if (agv.agvState == EnumMsg.State.离线)
                    {
                        s7Agv.iStatus = 4;
                    }
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iStatus)).CopyTo(singleState, 0 + index);
                    if (agv.strAgvError != "") { s7Agv.iAlarm = 1; } else { s7Agv.iAlarm = 0; }
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iAlarm)).CopyTo(singleState, 2 + index);
                    s7Agv.iAlarmMsg = 0;
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iAlarmMsg)).CopyTo(singleState, 4 + index);
                    s7Agv.iCurrentPose = short.Parse(agv.strBarcode);
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iCurrentPose)).CopyTo(singleState, 6 + index);
                    s7Agv.iBeforePose = short.Parse(string.IsNullOrEmpty(agv.beforePoint) == true ? "0" : agv.beforePoint);
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iBeforePose)).CopyTo(singleState, 8 + index);
                    s7Agv.iAfterPose = short.Parse(string.IsNullOrEmpty(agv.nextPoint) == true ? "0" : agv.nextPoint);
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iAfterPose)).CopyTo(singleState, 10 + index);
                    if (agv.agvState == EnumMsg.State.充电中)
                    {
                        s7Agv.iTaskStatus = 2;
                        s7Agv.iBatPowerSupplyStatus = 1;
                    }
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iTaskStatus)).CopyTo(singleState, 12 + index);
                    s7Agv.rSpeed = float.Parse(Convert.ToSingle(agv.rSpeed).ToString("0.0"));
                    ByteTransform.TransByte(Convert.ToSingle(s7Agv.rSpeed)).CopyTo(singleState, 14 + index);
                    s7Agv.rBatVoltage = float.Parse(Convert.ToSingle(agv.rBatVoltage).ToString("0.0"));
                    ByteTransform.TransByte(Convert.ToSingle(s7Agv.rBatVoltage)).CopyTo(singleState, 18 + index);
                    s7Agv.rBatCurrent = float.Parse(Convert.ToSingle(agv.rBatCurrent).ToString("0.0"));
                    //s7Agv.rBatCurrent = 24.2f;
                    ByteTransform.TransByte(Convert.ToSingle(s7Agv.rBatCurrent)).CopyTo(singleState, 22 + index);
                    s7Agv.iBatteryCapacity = short.Parse(agv.currentCharge.ToString());
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iBatteryCapacity)).CopyTo(singleState, 26 + index);
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iBatPowerSupplyStatus)).CopyTo(singleState, 28 + index);
                    s7Agv.iBatPowerSupplyHealth = agv.iBatPowerSupplyHealth;
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iBatPowerSupplyHealth)).CopyTo(singleState, 30 + index);
                    if (agv.heightState != EnumMsg.HeightState.高位) s7Agv.iActProductState = 0;
                    ByteTransform.TransByte(Convert.ToInt16(s7Agv.iActProductState)).CopyTo(singleState, 32 + index);
                }
                //交互信息反馈
                bool[] writePlc = new bool[8];
                int writeIndex = 0;
                foreach (var honister in App.HoisterList.OrderBy(a => a.hoisterNo))
                {
                    //答复工位请求下料
                    writePlc[writeIndex] = honister.write_isGet;
                    //工位上料完成
                    writePlc[writeIndex + 4] = honister.write_isPut;
                    writeIndex++;
                }
                singleState[68] = ComnMethod.BoolArrayToByte(writePlc);
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("HoisterTaskBuild", "HoisterTaskBuild任务建立失败" + Ex.StackTrace);
                return null;
            }
            return singleState;
        }

        public static byte[] StationTaskBuild(byte[] read)
        {
            try
            {

                #region 读取PLC信息
                var readHonister = ComnMethod.ByteToBoolArray(read[0]);
                var hoister = App.HoisterList.First();
                //升降料台心跳
                hoister.read_heart = readHonister[0];
                //升降料台是否有货
                hoister.read_isFull = readHonister[1];
                //升降料台是否允许取货
                hoister.read_isGet = readHonister[2];
                //升降料台是否允许放货
                hoister.read_isPut = readHonister[3];
                //升降料台取货完成
                hoister.read_isHoisterGetFinish = readHonister[4];
                //升降料台放货完成
                hoister.read_isHoisterPutFinish = readHonister[5];
                //升降料台取货完成离开完成确认
                hoister.read_isAgvGetAck = readHonister[6];
                //升降料台放货完成离开完成确认
                hoister.read_isAgvPutAck = readHonister[7];
                var readHonister1= ComnMethod.ByteToBoolArray(read[1]);
                //判断手动还是自动
                hoister.read_isHoisterManual = readHonister1[0];

                //AGV心跳
                hoister.write_agvHeart = hoister.write_agvHeart == hoister.read_heart ? !hoister.read_heart : hoister.read_heart;
              
                if (hoister.write_agvHeart != hoister.read_heart)
                {
                    hoister.writeTime = DateTime.Now;
                }
                else
                {
                    //判断长时间不变报警
                    float Sec = 0;
                    TimeSpan tsHeart = new TimeSpan(hoister.writeTime.Ticks);
                    TimeSpan tsNow = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan tsDurat = tsNow.Subtract(tsHeart).Duration();
                    float S = tsDurat.Seconds;
                    Sec = tsDurat.Hours * 3600 + tsDurat.Minutes * 60 + S;
                    if (Sec > 10)
                    {
                        hoister.errorMesg = "料台掉线!";
                        return null;
                    }
                }
                //agv心跳
                //hoister.write_agvHeart = !hoister.read_heart;

                //判断是否存在配送任务,存在不创建新任务
                if (hoister.read_isFull &&
                    !App.TaskList.ToList().Exists(a => a.strTaskID == EnumMsg.TaskType.配送.ToString()))                //&& hoister.read_isHoisterManual&& App.AgvList.Find(a => a.strAgvNo == "A11_3108").strBarcode == "743"
                {
                    //if (hoister.read_isHoisterManual && App.AgvList.Find(a => a.strAgvNo == "A11_3108").strBarcode == "743") //App.PointList.Find(a => a.strBarcode == "743").strBarcode
                    //{
                    var startSt = App.StationList.Find(a => a.strStationNo == "BeiLiao");
                    var startPoint = App.PointList.Find(a => a.strBarcode == startSt.strBarcode);
                    var endSt = App.StationList.Find(a => a.strStationNo == "ShangLiao");
                    var endPoint = App.PointList.Find(a => a.strBarcode == endSt.strBarcode);
                        //生成任务号
                        string strTaskNo = ComnMethod.GetTaskNo();
                    var result = TaskManage.TaskBuild(EnumMsg.TaskType.配送.ToString(), "配送", "", 1, "",
                     startSt.strStationNo, startPoint, endPoint, strTaskNo);
                    //}

                }

                byte[] writeBytes = new byte[2];
                var writeAgv = new bool[8];
                var writeAgv1 = new bool[8];
                writeAgv[0] = hoister.write_agvHeart;
                //AGV取货请求
                writeAgv[1] = hoister.write_isGet;
                //AGV放货请求
                writeAgv[2] = hoister.write_isPut;
                //AGV取货到位
                writeAgv[3] = hoister.write_isGetAck;
                //AGV放货到位
                writeAgv[4] = hoister.write_isPutAck;
                //提升机取货完成确认
                writeAgv[5] = hoister.write_isHoisterGetAck;
                //提升机放货完成确认
                writeAgv[6] = hoister.write_isHoisterPutAck;
                //AGV取货离开完成
                writeAgv[7] = hoister.write_isAgvGetFinish;
                //AGV放货完成离开
                writeAgv1[0] = hoister.write_isAgvPutFinish;

                writeBytes[0] = ComnMethod.BoolArrayToByte(writeAgv);
                writeBytes[1] = ComnMethod.BoolArrayToByte(writeAgv1);
                return writeBytes;
                #endregion
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("HoisterTaskBuild", "HoisterTaskBuild任务建立失败" + Ex.StackTrace);
                return null;
            }
        }

        #region 测试任务
        /// <summary>
        /// 循环行走任务
        /// </summary>
        public static void WalkTask()
        {
            try
            {
                for (int i = 0; i < App.AgvNo.Count; i++)
                {
                    string EndBarcode = "";

                    Agv agv = App.AgvList.FirstOrDefault(b => b.strAgvNo == App.AgvNo[i]);
                    if (agv.agvState != EnumMsg.State.自动空闲) continue;

                    FTask fTask = App.TaskList.FirstOrDefault(a => a.isTaskSubmit && a.strTaskAgv == agv.strAgvNo);
                    //int intTaskTime = 0;
                    //if (agv.taskTime != DateTime.MinValue)
                    //{
                    //    TimeSpan ts1 = new TimeSpan(agv.taskTime.Ticks);
                    //    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    //    TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                    //    intTaskTime = ts3.Minutes * 60 + ts3.Seconds;
                    //}
                    //if (intTaskTime < App.TaskTime) continue;

                    if (fTask == null)
                    {
                        if (agv.strBarcode != App.EndPoint[i])
                        {
                            EndBarcode = App.EndPoint[i];
                        }
                        else
                        {
                            EndBarcode = App.StartPoint[i];
                        }
                        Point StartPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        Point EndPoint = App.PointList.Find(a => a.strBarcode == EndBarcode);
                        TaskBuild("手动行走", "", "", 1, agv.strAgvNo, "", StartPoint, EndPoint, "");
                    }
                }
            }
            catch (Exception Ex)
            {

                MessageBox.Show(Ex.ToString());
            }
        }
        public static void WalkTasks()
        {
            try
            {
                for (int i = 0; i < App.AgvNo.Count; i++)
                {
                    string EndBarcode = "";
                    Agv agv = App.AgvList.FirstOrDefault(b => b.strAgvNo == App.AgvNo[i]);
                    FTask fTask = App.TaskList.FirstOrDefault(a => a.isTaskSubmit && a.strTaskAgv == agv.strAgvNo);
                    //int intTaskTime = 0;
                    //if (agv.taskTime != DateTime.MinValue)
                    //{
                    //    TimeSpan ts1 = new TimeSpan(agv.taskTime.Ticks);
                    //    TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                    //    TimeSpan ts3 = ts2.Subtract(ts1).Duration();
                    //    intTaskTime = ts3.Minutes * 60 + ts3.Seconds;
                    //}
                    //if (intTaskTime < App.TaskTime) return;

                    if (fTask == null)
                    {
                        if (agv.strBarcode != App.EndPoint[i])
                        {
                            EndBarcode = App.EndPoint[i];
                        }
                        else
                        {
                            EndBarcode = App.StartPoint[i];
                        }
                        Point StartPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        Point EndPoint = App.PointList.Find(a => a.strBarcode == EndBarcode);
                        TaskBuild("手动任务行走", "", "", 1, agv.strAgvNo, "", StartPoint, EndPoint, "");
                    }
                }

            }
            catch (Exception Ex)
            {

                MessageBox.Show(Ex.ToString());
            }
        }

        public static void RotateTask()
        {
            try
            {
                for (int i = 0; i < App.RotateAgvNo.Count; i++)
                {
                    Agv agv = App.AgvList.FirstOrDefault(b => b.strAgvNo == App.RotateAgvNo[i]);
                    FTask fTask = App.TaskList.FirstOrDefault(a => a.isTaskSubmit && a.strTaskAgv == agv.strAgvNo);
                    if (fTask == null)
                    {
                        Point StartPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        TaskBuild("循环耦合旋转", "", "", 1, agv.strAgvNo, "", StartPoint, StartPoint, "");
                    }
                }

            }
            catch (Exception Ex)
            {

                MessageBox.Show(Ex.ToString());
            }
        }

        public static void LiftTask()
        {
            try
            {
                for (int i = 0; i < App.LiftAgvNo.Count; i++)
                {
                    Agv agv = App.AgvList.FirstOrDefault(b => b.strAgvNo == App.LiftAgvNo[i]);
                    FTask fTask = App.TaskList.FirstOrDefault(a => a.isTaskSubmit && a.strTaskAgv == agv.strAgvNo);
                    if (fTask == null)
                    {
                        Point StartPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        TaskBuild("循环升降", "", "", 1, agv.strAgvNo, "", StartPoint, StartPoint, "");
                    }
                }

            }
            catch (Exception Ex)
            {

                MessageBox.Show(Ex.ToString());
            }
        }

        public static void PalletTask()
        {
            try
            {
                for (int i = 0; i < App.PalletAgvNo.Count; i++)
                {
                    Agv agv = App.AgvList.FirstOrDefault(b => b.strAgvNo == App.PalletAgvNo[i]);
                    FTask fTask = App.TaskList.FirstOrDefault(a => a.isTaskSubmit && a.strTaskAgv == agv.strAgvNo);
                    if (fTask == null)
                    {
                        Point StartPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        TaskBuild("循环托盘旋转", "", "", 1, agv.strAgvNo, "", StartPoint, StartPoint, "");
                    }
                }
            }
            catch (Exception Ex)
            {

                MessageBox.Show(Ex.ToString());
            }
        }
        #endregion
    }
}