|
@@ -60,28 +60,43 @@ namespace PRSServer
|
|
|
|
|
|
public int GetNumberOfItems()
|
|
|
{
|
|
|
+ using var profiler = new Profiler(true);
|
|
|
return Directory.EnumerateFiles(QueuePath).Count();
|
|
|
}
|
|
|
|
|
|
- public IEnumerable<GPSDeviceUpdate> GetFirstItems()
|
|
|
+ /// <summary>
|
|
|
+ /// Get the first (earliest) items of the directory.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>A list of (filename, update) tuples.</returns>
|
|
|
+ public IEnumerable<Tuple<string, GPSDeviceUpdate>> GetFirstItems()
|
|
|
{
|
|
|
+ using var profiler = new Profiler(true);
|
|
|
var files = Directory.EnumerateFiles(QueuePath).OrderBy(x => x);
|
|
|
|
|
|
foreach (var filename in files)
|
|
|
{
|
|
|
- GPSDeviceUpdate deviceUpdate;
|
|
|
- using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
|
|
|
+ GPSDeviceUpdate? deviceUpdate = null;
|
|
|
+ try
|
|
|
{
|
|
|
+ using var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
|
|
deviceUpdate = Serialization.ReadBinary<GPSDeviceUpdate>(fileStream, BinarySerializationSettings.Latest);
|
|
|
}
|
|
|
- yield return deviceUpdate;
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ // File is probably in use.
|
|
|
+ }
|
|
|
+ if(deviceUpdate is not null)
|
|
|
+ {
|
|
|
+ yield return new Tuple<string, GPSDeviceUpdate>(filename, deviceUpdate);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void QueueUpdate(GPSDeviceUpdate deviceUpdate)
|
|
|
{
|
|
|
- var filename = $"{DateTime.UtcNow.Ticks} - {deviceUpdate.AuditTrail}: {deviceUpdate.Location.ID}";
|
|
|
- using var fileStream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
|
|
|
+ var filename = Path.Combine(QueuePath, $"{DateTime.UtcNow.Ticks} - {deviceUpdate.Location.Tracker.ID}");
|
|
|
+
|
|
|
+ using var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
|
|
|
Serialization.WriteBinary(deviceUpdate, fileStream, BinarySerializationSettings.Latest);
|
|
|
}
|
|
|
|
|
@@ -154,7 +169,7 @@ namespace PRSServer
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- sigfoxListener = new Listener<SigfoxHandler, SigfoxHandlerProperties>(new SigfoxHandlerProperties(DeviceCache));
|
|
|
+ sigfoxListener = new Listener<SigfoxHandler, SigfoxHandlerProperties>(new SigfoxHandlerProperties(DeviceCache, UpdateQueue));
|
|
|
sigfoxListener.InitPort((ushort)Properties.SigfoxListenPort);
|
|
|
|
|
|
Logger.Send(LogType.Information, "", "Starting Sigfox Listener on port " + Properties.SigfoxListenPort);
|
|
@@ -171,14 +186,15 @@ namespace PRSServer
|
|
|
UpdateServerTimer.Start();
|
|
|
}
|
|
|
|
|
|
- private Queue<GPSDeviceUpdate> LocationQueueCache = new();
|
|
|
+ // List of (filename, update)
|
|
|
+ private Queue<Tuple<string, GPSDeviceUpdate>> LocationQueueCache = new();
|
|
|
|
|
|
private void GetLocationQueue(int nLocations)
|
|
|
{
|
|
|
LocationQueueCache.EnsureCapacity(LocationQueueCache.Count + nLocations);
|
|
|
- foreach(var deviceUpdate in UpdateQueue.GetFirstItems().Take(50))
|
|
|
+ foreach(var item in UpdateQueue.GetFirstItems().Take(nLocations))
|
|
|
{
|
|
|
- LocationQueueCache.Enqueue(deviceUpdate);
|
|
|
+ LocationQueueCache.Enqueue(item);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -192,17 +208,26 @@ namespace PRSServer
|
|
|
|
|
|
if (LocationQueueCache.Count > 0)
|
|
|
{
|
|
|
- var last = LocationQueueCache.Dequeue();
|
|
|
+ var (filename, update) = LocationQueueCache.Dequeue();
|
|
|
|
|
|
Logger.Send(LogType.Information, "",
|
|
|
- string.Format("Updating Server ({0}): {1} - {2}", UpdateQueue.GetNumberOfItems(), last.Location.DeviceID, last.AuditTrail));
|
|
|
- new Client<GPSTrackerLocation>().Save(last.Location, last.AuditTrail, (_, exception) =>
|
|
|
+ string.Format("Updating Server ({0}): {1} - {2}", UpdateQueue.GetNumberOfItems(), update.Location.DeviceID, update.AuditTrail));
|
|
|
+ new Client<GPSTrackerLocation>().Save(update.Location, update.AuditTrail, (_, exception) =>
|
|
|
{
|
|
|
if (exception is not null)
|
|
|
{
|
|
|
- Logger.Send(LogType.Error, "", $"Error saving GPS Tracker Location ({last.AuditTrail}): {CoreUtils.FormatException(exception)}");
|
|
|
+ Logger.Send(LogType.Error, "", $"Error saving GPS Tracker Location ({update.AuditTrail}): {CoreUtils.FormatException(exception)}");
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ File.Delete(filename);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ // Probably got deleted.
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|