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="Drone.cs" t="Include" />
<e p="DroneComands.cs" t="Include" />
<e p="DroneConstants.cs" t="Include" />
<e p="DroneFactory.cs" t="Include" />
<e p="IConsoleLogger.cs" t="Include" />
<e p="IDroneCommands.cs" t="Include" />
<e p="IDroneConstants.cs" t="Include" />
<e p="IDroneFactory.cs" t="Include" />
<e p="IMissions.cs" t="Include" />
<e p="IModule.cs" t="Include" />

View File

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

View File

@@ -1,5 +1,3 @@
using System.Runtime.CompilerServices;
namespace Tello_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
{
@@ -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);
}
}

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
{
public interface IConsoleLogger
{
event Action<string> LogReceived;
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
{
public interface IDroneFactory

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<IMissions>();
var consoleLogger = bootstrapper.Resolve<IConsoleLogger>();
var droneConstants = new DroneConstants();
droneConstants.DronePortNumber = 1;
droneConstants.DroneIPAddress = "d";
var consoleClient = new ConsoleClient(missions, consoleLogger);
consoleClient.Run();
}

View File

@@ -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();
}
}
}