public async Task <List <Drone.Model.DroneOutputModel> > GetDroneCoordinatesFromApi(string droneInputModel) { //string uri = settingsService.GetWebApiPath(); //string uri = Common.ServicesManager.GetClientUri(DroneService); string[] stringSeparators = new string[] { "\r\n" }; var commands = droneInputModel.TrimEnd().Split(stringSeparators, StringSplitOptions.None); List <DroneInputModel> inputModels = new List <DroneInputModel>(); var gridDimensions = commands[0].Split(' '); // First line sets the Battlefield dimensoins DroneControlModel droneControlModel = new DroneControlModel(); droneControlModel.GridTopX = int.Parse(gridDimensions[0]); droneControlModel.GridTopY = int.Parse(gridDimensions[1]); for (int i = 1; i < commands.Length; i += 2) { DroneInputModel model = new DroneInputModel(); var postion = commands[i].Split(' '); model.Position.PositionX = int.Parse(postion[0]); model.Position.PositionY = int.Parse(postion[1]); model.Position.Heading = postion[2]; model.Instructions = commands[i + 1]; droneControlModel.DroneInputs.Add(model); } string inputJson = JsonConvert.SerializeObject(droneControlModel); //var droneControlModel = JsonConvert.DeserializeObject<Drone.Model.DroneOutputModel>(droneControlJsonModel); string uri = "http://localhost/Drone.NavigationApi/api/DroneNavigation"; List <DroneOutputModel> FinalDroneCoordinates = new List <DroneOutputModel>(); try { using (HttpClient httpClient = new HttpClient()) { // FinalDroneCoordinates = JsonConvert.DeserializeObject<List<Drone.Model.DroneOutputModel>>( // await httpClient.GetStringAsync(uri) //); //var results = await httpClient.GetStringAsync(uri); //httpClient.GetAsync() StringContent content = new StringContent(JsonConvert.SerializeObject(droneControlModel), Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(uri, content); FinalDroneCoordinates = await response.Content.ReadAsAsync <List <Drone.Model.DroneOutputModel> >(); //FinalDroneCoordinates = JsonConvert.DeserializeObject<List<Drone.Model.DroneOutputModel>>( // await httpClient.PostAsJsonAsync(uri, droneControlModel).Result; //); return(FinalDroneCoordinates); } } catch (Exception ex) { //Log Exception Console.WriteLine(ex.Message); throw new Exception("Error occured while accessing the service API"); } return(FinalDroneCoordinates); }
public static DroneInputModel ExecuteCommand(DroneInputModel inputLocation, string moveDirection) { var commands = moveDirection.ToCharArray(); foreach (char command in commands) { switch (command) { case 'L': switch (inputLocation.Position.Heading) { case "E": inputLocation.Position.Heading = "N"; break; case "N": inputLocation.Position.Heading = "W"; break; case "W": inputLocation.Position.Heading = "S"; break; case "S": inputLocation.Position.Heading = "E"; break; } break; case 'R': switch (inputLocation.Position.Heading) { case "E": inputLocation.Position.Heading = "S"; break; case "N": inputLocation.Position.Heading = "E"; break; case "W": inputLocation.Position.Heading = "N"; break; case "S": inputLocation.Position.Heading = "W"; break; } break; case 'M': inputLocation = Move(inputLocation); break; } Console.WriteLine("OutPut : " + inputLocation.Position.PositionX + " " + inputLocation.Position.PositionY + " " + inputLocation.Position.Heading); } return(inputLocation); }
static void Main(string[] args) { DroneInputModel inputLocation = new DroneInputModel(); //inputLocation.Position.PositionX = 1; //inputLocation.Position.PositionY = 2; //inputLocation.Position.Heading = "N"; //string moveDirection = "LMLMLMLMM"; inputLocation.Position.PositionX = 3; inputLocation.Position.PositionY = 3; inputLocation.Position.Heading = "E"; string moveDirection = "MMRMMRMRRM"; var output = ExecuteCommand(inputLocation, moveDirection); Console.Read(); }
/// <summary> /// Mthod to move the x,y coordinates of the drone /// </summary> /// <param name="inputLocation"></param> /// <returns></returns> private DroneInputModel Move(DroneInputModel inputLocation) { string direction = inputLocation.Position.Heading; string move_direction = ""; bool can_move = true; var currentPositionX = inputLocation.Position.PositionX; var currentPositionY = inputLocation.Position.PositionY; if (direction == "E" || direction == "W") { move_direction = "X"; } else if (direction == "S" || direction == "N") { move_direction = "Y"; } if (direction == "E" || direction == "N") { if (direction == "E") { if ((currentPositionX < 0 || (currentPositionX) > GridTopX)) { can_move = false; } } if (direction == "N") { if (currentPositionY < 0 || (currentPositionY) > GridTopY) { can_move = false; } } if (can_move) { if (move_direction == "X") { inputLocation.Position.PositionX += 1; } else { inputLocation.Position.PositionY += 1; } } } else if (direction == "W" || direction == "S") { if (move_direction == "X" && currentPositionX > 0) { inputLocation.Position.PositionX -= 1; } else if (move_direction == "Y" && currentPositionY > 0) { inputLocation.Position.PositionY -= 1; } } return(inputLocation); }
/// <summary> /// Processes the navigation instructions /// </summary> /// <param name="droneInputCommand"></param> /// <returns></returns> private DroneOutputModel ExecuteCommand(DroneInputModel droneInputCommand) { var commands = droneInputCommand.Instructions.ToCharArray(); DroneOutputModel finalCoodinates = new DroneOutputModel(); foreach (char command in commands) { switch (command) { case 'L': switch (droneInputCommand.Position.Heading) { case "E": droneInputCommand.Position.Heading = "N"; break; case "N": droneInputCommand.Position.Heading = "W"; break; case "W": droneInputCommand.Position.Heading = "S"; break; case "S": droneInputCommand.Position.Heading = "E"; break; } break; case 'R': switch (droneInputCommand.Position.Heading) { case "E": droneInputCommand.Position.Heading = "S"; break; case "N": droneInputCommand.Position.Heading = "E"; break; case "W": droneInputCommand.Position.Heading = "N"; break; case "S": droneInputCommand.Position.Heading = "W"; break; } break; case 'M': droneInputCommand = Move(droneInputCommand); break; } } finalCoodinates.PositionX = droneInputCommand.Position.PositionX; finalCoodinates.PositionY = droneInputCommand.Position.PositionY; finalCoodinates.Heading = droneInputCommand.Position.Heading; return(finalCoodinates); }