Skip to content

Commit

Permalink
Merge pull request #273 from tmecklem/fix-timestamp-and-paging
Browse files Browse the repository at this point in the history
Fix timestamp and paging
  • Loading branch information
ps2 authored Oct 23, 2016
2 parents 1b09d22 + 2e258a5 commit 2b7224d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 3 additions & 1 deletion MinimedKit/GlucosePage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public class GlucosePage {
let calendar = Calendar.current
var date : Date = calendar.date(from: startTimestamp)!
for var event in eventsNeedingTimestamp {
date = calendar.date(byAdding: Calendar.Component.minute, value: 5, to: date)!
if !(event is NineteenSomethingGlucoseEvent) {
date = calendar.date(byAdding: Calendar.Component.minute, value: 5, to: date)!
}
event.timestamp = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
event.timestamp.calendar = calendar
eventsWithTimestamps.append(event)
Expand Down
24 changes: 17 additions & 7 deletions MinimedKitTests/GlucosePageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,27 @@ class GlucosePageTests: XCTestCase {

func testRelativeTimestamping() {
do {
let pageData = Data(hexadecimalString: "1028B61408131313131341BB".leftPadding(toLength: 2048, withPad: "0"))!
let calendar = Calendar.current
// a sensor timestamp, then "19-Something" and glucose relative timestamp records
let pageData = Data(hexadecimalString: "1028B6140813306BFB".leftPadding(toLength: 2048, withPad: "0"))!
let page = try GlucosePage(pageData: pageData)
let events = page.events

// a sensor timestamp followed by 5 "19-Something" relative timestamp records
XCTAssertEqual(events.count, 6)
XCTAssertEqual(events.count, 3)
//the initial timestamp comes from the sensor timestamp reference record
let expectedTimestamp = DateComponents(calendar: calendar,
year: 2016, month: 2, day: 8,
hour: 20, minute: 54)
XCTAssertEqual(events[0].timestamp, expectedTimestamp)

// expected final timestamp is reference sensor timestamp of 2016-02-08 20:54:00 + 5 * (5 minutes)
let expectedTimestamp = DateComponents(calendar: Calendar.current,
year: 2016, month: 2, day: 8, hour: 21, minute: 19)
XCTAssertEqual((events.last as? NineteenSomethingGlucoseEvent)?.timestamp, expectedTimestamp)
//The 19-Something needs a timestamp, but doesn't increment the relative counter
XCTAssertEqual(events[1].timestamp, expectedTimestamp)

//The GlucoseSensorData event needs a timestamp and does increment the relative counter by 5 minutes
let expectedGlucoseTimestamp = DateComponents(calendar: calendar,
year: 2016, month: 2, day: 8,
hour: 20, minute: 59)
XCTAssertEqual(events[2].timestamp, expectedGlucoseTimestamp)

} catch GlucosePage.GlucosePageError.invalidCRC {
XCTFail("page decoding threw invalid crc")
Expand Down
5 changes: 3 additions & 2 deletions RileyLinkKit/PumpOpsSynchronous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,10 @@ class PumpOpsSynchronous {

let currentGlucosePage = try readCurrentGlucosePage()
let startPage = Int(currentGlucosePage.pageNum)
let endPage = max(startPage - 1, 0)
//max lookback of 15 pages or when page is 0
let endPage = max(startPage - 15, 0)

pages: for pageNum in stride(from: startPage, to: endPage, by: -1) {
pages: for pageNum in stride(from: startPage, to: endPage - 1, by: -1) {
NSLog("Fetching page %d", pageNum)
let pageData: Data

Expand Down

0 comments on commit 2b7224d

Please sign in to comment.