Added DB functionality to record user's latest sim position + alt
This commit is contained in:
parent
44478b95c7
commit
a6b6d139a0
@ -7,5 +7,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MongoDB.Driver" Version="*"/>
|
<PackageReference Include="MongoDB.Driver" Version="*"/>
|
||||||
|
<PackageReference Include="MongoDB.Bson" Version="*"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
46
Mongo/Mongo.cs
Normal file
46
Mongo/Mongo.cs
Normal 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
27
Mongo/SimPositionModel.cs
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using EFBTracker.Tracking;
|
using EFBTracker.Tracking;
|
||||||
|
using EFBTracker.Mongo;
|
||||||
|
|
||||||
bool done = false;
|
bool done = false;
|
||||||
int listenPort = 49003;
|
int listenPort = 49003;
|
||||||
@ -23,13 +24,7 @@ using(UdpClient listener = new UdpClient(listenPort))
|
|||||||
{
|
{
|
||||||
SimPosition position = new SimPosition(data);
|
SimPosition position = new SimPosition(data);
|
||||||
Console.WriteLine($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude}");
|
Console.WriteLine($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude}");
|
||||||
|
await Mongo.UploadSimPosition("example@example.com", position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user