From 5b5757c7d40d2f456c0b3b1d8bb87544019e4165 Mon Sep 17 00:00:00 2001 From: bbod Date: Thu, 5 Sep 2019 00:51:07 -0600 Subject: [PATCH] Fixed a lot of bugs shoudl be able to write unit tests now. --- .../.idea.Tello Drone/.idea/contentModel.xml | 2 - Tello Drone/ConsoleClient.cs | 1 - Tello Drone/Drone.cs | 2 - Tello Drone/DroneComands.cs | 11 ++--- Tello Drone/DroneConstants.cs | 8 ---- Tello Drone/IConsoleLogger.cs | 4 -- Tello Drone/IDroneConstants.cs | 10 ----- Tello Drone/IDroneFactory.cs | 2 - Tello Drone/IUdpClientWrapper.cs | 3 +- Tello Drone/Missions.cs | 28 ++++++------ Tello Drone/Program.cs | 7 +-- Tello Drone/UdpClientWrapper.cs | 44 +++++++++---------- 12 files changed, 41 insertions(+), 81 deletions(-) delete mode 100644 Tello Drone/DroneConstants.cs delete mode 100644 Tello Drone/IDroneConstants.cs diff --git a/.idea/.idea.Tello Drone/.idea/contentModel.xml b/.idea/.idea.Tello Drone/.idea/contentModel.xml index 3a7b1d4..c7d4cba 100644 --- a/.idea/.idea.Tello Drone/.idea/contentModel.xml +++ b/.idea/.idea.Tello Drone/.idea/contentModel.xml @@ -19,11 +19,9 @@ - - diff --git a/Tello Drone/ConsoleClient.cs b/Tello Drone/ConsoleClient.cs index d96c27b..6a0ca76 100644 --- a/Tello Drone/ConsoleClient.cs +++ b/Tello Drone/ConsoleClient.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; namespace Tello_Drone { diff --git a/Tello Drone/Drone.cs b/Tello Drone/Drone.cs index 32ccadf..1a96561 100644 --- a/Tello Drone/Drone.cs +++ b/Tello Drone/Drone.cs @@ -1,5 +1,3 @@ -using System.Runtime.CompilerServices; - namespace Tello_Drone { public class Drone diff --git a/Tello Drone/DroneComands.cs b/Tello Drone/DroneComands.cs index 90e53c3..8b0fbeb 100644 --- a/Tello Drone/DroneComands.cs +++ b/Tello Drone/DroneComands.cs @@ -1,7 +1,4 @@ -using System; -using System.Net; -using System.Reflection.Metadata.Ecma335; -using System.Xml.Schema; + namespace Tello_Drone { @@ -15,7 +12,7 @@ namespace Tello_Drone _udpClient = udpClient; } - public void Forward(int x) => SendCommand($"forward {x}"); + public void Forward(int x) =>SendCommand() ($"forward {x}"); public void Reverse(int x) => SendCommand($"back {x}"); public void Up(int z) => SendCommand($"up {z}"); public void Down(int z) => SendCommand($"down {z}"); @@ -34,14 +31,14 @@ namespace Tello_Drone private void SendCommand(string message) { - var returnValue = _udpClient.TrySend(message, 0_500); + var returnValue = _udpClient.TrySend(message, 0_500, 3); if (returnValue == false) SendCommand("land"); } private bool TrySendCommand(string message) { - return _udpClient.TrySend(message, 0_500); + return _udpClient.TrySend(message, 0_500, 3); } } diff --git a/Tello Drone/DroneConstants.cs b/Tello Drone/DroneConstants.cs deleted file mode 100644 index 92ae8c6..0000000 --- a/Tello Drone/DroneConstants.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Tello_Drone -{ - public class DroneConstants : IDroneConstants - { - public string DroneIPAddress { get; set; } - public int DronePortNumber { get; set; } - } -} \ No newline at end of file diff --git a/Tello Drone/IConsoleLogger.cs b/Tello Drone/IConsoleLogger.cs index ecad6bc..2255225 100644 --- a/Tello Drone/IConsoleLogger.cs +++ b/Tello Drone/IConsoleLogger.cs @@ -1,11 +1,7 @@ -using System; - namespace Tello_Drone { public interface IConsoleLogger { - event Action LogReceived; - void Log(string message); } diff --git a/Tello Drone/IDroneConstants.cs b/Tello Drone/IDroneConstants.cs deleted file mode 100644 index f528587..0000000 --- a/Tello Drone/IDroneConstants.cs +++ /dev/null @@ -1,10 +0,0 @@ -using DryIoc; - -namespace Tello_Drone -{ - public interface IDroneConstants - { - string DroneIPAddress { get; set; } - int DronePortNumber { get; set; } - } -} \ No newline at end of file diff --git a/Tello Drone/IDroneFactory.cs b/Tello Drone/IDroneFactory.cs index c2b5457..41e31d2 100644 --- a/Tello Drone/IDroneFactory.cs +++ b/Tello Drone/IDroneFactory.cs @@ -1,5 +1,3 @@ -using System.Dynamic; - namespace Tello_Drone { public interface IDroneFactory diff --git a/Tello Drone/IUdpClientWrapper.cs b/Tello Drone/IUdpClientWrapper.cs index aec9b63..2be2257 100644 --- a/Tello Drone/IUdpClientWrapper.cs +++ b/Tello Drone/IUdpClientWrapper.cs @@ -1,10 +1,9 @@ using System; -using System.Net.Sockets; namespace Tello_Drone { public interface IUdpClientWrapper : IDisposable { - bool TrySend(string message, int timeOut); + bool TrySend(string message, int timeOut, int maxRetries); } } \ No newline at end of file diff --git a/Tello Drone/Missions.cs b/Tello Drone/Missions.cs index 6abb8e9..5d84ba8 100644 --- a/Tello Drone/Missions.cs +++ b/Tello Drone/Missions.cs @@ -1,37 +1,39 @@ -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using DryIoc; - namespace Tello_Drone { public class Missions : IMissions { - private readonly IConsoleLogger _consoleLogger; private Drone _drone; - private int setupRetry = 0; + private int _setupRetry; - public Missions( IDroneFactory droneFactory, IConsoleLogger consoleLogger) + public Missions( IDroneFactory droneFactory) { - _consoleLogger = consoleLogger; _drone = droneFactory.CreateDrone; } public void RunMission1() { MissionSetup(); - _drone.Down(3); - _drone.Up(3); + _drone.Up(20); + _drone.Down(20); + MissionTeardown(); } private void MissionSetup() { var inCommandMode = false; - while (inCommandMode != true && setupRetry < 3) + while (inCommandMode != true && _setupRetry < 3) { inCommandMode = _drone.Command(); - setupRetry++; - _consoleLogger.Log(inCommandMode.ToString()); + _setupRetry++; } + + if (!_drone.InitialTakeOff()) + _drone.InitialTakeOff(); + } + + private void MissionTeardown() + { + _drone.Land(); } } diff --git a/Tello Drone/Program.cs b/Tello Drone/Program.cs index b0c3c2c..946dc80 100644 --- a/Tello Drone/Program.cs +++ b/Tello Drone/Program.cs @@ -1,6 +1,4 @@ -using System; - -namespace Tello_Drone +namespace Tello_Drone { class Program { @@ -9,9 +7,6 @@ namespace Tello_Drone var bootstrapper = BootStrapper.BootstrapSystem(new CoreModule()); var missions = bootstrapper.Resolve(); var consoleLogger = bootstrapper.Resolve(); - var droneConstants = new DroneConstants(); - droneConstants.DronePortNumber = 1; - droneConstants.DroneIPAddress = "d"; var consoleClient = new ConsoleClient(missions, consoleLogger); consoleClient.Run(); } diff --git a/Tello Drone/UdpClientWrapper.cs b/Tello Drone/UdpClientWrapper.cs index a112716..5373902 100644 --- a/Tello Drone/UdpClientWrapper.cs +++ b/Tello Drone/UdpClientWrapper.cs @@ -1,39 +1,36 @@ using System; -using System.Diagnostics; using System.Net; -using System.Net.Http; using System.Net.Sockets; using System.Text; -using System.Threading.Tasks; -using System.Transactions; -using System.Xml.Schema; namespace Tello_Drone { internal class UdpClientWrapper : IUdpClientWrapper { private readonly UdpClient _client; - private int retryCount = 0; private IPEndPoint _sendIpEndPoint; - private IDroneConstants _droneConstants; private IConsoleLogger _consoleLogger; public UdpClientWrapper(IConsoleLogger consoleLogger) { _consoleLogger = consoleLogger; - _droneConstants = new DroneConstants(); consoleLogger.Log("Enter Drone IPAddress:"); - _droneConstants.DroneIPAddress = Console.ReadLine(); + var droneIpAddress = Console.ReadLine(); consoleLogger.Log("Enter Drone Port Number:"); - _droneConstants.DronePortNumber = Convert.ToInt32(Console.ReadLine()); - _client = new UdpClient(_droneConstants.DroneIPAddress, _droneConstants.DronePortNumber); - _sendIpEndPoint = new IPEndPoint(IPAddress.Parse(_droneConstants.DroneIPAddress), _droneConstants.DronePortNumber); + var dronePortNumber = Convert.ToInt32(Console.ReadLine()); + if (droneIpAddress != null) + { + _client = new UdpClient(droneIpAddress ?? throw new NullReferenceException($"{nameof(droneIpAddress)} was null."), dronePortNumber); + _sendIpEndPoint = new IPEndPoint(IPAddress.Parse(droneIpAddress), + dronePortNumber); + } } - public bool TrySend(string message, int timeout) + public bool TrySend(string message, int timeout, int maxRetries) { + _consoleLogger.Log($"Sending command to drone: {message}"); bool successfullFlag = false; - while(retryCount < 3 && successfullFlag == false) + while(maxRetries >= 0 && successfullFlag == false) { _client.Client.ReceiveTimeout = timeout; _client.Send(Encoding.ASCII.GetBytes(message), message.Length); @@ -42,25 +39,24 @@ namespace Tello_Drone { var bytes = _client.Receive(ref _sendIpEndPoint); var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length); + _consoleLogger.Log($"Drone responded with: {returnMessage}"); if (returnMessage == "ok") successfullFlag = true; - _consoleLogger.Log($"successfull flag = {successfullFlag}"); - } catch { - _consoleLogger.Log("Did not get a response from the drone retrying."); + _consoleLogger.Log($"Did not get a response from the drone will attempt {maxRetries.ToString()} more times"); } - retryCount++; + + maxRetries--; } return successfullFlag; } - - public void Dispose() - { - _client?.Dispose(); - } - + + public void Dispose() + { + _client?.Dispose(); + } } } \ No newline at end of file