almost there

This commit is contained in:
2019-09-04 18:36:26 -06:00
parent 8c9bebb783
commit e66bd40e06
9 changed files with 65 additions and 22 deletions

View File

@@ -19,10 +19,12 @@
<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="IDrone.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

@@ -5,6 +5,7 @@ namespace Tello_Drone
{
public class ConsoleClient
{
private readonly IMissions _missions;
private readonly IConsoleLogger _consoleLogger;
@@ -16,7 +17,6 @@ namespace Tello_Drone
public void Run()
{
_consoleLogger.Log("Choose a mission to run.");
_consoleLogger.Log("1: Mission 1");
_consoleLogger.Log("2: Mission 2");
@@ -37,5 +37,6 @@ namespace Tello_Drone
}
}
}

View File

@@ -11,6 +11,7 @@ namespace Tello_Drone
container.Register<IDrone, Drone>(Reuse.Singleton);
container.Register<IMissions, Missions>(Reuse.Singleton);
container.Register<IConsoleLogger, ConsoleLogger>(Reuse.Singleton);
//container.Register<IUdpClientWrapper, UdpClientWrapper>(Reuse.Singleton);
}
public void Resolve(IContainer container)

View File

@@ -1,4 +1,5 @@
using System;
using System.Net;
namespace Tello_Drone
{
@@ -6,9 +7,9 @@ namespace Tello_Drone
{
private readonly IUdpClientWrapper _udpClient;
public DroneComands()
public DroneComands(IConsoleLogger consoleLogger)
{
var udpClient = new UdpClientWrapper(127001, 8000);
var udpClient = new UdpClientWrapper(consoleLogger);
_udpClient = udpClient;
}

View File

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

View File

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

View File

@@ -15,7 +15,8 @@ namespace Tello_Drone
public void RunMission1()
{
_drone.Down();
_drone.Up();
}
private void MissionSetup()

View File

@@ -9,8 +9,10 @@ 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,41 +1,58 @@
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
internal class UdpClientWrapper : IUdpClientWrapper
{
private readonly UdpClient _client;
private const string LocalHost = "127.0.0.1";
private readonly int _port = 8889;
private IPEndPoint _ipEndPoint;
private int retryCount = 0;
private IPEndPoint _ipEndPoint;
private IDroneConstants _droneConstants;
private IConsoleLogger _consoleLogger;
public UdpClientWrapper(long ipAddress, int port)
public UdpClientWrapper(IConsoleLogger consoleLogger)
{
_ipEndPoint = new IPEndPoint(ipAddress, port);
_client = new UdpClient(LocalHost, _port);
_consoleLogger = consoleLogger;
_droneConstants = new DroneConstants();
consoleLogger.Log("Enter Drone IPAddress:");
_droneConstants.DroneIPAddress = Console.ReadLine();
consoleLogger.Log("Enter Drone Port Number:");
_droneConstants.DronePortNumber = Convert.ToInt32(Console.ReadLine());
_client = new UdpClient(_droneConstants.DroneIPAddress, _droneConstants.DronePortNumber);
_ipEndPoint = new IPEndPoint(IPAddress.Parse(_droneConstants.DroneIPAddress), _droneConstants.DronePortNumber);
}
public bool TrySend(string message, int timeOut)
{
if(retryCount < 3 )
{
bool successfullFlag = false;
while(retryCount < 3 && successfullFlag == false)
{
_client.Send(Encoding.UTF8.GetBytes(message), 1, _ipEndPoint.Address.ToString(), _ipEndPoint.Port);
var timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds <= timeOut)
_client.Client.ReceiveTimeout = 1_500;
_client.Send(Encoding.UTF8.GetBytes(message), 1);
try
{
var bytes = _client.Receive(ref _ipEndPoint);
var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
if (returnMessage == "Ok") return true;
if (returnMessage == "Ok")
successfullFlag = true;
}
catch
{
_consoleLogger.Log("SHIT BROKE");
}
retryCount++;
}
return false;
return successfullFlag;
}
public void Dispose()