Ivan Valkou 5 лет назад
Родитель
Сommit
1b79de5dfd

+ 1 - 0
FreeAPS/Sources/APS/BaseAPSManager.swift

@@ -37,6 +37,7 @@ final class BaseAPSManager: APSManager, Injectable {
     }
 
     func makeMeal() {
+        openAPS.makeClock()
         openAPS.makeMeal()
     }
 

+ 7 - 1
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -104,12 +104,18 @@ final class OpenAPS {
         }
     }
 
+    func makeClock() {
+        processQueue.async {
+            try? self.storage.save(Date(), as: Monitor.clock)
+        }
+    }
+
     func makeMeal() {
         processQueue.async {
             let pumphistory = self.loadFileFromStorage(name: Monitor.pumpHistory)
             let profile = self.loadFileFromStorage(name: Settings.profile)
             let basalProfile = self.loadFileFromStorage(name: Settings.basalProfile)
-            let clock = Date().rawJSON
+            let clock = self.loadFileFromStorage(name: Monitor.clock)
             let carbs = self.loadFileFromStorage(name: Monitor.carbHistory)
             let glucose = self.loadFileFromStorage(name: Monitor.glucose)
 

+ 15 - 4
FreeAPS/Sources/Helpers/JSON.swift

@@ -39,10 +39,6 @@ extension Bool: JSON {}
 extension Decimal: JSON {}
 
 extension Date: JSON {
-    var rawJSON: String {
-        Formatter.iso8601withFractionalSeconds.string(from: self)
-    }
-
     init?(from: String) {
         let dateFormatter = Formatter.iso8601withFractionalSeconds
         let string = from.replacingOccurrences(of: "\"", with: "")
@@ -62,3 +58,18 @@ extension RawJSON {
 
 extension Array: JSON where Element: JSON {}
 extension Dictionary: JSON where Key: JSON, Value: JSON {}
+
+enum JSONCoding {
+    static var encoder: JSONEncoder {
+        let encoder = JSONEncoder()
+        encoder.outputFormatting = .prettyPrinted
+        encoder.dateEncodingStrategy = .customISO8601
+        return encoder
+    }
+
+    static var decoder: JSONDecoder {
+        let decoder = JSONDecoder()
+        decoder.dateDecodingStrategy = .customISO8601
+        return decoder
+    }
+}

+ 4 - 3
FreeAPS/Sources/Models/BloodGlucose.swift

@@ -18,11 +18,12 @@ struct BloodGlucose: JSON {
 
     var sgv: Int?
     let direction: Direction?
-    let date: Date
+    let date: UInt64
+    let dateString: Date
     let filtered: Double?
     let noise: Int?
 
-    var glucose: Int { sgv ?? 0 }
+    var glucose: Int?
 
-    var isStateValid: Bool { glucose >= 39 && noise ?? 1 != 4 }
+    var isStateValid: Bool { sgv ?? 0 >= 39 && noise ?? 1 != 4 }
 }

+ 10 - 9
FreeAPS/Sources/Services/Network/NightscoutAPI.swift

@@ -9,7 +9,7 @@ class NightscoutAPI {
     }
 
     private enum Config {
-        static let entriesPath = "/api/v1/entries.json"
+        static let entriesPath = "/api/v1/entries/sgv.json"
         static let retryCount = 5
     }
 
@@ -22,12 +22,6 @@ class NightscoutAPI {
     let secret: String?
 
     private let service = NetworkService()
-
-    private lazy var decoder: JSONDecoder = {
-        let decoder = JSONDecoder()
-        decoder.dateDecodingStrategy = .millisecondsSince1970
-        return decoder
-    }()
 }
 
 extension NightscoutAPI {
@@ -69,8 +63,15 @@ extension NightscoutAPI {
                 }
                 return output.data
             }
-            .decode(type: [BloodGlucose].self, decoder: decoder)
-            .map { $0.filter { $0.isStateValid } }
+            .decode(type: [BloodGlucose].self, decoder: JSONCoding.decoder)
+            .map {
+                $0.filter { $0.isStateValid }
+                    .map {
+                        var reading = $0
+                        reading.glucose = $0.sgv
+                        return reading
+                    }
+            }
             .eraseToAnyPublisher()
     }
 }

+ 4 - 17
FreeAPS/Sources/Services/Storage/FileStorage.swift

@@ -19,32 +19,19 @@ protocol FileStorage {
 final class BaseFileStorage: FileStorage {
     private let processQueue = DispatchQueue.markedQueue(label: "BaseFileStorage.processQueue", qos: .utility)
 
-    private var encoder: JSONEncoder {
-        let encoder = JSONEncoder()
-        encoder.outputFormatting = .prettyPrinted
-        encoder.dateEncodingStrategy = .customISO8601
-        return encoder
-    }
-
-    private var decoder: JSONDecoder {
-        let decoder = JSONDecoder()
-        decoder.dateDecodingStrategy = .customISO8601
-        return decoder
-    }
-
     func save<Value: JSON>(_ value: Value, as name: String) throws {
         try processQueue.safeSync {
             if let value = value as? RawJSON, let data = value.data(using: .utf8) {
                 try Disk.save(data, to: .documents, as: name)
             } else {
-                try Disk.save(value, to: .documents, as: name, encoder: self.encoder)
+                try Disk.save(value, to: .documents, as: name, encoder: JSONCoding.encoder)
             }
         }
     }
 
     func retrieve<Value: JSON>(_ name: String, as type: Value.Type) throws -> Value {
         try processQueue.safeSync {
-            try Disk.retrieve(name, from: .documents, as: type, decoder: decoder)
+            try Disk.retrieve(name, from: .documents, as: type, decoder: JSONCoding.decoder)
         }
     }
 
@@ -59,13 +46,13 @@ final class BaseFileStorage: FileStorage {
 
     func append<Value: JSON>(_ newValue: Value, to name: String) throws {
         try processQueue.safeSync {
-            try Disk.append(newValue, to: name, in: .documents, decoder: decoder, encoder: encoder)
+            try Disk.append(newValue, to: name, in: .documents, decoder: JSONCoding.decoder, encoder: JSONCoding.encoder)
         }
     }
 
     func append<Value: JSON>(_ newValues: [Value], to name: String) throws {
         try processQueue.safeSync {
-            try Disk.append(newValues, to: name, in: .documents, decoder: decoder, encoder: encoder)
+            try Disk.append(newValues, to: name, in: .documents, decoder: JSONCoding.decoder, encoder: JSONCoding.encoder)
         }
     }