Need to work on Getting the Missions running, There is currently an issue with the UDP Client sending to a bad IP

This commit is contained in:
2019-09-02 23:13:05 -06:00
commit 8c9bebb783
28 changed files with 453 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using System;
using DryIoc;
namespace Tello_Drone
{
public class BootStrapper
{
private static readonly string NoBootstrapperMessage = $"Called {nameof(BootStrapper)} before it existed";
private static BootStrapper _bootStrapper;
private static IContainer _container;
public static BootStrapper Instance =>
_bootStrapper ?? throw new InvalidOperationException(NoBootstrapperMessage);
private BootStrapper(params IModule[] modules)
{
_container = new Container();
foreach (var module in modules)
{
module.Register(_container);
}
foreach (var module in modules)
{
module.Resolve(_container);
}
}
public static BootStrapper BootstrapSystem(params IModule[] modules) =>
_bootStrapper = new BootStrapper(modules);
public T Resolve<T>() => _container.Resolve<T>();
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Runtime.CompilerServices;
namespace Tello_Drone
{
public class ConsoleClient
{
private readonly IMissions _missions;
private readonly IConsoleLogger _consoleLogger;
public ConsoleClient(IMissions missions, IConsoleLogger consoleLogger)
{
_missions = missions;
_consoleLogger = consoleLogger;
}
public void Run()
{
_consoleLogger.Log("Choose a mission to run.");
_consoleLogger.Log("1: Mission 1");
_consoleLogger.Log("2: Mission 2");
_consoleLogger.Log("3: Mission 3");
var userInput = Console.ReadLine();
switch (userInput)
{
case "1":
_missions.RunMission1();
break;
default:
_consoleLogger.Log("Not A Valid Entry...Try Again");
Run();
break;
}
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Tello_Drone
{
public class ConsoleLogger : IConsoleLogger
{
public event Action<string> LogReceived;
public void Log(string message)
{
LogReceived?.Invoke(message);
Console.WriteLine(message);
}
}
}

21
Tello Drone/CoreModule.cs Normal file
View File

@@ -0,0 +1,21 @@
using DryIoc;
namespace Tello_Drone
{
public class CoreModule : IModule
{
public void Register(IContainer container)
{
container.Register<IDroneFactory, DroneFactory>(Reuse.Singleton);
container.Register<IDroneCommands, DroneComands>(Reuse.Singleton);
container.Register<IDrone, Drone>(Reuse.Singleton);
container.Register<IMissions, Missions>(Reuse.Singleton);
container.Register<IConsoleLogger, ConsoleLogger>(Reuse.Singleton);
}
public void Resolve(IContainer container)
{
}
}
}

21
Tello Drone/Drone.cs Normal file
View File

@@ -0,0 +1,21 @@
using System.Runtime.CompilerServices;
namespace Tello_Drone
{
public class Drone : IDrone
{
private readonly IDroneCommands _droneCommands;
public Drone(IDroneCommands droneCommands)
{
_droneCommands = droneCommands;
}
public bool Forward() => _droneCommands.Forward();
public bool Reverse() => _droneCommands.Reverse();
public bool Up() => _droneCommands.Up();
public bool Down() => _droneCommands.Down();
}
}

View File

@@ -0,0 +1,28 @@
using System;
namespace Tello_Drone
{
public class DroneComands : IDroneCommands
{
private readonly IUdpClientWrapper _udpClient;
public DroneComands()
{
var udpClient = new UdpClientWrapper(127001, 8000);
_udpClient = udpClient;
}
public bool Forward() => SendCommand("Forward");
public bool Reverse() => SendCommand("Reverse");
public bool Up() => SendCommand("Up");
public bool Down() => SendCommand("Down");
private bool SendCommand(string message)
{
return _udpClient.TrySend(message, 1_500);
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Tello_Drone
{
public class DroneFactory : IDroneFactory
{
private readonly IDroneCommands _droneCommands;
private Drone _createDrone;
public DroneFactory(IDroneCommands droneCommands)
{
_droneCommands = droneCommands;
}
public Drone CreateDrone => new Drone(_droneCommands);
}
}

View File

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

8
Tello Drone/IDrone.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace Tello_Drone
{
public interface IDrone
{
}
}

View File

@@ -0,0 +1,10 @@
namespace Tello_Drone
{
public interface IDroneCommands
{
bool Forward();
bool Reverse();
bool Up();
bool Down();
}
}

View File

@@ -0,0 +1,10 @@
using System.Dynamic;
namespace Tello_Drone
{
public interface IDroneFactory
{
Drone CreateDrone { get; }
}
}

7
Tello Drone/IMissions.cs Normal file
View File

@@ -0,0 +1,7 @@
namespace Tello_Drone
{
public interface IMissions
{
void RunMission1();
}
}

10
Tello Drone/IModule.cs Normal file
View File

@@ -0,0 +1,10 @@
using DryIoc;
namespace Tello_Drone
{
public interface IModule
{
void Register(IContainer container);
void Resolve(IContainer container);
}
}

View File

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

26
Tello Drone/Missions.cs Normal file
View File

@@ -0,0 +1,26 @@
using System.Runtime.CompilerServices;
namespace Tello_Drone
{
public class Missions : IMissions
{
private readonly IConsoleLogger _consoleLogger;
private Drone _drone;
public Missions( IDroneFactory droneFactory, IConsoleLogger consoleLogger)
{
_consoleLogger = consoleLogger;
_drone = droneFactory.CreateDrone;
}
public void RunMission1()
{
}
private void MissionSetup()
{
}
}
}

17
Tello Drone/Program.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
namespace Tello_Drone
{
class Program
{
static void Main(string[] args)
{
var bootstrapper = BootStrapper.BootstrapSystem(new CoreModule());
var missions = bootstrapper.Resolve<IMissions>();
var consoleLogger = bootstrapper.Resolve<IConsoleLogger>();
var consoleClient = new ConsoleClient(missions, consoleLogger);
consoleClient.Run();
}
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>Tello_Drone</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DryIoc" Version="4.0.6" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,48 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Tello_Drone
{
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;
public UdpClientWrapper(long ipAddress, int port)
{
_ipEndPoint = new IPEndPoint(ipAddress, port);
_client = new UdpClient(LocalHost, _port);
}
public bool TrySend(string message, int timeOut)
{
if(retryCount < 3 )
{
_client.Send(Encoding.UTF8.GetBytes(message), 1, _ipEndPoint.Address.ToString(), _ipEndPoint.Port);
var timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds <= timeOut)
{
var bytes = _client.Receive(ref _ipEndPoint);
var returnMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
if (returnMessage == "Ok") return true;
}
}
return false;
}
public void Dispose()
{
_client?.Dispose();
}
}
}