|
@@ -9,7 +9,7 @@ public struct ShareGlucoseData: Decodable {
|
|
|
var sgv: Int
|
|
var sgv: Int
|
|
|
var date: TimeInterval
|
|
var date: TimeInterval
|
|
|
var direction: String?
|
|
var direction: String?
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
enum CodingKeys: String, CodingKey {
|
|
enum CodingKeys: String, CodingKey {
|
|
|
case sgv // Sensor Blood Glucose
|
|
case sgv // Sensor Blood Glucose
|
|
|
case mbg // Manual Blood Glucose
|
|
case mbg // Manual Blood Glucose
|
|
@@ -17,7 +17,7 @@ public struct ShareGlucoseData: Decodable {
|
|
|
case date
|
|
case date
|
|
|
case direction
|
|
case direction
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Decoder initializer for handling JSON data
|
|
// Decoder initializer for handling JSON data
|
|
|
public init(from decoder: Decoder) throws {
|
|
public init(from decoder: Decoder) throws {
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
@@ -31,12 +31,12 @@ public struct ShareGlucoseData: Decodable {
|
|
|
} else {
|
|
} else {
|
|
|
throw DecodingError.dataCorruptedError(forKey: .sgv, in: container, debugDescription: "Expected to decode Double for sgv, mbg or glucose.")
|
|
throw DecodingError.dataCorruptedError(forKey: .sgv, in: container, debugDescription: "Expected to decode Double for sgv, mbg or glucose.")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Decode the date and optional direction
|
|
// Decode the date and optional direction
|
|
|
date = try container.decode(TimeInterval.self, forKey: .date)
|
|
date = try container.decode(TimeInterval.self, forKey: .date)
|
|
|
direction = try container.decodeIfPresent(String.self, forKey: .direction)
|
|
direction = try container.decodeIfPresent(String.self, forKey: .direction)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
public init(sgv: Int, date: TimeInterval, direction: String?) {
|
|
public init(sgv: Int, date: TimeInterval, direction: String?) {
|
|
|
self.sgv = sgv
|
|
self.sgv = sgv
|
|
|
self.date = date
|
|
self.date = date
|
|
@@ -45,36 +45,39 @@ public struct ShareGlucoseData: Decodable {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private var TrendTable: [String] = [
|
|
private var TrendTable: [String] = [
|
|
|
- "NONE", // 0
|
|
|
|
|
- "DoubleUp", // 1
|
|
|
|
|
- "SingleUp", // 2
|
|
|
|
|
- "FortyFiveUp", // 3
|
|
|
|
|
- "Flat", // 4
|
|
|
|
|
- "FortyFiveDown", // 5
|
|
|
|
|
- "SingleDown", // 6
|
|
|
|
|
- "DoubleDown", // 7
|
|
|
|
|
- "NOT COMPUTABLE", // 8
|
|
|
|
|
- "RATE OUT OF RANGE", // 9
|
|
|
|
|
|
|
+ "NONE", // 0
|
|
|
|
|
+ "DoubleUp", // 1
|
|
|
|
|
+ "SingleUp", // 2
|
|
|
|
|
+ "FortyFiveUp", // 3
|
|
|
|
|
+ "Flat", // 4
|
|
|
|
|
+ "FortyFiveDown", // 5
|
|
|
|
|
+ "SingleDown", // 6
|
|
|
|
|
+ "DoubleDown", // 7
|
|
|
|
|
+ "NOT COMPUTABLE", // 8
|
|
|
|
|
+ "RATE OUT OF RANGE" // 9
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
-public extension ShareClient {
|
|
|
|
|
- func fetchData(_ entries: Int, callback: @escaping (ShareError?, [ShareGlucoseData]?) -> Void) {
|
|
|
|
|
- fetchLast(entries) { error, result in
|
|
|
|
|
- guard error == nil || result != nil else {
|
|
|
|
|
- return callback(error, nil)
|
|
|
|
|
|
|
+// TODO: probably better to make this an inherited class rather than an extension
|
|
|
|
|
+extension ShareClient {
|
|
|
|
|
+
|
|
|
|
|
+ public func fetchData(_ entries: Int, callback: @escaping (ShareError?, [ShareGlucoseData]?) -> Void) {
|
|
|
|
|
+
|
|
|
|
|
+ self.fetchLast(entries) { (error, result) -> () in
|
|
|
|
|
+ guard error == nil, let result = result else {
|
|
|
|
|
+ return callback(error ?? .fetchError, nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// parse data to conanical form
|
|
// parse data to conanical form
|
|
|
var shareData = [ShareGlucoseData]()
|
|
var shareData = [ShareGlucoseData]()
|
|
|
- for i in 0 ..< result!.count {
|
|
|
|
|
- var trend = Int(result![i].trend)
|
|
|
|
|
- if trend < 0 || trend > TrendTable.count - 1 {
|
|
|
|
|
|
|
+ for item in result {
|
|
|
|
|
+ var trend = Int(item.trend)
|
|
|
|
|
+ if trend < 0 || trend >= TrendTable.count {
|
|
|
trend = 0
|
|
trend = 0
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let newShareData = ShareGlucoseData(
|
|
let newShareData = ShareGlucoseData(
|
|
|
- sgv: Int(result![i].glucose),
|
|
|
|
|
- date: result![i].timestamp.timeIntervalSince1970,
|
|
|
|
|
|
|
+ sgv: Int(item.glucose),
|
|
|
|
|
+ date: item.timestamp.timeIntervalSince1970,
|
|
|
direction: TrendTable[trend]
|
|
direction: TrendTable[trend]
|
|
|
)
|
|
)
|
|
|
shareData.append(newShareData)
|
|
shareData.append(newShareData)
|