|
|
@@ -1,3 +1,9 @@
|
|
|
+//
|
|
|
+// GarminWatchState.swift
|
|
|
+// Trio
|
|
|
+//
|
|
|
+// Created by Cengiz Deniz on 25.01.25.
|
|
|
+//
|
|
|
import Foundation
|
|
|
import SwiftUI
|
|
|
|
|
|
@@ -9,38 +15,16 @@ import SwiftUI
|
|
|
struct GarminWatchState: Hashable, Equatable, Sendable, Encodable {
|
|
|
/// Timestamp of the glucose reading in milliseconds since Unix epoch
|
|
|
var date: UInt64?
|
|
|
-
|
|
|
- /// Sensor glucose value in raw mg/dL (no unit conversion applied)
|
|
|
var sgv: Int16?
|
|
|
-
|
|
|
- /// Change in glucose since previous reading as an integer
|
|
|
var delta: Int16?
|
|
|
-
|
|
|
- /// Glucose trend direction (e.g., "Flat", "FortyFiveUp", "SingleUp")
|
|
|
var direction: String?
|
|
|
-
|
|
|
- /// Signal noise level (optional, typically not used)
|
|
|
var noise: Double?
|
|
|
-
|
|
|
- /// Unit hint for the watchface ("mgdl" or "mmol")
|
|
|
var units_hint: String?
|
|
|
-
|
|
|
- /// Insulin on board as a decimal value (only in first array entry)
|
|
|
var iob: Double?
|
|
|
-
|
|
|
- /// Current temp basal rate in U/hr (only in first array entry)
|
|
|
var tbr: Double?
|
|
|
-
|
|
|
- /// Carbs on board as a decimal value (only in first array entry)
|
|
|
var cob: Double?
|
|
|
-
|
|
|
- /// Predicted eventual blood glucose (excluded if data type 2 is set to TBR)
|
|
|
var eventualBG: Int16?
|
|
|
-
|
|
|
- /// Current insulin sensitivity factor as an integer (only in first array entry)
|
|
|
var isf: Int16?
|
|
|
-
|
|
|
- /// AutoISF sensitivity ratio (included only if data type 1 is set to sensRatio)
|
|
|
var sensRatio: Double?
|
|
|
|
|
|
// MARK: - Display Configuration Fields
|
|
|
@@ -105,6 +89,7 @@ struct GarminWatchState: Hashable, Equatable, Sendable, Encodable {
|
|
|
}
|
|
|
|
|
|
/// Custom encoding that excludes nil values from the JSON output
|
|
|
+ /// Double values are rounded to 2 decimal places to prevent floating point artifacts
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
try container.encodeIfPresent(date, forKey: .date)
|
|
|
@@ -113,12 +98,13 @@ struct GarminWatchState: Hashable, Equatable, Sendable, Encodable {
|
|
|
try container.encodeIfPresent(direction, forKey: .direction)
|
|
|
try container.encodeIfPresent(noise, forKey: .noise)
|
|
|
try container.encodeIfPresent(units_hint, forKey: .units_hint)
|
|
|
- try container.encodeIfPresent(iob, forKey: .iob)
|
|
|
- try container.encodeIfPresent(tbr, forKey: .tbr)
|
|
|
+ // Round Double values to 2 decimal places to prevent floating point artifacts like "0.5600000000000001"
|
|
|
+ try container.encodeIfPresent(iob?.roundedDouble(toPlaces: 2), forKey: .iob)
|
|
|
+ try container.encodeIfPresent(tbr?.roundedDouble(toPlaces: 2), forKey: .tbr)
|
|
|
try container.encodeIfPresent(cob, forKey: .cob)
|
|
|
try container.encodeIfPresent(eventualBG, forKey: .eventualBG)
|
|
|
try container.encodeIfPresent(isf, forKey: .isf)
|
|
|
- try container.encodeIfPresent(sensRatio, forKey: .sensRatio)
|
|
|
+ try container.encodeIfPresent(sensRatio?.roundedDouble(toPlaces: 2), forKey: .sensRatio)
|
|
|
try container.encodeIfPresent(displayPrimaryAttributeChoice, forKey: .displayPrimaryAttributeChoice)
|
|
|
try container.encodeIfPresent(displaySecondaryAttributeChoice, forKey: .displaySecondaryAttributeChoice)
|
|
|
}
|