Added DB functionality to record user's latest sim position + alt

This commit is contained in:
Luke Else 2022-02-18 11:41:56 +00:00
parent 44478b95c7
commit a6b6d139a0
4 changed files with 76 additions and 7 deletions

View File

@ -7,5 +7,6 @@
</PropertyGroup>
<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,6 +2,7 @@
using System.Net.Sockets;
using System.Text;
using EFBTracker.Tracking;
using EFBTracker.Mongo;
bool done = false;
int listenPort = 49003;
@ -23,13 +24,7 @@ using(UdpClient listener = new UdpClient(listenPort))
{
SimPosition position = new SimPosition(data);
Console.WriteLine($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude}");
}
await Mongo.UploadSimPosition("example@example.com", position);
}
}
}