123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.DigitalMatter;
- using NPOI.HSSF.Record.CF;
- using PRS.Shared;
- using Syncfusion.Data;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Media.Media3D;
- namespace PRSServer
- {
- /// <summary>
- /// Represents one conversation between OEM and our GPS Engine.
- /// </summary>
- internal class OEMConnection
- {
- private TcpClient Client;
- private NetworkStream Stream;
- private byte[] Buffer;
- private List<byte> ConsolidatedBuffer;
- private bool Finished = false;
- private bool Confirmed = false;
- private Task? ReadTask;
- private CancellationTokenSource TokenSource = new();
- // References to necessary caches
- private GPSDeviceCache Devices;
- private GPSUpdateQueue Queue;
- // Fields of this conversation; these should be initialised by the 'Hello' message.
- private string Product;
- private string Serial;
- // Anything more recent than this threshold is ignored.
- private static TimeSpan AgeThreshold = TimeSpan.FromMinutes(2);
- // Data that forms the the request.
- private List<DMDataRequest> Data = new();
- public delegate void CompletedEvent();
- public CompletedEvent? OnCompletion;
- public OEMConnection(TcpClient client, GPSDeviceCache cache, GPSUpdateQueue queue)
- {
- Client = client;
- Stream = Client.GetStream();
- Devices = cache;
- Queue = queue;
- }
- private DMHelloResponse HandleHello(DMHelloRequest hello)
- {
- Product = DMFactory.GetDeviceName(hello.ProductID);
- Serial = hello.SerialNumber.ToString();
- Logger.Send(LogType.Information, Serial, string.Format("Hello {0} ({1})", Product, Serial));
- return new DMHelloResponse();
- }
- private DMMessage? HandleData(DMDataRequest data)
- {
- Logger.Send(LogType.Information, Serial, string.Format("{0} DataRecords Received", data.Records.Length));
- var iRecord = 1;
- foreach (var record in data.Records)
- {
- Logger.Send(LogType.Information, Serial,
- string.Format("- Data Record #{0}: {1:dd MMM yy hh-mm-ss} ({2} Fields)", iRecord,
- record.TimeStampToDateTime(record.TimeStamp), record.Fields.Length));
- iRecord++;
- foreach (var field in record.Fields)
- Logger.Send(LogType.Information, Serial,
- string.Format(" [{0}] {1}: {2}", field.IsValid() ? "X" : " ", DMFactory.GetFieldName(field.Type),
- field));
- }
- Data.Add(data);
- return null;
- }
- private GPSTrackerLocation? HandleTracker(DMGPSField gps, DMRecord record)
- {
- var flags = gps.StatusFlags();
- // Sometimes we get a ping that has both "Valid" and "NoSignal" set
- // In this case, lets treat it as valid
- if (flags.Contains(GPSStatus.NoSignal) && !flags.Contains(GPSStatus.Valid))
- {
- Logger.Send(LogType.Information, Serial, $"- Skipping: Invalid Signal ({Serial})");
- return null;
- }
- var timestamp = record.TimeStampToDateTime(record.TimeStamp);
- var age = timestamp - Devices[Serial].TimeStamp;
- if (age <= AgeThreshold)
- {
- Logger.Send(LogType.Information, Serial, $"- Skipping: Recent Update ({Serial}) {age:mm\\:ss}");
- return null;
- }
- var device = Devices[Serial];
- var location = new GPSTrackerLocation();
- location.DeviceID = Serial;
- location.Tracker.ID = device.ID;
- location.Location.Timestamp = timestamp;
- location.Location.Latitude = (double)gps.Latitude / 10000000.0F;
- location.Location.Longitude = (double)gps.Longitude / 10000000.0F;
- var analoguedata = record.GetFields<DMAnalogueDataField16>().FirstOrDefault();
- if (analoguedata != null)
- location.BatteryLevel = analoguedata.BatteryStrength
- ?? device.CalculateBatteryLevel(analoguedata.InternalVoltage);
- return location;
- }
- private GPSTrackerLocation? HandleBluetoothTag(DMGPSField gps, DMRecord record, DMBluetoothTag tag)
- {
- var tagID = tag.ID();
- if (!Devices.ContainsKey(tagID) && tagID.Length == 17 && tagID.Split(':').Length == 6)
- {
- var truncated = tagID[..15];
- var newtag = Devices.Keys.FirstOrDefault(x => x.StartsWith(truncated));
- Logger.Send(LogType.Information, Serial, $"- Truncating BT Tag: {tagID} -> {truncated} -> {newtag}");
- if (!string.IsNullOrWhiteSpace(newtag))
- tagID = newtag;
- }
- if (!Devices.ContainsKey(tagID))
- {
- Logger.Send(LogType.Information, Serial, $"- Skipping: Unknown Tag ({tagID})");
- return null;
- }
- var timestamp = record.TimeStampToDateTime(record.TimeStamp);
- var age = timestamp - Devices[tagID].TimeStamp;
- if (age <= AgeThreshold)
- {
- Logger.Send(LogType.Information, Serial, $"- Skipping: Recent Update ({tagID}) {age:mm\\:ss}");
- return null;
- }
- var device = Devices[tagID];
- var btloc = new GPSTrackerLocation();
- btloc.DeviceID = tagID;
- btloc.Tracker.ID = device.ID;
- btloc.Location.Timestamp = timestamp;
- btloc.Location.Latitude = (double)gps.Latitude / 10000000.0F;
- btloc.Location.Longitude = (double)gps.Longitude / 10000000.0F;
- if (tag is DMGuppyBluetoothTag guppy)
- {
- btloc.BatteryLevel = device.CalculateBatteryLevel(guppy.BatteryVoltage);
- //guppy.BatteryVoltage * 5F / 3F;
- }
- else if (tag is DMSensorNodeBluetoothTag sensornode)
- {
- // Need to check with Kenrick about the calcs here..
- // Guppies have 1 battery (ie 1.5V) while Sensornodes have 3 (4.5V)
- btloc.BatteryLevel = device.CalculateBatteryLevel(sensornode.BatteryVoltage);
- //btloc.BatteryLevel = sensornode.BatteryVoltage * 5F / 3F;
- }
- return btloc;
- }
- private IEnumerable<GPSTrackerLocation> HandleRecord(DMRecord record)
- {
- if (Devices.ContainsKey(Serial))
- {
- if (record.Fields.FirstOrDefault(x => x is DMGPSField && x.IsValid()) is DMGPSField gps)
- {
- if (record.TimeStamp != 0 && gps.Latitude != 0 && gps.Longitude != 0)
- {
- if (HandleTracker(gps, record) is GPSTrackerLocation trackerLocation)
- {
- yield return trackerLocation;
- }
- foreach (DMBluetoothTagList taglist in record.GetFields<DMBluetoothTagList>())
- foreach (var item in taglist.Items.Where(x => x.LogReason != 2))
- {
- if (HandleBluetoothTag(gps, record, item.Tag) is GPSTrackerLocation location)
- {
- yield return location;
- }
- }
- foreach (DMBluetoothTagData tag in record.GetFields<DMBluetoothTagData>())
- if (tag.LogReason != 2 && tag.TimeStamp != 0 && tag.Latitude != 0 && tag.Longitude != 0)
- {
- if (HandleBluetoothTag(gps, record, tag.Tag) is GPSTrackerLocation location)
- {
- yield return location;
- }
- }
- }
- else
- {
- Logger.Send(LogType.Information, Serial,
- string.Format("- Skipping: Invalid GPS Data ({0}) {1}{2}{3}", Serial,
- gps.TimeStamp == 0 ? "Bad TimeStamp " : "", gps.Latitude == 0 ? "Bad Latitude " : "",
- gps.Longitude == 0 ? "Bad Longitude " : "").Trim());
- }
- }
- else
- {
- Logger.Send(LogType.Information, Serial,
- string.Format("- Skipping: Missing GPS Data ({0})", Serial));
- }
- }
- else
- {
- Logger.Send(LogType.Information, Serial, string.Format("- Skipping: Unknown Device ({0})", Serial));
- }
- }
- private DMConfirmResponse HandleConfirm(DMConfirmRequest confirm)
- {
- Logger.Send(LogType.Information, Serial, string.Format("Goodbye {0} ({1})", Product, Serial));
- var updates = new List<GPSTrackerLocation>();
- foreach (var data in Data)
- foreach (var record in data.Records)
- {
- foreach(var update in HandleRecord(record))
- {
- updates.Add(update);
- }
- }
- if (updates.Any())
- {
- Logger.Send(LogType.Information, Serial,
- string.Format("Sending updates ({0}): {1}", updates.Count,
- string.Join(", ", updates.Select(x => x.DeviceID).Distinct())));
- foreach (var update in updates)
- {
- Logger.Send(LogType.Information, Serial,
- string.Format("- Updating Device Cache: ({0}): {1:yyyy-MM-dd hh:mm:ss}", update.DeviceID,
- update.Location.Timestamp));
- //if (m_devices.ContainsKey(update.DeviceID))
- var oldDevice = Devices[update.DeviceID];
- Devices[update.DeviceID] =
- new Device(oldDevice.ID, update.Location.Timestamp, oldDevice.BatteryFormula);
- }
- foreach (var update in updates)
- {
- Queue.QueueUpdate($"Updated by {Product} ({Serial})", update);
- }
- }
- return new DMConfirmResponse { Status = 1 };
- }
- private void HandleMessage(DMMessage message)
- {
- DMMessage? response;
- if (message is DMHelloRequest hello)
- {
- response = HandleHello(hello);
- }
- else if (message is DMDataRequest data)
- {
- response = HandleData(data);
- }
- else if (message is DMConfirmRequest confirm)
- {
- response = HandleConfirm(confirm);
- Confirmed = true;
- }
- else
- {
- Logger.Send(LogType.Information, "", $"Unknown message type {message.Type}");
- response = null;
- }
- if(response is not null)
- {
- Stream.Write(response.EncodeArray());
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <remarks>
- /// Read occurs asynchronously, so essentially this method spins off a read thread and then returns.
- /// If we call <see cref="Stop"/> before data comes in, the read does not occur.
- /// </remarks>
- private void DoRead()
- {
- var readData = false;
- // We pass in the cancellation token to prevent the task from continuing if we call stop.
- ReadTask = Stream.ReadAsync(Buffer, 0, Buffer.Length).ContinueWith(t =>
- {
- if(t.Exception is not null)
- {
- if (Finished)
- {
- Finish();
- }
- else
- {
- Logger.Send(LogType.Error, Environment.CurrentManagedThreadId.ToString(), CoreUtils.FormatException(t.Exception));
- }
- }
- else
- {
- readData = true;
- var nBytes = t.Result;
- if (nBytes > 0)
- {
- ConsolidatedBuffer.AddRange(Buffer.Take(nBytes));
- try
- {
- while (ConsolidatedBuffer.Count > 0)
- {
- DMMessage? message = null;
- try
- {
- var payloadLength = DMFactory.PeekPayloadLength(ConsolidatedBuffer);
- if (nBytes == Buffer.Length && ConsolidatedBuffer.Count < payloadLength)
- {
- // Probably we need more data.
- break;
- }
- else
- {
- message = DMFactory.ParseMessage(ConsolidatedBuffer);
- }
- }
- catch (Exception e)
- {
- Logger.Send(
- LogType.Error,
- Environment.CurrentManagedThreadId.ToString(),
- string.Format("Unable to Parse Record: {0} ({1})", e.Message, BitConverter.ToString(ConsolidatedBuffer.ToArray()))
- );
- break;
- }
- if (message is not null)
- {
- HandleMessage(message);
- ConsolidatedBuffer.RemoveRange(0, message.CheckSum + 5);
- }
- }
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, Environment.CurrentManagedThreadId.ToString(), e.Message + "\n" + e.StackTrace);
- }
- }
- else
- {
- Finished = true;
- }
- if (Finished)
- {
- Finish();
- }
- else
- {
- // If we still have stuff to do, try reading again. Note that this isn't a recursive problem or anything, because
- // this method returns very soon.
- DoRead();
- }
- }
- }, TokenSource.Token);
-
- // After 15 seconds, close the connection (but only if it has been confirmed).
- var delay = Task.Delay(15_000).ContinueWith(t =>
- {
- if (!readData)
- {
- Finished = true;
- if (Confirmed)
- {
- Logger.Send(LogType.Error, "", "Closing connection after 15 seconds of silence.");
- Stream.Close();
- }
- else
- {
- Logger.Send(LogType.Error, "", "No data read for 15 seconds, but connection has not been confirmed.");
- }
- }
- });
- }
- public void Run()
- {
- Buffer = new byte[2048];
- ConsolidatedBuffer = new List<byte>();
- DoRead();
- }
- private void Finish()
- {
- Stream.Close();
- Client.Close();
- OnCompletion?.Invoke();
- }
- public void Stop()
- {
- // Cancel any read tasks which are yet to start.
- TokenSource.Cancel();
- Stream.Close();
- Client.Close();
- try
- {
- // Let the latest task finish.
- if(ReadTask?.Wait(1000) == false)
- {
- Logger.Send(LogType.Error, "", "The thread didn't die in time.");
- }
- }
- catch (AggregateException ae)
- {
- foreach(var exception in ae.InnerExceptions)
- {
- if(exception is not TaskCanceledException)
- {
- throw;
- }
- }
- }
- }
- }
- }
|