Jelajahi Sumber

add CIAGE in nightscout (trixing patch)

avouspierre 3 tahun lalu
induk
melakukan
efb441b44c

+ 8 - 0
FreeAPS/Sources/APS/DeviceDataManager.swift

@@ -349,6 +349,10 @@ extension BaseDeviceDataManager: PumpManagerDelegate {
                 return
             }
             pumpExpiresAtDate.send(endTime)
+
+            if let startTime = omnipod.state.podState?.activatedAt {
+                storage.save(startTime, as: OpenAPS.Monitor.podAge)
+            }
         }
 
         if let omnipodBLE = pumpManager as? OmniBLEPumpManager {
@@ -378,6 +382,10 @@ extension BaseDeviceDataManager: PumpManagerDelegate {
                 return
             }
             pumpExpiresAtDate.send(endTime)
+
+            if let startTime = omnipodBLE.state.podState?.activatedAt {
+                storage.save(startTime, as: OpenAPS.Monitor.podAge)
+            }
         }
     }
 

+ 2 - 0
FreeAPS/Sources/APS/OpenAPS/Constants.swift

@@ -52,6 +52,7 @@ extension OpenAPS {
         static let meal = "monitor/meal.json"
         static let glucose = "monitor/glucose.json"
         static let iob = "monitor/iob.json"
+        static let podAge = "monitor/pod-age.json"
     }
 
     enum Enact {
@@ -78,6 +79,7 @@ extension OpenAPS {
         static let uploadedTempTargets = "upload/uploaded-temptargets.json"
         static let uploadedGlucose = "upload/uploaded-glucose.json"
         static let uploadedProfile = "upload/uploaded-profile.json"
+        static let uploadedPodAge = "upload/uploaded-pod-age.json"
     }
 
     enum FreeAPS {

+ 67 - 1
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -136,6 +136,15 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                             carbInput: nil
                         )
                     ]
+                case .alarm:
+                    return [
+                        PumpHistoryEvent(
+                            id: id,
+                            type: .pumpAlarm,
+                            timestamp: event.date,
+                            note: event.title
+                        )
+                    ]
                 default:
                     return []
                 }
@@ -260,9 +269,66 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
             }
         }
 
+        let misc = events.compactMap { event -> NigtscoutTreatment? in
+            switch event.type {
+            case .prime:
+                return NigtscoutTreatment(
+                    duration: event.duration,
+                    rawDuration: nil,
+                    rawRate: nil,
+                    absolute: nil,
+                    rate: nil,
+                    eventType: .nsSiteChange,
+                    createdAt: event.timestamp,
+                    enteredBy: NigtscoutTreatment.local,
+                    bolus: event,
+                    insulin: nil,
+                    notes: nil,
+                    carbs: nil,
+                    targetTop: nil,
+                    targetBottom: nil
+                )
+            case .rewind:
+                return NigtscoutTreatment(
+                    duration: nil,
+                    rawDuration: nil,
+                    rawRate: nil,
+                    absolute: nil,
+                    rate: nil,
+                    eventType: .nsInsulinChange,
+                    createdAt: event.timestamp,
+                    enteredBy: NigtscoutTreatment.local,
+                    bolus: nil,
+                    insulin: nil,
+                    notes: nil,
+                    carbs: nil,
+                    targetTop: nil,
+                    targetBottom: nil
+                )
+            case .pumpAlarm:
+                return NigtscoutTreatment(
+                    duration: 30, // minutes
+                    rawDuration: nil,
+                    rawRate: nil,
+                    absolute: nil,
+                    rate: nil,
+                    eventType: .nsAnnouncement,
+                    createdAt: event.timestamp,
+                    enteredBy: NigtscoutTreatment.local,
+                    bolus: nil,
+                    insulin: nil,
+                    notes: "Alarm \(String(describing: event.note)) \(event.type)",
+                    carbs: nil,
+                    targetTop: nil,
+                    targetBottom: nil
+                )
+            default: return nil
+            }
+        }
+
         let uploaded = storage.retrieve(OpenAPS.Nightscout.uploadedPumphistory, as: [NigtscoutTreatment].self) ?? []
 
-        let treatments = Array(Set([bolusesAndCarbs, temps].flatMap { $0 }).subtracting(Set(uploaded)))
+        let treatments = Array(Set([bolusesAndCarbs, temps, misc].flatMap { $0 }).subtracting(Set(uploaded)))
 
         return treatments.sorted { $0.createdAt! > $1.createdAt! }
     }

+ 33 - 0
FreeAPS/Sources/Models/PumpHistoryEvent.swift

@@ -10,6 +10,31 @@ struct PumpHistoryEvent: JSON, Equatable {
     let rate: Decimal?
     let temp: TempType?
     let carbInput: Int?
+    let note: String?
+
+    init(
+        id: String,
+        type: EventType,
+        timestamp: Date,
+        amount: Decimal? = nil,
+        duration: Int? = nil,
+        durationMin: Int? = nil,
+        rate: Decimal? = nil,
+        temp: TempType? = nil,
+        carbInput: Int? = nil,
+        note: String? = nil
+    ) {
+        self.id = id
+        self.type = type
+        self.timestamp = timestamp
+        self.amount = amount
+        self.duration = duration
+        self.durationMin = durationMin
+        self.rate = rate
+        self.temp = temp
+        self.carbInput = carbInput
+        self.note = note
+    }
 }
 
 enum EventType: String, JSON {
@@ -22,6 +47,8 @@ enum EventType: String, JSON {
     case tempBasalDuration = "TempBasalDuration"
     case pumpSuspend = "PumpSuspend"
     case pumpResume = "PumpResume"
+    case pumpAlarm = "PumpAlarm"
+    case pumpBattery = "PumpBattery"
     case rewind = "Rewind"
     case prime = "Prime"
     case journalCarbs = "JournalEntryMealMarker"
@@ -29,6 +56,11 @@ enum EventType: String, JSON {
     case nsTempBasal = "Temp Basal"
     case nsCarbCorrection = "Carb Correction"
     case nsTempTarget = "Temporary Target"
+    case nsInsulinChange = "Insulin Change"
+    case nsSiteChange = "Site Change"
+    case nsBatteryChange = "Pump Battery Change"
+    case nsAnnouncement = "Announcement"
+    case nsSensorChange = "Sensor Start"
 }
 
 enum TempType: String, JSON {
@@ -47,5 +79,6 @@ extension PumpHistoryEvent {
         case rate
         case temp
         case carbInput = "carb_input"
+        case note
     }
 }

+ 27 - 0
FreeAPS/Sources/Services/Network/NightscoutManager.swift

@@ -237,6 +237,33 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
                 } receiveValue: {}
                 .store(in: &self.lifetime)
         }
+
+        uploadPodAge()
+    }
+
+    func uploadPodAge() {
+        let uploadedPodAge = storage.retrieve(OpenAPS.Nightscout.uploadedPodAge, as: [NigtscoutTreatment].self) ?? []
+        if let podAge = storage.retrieve(OpenAPS.Monitor.podAge, as: Date.self),
+           uploadedPodAge.last?.createdAt == nil || podAge != uploadedPodAge.last!.createdAt!
+        {
+            let siteTreatment = NigtscoutTreatment(
+                duration: nil,
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: .nsSiteChange,
+                createdAt: podAge,
+                enteredBy: NigtscoutTreatment.local,
+                bolus: nil,
+                insulin: nil,
+                notes: nil,
+                carbs: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+            uploadTreatments([siteTreatment], fileToSave: OpenAPS.Nightscout.uploadedPodAge)
+        }
     }
 
     func uploadProfile() {