using SQLite;
namespace FieldLogger.Models;
///
/// One logged locate point: the full UM receiver PBDL packet joined with the
/// GNSS fix that was current when the operator pressed the log button.
///
[Table("points")]
public sealed class LoggedPoint
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public int JobId { get; set; }
public DateTime TimestampUtc { get; set; } = DateTime.UtcNow;
// ---- UM locating receiver (PBDL schema 1) ----
public int Mode { get; set; }
public int Frequency { get; set; }
public int FreqType { get; set; }
public int Signal { get; set; }
public int GainDb { get; set; }
public string DepthRaw { get; set; } = "";
public double? DepthMeters { get; set; }
public string CurrentRaw { get; set; } = "";
public double? CurrentMilliamps { get; set; }
public int CompassAngle { get; set; }
public int GuidanceArrows { get; set; }
public int LdPhase { get; set; }
public bool Clipping { get; set; }
public int DepthCurrentSetting { get; set; }
public int LrArrowStyle { get; set; }
public int AudioVolume { get; set; }
public int AudioModulation { get; set; }
public int AudioSound { get; set; }
public double LocatorBatteryVoltage { get; set; }
public int LocatorBatteryType { get; set; }
public int Backlight { get; set; }
public int Utility { get; set; }
public bool MenuInUse { get; set; }
public string LocatorRawPacket { get; set; } = "";
public string LocatorSerialNumber { get; set; } = "";
public string LocatorModel { get; set; } = "";
// ---- GNSS fix (Maglink $GNPOS) ----
/// False when no fresh GNSS fix was available at log time; position fields are then unset.
public bool GpsValid { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public double Altitude { get; set; }
public double AltitudeCorrected { get; set; }
public int FixStatus { get; set; }
public double Hdop { get; set; }
public double Hrms { get; set; }
public double Vrms { get; set; }
public int SatellitesUsed { get; set; }
public int SatellitesVisible { get; set; }
public double SpeedKmh { get; set; }
public double HeadingDegrees { get; set; }
public bool NtripConnected { get; set; }
public double CorrectionAgeSeconds { get; set; }
public long GpsUnixTimestamp { get; set; }
public double TiltAngle { get; set; }
public double GpsBatteryVoltage { get; set; }
public int GpsBatteryPercent { get; set; }
public string GpsRawSentence { get; set; } = "";
public string GpsSerialNumber { get; set; } = "";
/// Stable UUIDv7 used for MQTT retries and cloud idempotency.
[Indexed(Unique = true)]
public string? SyncPointId { get; set; }
/// Machine-readable terminal/configuration error; null while pending or after success.
public string? SyncError { get; set; }
public bool Synced { get; set; }
[Ignore]
public GnssFixStatus FixStatusEnum => (GnssFixStatus)FixStatus;
[Ignore]
public UmUtility UtilityEnum => (UmUtility)Utility;
public static LoggedPoint From(int jobId, UmLogPacket packet, UmDeviceInfo? locatorInfo,
GnssFix? fix, GnssDeviceInfo? gpsInfo)
{
var p = new LoggedPoint
{
JobId = jobId,
TimestampUtc = DateTime.UtcNow,
Mode = (int)packet.Mode,
Frequency = packet.Frequency,
FreqType = (int)packet.FreqType,
Signal = packet.Signal,
GainDb = packet.GainDb,
DepthRaw = packet.DepthRaw,
DepthMeters = packet.DepthMeters,
CurrentRaw = packet.CurrentRaw,
CurrentMilliamps = packet.CurrentMilliamps,
CompassAngle = packet.CompassAngle,
GuidanceArrows = packet.GuidanceArrows,
LdPhase = packet.LdPhase,
Clipping = packet.Clipping,
DepthCurrentSetting = packet.DepthCurrentSetting,
LrArrowStyle = packet.LrArrowStyle,
AudioVolume = packet.AudioVolume,
AudioModulation = packet.AudioModulation,
AudioSound = packet.AudioSound,
LocatorBatteryVoltage = packet.BatteryVoltage,
LocatorBatteryType = (int)packet.BatteryType,
Backlight = packet.Backlight,
Utility = (int)packet.Utility,
MenuInUse = packet.MenuInUse,
LocatorRawPacket = packet.RawText,
LocatorSerialNumber = locatorInfo?.SerialNumber ?? "",
LocatorModel = locatorInfo?.ModelName ?? "",
GpsSerialNumber = gpsInfo?.SerialNumber ?? "",
};
if (fix is not null)
{
p.GpsValid = fix.HasPosition;
p.Latitude = fix.Latitude;
p.Longitude = fix.Longitude;
p.Altitude = fix.Altitude;
p.AltitudeCorrected = fix.AltitudeCorrected;
p.FixStatus = (int)fix.Status;
p.Hdop = fix.Hdop;
p.Hrms = fix.Hrms;
p.Vrms = fix.Vrms;
p.SatellitesUsed = fix.SatellitesUsed;
p.SatellitesVisible = fix.SatellitesVisible;
p.SpeedKmh = fix.SpeedKmh;
p.HeadingDegrees = fix.Heading;
p.NtripConnected = fix.NtripConnected;
p.CorrectionAgeSeconds = fix.CorrectionAgeSeconds;
p.GpsUnixTimestamp = fix.UnixTimestamp;
p.TiltAngle = fix.TiltAngle;
p.GpsBatteryVoltage = fix.BatteryVoltage;
p.GpsBatteryPercent = fix.BatteryPercent;
p.GpsRawSentence = fix.RawSentence;
}
return p;
}
}