public void Procedure(object ojb)
        {
            ProcedureForkLiftToBuffer FlToBuf = (ProcedureForkLiftToBuffer)ojb;
            RobotUnity rb = FlToBuf.robot;

            doorServiceCtrl = new DoorServiceCtrl();
            doorServiceCtrl = getDoorService();

            ds = doorServiceCtrl.doorService;
            ds.setRb(rb);
            TrafficManagementService Traffic         = FlToBuf.Traffic;
            ForkLiftToMachineInfo    flToMachineInfo = new ForkLiftToMachineInfo();

            rb.mcuCtrl.lampRbOn();
            robot.ShowText(" Start -> " + procedureCode);

            /*  endPointBuffer = FlToBuf.GetFrontLineBuffer(true);
             * if (endPointBuffer == null)
             * {
             *    robot.bayId = -1;
             *    Console.WriteLine("Error Data Request" + order.dataRequest);
             *    order.status = StatusOrderResponseCode.ERROR_GET_FRONTLINE;
             *    TrafficRountineConstants.ReleaseAll(robot);
             *    robot.orderItem = null;
             *    robot.SwitchToDetectLine(false);
             *    robot.ReleaseWorkingZone();
             *    if (Traffic.RobotIsInArea("READY", robot.properties.pose.Position))
             *    {
             *        TrafficRountineConstants.RegIntZone_READY.Release(robot);
             *        robot.robotTag = RobotStatus.IDLE;
             *        robot.SetSafeYellowcircle(false);
             *        robot.SetSafeBluecircle(false);
             *        robot.SetSafeSmallcircle(false);
             *        robot.TurnOnSupervisorTraffic(false);
             *       // rb.mcuCtrl.lampRbOff();
             *        procedureCode = ProcedureCode.PROC_CODE_ROBOT_TO_READY;
             *    }
             *    else
             *        procedureCode = ProcedureCode.PROC_CODE_FORKLIFT_TO_BUFFER;
             *    ReleaseProcedureHandler(this);
             *    ProRun = false;
             *    robot.ShowText("RELEASED");
             *    UpdateInformationInProc(this, ProcessStatus.S);
             *    order.endTimeProcedure = DateTime.Now;
             *    order.totalTimeProcedure = order.endTimeProcedure.Subtract(order.startTimeProcedure).TotalMinutes;
             *    SaveOrderItem(order);
             *    KillEvent();
             * }
             * else
             * {
             *    order.frontLinePos = endPointBuffer.Position;
             *    robot.bayId = bayId;
             * }*/
            while (ProRun)
            {
                // JPallet aa= GetInfoPallet_P_InBuffer(TrafficRobotUnity.PistonPalletCtrl.PISTON_PALLET_DOWN);
                switch (StateForkLift)
                {
                case ForkLift.FORBUF_IDLE:
                    robot.ShowText("FORBUF_IDLE");
                    break;

                case ForkLift.FORBUF_GET_FRONTLINE:
                    endPointBuffer = FlToBuf.GetFrontLineBuffer(true);
                    if (endPointBuffer == null)
                    {
                        if (countFrontLineNull++ < 10)
                        {
                            break;
                        }
                        countFrontLineNull = 0;
                        robot.bayId        = -1;
                        robot.ShowText("Error Data Request FrontLine ForkLift" + order.dataRequest);
                        order.status = StatusOrderResponseCode.ERROR_GET_FRONTLINE;
                        TrafficRountineConstants.ReleaseAll(robot);
                        robot.orderItem = null;
                        robot.SwitchToDetectLine(false);
                        robot.ReleaseWorkingZone();
                        if (Traffic.RobotIsInArea("READY", robot.properties.pose.Position))
                        {
                            TrafficRountineConstants.RegIntZone_READY.Release(robot);
                            robot.robotTag = RobotStatus.IDLE;
                            robot.SetSafeYellowcircle(false);
                            robot.SetSafeBluecircle(false);
                            robot.SetSafeSmallcircle(false);
                            robot.TurnOnSupervisorTraffic(false);
                            // rb.mcuCtrl.lampRbOff();
                            procedureCode = ProcedureCode.PROC_CODE_ROBOT_TO_READY;
                        }
                        else
                        {
                            procedureCode = ProcedureCode.PROC_CODE_FORKLIFT_TO_BUFFER;
                        }

                        ProRun = false;
                        robot.ShowText("RELEASED FL---");
                        UpdateInformationInProc(this, ProcessStatus.S);
                        order.endTimeProcedure   = DateTime.Now;
                        order.totalTimeProcedure = order.endTimeProcedure.Subtract(order.startTimeProcedure).TotalMinutes;
                        SaveOrderItem(order);
                        KillEvent();
                        ReleaseProcedureHandler(this);
                    }
                    else
                    {
                        order.frontLinePos = endPointBuffer.Position;
                        robot.bayId        = bayId;
                        StateForkLift      = ForkLift.FORBUF_SELECT_BEHAVIOR_ONZONE;
                    }
                    break;

                case ForkLift.FORBUF_SELECT_BEHAVIOR_ONZONE:
                    order.status = StatusOrderResponseCode.GOING_AND_PICKING_UP;
                    if (Traffic.RobotIsInArea("READY", robot.properties.pose.Position))
                    {
                        if (rb.PreProcedureAs == ProcedureControlAssign.PRO_READY)
                        {
                            //if (false == rb.CheckInGateFromReadyZoneBehavior(doorServiceCtrl.PointFrontLine.Position))
                            {
                                robot.ShowText("FORBUF_ROBOT_GOTO_BACK_FRONTLINE_READY");
                                registryRobotJourney.startPlaceName = Traffic.DetermineArea(robot.properties.pose.Position, TypeZone.OPZS);
                                registryRobotJourney.startPoint     = robot.properties.pose.Position;
                                registryRobotJourney.endPoint       = doorServiceCtrl.PointFrontLine.Position;
                                StateForkLift = ForkLift.FORBUF_ROBOT_GOTO_BACK_FRONTLINE_READY;
                            }
                        }
                    }
                    else if (Traffic.RobotIsInArea("VIM", robot.properties.pose.Position))
                    {
                        robot.robotTag = RobotStatus.WORKING;
                        if (rb.SendPoseStamped(doorServiceCtrl.PointFrontLine))
                        {
                            StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM_REG;
                            // Cap Nhat Thong Tin CHuyen Di
                            registryRobotJourney.startPlaceName = Traffic.DetermineArea(robot.properties.pose.Position, TypeZone.OPZS);
                            registryRobotJourney.startPoint     = robot.properties.pose.Position;
                            registryRobotJourney.endPoint       = doorServiceCtrl.PointFrontLine.Position;
                            robot.ShowText("FORBUF_ROBOT_WAITTING_GOTO_GATE");
                        }
                    }
                    else if (Traffic.RobotIsInArea("OUTER", robot.properties.pose.Position))
                    {
                        // public void Start (ForkLiftToBuffer state = ForkLiftToBuffer.FORBUF_ROBOT_RELEASED) {
                        robot.robotTag = RobotStatus.WORKING;
                        if (rb.SendPoseStamped(doorServiceCtrl.PointFrontLine))
                        {
                            StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM_REG;
                            registryRobotJourney.startPlaceName = Traffic.DetermineArea(robot.properties.pose.Position, TypeZone.OPZS);
                            registryRobotJourney.startPoint     = robot.properties.pose.Position;
                            registryRobotJourney.endPoint       = doorServiceCtrl.PointFrontLine.Position;
                            robot.ShowText("FORBUF_ROBOT_WAITTING_GOTO_CHECKIN_GATE");
                        }
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_GOTO_BACK_FRONTLINE_READY:
                    if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                    {
                        break;
                    }
                    robot.ShowText("Detetect Inside Station Check Ready");
                    if (rb.SendCmdPosPallet(RequestCommandPosPallet.REQUEST_GOBACK_FRONTLINE_TURN_LEFT))
                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();
                        do
                        {
                            robot.onFlagGoBackReady = true;
                            if (resCmd == ResponseCommand.RESPONSE_FINISH_GOBACK_FRONTLINE || Traffic.RobotIsInArea("READY_FRONTLINE", robot.properties.pose.Position))
                            {
                                robot.onFlagGoBackReady = false;
                                robot.robotTag          = RobotStatus.WORKING;
                                if (rb.SendPoseStamped(doorServiceCtrl.PointFrontLine))
                                {
                                    resCmd        = ResponseCommand.RESPONSE_NONE;
                                    StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_READY;
                                    robot.ShowText("FORBUF_ROBOT_WAITTING_GOTO_GATE");
                                    break;
                                }
                            }
                            else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                            {
                                errorCode = ErrorCode.DETECT_LINE_ERROR;
                                CheckUserHandleError(this);
                                break;
                            }
                            if (sw.ElapsedMilliseconds > TIME_OUT_WAIT_GOTO_FRONTLINE)
                            {
                                errorCode = ErrorCode.DETECT_LINE_ERROR;
                                CheckUserHandleError(this);
                                break;
                            }
                            Thread.Sleep(100);
                        } while (ProRunStopW);
                        sw.Stop();
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_READY:
                    // dò ra điểm đích đến và xóa đăng ký vùng
                    // TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    if (Traffic.RobotIsInArea("C3", rb.properties.pose.Position))
                    {
                        ds.setDoorBusy(true);
                        ds.openDoor(DoorService.DoorType.DOOR_BACK);
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_READY_OPEN_DOOR;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_READY_OPEN_DOOR:
                    // dò ra điểm đích đến và xóa đăng ký vùng
                    TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    if (resCmd == ResponseCommand.RESPONSE_LASER_CAME_POINT)
                    {
                        robot.setTrafficAllCircles(false, false, false, false);
                        TrafficRountineConstants.RegIntZone_READY.Release(robot);
                        robot.SwitchToDetectLine(true);
                        resCmd        = ResponseCommand.RESPONSE_NONE;
                        StateForkLift = ForkLift.FORBUF_ROBOT_CAME_GATE_POSITION;
                        robot.ShowText("FORBUF_ROBOT_CAME_GATE_POSITION");
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM_REG:
                    // kiem tra vung đăng ký tai khu vuc xac định
                    if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                    {
                        break;
                    }
                    else
                    {
                        // dò ra điểm đích đến và xóa đăng ký vùng
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM:
                    // kiem tra vung đăng ký tai khu vuc xac định
                    //TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    if (Traffic.RobotIsInArea("C3", rb.properties.pose.Position))
                    {
                        ds.setDoorBusy(true);
                        ds.openDoor(DoorService.DoorType.DOOR_BACK);
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM_OPEN_DOOR;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOTO_GATE_FROM_VIM_OPEN_DOOR:
                    // kiem tra vung đăng ký tai khu vuc xac định
                    TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    if (resCmd == ResponseCommand.RESPONSE_LASER_CAME_POINT)
                    {
                        // robot.setTrafficAllCircles(false, false, false, false);
                        TrafficRountineConstants.RegIntZone_READY.Release(robot);
                        robot.SwitchToDetectLine(true);
                        resCmd = ResponseCommand.RESPONSE_NONE;
                        //rb.prioritLevel.OnAuthorizedPriorityProcedure = true;
                        StateForkLift = ForkLift.FORBUF_ROBOT_CAME_GATE_POSITION;
                        robot.ShowText("FORBUF_ROBOT_CAME_GATE_POSITION");
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_CAME_GATE_POSITION:     // da den khu vuc cong , gui yeu cau mo cong.
                    robot.robotRegistryToWorkingZone.onRobotwillCheckInsideGate = false;
                    //ds.setDoorBusy(true);
                    //ds.openDoor(DoorService.DoorType.DOOR_BACK);
                    StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_OPEN_DOOR;
                    robot.ShowText("FORBUF_ROBOT_WAITTING_OPEN_DOOR");
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_OPEN_DOOR:     //doi mo cong
                    RetState ret = ds.checkOpen(DoorService.DoorType.DOOR_BACK);
                    if (RetState.DOOR_CTRL_SUCCESS == ret || robot.onForceGoToGate)
                    {
                        ds.resetWaitOpenBack();
                        robot.onForceGoToGate = false;
                        StateForkLift         = ForkLift.FORBUF_ROBOT_OPEN_DOOR_SUCCESS;
                        robot.ShowText("FORBUF_ROBOT_OPEN_DOOR_SUCCESS");
                    }
                    else if (RetState.DOOR_CTRL_ERROR == ret)
                    {
                        robot.ShowText("FORBUF_ROBOT_OPEN_DOOR_ERROR");
                        //StateForkLift = ForkLift.FORBUF_ROBOT_CAME_GATE_POSITION;
                        Thread.Sleep(1000);
                        ds.setDoorBusy(true);
                        ds.openDoor(DoorService.DoorType.DOOR_BACK);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_OPEN_DOOR_SUCCESS:     // mo cua thang cong ,gui toa do line de robot di vao gap hang
                    // rb.SendCmdLineDetectionCtrl(RequestCommandLineDetect.REQUEST_LINEDETECT_PALLETUP);
                    if (rb.SendCmdAreaPallet(doorServiceCtrl.infoPallet))
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_PICKUP_PALLET_IN;
                        robot.ShowText("FORBUF_ROBOT_WAITTING_PICKUP_PALLET_IN");
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_PICKUP_PALLET_IN:     // doi robot gap hang
                    try
                    {
                        DoorStatus ret1 = ds.getStatusDoor(DoorType.DOOR_BACK);
                        if (DoorStatus.DOOR_OPEN == ret1)
                        {
                            robot.SetSpeedTraffic(RobotSpeedLevel.ROBOT_SPEED_NORMAL, false);
                            if (resCmd == ResponseCommand.RESPONSE_LINEDETECT_PALLETUP)
                            {
                                resCmd        = ResponseCommand.RESPONSE_NONE;
                                StateForkLift = ForkLift.FORBUF_ROBOT_FINISH_PALLET_UP;
                                Console.WriteLine("pallet ID" + order.palletId);

                                robot.robotBahaviorAtGate = RobotBahaviorAtReadyGate.GOING_OUTSIDE_GATE;
                                robot.ShowText("FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_GATE");
                            }
                            else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                            {
                                errorCode = ErrorCode.DETECT_LINE_ERROR;
                                CheckUserHandleError(this);
                            }
                        }
                        //else if (DoorStatus.DOOR_UNKNOW == ret1)
                        //{

                        //}
                        else    /* if(DoorStatus.DOOR_CLOSE == ret1)*/
                        {
                            robot.ShowText("DOOR_ERROR_(-___-)");
                            robot.SetSpeedTraffic(RobotSpeedLevel.ROBOT_SPEED_STOP, true);
                            ds.setDoorBusy(true);
                            ds.openDoor(DoorType.DOOR_BACK);
                            Thread.Sleep(7000);
                        }
                    }
                    catch
                    {
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_FINISH_PALLET_UP:

                    String destName = Traffic.DetermineArea(endPointBuffer.Position, TypeZone.MAIN_ZONE);
                    if (destName.Equals("VIM"))
                    {
                        if (checkAnyRobotAtElevator(robot))
                        {
                            break;
                        }
                        else
                        {
                            StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_GATE;
                        }
                    }
                    else
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_GATE;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_GATE:
                    // kiem tra da toi vung dong cong
                    if (!Traffic.RobotIsInArea("CLOSE-GATE", robot.properties.pose.Position))
                    {
                        DoorStatus ret2 = ds.getStatusDoor(DoorType.DOOR_BACK);
                        if (DoorStatus.DOOR_OPEN == ret2)
                        {
                            robot.SetSpeedTraffic(RobotSpeedLevel.ROBOT_SPEED_NORMAL, false);
                            if (resCmd == ResponseCommand.RESPONSE_FINISH_GOBACK_FRONTLINE)
                            {
                                Global_Object.setGateStatus(order.gate, false);
                                resCmd = ResponseCommand.RESPONSE_NONE;
                                ds.LampSetStateOff(DoorType.DOOR_FRONT);
                                ds.closeDoor(DoorType.DOOR_BACK);
                                ds.setDoorBusy(false);
                                StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_CLOSE_GATE;
                                robot.ShowText("FORBUF_ROBOT_WAITTING_CLOSE_GATE");
                            }
                            else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                            {
                                errorCode = ErrorCode.DETECT_LINE_ERROR;
                                CheckUserHandleError(this);
                            }
                        }
                        //else if (DoorStatus.DOOR_UNKNOW == ret1)
                        //{

                        //}
                        else    /* if(DoorStatus.DOOR_CLOSE == ret1)*/
                        {
                            robot.ShowText("DOOR_ERROR_(-___-)");
                            robot.SetSpeedTraffic(RobotSpeedLevel.ROBOT_SPEED_STOP, true);
                            ds.setDoorBusy(true);
                            ds.openDoor(DoorType.DOOR_BACK);
                            Thread.Sleep(6000);
                        }
                    }
                    else
                    {
                        robot.SetSpeedTraffic(RobotSpeedLevel.ROBOT_SPEED_NORMAL, false);
                        if (resCmd == ResponseCommand.RESPONSE_FINISH_GOBACK_FRONTLINE)
                        {
                            Global_Object.setGateStatus(order.gate, false);
                            resCmd = ResponseCommand.RESPONSE_NONE;
                            ds.LampSetStateOff(DoorType.DOOR_FRONT);
                            ds.closeDoor(DoorType.DOOR_BACK);
                            ds.setDoorBusy(false);
                            StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_CLOSE_GATE;
                            robot.ShowText("FORBUF_ROBOT_WAITTING_CLOSE_GATE");
                        }
                        else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                        {
                            errorCode = ErrorCode.DETECT_LINE_ERROR;
                            CheckUserHandleError(this);
                        }
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_CLOSE_GATE:
                    try
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_CHECK_GOTO_BUFFER_OR_MACHINE;
                        robot.SwitchToDetectLine(false);
                        registryRobotJourney.startPlaceName = Traffic.DetermineArea(robot.properties.pose.Position, TypeZone.OPZS);
                        registryRobotJourney.startPoint     = robot.properties.pose.Position;
                        registryRobotJourney.endPoint       = endPointBuffer.Position;
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_CHECK_GOTO_BUFFER_OR_MACHINE:
                    order.status    = StatusOrderResponseCode.DELIVERING;
                    flToMachineInfo = GetPriorityTaskForkLiftToMachine(order.productDetailId);
                    if (flToMachineInfo == null)
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_ZONE_BUFFER_READY;
                        registryRobotJourney.startPlaceName = Traffic.DetermineArea(robot.properties.pose.Position, TypeZone.OPZS);
                        registryRobotJourney.startPoint     = robot.properties.pose.Position;
                        registryRobotJourney.endPoint       = endPointBuffer.Position;
                    }
                    else
                    {
                        FreePlanedBuffer(order.palletId_P);
                        UpdatePalletState(PalletStatus.W, flToMachineInfo.palletId, flToMachineInfo.planId);
                        StateForkLift = ForkLift.FORMAC_ROBOT_GOTO_FRONTLINE_MACHINE_FROM_VIM_REG;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_ZONE_BUFFER_READY:
                    if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                    {
                        break;
                    }
                    Pose destPos1 = endPointBuffer;
                    if (rb.SendPoseStamped(destPos1))
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER;
                        robot.ShowText("FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER");
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER_FROM_VIM_REG:
                    if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                    {
                        break;
                    }
                    else
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER_FROM_VIM;
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER_FROM_VIM:
                    TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    try
                    {
                        if (resCmd == ResponseCommand.RESPONSE_LASER_CAME_POINT)
                        {
                            robot.SwitchToDetectLine(true);
                            resCmd        = ResponseCommand.RESPONSE_NONE;
                            StateForkLift = ForkLift.FORBUF_ROBOT_SEND_CMD_DROPDOWN_PALLET_BUFFER;
                        }
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_CAME_FRONTLINE_BUFFER:
                    // xóa đăng ký vùng
                    TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    if (TrafficCheckInBuffer(goalFrontLinePos, bayId))
                    {
                        break;
                    }
                    try
                    {
                        if (resCmd == ResponseCommand.RESPONSE_LASER_CAME_POINT)
                        {
                            robot.SwitchToDetectLine(true);
                            resCmd        = ResponseCommand.RESPONSE_NONE;
                            StateForkLift = ForkLift.FORBUF_ROBOT_SEND_CMD_DROPDOWN_PALLET_BUFFER;
                        }
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_SEND_CMD_DROPDOWN_PALLET_BUFFER:
                    JResult = FlToBuf.GetInfoPallet_P_InBuffer(PistonPalletCtrl.PISTON_PALLET_DOWN);
                    String data = JsonConvert.SerializeObject(JResult.jInfoPallet);
                    if (rb.SendCmdAreaPallet(data))
                    {
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_DROPDOWN_PALLET_BUFFER;
                        robot.ShowText("FORBUF_ROBOT_WAITTING_DROPDOWN_PALLET_BUFFER");
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_DROPDOWN_PALLET_BUFFER:
                    if (resCmd == ResponseCommand.RESPONSE_LINEDETECT_PALLETDOWN)
                    {
                        resCmd = ResponseCommand.RESPONSE_NONE;
                        FlToBuf.UpdatePalletState(PalletStatus.W, JResult.palletId, order.planId);
                        //   rb.SendCmdPosPallet (RequestCommandPosPallet.REQUEST_GOBACK_FRONTLINE);
                        StateForkLift = ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_BUFFER;
                        robot.ShowText("FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_BUFFER");
                    }
                    else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                    {
                        Console.WriteLine("Loi Update :ForkLift.FORBUF_ROBOT_WAITTING_DROPDOWN_PALLET_BUFFER");
                        errorCode = ErrorCode.DETECT_LINE_ERROR;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_BUFFER:     // đợi
                    if (resCmd == ResponseCommand.RESPONSE_FINISH_GOBACK_FRONTLINE)
                    {
                        robot.bayId    = -1;
                        robot.bayIdReg = false;
                        robot.ReleaseWorkingZone();
                        robot.SwitchToDetectLine(false);
                        resCmd = ResponseCommand.RESPONSE_NONE;
                        //rb.prioritLevel.OnAuthorizedPriorityProcedure = false;
                        StateForkLift = ForkLift.FORBUF_ROBOT_RELEASED;
                        robot.ShowText("FORBUF_ROBOT_RELEASED");
                    }
                    else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                    {
                        Console.WriteLine("Loi Update :ForkLift.FORBUF_ROBOT_WAITTING_GOBACK_FRONTLINE_BUFFER");
                        errorCode = ErrorCode.DETECT_LINE_ERROR;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORBUF_ROBOT_RELEASED:     // trả robot về robotmanagement để nhận quy trình mới
                    rb.PreProcedureAs = ProcedureControlAssign.PRO_FORKLIFT_TO_BUFFER;
                    ReleaseProcedureHandler(this);
                    ProRun = false;
                    robot.ShowText("RELEASED");
                    UpdateInformationInProc(this, ProcessStatus.S);
                    order.status = StatusOrderResponseCode.FINISHED;
                    break;

                case ForkLift.FORMAC_ROBOT_GOTO_FRONTLINE_MACHINE_FROM_VIM_REG:
                    if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                    {
                        break;
                    }
                    else
                    {
                        StateForkLift = ForkLift.FORMAC_ROBOT_GOTO_FRONTLINE_MACHINE_FROM_VIM;
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_GOTO_FRONTLINE_MACHINE_FROM_VIM:
                    TrafficRountineConstants.DetectRelease(registryRobotJourney);
                    try
                    {
                        if (rb.SendPoseStamped(flToMachineInfo.frontLinePose))
                        {
                            StateForkLift = ForkLift.FORMAC_ROBOT_WAITTING_CAME_FRONTLINE_MACHINE;
                            robot.ShowText("FORMAC_ROBOT_WAITTING_CAME_FRONTLINE_MACHINE");
                            onFlagResetedGate = false;
                        }
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_GOTO_FRONTLINE_MACHINE:
                    try
                    {
                        if (TrafficRountineConstants.DetetectInsideStationCheck(registryRobotJourney))
                        {
                            break;
                        }
                        if (rb.SendPoseStamped(flToMachineInfo.frontLinePose))
                        {
                            StateForkLift = ForkLift.FORMAC_ROBOT_WAITTING_CAME_FRONTLINE_MACHINE;
                            robot.ShowText("FORMAC_ROBOT_WAITTING_CAME_FRONTLINE_MACHINE");
                            onFlagResetedGate = false;
                        }
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_WAITTING_CAME_FRONTLINE_MACHINE:
                    try
                    {
                        TrafficRountineConstants.DetectRelease(registryRobotJourney);
                        if (resCmd == ResponseCommand.RESPONSE_LASER_CAME_POINT)
                        {
                            robot.SwitchToDetectLine(true);
                            StateForkLift = ForkLift.FORMAC_ROBOT_SEND_CMD_DROPDOWN_PALLET_MACHINE;
                        }
                    }
                    catch (System.Exception)
                    {
                        errorCode = ErrorCode.CAN_NOT_GET_DATA;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_SEND_CMD_DROPDOWN_PALLET_MACHINE:
                    if (rb.SendCmdAreaPallet(flToMachineInfo.infoPallet))
                    {
                        StateForkLift = ForkLift.FORMAC_ROBOT_WAITTING_DROPDOWN_PALLET_MACHINE;
                        robot.ShowText("FORMAC_ROBOT_WAITTING_DROPDOWN_PALLET_MACHINE");
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_WAITTING_DROPDOWN_PALLET_MACHINE:
                    if (resCmd == ResponseCommand.RESPONSE_LINEDETECT_PALLETDOWN)
                    {
                        resCmd = ResponseCommand.RESPONSE_NONE;
                        // FlToBuf.UpdatePalletState(PalletStatus.W);
                        //   rb.SendCmdPosPallet (RequestCommandPosPallet.REQUEST_GOBACK_FRONTLINE);
                        StateForkLift = ForkLift.FORMAC_ROBOT_WAITTING_GOBACK_FRONTLINE_MACHINE;
                        robot.ShowText("FORMAC_ROBOT_WAITTING_GOBACK_FRONTLINE_MACHINE");
                    }
                    else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                    {
                        errorCode = ErrorCode.DETECT_LINE_ERROR;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_WAITTING_GOBACK_FRONTLINE_MACHINE:     // đợi
                    if (resCmd == ResponseCommand.RESPONSE_FINISH_GOBACK_FRONTLINE)
                    {
                        robot.SwitchToDetectLine(false);
                        resCmd = ResponseCommand.RESPONSE_NONE;
                        //rb.prioritLevel.OnAuthorizedPriorityProcedure = false;
                        StateForkLift = ForkLift.FORMAC_ROBOT_RELEASED;
                        robot.ShowText("FORMAC_ROBOT_RELEASED");
                    }
                    else if (resCmd == ResponseCommand.RESPONSE_ERROR)
                    {
                        errorCode = ErrorCode.DETECT_LINE_ERROR;
                        CheckUserHandleError(this);
                    }
                    break;

                case ForkLift.FORMAC_ROBOT_RELEASED:     // trả robot về robotmanagement để nhận quy trình mới
                    ds.removeListCtrlDoorBack();
                    TrafficRountineConstants.ReleaseAll(robot);
                    robot.orderItem = null;
                    // Global_Object.onFlagDoorBusy = false;
                    robot.SwitchToDetectLine(false);
                    // robot.robotTag = RobotStatus.IDLE;
                    robot.ReleaseWorkingZone();
                    rb.PreProcedureAs = ProcedureControlAssign.PRO_FORKLIFT_TO_BUFFER;
                    // if (errorCode == ErrorCode.RUN_OK) {

                    // } else {
                    //     ErrorProcedureHandler (this);
                    // }
                    ProRun = false;
                    robot.ShowText("RELEASED");
                    UpdateInformationInProc(this, ProcessStatus.S);
                    order.status             = StatusOrderResponseCode.FINISHED;
                    order.endTimeProcedure   = DateTime.Now;
                    order.totalTimeProcedure = order.endTimeProcedure.Subtract(order.startTimeProcedure).TotalMinutes;
                    KillEvent();
                    ReleaseProcedureHandler(this);
                    break;

                case ForkLift.FORMAC_ROBOT_DESTROY:     // trả robot về robotmanagement để nhận quy trình mới
                    ds.removeListCtrlDoorBack();
                    TrafficRountineConstants.ReleaseAll(robot);
                    robot.SwitchToDetectLine(false);
                    robot.ReleaseWorkingZone();
                    robot.robotBahaviorAtGate = RobotBahaviorAtReadyGate.IDLE;
                    //robot.prioritLevel.OnAuthorizedPriorityProcedure = false;
                    ProRun = false;
                    UpdateInformationInProc(this, ProcessStatus.F);
                    order.status      = StatusOrderResponseCode.ROBOT_ERROR;
                    selectHandleError = SelectHandleError.CASE_ERROR_EXIT;
                    procedureStatus   = ProcedureStatus.PROC_KILLED;
                    // RestoreOrderItem();
                    //FreePlanedBuffer(order.palletId_P);
                    order.endTimeProcedure   = DateTime.Now;
                    order.totalTimeProcedure = order.endTimeProcedure.Subtract(order.startTimeProcedure).TotalMinutes;
                    KillEvent();
                    break;

                //////////////////////////////////////////////////////
                default:
                    break;
                }
                Thread.Sleep(700);
            }
            StateForkLift = ForkLift.FORBUF_IDLE;
        }
        public void Register(ProcedureItemSelected ProcedureItem, RobotUnity robot, OrderItem orderItem)
        {
            CleanUp();
            switch (ProcedureItem)
            {
            case ProcedureItemSelected.PROCEDURE_FORLIFT_TO_BUFFER:
            {
                ProcedureForkLiftToBuffer procfb = new ProcedureForkLiftToBuffer(robot, doorService, trafficService);
                procfb.Registry(deviceService);
                ProcedureDataItems profbDataItems = new ProcedureDataItems();
                profbDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocfb = new RegisterProcedureItem()
                {
                    item = procfb, robot = robot, procedureDataItems = profbDataItems
                };
                procfb.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procfb.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                //RegisterProcedureItemList.Add (itemprocfb);
                procfb.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pFB = procfb;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_FORKLIFT_TO_BUFFER;
                procfb.Registry(robotManagementService);
                procfb.Start();

                break;
            }

            case ProcedureItemSelected.PROCEDURE_BUFFER_TO_MACHINE:
                ProcedureBufferToMachine procbm = new ProcedureBufferToMachine(robot, trafficService);
                procbm.Registry(deviceService);
                ProcedureDataItems prcobmDataItems = new ProcedureDataItems();
                prcobmDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocbm = new RegisterProcedureItem()
                {
                    item = procbm, robot = robot, procedureDataItems = prcobmDataItems
                };
                procbm.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procbm.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                //RegisterProcedureItemList.Add (itemprocbm);
                procbm.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pBM = procbm;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_BUFFER_TO_MACHINE;
                procbm.Registry(robotManagementService);
                procbm.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_BUFFER_TO_RETURN:
                ProcedureBufferToReturn procbr          = new ProcedureBufferToReturn(robot, trafficService);
                ProcedureDataItems      prcobrDataItems = new ProcedureDataItems();
                prcobrDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocbr = new RegisterProcedureItem()
                {
                    item = procbr, robot = robot, procedureDataItems = prcobrDataItems
                };
                procbr.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procbr.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocbr);
                procbr.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pBR = procbr;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_BUFFER_TO_RETURN;
                procbr.Registry(robotManagementService);
                procbr.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_PALLETEMPTY_MACHINE_TO_RETURN:
                ProcedureMachineToReturn procmr          = new ProcedureMachineToReturn(robot, trafficService);
                ProcedureDataItems       prcomrDataItems = new ProcedureDataItems();
                prcomrDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocmr = new RegisterProcedureItem()
                {
                    item = procmr, robot = robot, procedureDataItems = prcomrDataItems
                };
                procmr.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procmr.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                //RegisterProcedureItemList.Add (itemprocmr);
                procmr.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pMR = procmr;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_MACHINE_TO_RETURN;
                procmr.Registry(robotManagementService);
                procmr.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_RETURN_TO_GATE:
                ProcedureReturnToGate procrg         = new ProcedureReturnToGate(robot, doorService, trafficService);
                ProcedureDataItems    prorgDataItems = new ProcedureDataItems();
                prorgDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocrg = new RegisterProcedureItem()
                {
                    item = procrg, robot = robot, procedureDataItems = prorgDataItems
                };
                procrg.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procrg.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocrg);
                procrg.AssignAnOrder(orderItem);
                procrg.Registry(robotManagementService);
                procrg.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_ROBOT_TO_CHARGE:
                ProcedureRobotToCharger procrc          = new ProcedureRobotToCharger(robot, chargerService, robot.properties.ChargeID);
                ProcedureDataItems      procrcDataItems = new ProcedureDataItems();
                procrcDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocrc = new RegisterProcedureItem()
                {
                    item = procrc, robot = robot, procedureDataItems = procrcDataItems
                };
                procrc.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procrc.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                //  RegisterProcedureItemList.Add (itemprocrc);
                robot.proRegistryInRobot.pRC = procrc;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_CHARGE;
                procrc.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_ROBOT_TO_READY:
                robot.ShowText(robot.properties.Label + "Add PROCEDURE_ROBOT_TO_READY ----------(--___--)---------------");
                ProcedureRobotToReady procrr          = new ProcedureRobotToReady(robot, robot.properties.ChargeID, trafficService, chargerService, null);
                ProcedureDataItems    procrrDataItems = new ProcedureDataItems();
                procrrDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocrr = new RegisterProcedureItem()
                {
                    item = procrr, robot = robot, procedureDataItems = procrrDataItems
                };
                procrr.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procrr.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocrr);
                robot.proRegistryInRobot.pRR = procrr;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_READY;
                procrr.Registry(deviceService);
                procrr.Registry(robotManagementService);
                procrr.Registry(assigmentTask);
                procrr.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_ROBOT_READY_TO_READY:
                ProcedureRobotToReady procRrr          = new ProcedureRobotToReady(robot, robot.properties.ChargeID, trafficService, chargerService, doorService.DoorMezzamineUp.config.PointCheckInGate);
                ProcedureDataItems    procRrrDataItems = new ProcedureDataItems();
                procRrrDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocRrr = new RegisterProcedureItem()
                {
                    item = procRrr, robot = robot, procedureDataItems = procRrrDataItems
                };
                procRrr.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procRrr.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add(itemprocRrr);
                robot.proRegistryInRobot.pRR = procRrr;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_READY;
                procRrr.Registry(robotManagementService);
                procRrr.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_FORLIFT_TO_MACHINE:
                ProcedureForkLiftToMachine procfm         = new ProcedureForkLiftToMachine(robot, doorService, trafficService);
                ProcedureDataItems         profmDataItems = new ProcedureDataItems();
                profmDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocfm = new RegisterProcedureItem()
                {
                    item = procfm, robot = robot, procedureDataItems = profmDataItems
                };
                procfm.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procfm.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                //RegisterProcedureItemList.Add(itemprocfm);
                procfm.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pFM = procfm;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_FORKLIFT_TO_MACHINE;
                procfm.Registry(robotManagementService);
                procfm.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_BUFFER_TO_BUFFER:
                ProcedureBufferReturnToBuffer401 procbb = new ProcedureBufferReturnToBuffer401(robot, trafficService);
                ProcedureDataItems prcobbDataItems      = new ProcedureDataItems();
                prcobbDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocbb = new RegisterProcedureItem()
                {
                    item = procbb, robot = robot, procedureDataItems = prcobbDataItems
                };
                procbb.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procbb.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocbr);
                procbb.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pBB = procbb;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_BUFFER_TO_BUFFER;
                procbb.Registry(robotManagementService);
                procbb.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_BUFFER_TO_GATE:
                ProcedureBufferToGate procbg         = new ProcedureBufferToGate(robot, doorService, trafficService);
                ProcedureDataItems    probgDataItems = new ProcedureDataItems();
                probgDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocbg = new RegisterProcedureItem()
                {
                    item = procbg, robot = robot, procedureDataItems = probgDataItems
                };
                procbg.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procbg.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocrg);
                procbg.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pBG = procbg;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_BUFFER_TO_GATE;
                procbg.Registry(robotManagementService);
                procbg.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_MACHINE_TO_GATE:
                ProcedureMachineToGate procmg         = new ProcedureMachineToGate(robot, doorService, trafficService);
                ProcedureDataItems     promgDataItems = new ProcedureDataItems();
                promgDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocmg = new RegisterProcedureItem()
                {
                    item = procmg, robot = robot, procedureDataItems = promgDataItems
                };
                procmg.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procmg.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocrg);
                procmg.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pMG = procmg;
                robot.ProcedureRobotAssigned = ProcedureControlAssign.PRO_MACHINE_TO_GATE;
                procmg.Start();
                break;

            case ProcedureItemSelected.PROCEDURE_MACHINE_TO_BUFFER_RETURN:
                ProcedureMachineToBufferReturn procmbr         = new ProcedureMachineToBufferReturn(robot, trafficService);
                ProcedureDataItems             prombrDataItems = new ProcedureDataItems();
                prombrDataItems.StartTaskTime = DateTime.Now;
                RegisterProcedureItem itemprocmbr = new RegisterProcedureItem()
                {
                    item = procmbr, robot = robot, procedureDataItems = prombrDataItems
                };
                procmbr.ReleaseProcedureHandler += ReleaseProcedureItemHandler;
                procmbr.ErrorProcedureHandler   += ErrorApprearInProcedureItem;
                // RegisterProcedureItemList.Add (itemprocrg);
                procmbr.AssignAnOrder(orderItem);
                robot.proRegistryInRobot.pMBR = procmbr;
                robot.ProcedureRobotAssigned  = ProcedureControlAssign.PRO_MACHINE_TO_BUFFER_RETURN;
                procmbr.Registry(robotManagementService);
                procmbr.Start();
                break;
            }
        }