Compare commits

...

8 Commits

8 changed files with 290 additions and 5 deletions

26
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/EFBTracker.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

41
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/EFBTracker.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/EFBTracker.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/EFBTracker.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

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

@ -1,2 +1,61 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System.Net;
using System.Net.Sockets;
using System.Text;
using EFBTracker.Tracking;
using EFBTracker.Mongo;
string email;
while (true)
{
try
{
Console.Write("Please enter your AutoRouter E-Mail: ");
email = Console.ReadLine().Trim().ToLower();
if (email.Count(x => (x == '@')) == 1)
{
break;
}
Console.Clear();
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)
{
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();
}
}

58
Tracking/Packet.cs Normal file
View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
namespace EFBTracker.Tracking
{
public class Packet
{
public string Type { get; set; }
public int Id { get; set; }
public float[] Data { get; set; } = new float[4];
public Packet(string type, int id, float[] data){
Type = type;
Id = id;
Data = data;
}
public static Packet[]? ReadPackets(byte[] data){
if((data.Length - 5) % 36 == 0){//If there are correctly sized packets
int packetPos = 0;
List<Packet> packets = new List<Packet>();
//Next 4 bytes are packet header
string header = Encoding.UTF8.GetString(data, packetPos, 4);
packetPos += 5;
while(packetPos < data.Length)
{
int id = BitConverter.ToInt32(data, packetPos);
packetPos += 4;
//Array to take 4 values
float[] values = new float[4];
for (var i = 0; i < values.Length; i++)
{//Read 4 values from packet and save into array
var value = BitConverter.ToSingle(data, packetPos);
values[i] = value;
packetPos += 8;
}
//Generate new packet object and add to list
packets.Add(new Packet(header, id, values));
}
return packets.ToArray();
}
return null;
}
}
}

26
Tracking/SimPosition.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFBTracker.Tracking
{
public class SimPosition
{
public float Latitude { get; set; }
public float Longitude { get; set; }
public int Altitude { get; set; }
public SimPosition(Packet[] data){
if (data[0].Data != null)
{
//Use Linq to search through the packets for a given id and use that data
Latitude = (data.Where(x => x.Id == 22).Select(x => x.Data[0]).ToArray())[0];
Longitude = (data.Where(x => x.Id == 23).Select(x => x.Data[0]).ToArray())[0];
Altitude = Convert.ToInt32((data.Where(x => x.Id == 24).Select(x => x.Data[0]).ToArray())[0]);
}
}
}
}