Compare commits

...

4 Commits

4 changed files with 129 additions and 26 deletions

View File

@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="*"/>
<PackageReference Include="MongoDB.Bson" Version="*"/>
</ItemGroup>
</Project>

46
Mongo/Mongo.cs Normal file
View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using EFBTracker.Tracking;
namespace EFBTracker.Mongo
{
public class Mongo
{
public static async Task<bool> UploadSimPosition(string email, SimPosition position){
try
{
MongoClient client = new MongoClient(
Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User)
);
MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB");
MongoCollectionBase<SimPositionModel> collection = (MongoCollectionBase<SimPositionModel>)database.GetCollection<SimPositionModel>("Simdata");
SimPositionModel data = new SimPositionModel(email, position);
FilterDefinition<SimPositionModel> userFilter = Builders<SimPositionModel>.Filter.Eq(x => x.EMail, email);
UpdateDefinition<SimPositionModel> updateDefinition = Builders<SimPositionModel>.Update
.Set(x => x.LatestPosition, position)
.Set(x => x.LatestPacketUpdate, DateTime.Now);
var confirmation = await collection.UpdateOneAsync(userFilter, updateDefinition);
if (confirmation.ModifiedCount == 0)
{
collection.InsertOne(data);
}
return true;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}

27
Mongo/SimPositionModel.cs Normal file
View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFBTracker.Tracking;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using MongoDB.Driver;
namespace EFBTracker.Mongo
{
public class SimPositionModel
{
[BsonId]
public ObjectId Id { get; set; }
public string EMail { get; set; } = "";
public DateTime LatestPacketUpdate { get; set; }
public SimPosition? LatestPosition { get; set; } = null;
public SimPositionModel(string email, SimPosition position){
EMail = email;
LatestPacketUpdate = DateTime.Now;
LatestPosition = position;
}
}
}

View File

@ -2,34 +2,62 @@
using System.Net.Sockets;
using System.Text;
using EFBTracker.Tracking;
using EFBTracker.Mongo;
bool done = false;
int listenPort = 49003;
using(UdpClient listener = new UdpClient(listenPort))
string email;
while (true)
{
IPEndPoint listenEndPoint = new IPEndPoint(2130706433, listenPort);
while(!done)
try
{
byte[] receivedData = listener.Receive(ref listenEndPoint);
Console.Write("Please enter your AutoRouter E-Mail: ");
email = Console.ReadLine().Trim().ToLower();
if (email.Count(x => (x == '@')) == 1)
{
break;
}
Console.Clear();
Console.WriteLine("Received broadcast message from client {0}", listenEndPoint.ToString());
Console.WriteLine("Decoded data is:");
Packet[]? data = Packet.ReadPackets(receivedData);
if (data != null)
{
SimPosition position = new SimPosition(data);
Console.WriteLine($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude}");
}
Console.WriteLine("Please enter a valid email!");
}
catch (System.Exception ex)
{
}
}
int listenPort = 49003;
using(UdpClient listener = new UdpClient(listenPort))
{
try
{
IPEndPoint listenEndPoint = new IPEndPoint(2130706433, listenPort);
while(true)
{
Console.WriteLine("Awaiting Simulator conenction...");
byte[] receivedData = listener.Receive(ref listenEndPoint);
Console.Clear();
Console.Write($"Currently logged in as {email} \n \n");
Console.Write("Receiving broadcast message from {0} \n", listenEndPoint.ToString());
Console.Write("Decoded data is: \n");
Packet[]? data = Packet.ReadPackets(receivedData);
if (data != null)
{
SimPosition position = new SimPosition(data);
await Mongo.UploadSimPosition(email, position);
Console.Write($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude} \n");
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}