コード例 #1
0
        /// <summary>
        /// Util function for Right and Left commands
        /// </summary>
        /// <param name="robotId"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        private IHttpActionResult TurnRobot(string robotId, RotationalDirection direction)
        {
            try
            {
                //do validation first here.
                if (string.IsNullOrEmpty(robotId))
                {
                    var feedback = new CommandFeedback
                    {
                        IsSuccess = false,
                        ErrorCode = CommandErrorCode.RobotNotPositioned
                    };
                    return(Ok(JsonConvert.SerializeObject(feedback)));
                }

                var robot = RobotService.GetRobot(robotId);

                //if we cannot find robot, we need to return error message.
                if (robot == null)
                {
                    var feedback = new CommandFeedback
                    {
                        IsSuccess = false,
                        ErrorCode = CommandErrorCode.RobotNotPositioned
                    };
                    return(Ok(JsonConvert.SerializeObject(feedback)));
                }

                CommandFeedback result;

                if (direction == RotationalDirection.Left)
                {
                    result = _rotateLeft.Impact(robot);
                }
                else if (direction == RotationalDirection.Right)
                {
                    result = _rotateRight.Impact(robot);
                }
                else
                {
                    throw new Exception("Invalid direction/Face provided for robot move.");
                }

                if (result.IsSuccess)
                {
                    RobotService.PersistRobot(robot);
                }

                return(Ok(JsonConvert.SerializeObject(result)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
コード例 #2
0
        /// <summary>
        /// Rotates robot to the right
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TurnRight(object sender, EventArgs e)
        {
            if (!_robot.IsPositioned)
            {
                MessageBox.Show(Properties.Resources.Error_Robot_NotPlaced);
                return;
            }

            var result = _rotateRight.Impact(_robot);//run command on robot

            if (result.IsSuccess)
            {
                RefreshRobotOnScreen(); //refresh screen to see impact, validation not needed for rotational commands
                return;
            }
            //if we get here then there is a problem
            MessageBox.Show(Properties.Resources.Error_CouldNot_Rotate_Robot);
        }