Fixed a lot of bugs shoudl be able to write unit tests now.

This commit is contained in:
2019-09-05 00:51:07 -06:00
parent 9e6ce4d9f6
commit 5b5757c7d4
12 changed files with 41 additions and 81 deletions

View File

@@ -19,11 +19,9 @@
<e p="CoreModule.cs" t="Include" /> <e p="CoreModule.cs" t="Include" />
<e p="Drone.cs" t="Include" /> <e p="Drone.cs" t="Include" />
<e p="DroneComands.cs" t="Include" /> <e p="DroneComands.cs" t="Include" />
<e p="DroneConstants.cs" t="Include" />
<e p="DroneFactory.cs" t="Include" /> <e p="DroneFactory.cs" t="Include" />
<e p="IConsoleLogger.cs" t="Include" /> <e p="IConsoleLogger.cs" t="Include" />
<e p="IDroneCommands.cs" t="Include" /> <e p="IDroneCommands.cs" t="Include" />
<e p="IDroneConstants.cs" t="Include" />
<e p="IDroneFactory.cs" t="Include" /> <e p="IDroneFactory.cs" t="Include" />
<e p="IMissions.cs" t="Include" /> <e p="IMissions.cs" t="Include" />
<e p="IModule.cs" t="Include" /> <e p="IModule.cs" t="Include" />

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Runtime.CompilerServices;
namespace Tello_Drone namespace Tello_Drone
{ {

View File

@@ -1,5 +1,3 @@
using System.Runtime.CompilerServices;
namespace Tello_Drone namespace Tello_Drone
{ {
public class Drone public class Drone

View File

@@ -1,7 +1,4 @@
using System;
using System.Net;
using System.Reflection.Metadata.Ecma335;
using System.Xml.Schema;
namespace Tello_Drone namespace Tello_Drone
{ {
@@ -15,7 +12,7 @@ namespace Tello_Drone
_udpClient = udpClient; _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 Reverse(int x) => SendCommand($"back {x}");
public void Up(int z) => SendCommand($"up {z}"); public void Up(int z) => SendCommand($"up {z}");
public void Down(int z) => SendCommand($"down {z}"); public void Down(int z) => SendCommand($"down {z}");
@@ -34,14 +31,14 @@ namespace Tello_Drone
private void SendCommand(string message) private void SendCommand(string message)
{ {
var returnValue = _udpClient.TrySend(message, 0_500); var returnValue = _udpClient.TrySend(message, 0_500, 3);
if (returnValue == false) if (returnValue == false)
SendCommand("land"); SendCommand("land");
} }
private bool TrySendCommand(string message) private bool TrySendCommand(string message)
{ {
return _udpClient.TrySend(message, 0_500); return _udpClient.TrySend(message, 0_500, 3);
} }
} }

View File

@@ -1,8 +0,0 @@
namespace Tello_Drone
{
public class DroneConstants : IDroneConstants
{
public string DroneIPAddress { get; set; }
public int DronePortNumber { get; set; }
}
}

View File

@@ -1,11 +1,7 @@
using System;
namespace Tello_Drone namespace Tello_Drone
{ {
public interface IConsoleLogger public interface IConsoleLogger
{ {
event Action<string> LogReceived;
void Log(string message); void Log(string message);
} }

View File

@@ -1,10 +0,0 @@
using DryIoc;
namespace Tello_Drone
{
public interface IDroneConstants
{
string DroneIPAddress { get; set; }
int DronePortNumber { get; set; }
}
}

View File

@@ -1,5 +1,3 @@
using System.Dynamic;
namespace Tello_Drone namespace Tello_Drone
{ {
public interface IDroneFactory public interface IDroneFactory

View File

@@ -1,10 +1,9 @@
using System; using System;
using System.Net.Sockets;
namespace Tello_Drone namespace Tello_Drone
{ {
public interface IUdpClientWrapper : IDisposable public interface IUdpClientWrapper : IDisposable
{ {
bool TrySend(string message, int timeOut); bool TrySend(string message, int timeOut, int maxRetries);
} }
} }

View File

@@ -1,37 +1,39 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using DryIoc;
namespace Tello_Drone namespace Tello_Drone
{ {
public class Missions : IMissions public class Missions : IMissions
{ {
private readonly IConsoleLogger _consoleLogger;
private Drone _drone; 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; _drone = droneFactory.CreateDrone;
} }
public void RunMission1() public void RunMission1()
{ {
MissionSetup(); MissionSetup();
_drone.Down(3); _drone.Up(20);
_drone.Up(3); _drone.Down(20);
MissionTeardown();
} }
private void MissionSetup() private void MissionSetup()
{ {
var inCommandMode = false; var inCommandMode = false;
while (inCommandMode != true && setupRetry < 3) while (inCommandMode != true && _setupRetry < 3)
{ {
inCommandMode = _drone.Command(); inCommandMode = _drone.Command();
setupRetry++; _setupRetry++;
_consoleLogger.Log(inCommandMode.ToString());
} }
if (!_drone.InitialTakeOff())
_drone.InitialTakeOff();
}
private void MissionTeardown()
{
_drone.Land();
} }
} }

View File

@@ -1,6 +1,4 @@
using System; namespace Tello_Drone
namespace Tello_Drone
{ {
class Program class Program
{ {
@@ -9,9 +7,6 @@ namespace Tello_Drone
var bootstrapper = BootStrapper.BootstrapSystem(new CoreModule()); var bootstrapper = BootStrapper.BootstrapSystem(new CoreModule());
var missions = bootstrapper.Resolve<IMissions>(); var missions = bootstrapper.Resolve<IMissions>();
var consoleLogger = bootstrapper.Resolve<IConsoleLogger>(); var consoleLogger = bootstrapper.Resolve<IConsoleLogger>();
var droneConstants = new DroneConstants();
droneConstants.DronePortNumber = 1;
droneConstants.DroneIPAddress = "d";
var consoleClient = new ConsoleClient(missions, consoleLogger); var consoleClient = new ConsoleClient(missions, consoleLogger);
consoleClient.Run(); consoleClient.Run();
} }

View File

@@ -1,39 +1,36 @@
using System; using System;
using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Xml.Schema;
namespace Tello_Drone namespace Tello_Drone
{ {
internal class UdpClientWrapper : IUdpClientWrapper internal class UdpClientWrapper : IUdpClientWrapper
{ {
private readonly UdpClient _client; private readonly UdpClient _client;
private int retryCount = 0;
private IPEndPoint _sendIpEndPoint; private IPEndPoint _sendIpEndPoint;
private IDroneConstants _droneConstants;
private IConsoleLogger _consoleLogger; private IConsoleLogger _consoleLogger;
public UdpClientWrapper(IConsoleLogger consoleLogger) public UdpClientWrapper(IConsoleLogger consoleLogger)
{ {
_consoleLogger = consoleLogger; _consoleLogger = consoleLogger;
_droneConstants = new DroneConstants();
consoleLogger.Log("Enter Drone IPAddress:"); consoleLogger.Log("Enter Drone IPAddress:");
_droneConstants.DroneIPAddress = Console.ReadLine(); var droneIpAddress = Console.ReadLine();
consoleLogger.Log("Enter Drone Port Number:"); consoleLogger.Log("Enter Drone Port Number:");
_droneConstants.DronePortNumber = Convert.ToInt32(Console.ReadLine()); var dronePortNumber = Convert.ToInt32(Console.ReadLine());
_client = new UdpClient(_droneConstants.DroneIPAddress, _droneConstants.DronePortNumber); if (droneIpAddress != null)
_sendIpEndPoint = new IPEndPoint(IPAddress.Parse(_droneConstants.DroneIPAddress), _droneConstants.DronePortNumber);
}
public bool TrySend(string message, int timeout)
{ {
_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, int maxRetries)
{
_consoleLogger.Log($"Sending command to drone: {message}");
bool successfullFlag = false; bool successfullFlag = false;
while(retryCount < 3 && successfullFlag == false) while(maxRetries >= 0 && successfullFlag == false)
{ {
_client.Client.ReceiveTimeout = timeout; _client.Client.ReceiveTimeout = timeout;
_client.Send(Encoding.ASCII.GetBytes(message), message.Length); _client.Send(Encoding.ASCII.GetBytes(message), message.Length);
@@ -42,25 +39,24 @@ namespace Tello_Drone
{ {
var bytes = _client.Receive(ref _sendIpEndPoint); var bytes = _client.Receive(ref _sendIpEndPoint);
var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length); var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
_consoleLogger.Log($"Drone responded with: {returnMessage}");
if (returnMessage == "ok") if (returnMessage == "ok")
successfullFlag = true; successfullFlag = true;
_consoleLogger.Log($"successfull flag = {successfullFlag}");
} }
catch 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; return successfullFlag;
} }
public void Dispose() public void Dispose()
{ {
_client?.Dispose(); _client?.Dispose();
} }
} }
} }