Skip to content

Commit 945ee4b

Browse files
committed
hotfix: more elegant solution for dexcom share encode Trend as string
1 parent 02bd768 commit 945ee4b

File tree

1 file changed

+11
-39
lines changed

1 file changed

+11
-39
lines changed

ShareClient/ShareClient.swift

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,6 @@ public class ShareClient {
142142
}
143143
}
144144

145-
// in Dec 2021, Dexcom changed json for Share so trend is a string rather than an int
146-
// from link below, get translation between old int and new string
147-
// https://github.com/nightscout/share2nightscout-bridge/blob/976fce4/index.js
148-
/* var DIRECTIONS = {
149-
NONE: 0
150-
, DoubleUp: 1
151-
, SingleUp: 2
152-
, FortyFiveUp: 3
153-
, Flat: 4
154-
, FortyFiveDown: 5
155-
, SingleDown: 6
156-
, DoubleDown: 7
157-
, 'NOT COMPUTABLE': 8
158-
, 'RATE OUT OF RANGE': 9
159-
};
160-
*/
161-
162145
private func fetchLastWithRetries(_ n: Int, remaining: Int, callback: @escaping (ShareError?, [ShareGlucose]?) -> Void) {
163146
ensureToken() { (error) in
164147
guard error == nil else {
@@ -200,31 +183,20 @@ public class ShareClient {
200183
}
201184

202185
var transformed: Array<ShareGlucose> = []
186+
// Dec 2021, Dexcom Share modified json encoding of Trend from int to string
187+
let trendmap = ["": 0, "DoubleUp":1, "SingleUp":2, "FortyFiveUp":3, "Flat":4, "FortyFiveDown":5, "SingleDown":6, "DoubleDown": 7, "NotComputable":8, "RateOutOfRange":9]
203188
for sgv in sgvs {
204-
var trend = UInt8(0) // corresponds to trendString of "NONE"
205-
if let glucose = sgv["Value"] as? Int, let trendString = sgv["Trend"] as? String, let wt = sgv["WT"] as? String {
206-
if trendString == "DoubleUp" {
207-
trend = 1
208-
} else if trendString == "SingleUp" {
209-
trend = 2
210-
} else if trendString == "FortyFiveUp" {
211-
trend = 3
212-
} else if trendString == "Flat" {
213-
trend = 4
214-
} else if trendString == "FortyFiveDown" {
215-
trend = 5
216-
} else if trendString == "SingleDown" {
217-
trend = 6
218-
} else if trendString == "DoubleDown" {
219-
trend = 7
220-
} else if trendString == "NOT COMPUTABLE" {
221-
trend = 8
222-
} else if trendString == "RATE OUT OF RANGE" {
223-
trend = 9
224-
}
189+
if let glucose = sgv["Value"] as? Int, let strend = sgv["Trend"] as? String, let wt = sgv["WT"] as? String {
190+
let itrend = trendmap[strend, default: 0]
191+
transformed.append(ShareGlucose(
192+
glucose: UInt16(glucose),
193+
trend: UInt8(itrend),
194+
timestamp: try self.parseDate(wt)
195+
))
196+
} else if let glucose = sgv["Value"] as? Int, let trend = sgv["Trend"] as? Int, let wt = sgv["WT"] as? String {
225197
transformed.append(ShareGlucose(
226198
glucose: UInt16(glucose),
227-
trend: trend,
199+
trend: UInt8(trend),
228200
timestamp: try self.parseDate(wt)
229201
))
230202
} else {

0 commit comments

Comments
 (0)