From 5ab524306422402097c0704aa3a106dca31996a2 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Sun, 16 Oct 2016 22:06:11 -0400 Subject: [PATCH 01/18] Add import Foundation to HistoryPage --- MinimedKit/HistoryPage.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MinimedKit/HistoryPage.swift b/MinimedKit/HistoryPage.swift index b7bb505c7..6442cdd38 100644 --- a/MinimedKit/HistoryPage.swift +++ b/MinimedKit/HistoryPage.swift @@ -6,6 +6,8 @@ // Copyright © 2016 Pete Schwamb. All rights reserved. // +import Foundation + public class HistoryPage { public enum HistoryPageError: Error { From 4db513c41a4448958b27ee1ad6c3b81b8f2cf40e Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Sun, 16 Oct 2016 22:35:32 -0400 Subject: [PATCH 02/18] Add GlucosePage and enumeration of event types Add GlucosePage CRC check tests and Data extension to reverseBytes since GlucosePages are easier to decode in reverse. --- MinimedKit/GlucoseEvents/GlucoseEvent.swift | 23 ++++++++++ MinimedKit/GlucosePage.swift | 29 ++++++++++++ MinimedKitTests/GlucosePageTests.swift | 50 +++++++++++++++++++++ RileyLink.xcodeproj/project.pbxproj | 20 +++++++++ 4 files changed, 122 insertions(+) create mode 100644 MinimedKit/GlucoseEvents/GlucoseEvent.swift create mode 100644 MinimedKit/GlucosePage.swift create mode 100644 MinimedKitTests/GlucosePageTests.swift diff --git a/MinimedKit/GlucoseEvents/GlucoseEvent.swift b/MinimedKit/GlucoseEvents/GlucoseEvent.swift new file mode 100644 index 000000000..28dcaf7a2 --- /dev/null +++ b/MinimedKit/GlucoseEvents/GlucoseEvent.swift @@ -0,0 +1,23 @@ +// +// GlucoseEvent.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/16/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public protocol GlucoseEvent : DictionaryRepresentable { + + init?(availableData: Data, pumpModel: PumpModel) + + var rawData: Data { + get + } + + var length: Int { + get + } + +} diff --git a/MinimedKit/GlucosePage.swift b/MinimedKit/GlucosePage.swift new file mode 100644 index 000000000..45a1f5b2e --- /dev/null +++ b/MinimedKit/GlucosePage.swift @@ -0,0 +1,29 @@ +// +// GlucosePage.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/16/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public class GlucosePage { + + public enum GlucosePageError: Error { + case invalidCRC + case unknownEventType(eventType: UInt8) + } + + public let events: [GlucoseEvent] + + public init(pageData: Data, pumpModel: PumpModel) throws { + + guard checkCRC16(pageData) else { + events = [GlucoseEvent]() + throw GlucosePageError.invalidCRC + } + + events = [GlucoseEvent]() + } +} diff --git a/MinimedKitTests/GlucosePageTests.swift b/MinimedKitTests/GlucosePageTests.swift new file mode 100644 index 000000000..b8b9f2531 --- /dev/null +++ b/MinimedKitTests/GlucosePageTests.swift @@ -0,0 +1,50 @@ +// +// GlucosePageTests.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/16/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import XCTest +@testable import MinimedKit + +class GlucosePageTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testGlucosePageCRC() { + let pumpModel = PumpModel.Model551 + do { + let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C95")!, pumpModel: pumpModel) + } catch GlucosePage.GlucosePageError.invalidCRC { + XCTFail("page decoding threw invalid crc") + } catch GlucosePage.GlucosePageError.unknownEventType(let eventType) { + XCTFail("unknown event type" + String(eventType)) + } catch { + XCTFail("Unexpected exception...") + } + } + + func testGlucosePageInvalidCRC() { + let pumpModel = PumpModel.Model551 + do { + let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C94")!, pumpModel: pumpModel) + XCTFail("Should have thrown InvalidCRC") + } catch GlucosePage.GlucosePageError.invalidCRC { + // Happy path + } catch GlucosePage.GlucosePageError.unknownEventType(let eventType) { + XCTFail("unknown event type" + String(eventType)) + } catch { + XCTFail("Unexpected exception...") + } + } +} diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index b0c50e087..a62d27cbf 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -86,6 +86,9 @@ 43EC9DCB1B786C6200DB0D18 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43EC9DCA1B786C6200DB0D18 /* LaunchScreen.xib */; }; 43F348061D596270009933DC /* HKUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F348051D596270009933DC /* HKUnit.swift */; }; 43FF221C1CB9B9DE00024F30 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */; }; + 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44721DB46A5200340EED /* GlucosePageTests.swift */; }; + 54BC44751DB46B0A00340EED /* GlucosePage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44741DB46B0A00340EED /* GlucosePage.swift */; }; + 54BC44781DB46C7D00340EED /* GlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */; }; C10AB08D1C855613000F102E /* FindDeviceMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */; }; C10AB08F1C855F34000F102E /* DeviceLinkMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */; }; C10D9BC41C8269D500378342 /* MinimedKit.h in Headers */ = {isa = PBXBuildFile; fileRef = C10D9BC31C8269D500378342 /* MinimedKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -470,6 +473,9 @@ 43EC9DCA1B786C6200DB0D18 /* LaunchScreen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LaunchScreen.xib; sourceTree = ""; }; 43F348051D596270009933DC /* HKUnit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HKUnit.swift; sourceTree = ""; }; 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponents.swift; sourceTree = ""; }; + 54BC44721DB46A5200340EED /* GlucosePageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePageTests.swift; sourceTree = ""; }; + 54BC44741DB46B0A00340EED /* GlucosePage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePage.swift; sourceTree = ""; }; + 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseEvent.swift; sourceTree = ""; }; C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FindDeviceMessageBody.swift; sourceTree = ""; }; C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceLinkMessageBody.swift; sourceTree = ""; }; C10D9BC11C8269D500378342 /* MinimedKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MinimedKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -864,15 +870,25 @@ path = Crypto; sourceTree = ""; }; + 54BC44761DB46C3100340EED /* GlucoseEvents */ = { + isa = PBXGroup; + children = ( + 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */, + ); + path = GlucoseEvents; + sourceTree = ""; + }; C10D9BC21C8269D500378342 /* MinimedKit */ = { isa = PBXGroup; children = ( C1EAD6B81C826B92006DBA60 /* Extensions */, C1EAD6BC1C826B92006DBA60 /* Messages */, + 54BC44761DB46C3100340EED /* GlucoseEvents */, C1842BB91C8E15C600DB42AC /* PumpEvents */, C1EAD6AE1C826B6D006DBA60 /* AlertType.swift */, C1EAD6DD1C82B78C006DBA60 /* CRC8.swift */, C1EAD6E11C82BA7A006DBA60 /* CRC16.swift */, + 54BC44741DB46B0A00340EED /* GlucosePage.swift */, C1EB955C1C887FE5002517DF /* HistoryPage.swift */, C10D9BC51C8269D500378342 /* Info.plist */, C1EAD6AF1C826B6D006DBA60 /* MessageBody.swift */, @@ -896,6 +912,7 @@ C10D9BD31C8269D500378342 /* Info.plist */, C1EAD6DF1C82B910006DBA60 /* CRC8Tests.swift */, C1EAD6E31C82BA87006DBA60 /* CRC16Tests.swift */, + 54BC44721DB46A5200340EED /* GlucosePageTests.swift */, C12198621C8DF4C800BC374C /* HistoryPageTests.swift */, C1C357901C92733A009BDD4F /* MeterMessageTests.swift */, C1EAD6D11C826C43006DBA60 /* MinimedKitTests.swift */, @@ -1885,6 +1902,7 @@ C1EAD6C91C826B92006DBA60 /* MySentryAckMessageBody.swift in Sources */, C1EAD6CE1C826B92006DBA60 /* ReadSettingsCarelinkMessageBody.swift in Sources */, C12198AD1C8F332500BC374C /* TimestampedPumpEvent.swift in Sources */, + 54BC44751DB46B0A00340EED /* GlucosePage.swift in Sources */, C1EAD6DE1C82B78C006DBA60 /* CRC8.swift in Sources */, C1EAD6C51C826B92006DBA60 /* Int.swift in Sources */, C1842C021C8FA45100DB42AC /* JournalEntryExerciseMarkerPumpEvent.swift in Sources */, @@ -1905,6 +1923,7 @@ C1842C211C8FA45100DB42AC /* ChangeAlarmNotifyModePumpEvent.swift in Sources */, C1842BC31C8E931E00DB42AC /* BolusNormalPumpEvent.swift in Sources */, 43CA932F1CB8CFA1000026B5 /* ReadTimeCarelinkMessageBody.swift in Sources */, + 54BC44781DB46C7D00340EED /* GlucoseEvent.swift in Sources */, C1842C221C8FA45100DB42AC /* ChangeAlarmClockTimePumpEvent.swift in Sources */, C1842BFD1C8FA45100DB42AC /* ResumePumpEvent.swift in Sources */, C1EAD6CC1C826B92006DBA60 /* MySentryPumpStatusMessageBody.swift in Sources */, @@ -1948,6 +1967,7 @@ C12198A31C8DFC3600BC374C /* BolusCarelinkMessageBodyTests.swift in Sources */, C121985F1C8DE77D00BC374C /* FindDeviceMessageBodyTests.swift in Sources */, C1EAD6D71C826C43006DBA60 /* NSDataTests.swift in Sources */, + 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */, C143031A1C9A610B00A40450 /* GetBatteryCarelinkMessageBodyTests.swift in Sources */, 43CA93351CB9727F000026B5 /* ChangeTempBasalCarelinkMessageBodyTests.swift in Sources */, C1EAD6E01C82B910006DBA60 /* CRC8Tests.swift in Sources */, From 64a377bbb5ae3c5b3df9afcb55994ece1857a6a7 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Sun, 16 Oct 2016 22:45:56 -0400 Subject: [PATCH 03/18] Add reverseBytes extension to Data --- MinimedKit/Extensions/NSData.swift | 8 ++++++++ MinimedKitTests/NSDataTests.swift | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/MinimedKit/Extensions/NSData.swift b/MinimedKit/Extensions/NSData.swift index 284a8a816..b91a95138 100644 --- a/MinimedKit/Extensions/NSData.swift +++ b/MinimedKit/Extensions/NSData.swift @@ -71,6 +71,14 @@ extension Data { */ } +extension Data { + func reverseBytes() -> Data { + let byteArray = self.withUnsafeBytes { + [UInt8](UnsafeBufferPointer(start: $0, count: self.count)) + } + return Data(bytes: byteArray.reversed()) + } +} extension Data { init?(hexadecimalString: String) { diff --git a/MinimedKitTests/NSDataTests.swift b/MinimedKitTests/NSDataTests.swift index 08a3ff2d2..859f5e02b 100644 --- a/MinimedKitTests/NSDataTests.swift +++ b/MinimedKitTests/NSDataTests.swift @@ -49,4 +49,11 @@ class NSDataTests: XCTestCase { data?.copyBytes(to: &bytes, count: 4) XCTAssertEqual([0xa2, 0x59, 0x40, 0x40], bytes) } + + func testReverseBytes() { + let data = Data(hexadecimalString: "as594041") + let reversedData = Data(hexadecimalString: "414059as") + + XCTAssertEqual(reversedData, data?.reverseBytes()) + } } From 6bda586f167e745d6d240dd2f0cc5b62b3ba4af7 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Mon, 17 Oct 2016 00:52:14 -0400 Subject: [PATCH 04/18] Ensure that event name sequence matches decocare output In writing this test and the code that passed the test, I encountered a strange mismatch from decocare. Decocare had more sgv entries, but they had values of 0. Additional discovery and discussion led to a change in decocare: https://github.com/openaps/decocare/pull/11 --- MinimedKit/GlucoseEventType.swift | 59 ++++++++++++++++ .../BatteryChangeGlucoseEvent.swift | 30 ++++++++ .../CalBGForGHGlucoseEvent.swift | 30 ++++++++ .../GlucoseEvents/DataEndGlucoseEvent.swift | 31 +++++++++ .../DateTimeChangeGlucoseEvent.swift | 30 ++++++++ .../GlucoseEvents/Fokko7GlucoseEvent.swift | 30 ++++++++ .../GlucoseSensorDataGlucoseEvent.swift | 30 ++++++++ .../NineteenSomethingGlucoseEvent.swift | 31 +++++++++ .../ReferenceTimestampedGlucoseEvent.swift | 15 ++++ .../RelativeTimestampedGlucoseEvent.swift | 14 ++++ .../SensorCalFactorGlucoseEvent.swift | 30 ++++++++ .../GlucoseEvents/SensorCalGlucoseEvent.swift | 32 +++++++++ .../SensorStatusGlucoseEvent.swift | 31 +++++++++ .../SensorSyncGlucoseEvent.swift | 31 +++++++++ .../SensorTimestampGlucoseEvent.swift | 30 ++++++++ .../SensorWeakSignalGlucoseEvent.swift | 31 +++++++++ .../TenSomethingGlucoseEvent.swift | 30 ++++++++ MinimedKit/GlucosePage.swift | 42 +++++++++++- MinimedKitTests/GlucosePageTests.swift | 23 +++++++ RileyLink.xcodeproj/project.pbxproj | 68 +++++++++++++++++++ 20 files changed, 647 insertions(+), 1 deletion(-) create mode 100644 MinimedKit/GlucoseEventType.swift create mode 100644 MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/DateTimeChangeGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/NineteenSomethingGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/ReferenceTimestampedGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/RelativeTimestampedGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorTimestampGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/SensorWeakSignalGlucoseEvent.swift create mode 100644 MinimedKit/GlucoseEvents/TenSomethingGlucoseEvent.swift diff --git a/MinimedKit/GlucoseEventType.swift b/MinimedKit/GlucoseEventType.swift new file mode 100644 index 000000000..0ea48dfba --- /dev/null +++ b/MinimedKit/GlucoseEventType.swift @@ -0,0 +1,59 @@ +// +// GlucoseEventType.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/16/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public enum GlucoseEventType: UInt8 { + case dataEnd = 0x01 + case sensorWeakSignal = 0x02 + case sensorCal = 0x03 + case fokko7 = 0x07 + case sensorTimestamp = 0x08 + case batteryChange = 0x0a + case sensorStatus = 0x0b + case dateTimeChange = 0x0c + case sensorSync = 0x0d + case calBGForGH = 0x0e + case sensorCalFactor = 0x0f + case tenSomething = 0x10 + case nineteenSomething = 0x13 + case glucoseSensorDataEvent + + public var eventType: GlucoseEvent.Type { + switch self { + case .dataEnd: + return DataEndGlucoseEvent.self + case .sensorWeakSignal: + return SensorWeakSignalGlucoseEvent.self + case .sensorCal: + return SensorCalGlucoseEvent.self + case .fokko7: + return Fokko7GlucoseEvent.self + case .sensorTimestamp: + return SensorTimestampGlucoseEvent.self + case .batteryChange: + return BatteryChangeGlucoseEvent.self + case .sensorStatus: + return SensorStatusGlucoseEvent.self + case .dateTimeChange: + return DateTimeChangeGlucoseEvent.self + case .sensorSync: + return SensorSyncGlucoseEvent.self + case .calBGForGH: + return CalBGForGHGlucoseEvent.self + case .sensorCalFactor: + return SensorCalFactorGlucoseEvent.self + case .tenSomething: + return TenSomethingGlucoseEvent.self + case .nineteenSomething: + return NineteenSomethingGlucoseEvent.self + case .glucoseSensorDataEvent: + return GlucoseSensorDataGlucoseEvent.self + } + } +} diff --git a/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift new file mode 100644 index 000000000..a93288be8 --- /dev/null +++ b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift @@ -0,0 +1,30 @@ +// +// BatteryChangeGlucoseEvent.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/16/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public struct BatteryChangeGlucoseEvent : GlucoseEvent { + public let length: Int + public let rawData: Data + + public init?(availableData: Data, pumpModel: PumpModel) { + length = 5 + + guard length <= availableData.count else { + return nil + } + + rawData = availableData.subdata(in: 0.. GlucoseEvent { + let remainingData = pageData.subdata(in: offset.. Date: Tue, 18 Oct 2016 21:49:57 -0400 Subject: [PATCH 05/18] Decode independent events Independent glucose events are events that have a timestamp in the record and do not serve as a reference timestamp for other records. --- MinimedKit/Extensions/NSDateComponents.swift | 13 +++++++ .../BatteryChangeGlucoseEvent.swift | 2 + .../CalBGForGHGlucoseEvent.swift | 8 ++++ .../DateTimeChangeGlucoseEvent.swift | 2 + .../SensorStatusGlucoseEvent.swift | 2 + .../SensorSyncGlucoseEvent.swift | 2 + .../BatteryChangeGlucoseEventTests.swift | 32 +++++++++++++++ .../CalBGForGHGlucoseEventTests.swift | 35 +++++++++++++++++ .../DateTimeChangeGlucoseEventTests.swift | 34 ++++++++++++++++ .../SensorStatusGlucoseEventTests.swift | 33 ++++++++++++++++ .../SensorSyncGlucoseEventTests.swift | 39 +++++++++++++++++++ MinimedKitTests/NSDateComponentsTests.swift | 9 +++++ RileyLink.xcodeproj/project.pbxproj | 32 ++++++++++++++- 13 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 MinimedKitTests/GlucoseEvents/BatteryChangeGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/DateTimeChangeGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/SensorStatusGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/SensorSyncGlucoseEventTests.swift diff --git a/MinimedKit/Extensions/NSDateComponents.swift b/MinimedKit/Extensions/NSDateComponents.swift index 8f758190e..6fd023610 100644 --- a/MinimedKit/Extensions/NSDateComponents.swift +++ b/MinimedKit/Extensions/NSDateComponents.swift @@ -47,4 +47,17 @@ extension DateComponents { calendar = Calendar(identifier: Calendar.Identifier.gregorian) } + + init(glucoseEventBytes: Data) { + self.init() + + year = Int(glucoseEventBytes[3] & 0b01111111) + 2000 + month = Int((glucoseEventBytes[0] & 0b11000000) >> 4 + + (glucoseEventBytes[1] & 0b11000000) >> 6) + day = Int(glucoseEventBytes[2] & 0b00011111) + hour = Int(glucoseEventBytes[0] & 0b00011111) + minute = Int(glucoseEventBytes[1] & 0b00111111) + + calendar = Calendar(identifier: Calendar.Identifier.gregorian) + } } diff --git a/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift index a93288be8..298cc9d67 100644 --- a/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift @@ -11,6 +11,7 @@ import Foundation public struct BatteryChangeGlucoseEvent : GlucoseEvent { public let length: Int public let rawData: Data + public let timestamp: DateComponents public init?(availableData: Data, pumpModel: PumpModel) { length = 5 @@ -20,6 +21,7 @@ public struct BatteryChangeGlucoseEvent : GlucoseEvent { } rawData = availableData.subdata(in: 0.. Int { + return Int(availableData[idx] as UInt8) + } + rawData = availableData.subdata(in: 0.. Date: Tue, 18 Oct 2016 22:15:59 -0400 Subject: [PATCH 06/18] Add event parsing for reference timestamp glucose records Reference timestamped records are used to apply a timestamp to other records using an offset (5 minutes). --- .../SensorCalFactorGlucoseEvent.swift | 8 +++++ .../SensorTimestampGlucoseEvent.swift | 2 ++ .../TenSomethingGlucoseEvent.swift | 2 ++ .../SensorCalFactorGlucoseEventTests.swift | 35 +++++++++++++++++++ .../SensorTimestampGlucoseEventTests.swift | 34 ++++++++++++++++++ .../TenSomethingGlucoseEventTests.swift | 34 ++++++++++++++++++ RileyLink.xcodeproj/project.pbxproj | 12 +++++++ 7 files changed, 127 insertions(+) create mode 100644 MinimedKitTests/GlucoseEvents/SensorCalFactorGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/SensorTimestampGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/TenSomethingGlucoseEventTests.swift diff --git a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift index 9fd84764f..de034d8de 100644 --- a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift @@ -11,6 +11,8 @@ import Foundation public struct SensorCalFactorGlucoseEvent : ReferenceTimestampedGlucoseEvent { public let length: Int public let rawData: Data + public let timestamp: DateComponents + public let factor: Float public init?(availableData: Data, pumpModel: PumpModel) { length = 7 @@ -19,7 +21,13 @@ public struct SensorCalFactorGlucoseEvent : ReferenceTimestampedGlucoseEvent { return nil } + func d(_ idx:Int) -> Int { + return Int(availableData[idx] as UInt8) + } + rawData = availableData.subdata(in: 0.. Date: Tue, 18 Oct 2016 22:37:21 -0400 Subject: [PATCH 07/18] Add event parsing for relative timestamp records MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Relative timestamp records need a timestamp, but don’t have one in their own raw data, so that’s not included in this commit. --- .../GlucoseSensorDataGlucoseEvent.swift | 2 + .../GlucoseEvents/SensorCalGlucoseEvent.swift | 6 +++ .../GlucoseSensorDataGlucoseEventTests.swift | 32 +++++++++++++++ .../SensorCalGlucoseEventTests.swift | 40 +++++++++++++++++++ RileyLink.xcodeproj/project.pbxproj | 8 ++++ 5 files changed, 88 insertions(+) create mode 100644 MinimedKitTests/GlucoseEvents/GlucoseSensorDataGlucoseEventTests.swift create mode 100644 MinimedKitTests/GlucoseEvents/SensorCalGlucoseEventTests.swift diff --git a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift index 707c7e920..c1ef5ae6c 100644 --- a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift @@ -11,6 +11,7 @@ import Foundation public struct GlucoseSensorDataGlucoseEvent : RelativeTimestampedGlucoseEvent { public let length: Int public let rawData: Data + public let sgv: Int public init?(availableData: Data, pumpModel: PumpModel) { length = 1 @@ -20,6 +21,7 @@ public struct GlucoseSensorDataGlucoseEvent : RelativeTimestampedGlucoseEvent { } rawData = availableData.subdata(in: 0.. Int { + return Int(availableData[idx] as UInt8) + } + rawData = availableData.subdata(in: 0.. Date: Tue, 18 Oct 2016 23:25:06 -0400 Subject: [PATCH 08/18] Add relative timestamps to records when parsing page data --- .../GlucoseEvents/DataEndGlucoseEvent.swift | 2 ++ .../GlucoseEvents/Fokko7GlucoseEvent.swift | 2 ++ MinimedKit/GlucoseEvents/GlucoseEvent.swift | 4 ++++ .../GlucoseSensorDataGlucoseEvent.swift | 2 ++ .../NineteenSomethingGlucoseEvent.swift | 2 ++ .../RelativeTimestampedGlucoseEvent.swift | 4 ++++ .../GlucoseEvents/SensorCalGlucoseEvent.swift | 2 ++ .../SensorWeakSignalGlucoseEvent.swift | 2 ++ MinimedKit/GlucosePage.swift | 19 ++++++++++++--- MinimedKitTests/GlucosePageTests.swift | 23 +++++++++++++++++++ MinimedKitTests/NSStringExtensions.swift | 20 ++++++++++++++++ RileyLink.xcodeproj/project.pbxproj | 14 +++++++---- 12 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 MinimedKitTests/NSStringExtensions.swift diff --git a/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift b/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift index a14f40c2a..30ba19b10 100644 --- a/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift @@ -11,6 +11,7 @@ import Foundation public struct DataEndGlucoseEvent : RelativeTimestampedGlucoseEvent { public let length: Int public let rawData: Data + public var timestamp: DateComponents public init?(availableData: Data, pumpModel: PumpModel) { length = 1 @@ -20,6 +21,7 @@ public struct DataEndGlucoseEvent : RelativeTimestampedGlucoseEvent { } rawData = availableData.subdata(in: 0.. [GlucoseEvent] { + var eventsWithTimestamps = [GlucoseEvent]() + 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)! + event.timestamp = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date) + event.timestamp.calendar = calendar + eventsWithTimestamps.append(event) + } + return eventsWithTimestamps + } + while offset < length { // Slurp up 0's if pageData[offset] as UInt8 == 0 { @@ -52,10 +65,10 @@ public class GlucosePage { let event = matchEvent(offset) if let event = event as? RelativeTimestampedGlucoseEvent { - eventsNeedingTimestamp.append(event) + eventsNeedingTimestamp.insert(event, at: 0) } else if let event = event as? ReferenceTimestampedGlucoseEvent { - let eventsWithTimestamps : [GlucoseEvent] = eventsNeedingTimestamp - tempEvents += eventsWithTimestamps + let eventsWithTimestamp = addTimestampsToEvents(startTimestamp: event.timestamp, eventsNeedingTimestamp: eventsNeedingTimestamp).reversed() + tempEvents.append(contentsOf: eventsWithTimestamp) eventsNeedingTimestamp.removeAll() tempEvents.append(event) } else { diff --git a/MinimedKitTests/GlucosePageTests.swift b/MinimedKitTests/GlucosePageTests.swift index d9420ca79..0cb46cad1 100644 --- a/MinimedKitTests/GlucosePageTests.swift +++ b/MinimedKitTests/GlucosePageTests.swift @@ -48,6 +48,29 @@ class GlucosePageTests: XCTestCase { } } + func testRelativeTimestamping() { + let pumpModel = PumpModel.Model551 + do { + let pageData = Data(hexadecimalString: "1028B61408131313131341BB".leftPadding(toLength: 2048, withPad: "0"))! + let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + let events = page.events + + // a sensor timestamp followed by 5 "19-Something" relative timestamp records + XCTAssertEqual(events.count, 6) + + // 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.first as! NineteenSomethingGlucoseEvent).timestamp, expectedTimestamp) + + } catch GlucosePage.GlucosePageError.invalidCRC { + XCTFail("page decoding threw invalid crc") + } catch GlucosePage.GlucosePageError.unknownEventType(let eventType) { + XCTFail("unknown event type" + String(eventType)) + } catch { + XCTFail("Unexpected exception...") + } + } func testGlucosePageEventDecoding() { let pumpModel = PumpModel.Model551 diff --git a/MinimedKitTests/NSStringExtensions.swift b/MinimedKitTests/NSStringExtensions.swift new file mode 100644 index 000000000..ad445a477 --- /dev/null +++ b/MinimedKitTests/NSStringExtensions.swift @@ -0,0 +1,20 @@ +// +// NSStringExtensions.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/18/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +extension String { + func leftPadding(toLength: Int, withPad character: Character) -> String { + let newLength = self.characters.count + if newLength < toLength { + return String(repeatElement(character, count: toLength - newLength)) + self + } else { + return self.substring(from: index(self.startIndex, offsetBy: newLength - toLength)) + } + } +} diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index ac162f5b9..05be2d35c 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -116,6 +116,7 @@ 54BC44AF1DB70C3E00340EED /* TenSomethingGlucoseEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44AE1DB70C3E00340EED /* TenSomethingGlucoseEventTests.swift */; }; 54BC44B11DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */; }; 54BC44B31DB711BE00340EED /* SensorCalGlucoseEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */; }; + 54BC44B51DB7184D00340EED /* NSStringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B41DB7184D00340EED /* NSStringExtensions.swift */; }; C10AB08D1C855613000F102E /* FindDeviceMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */; }; C10AB08F1C855F34000F102E /* DeviceLinkMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */; }; C10D9BC41C8269D500378342 /* MinimedKit.h in Headers */ = {isa = PBXBuildFile; fileRef = C10D9BC31C8269D500378342 /* MinimedKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -530,6 +531,7 @@ 54BC44AE1DB70C3E00340EED /* TenSomethingGlucoseEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TenSomethingGlucoseEventTests.swift; sourceTree = ""; }; 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseSensorDataGlucoseEventTests.swift; sourceTree = ""; }; 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SensorCalGlucoseEventTests.swift; sourceTree = ""; }; + 54BC44B41DB7184D00340EED /* NSStringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSStringExtensions.swift; sourceTree = ""; }; C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FindDeviceMessageBody.swift; sourceTree = ""; }; C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceLinkMessageBody.swift; sourceTree = ""; }; C10D9BC11C8269D500378342 /* MinimedKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MinimedKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -952,15 +954,15 @@ isa = PBXGroup; children = ( 54BC44A01DB6F74300340EED /* BatteryChangeGlucoseEventTests.swift */, - 54BC44A21DB7021B00340EED /* SensorStatusGlucoseEventTests.swift */, + 54BC44A81DB704A600340EED /* CalBGForGHGlucoseEventTests.swift */, 54BC44A41DB702C800340EED /* DateTimeChangeGlucoseEventTests.swift */, + 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */, + 54BC44AC1DB70A5E00340EED /* SensorCalFactorGlucoseEventTests.swift */, + 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */, + 54BC44A21DB7021B00340EED /* SensorStatusGlucoseEventTests.swift */, 54BC44A61DB703E900340EED /* SensorSyncGlucoseEventTests.swift */, - 54BC44A81DB704A600340EED /* CalBGForGHGlucoseEventTests.swift */, 54BC44AA1DB7093700340EED /* SensorTimestampGlucoseEventTests.swift */, - 54BC44AC1DB70A5E00340EED /* SensorCalFactorGlucoseEventTests.swift */, 54BC44AE1DB70C3E00340EED /* TenSomethingGlucoseEventTests.swift */, - 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */, - 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */, ); path = GlucoseEvents; sourceTree = ""; @@ -1010,6 +1012,7 @@ 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */, 43B0ADBF1D0FC03200AAD278 /* NSDateComponentsTests.swift */, C1EAD6DB1C82A4AB006DBA60 /* RFToolsTests.swift */, + 54BC44B41DB7184D00340EED /* NSStringExtensions.swift */, ); path = MinimedKitTests; sourceTree = ""; @@ -2086,6 +2089,7 @@ C143031A1C9A610B00A40450 /* GetBatteryCarelinkMessageBodyTests.swift in Sources */, 43CA93351CB9727F000026B5 /* ChangeTempBasalCarelinkMessageBodyTests.swift in Sources */, C1EAD6E01C82B910006DBA60 /* CRC8Tests.swift in Sources */, + 54BC44B51DB7184D00340EED /* NSStringExtensions.swift in Sources */, 54BC44AB1DB7093700340EED /* SensorTimestampGlucoseEventTests.swift in Sources */, 43CA93331CB9726A000026B5 /* ChangeTimeCarelinMessageBodyTests.swift in Sources */, C1EAD6DC1C82A4AB006DBA60 /* RFToolsTests.swift in Sources */, From 438cc8344a3bfb357774e39aaac4747c1d5cd822 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 19 Oct 2016 18:27:52 -0400 Subject: [PATCH 09/18] Add MessageBody classes for fetching glucose pages --- MinimedKit/MessageType.swift | 6 ++++ .../Messages/GetGlucosePageMessageBody.swift | 35 +++++++++++++++++++ .../ReadCurrentGlucosePageMessageBody.swift | 30 ++++++++++++++++ .../GetGlucosePageMessageBodyTests.swift | 30 ++++++++++++++++ ...adCurrentGlucosePageMessageBodyTests.swift | 35 +++++++++++++++++++ RileyLink.xcodeproj/project.pbxproj | 20 +++++++++-- 6 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 MinimedKit/Messages/GetGlucosePageMessageBody.swift create mode 100644 MinimedKit/Messages/ReadCurrentGlucosePageMessageBody.swift create mode 100644 MinimedKitTests/Messages/GetGlucosePageMessageBodyTests.swift create mode 100644 MinimedKitTests/Messages/ReadCurrentGlucosePageMessageBodyTests.swift diff --git a/MinimedKit/MessageType.swift b/MinimedKit/MessageType.swift index e1b75bd32..45481924a 100644 --- a/MinimedKit/MessageType.swift +++ b/MinimedKit/MessageType.swift @@ -27,7 +27,9 @@ public enum MessageType: UInt8 { case getHistoryPage = 0x80 case getPumpModel = 0x8d case readTempBasal = 0x98 + case getGlucosePage = 0x9A case readSettings = 0xc0 + case readCurrentGlucosePage = 0xcd case readPumpStatus = 0xce var bodyType: MessageBody.Type { @@ -62,6 +64,10 @@ public enum MessageType: UInt8 { return ReadRemainingInsulinMessageBody.self case .readPumpStatus: return ReadPumpStatusMessageBody.self + case .readCurrentGlucosePage: + return ReadCurrentGlucosePageMessageBody.self + case .getGlucosePage: + return GetGlucosePageMessageBody.self default: return UnknownMessageBody.self } diff --git a/MinimedKit/Messages/GetGlucosePageMessageBody.swift b/MinimedKit/Messages/GetGlucosePageMessageBody.swift new file mode 100644 index 000000000..0f15b04ed --- /dev/null +++ b/MinimedKit/Messages/GetGlucosePageMessageBody.swift @@ -0,0 +1,35 @@ +// +// GetGlucosePageMessageBody.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public class GetGlucosePageMessageBody: CarelinkLongMessageBody { + public let lastFrame: Bool + public let frameNumber: Int + public let frame: Data + + public required init?(rxData: Data) { + guard rxData.count == type(of: self).length else { + return nil + } + frameNumber = Int(rxData[0] as UInt8) & 0b1111111 + lastFrame = (rxData[0] as UInt8) & 0b10000000 > 0 + frame = rxData.subdata(in: 1..<65) + super.init(rxData: rxData) + } + + public required init(pageNum: UInt32) { + let numArgs = 4 + lastFrame = false + frame = Data() + frameNumber = 0 + let data = Data(hexadecimalString: String(format: "%02x%08x", numArgs, pageNum))! + super.init(rxData: data)! + } + +} diff --git a/MinimedKit/Messages/ReadCurrentGlucosePageMessageBody.swift b/MinimedKit/Messages/ReadCurrentGlucosePageMessageBody.swift new file mode 100644 index 000000000..0d2f6274f --- /dev/null +++ b/MinimedKit/Messages/ReadCurrentGlucosePageMessageBody.swift @@ -0,0 +1,30 @@ +// +// ReadCurrentGlucosePageMessageBody.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public class ReadCurrentGlucosePageMessageBody: CarelinkLongMessageBody { + + public let pageNum: UInt32 + public let glucose: Int + public let isig: Int + + public required init?(rxData: Data) { + guard rxData.count == type(of: self).length else { + return nil + } + + self.pageNum = rxData.subdata(in: 1..<5).withUnsafeBytes({ (bytes: UnsafePointer) -> UInt32 in + return UInt32(bigEndian: bytes.pointee) + }) + self.glucose = Int(rxData[6] as UInt8) + self.isig = Int(rxData[8] as UInt8) + + super.init(rxData: rxData) + } +} diff --git a/MinimedKitTests/Messages/GetGlucosePageMessageBodyTests.swift b/MinimedKitTests/Messages/GetGlucosePageMessageBodyTests.swift new file mode 100644 index 000000000..b516004c3 --- /dev/null +++ b/MinimedKitTests/Messages/GetGlucosePageMessageBodyTests.swift @@ -0,0 +1,30 @@ +// +// GetGlucosePageMessageBodyTests.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import XCTest +@testable import MinimedKit + +class GetGlucosePageMessageBodyTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testTxDataEncoding() { + let messageBody = GetGlucosePageMessageBody(pageNum: 13) + + XCTAssertEqual(messageBody.txData.subdata(in: 0..<5).hexadecimalString, "040000000d") + } + +} diff --git a/MinimedKitTests/Messages/ReadCurrentGlucosePageMessageBodyTests.swift b/MinimedKitTests/Messages/ReadCurrentGlucosePageMessageBodyTests.swift new file mode 100644 index 000000000..99a655c7c --- /dev/null +++ b/MinimedKitTests/Messages/ReadCurrentGlucosePageMessageBodyTests.swift @@ -0,0 +1,35 @@ +// +// ReadCurrentGlucosePageMessageBodyTests.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import XCTest +@testable import MinimedKit + +class ReadCurrentGlucosePageMessageBodyTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testResponseInitializer() { + var responseData = Data(hexadecimalString: "0000000D6100100020")! + responseData.append(contentsOf: [UInt8](repeating: 0, count: 65 - responseData.count)) + + let messageBody = ReadCurrentGlucosePageMessageBody(rxData: responseData)! + + XCTAssertEqual(messageBody.pageNum, 3425) + XCTAssertEqual(messageBody.glucose, 16) + XCTAssertEqual(messageBody.isig, 32) + } + +} diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index 05be2d35c..225140f71 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -86,6 +86,8 @@ 43EC9DCB1B786C6200DB0D18 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43EC9DCA1B786C6200DB0D18 /* LaunchScreen.xib */; }; 43F348061D596270009933DC /* HKUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F348051D596270009933DC /* HKUnit.swift */; }; 43FF221C1CB9B9DE00024F30 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */; }; + 541688DB1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */; }; + 541688DD1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */; }; 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44721DB46A5200340EED /* GlucosePageTests.swift */; }; 54BC44751DB46B0A00340EED /* GlucosePage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44741DB46B0A00340EED /* GlucosePage.swift */; }; 54BC44781DB46C7D00340EED /* GlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */; }; @@ -117,6 +119,8 @@ 54BC44B11DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */; }; 54BC44B31DB711BE00340EED /* SensorCalGlucoseEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */; }; 54BC44B51DB7184D00340EED /* NSStringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B41DB7184D00340EED /* NSStringExtensions.swift */; }; + 54BC44B71DB81B5100340EED /* GetGlucosePageMessageBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B61DB81B5100340EED /* GetGlucosePageMessageBodyTests.swift */; }; + 54BC44B91DB81D6100340EED /* GetGlucosePageMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44B81DB81D6100340EED /* GetGlucosePageMessageBody.swift */; }; C10AB08D1C855613000F102E /* FindDeviceMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */; }; C10AB08F1C855F34000F102E /* DeviceLinkMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */; }; C10D9BC41C8269D500378342 /* MinimedKit.h in Headers */ = {isa = PBXBuildFile; fileRef = C10D9BC31C8269D500378342 /* MinimedKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -501,6 +505,8 @@ 43EC9DCA1B786C6200DB0D18 /* LaunchScreen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LaunchScreen.xib; sourceTree = ""; }; 43F348051D596270009933DC /* HKUnit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HKUnit.swift; sourceTree = ""; }; 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponents.swift; sourceTree = ""; }; + 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBodyTests.swift; sourceTree = ""; }; + 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBody.swift; sourceTree = ""; }; 54BC44721DB46A5200340EED /* GlucosePageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePageTests.swift; sourceTree = ""; }; 54BC44741DB46B0A00340EED /* GlucosePage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePage.swift; sourceTree = ""; }; 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseEvent.swift; sourceTree = ""; }; @@ -532,6 +538,8 @@ 54BC44B01DB70F4A00340EED /* GlucoseSensorDataGlucoseEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseSensorDataGlucoseEventTests.swift; sourceTree = ""; }; 54BC44B21DB711BE00340EED /* SensorCalGlucoseEventTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SensorCalGlucoseEventTests.swift; sourceTree = ""; }; 54BC44B41DB7184D00340EED /* NSStringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSStringExtensions.swift; sourceTree = ""; }; + 54BC44B61DB81B5100340EED /* GetGlucosePageMessageBodyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetGlucosePageMessageBodyTests.swift; sourceTree = ""; }; + 54BC44B81DB81D6100340EED /* GetGlucosePageMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetGlucosePageMessageBody.swift; sourceTree = ""; }; C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FindDeviceMessageBody.swift; sourceTree = ""; }; C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceLinkMessageBody.swift; sourceTree = ""; }; C10D9BC11C8269D500378342 /* MinimedKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MinimedKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1026,11 +1034,13 @@ C12198601C8DEB7B00BC374C /* DeviceLinkMessageBodyTests.swift */, C121985E1C8DE77D00BC374C /* FindDeviceMessageBodyTests.swift */, C14303191C9A610B00A40450 /* GetBatteryCarelinkMessageBodyTests.swift */, + 54BC44B61DB81B5100340EED /* GetGlucosePageMessageBodyTests.swift */, C14303171C97CC6B00A40450 /* GetPumpModelCarelinkMessageBodyTests.swift */, C1EAD6D21C826C43006DBA60 /* MySentryPumpStatusMessageBodyTests.swift */, + 43A068EB1CF6BA6900F9EFE4 /* ReadRemainingInsulinMessageBodyTests.swift */, C1EAD6D41C826C43006DBA60 /* ReadSettingsCarelinkMessageBodyTests.swift */, 43CA93301CB97191000026B5 /* ReadTempBasalCarelinkMessageBodyTests.swift */, - 43A068EB1CF6BA6900F9EFE4 /* ReadRemainingInsulinMessageBodyTests.swift */, + 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */, ); path = Messages; sourceTree = ""; @@ -1407,6 +1417,7 @@ C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */, C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */, C1711A5B1C953F3000CB25BD /* GetBatteryCarelinkMessageBody.swift */, + 54BC44B81DB81D6100340EED /* GetGlucosePageMessageBody.swift */, C1711A5D1C977BD000CB25BD /* GetHistoryPageCarelinkMessageBody.swift */, C1711A591C952D2900CB25BD /* GetPumpModelCarelinkMessageBody.swift */, C1EAD6BE1C826B92006DBA60 /* MySentryAckMessageBody.swift */, @@ -1415,12 +1426,13 @@ C1EAD6C11C826B92006DBA60 /* MySentryPumpStatusMessageBody.swift */, C1EAD6C21C826B92006DBA60 /* PowerOnCarelinkMessageBody.swift */, C14303151C97C98000A40450 /* PumpAckMessageBody.swift */, + C178845C1D4EF3D800405663 /* ReadPumpStatusMessageBody.swift */, 433568751CF67FA800FD9D54 /* ReadRemainingInsulinMessageBody.swift */, C1EAD6C31C826B92006DBA60 /* ReadSettingsCarelinkMessageBody.swift */, 43CA932C1CB8CFA1000026B5 /* ReadTempBasalCarelinkMessageBody.swift */, 43CA932D1CB8CFA1000026B5 /* ReadTimeCarelinkMessageBody.swift */, C1EAD6C41C826B92006DBA60 /* UnknownMessageBody.swift */, - C178845C1D4EF3D800405663 /* ReadPumpStatusMessageBody.swift */, + 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */, ); path = Messages; sourceTree = ""; @@ -2030,6 +2042,7 @@ C1EAD6C61C826B92006DBA60 /* NSData.swift in Sources */, C1842C211C8FA45100DB42AC /* ChangeAlarmNotifyModePumpEvent.swift in Sources */, C1842BC31C8E931E00DB42AC /* BolusNormalPumpEvent.swift in Sources */, + 541688DD1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift in Sources */, 43CA932F1CB8CFA1000026B5 /* ReadTimeCarelinkMessageBody.swift in Sources */, 54BC44781DB46C7D00340EED /* GlucoseEvent.swift in Sources */, C1842C221C8FA45100DB42AC /* ChangeAlarmClockTimePumpEvent.swift in Sources */, @@ -2047,6 +2060,7 @@ C1842C0A1C8FA45100DB42AC /* ChangeVariableBolusPumpEvent.swift in Sources */, C1842C191C8FA45100DB42AC /* ChangeBolusScrollStepSizePumpEvent.swift in Sources */, C1842C171C8FA45100DB42AC /* ChangeCaptureEventEnablePumpEvent.swift in Sources */, + 54BC44B91DB81D6100340EED /* GetGlucosePageMessageBody.swift in Sources */, 43B0ADC41D12506A00AAD278 /* NSDateFormatter.swift in Sources */, C1EAD6DA1C829104006DBA60 /* RFTools.swift in Sources */, 43CA932B1CB8CF76000026B5 /* ChangeTimeCarelinkMessageBody.swift in Sources */, @@ -2083,11 +2097,13 @@ 43FF221C1CB9B9DE00024F30 /* NSDateComponents.swift in Sources */, C12198A31C8DFC3600BC374C /* BolusCarelinkMessageBodyTests.swift in Sources */, C121985F1C8DE77D00BC374C /* FindDeviceMessageBodyTests.swift in Sources */, + 541688DB1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift in Sources */, C1EAD6D71C826C43006DBA60 /* NSDataTests.swift in Sources */, 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */, 54BC44AF1DB70C3E00340EED /* TenSomethingGlucoseEventTests.swift in Sources */, C143031A1C9A610B00A40450 /* GetBatteryCarelinkMessageBodyTests.swift in Sources */, 43CA93351CB9727F000026B5 /* ChangeTempBasalCarelinkMessageBodyTests.swift in Sources */, + 54BC44B71DB81B5100340EED /* GetGlucosePageMessageBodyTests.swift in Sources */, C1EAD6E01C82B910006DBA60 /* CRC8Tests.swift in Sources */, 54BC44B51DB7184D00340EED /* NSStringExtensions.swift in Sources */, 54BC44AB1DB7093700340EED /* SensorTimestampGlucoseEventTests.swift in Sources */, From 870ade0fb5fa2e0c98f042e5054d5a362f61413b Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 19 Oct 2016 21:02:22 -0400 Subject: [PATCH 10/18] Add relevant data to dictionary representations --- MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift | 1 + MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift | 1 + MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift | 1 + MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift | 1 + 4 files changed, 4 insertions(+) diff --git a/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift b/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift index 0c358c2a4..5b4d5c799 100644 --- a/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift @@ -33,6 +33,7 @@ public struct CalBGForGHGlucoseEvent : GlucoseEvent { public var dictionaryRepresentation: [String: Any] { return [ "name": "CalBGForGH", + "amount": amount ] } } diff --git a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift index 7bfbfbfb5..7b6c174e7 100644 --- a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift @@ -29,6 +29,7 @@ public struct GlucoseSensorDataGlucoseEvent : RelativeTimestampedGlucoseEvent { public var dictionaryRepresentation: [String: Any] { return [ "name": "GlucoseSensorData", + "sgv": sgv ] } } diff --git a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift index de034d8de..c0a893ad3 100644 --- a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift @@ -33,6 +33,7 @@ public struct SensorCalFactorGlucoseEvent : ReferenceTimestampedGlucoseEvent { public var dictionaryRepresentation: [String: Any] { return [ "name": "SensorCalFactor", + "factor": factor ] } } diff --git a/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift index 5b5f8395f..9ee96814c 100644 --- a/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift @@ -33,6 +33,7 @@ public struct SensorCalGlucoseEvent : RelativeTimestampedGlucoseEvent { public var dictionaryRepresentation: [String: Any] { return [ "name": "SensorCal", + "waiting": waiting ] } } From 9209656df13c2ef8f765daf21e34f221ea99caf8 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 19 Oct 2016 22:24:11 -0400 Subject: [PATCH 11/18] Add handling for currently unknown opcodes --- MinimedKit/GlucoseEventType.swift | 9 +++-- .../GlucoseEvents/UnknownGlucoseEvent.swift | 35 +++++++++++++++++++ MinimedKit/GlucosePage.swift | 5 +-- MinimedKitTests/GlucosePageTests.swift | 24 +++++++++++-- RileyLink.xcodeproj/project.pbxproj | 4 +++ 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift diff --git a/MinimedKit/GlucoseEventType.swift b/MinimedKit/GlucoseEventType.swift index 0ea48dfba..dd002326a 100644 --- a/MinimedKit/GlucoseEventType.swift +++ b/MinimedKit/GlucoseEventType.swift @@ -22,6 +22,7 @@ public enum GlucoseEventType: UInt8 { case sensorCalFactor = 0x0f case tenSomething = 0x10 case nineteenSomething = 0x13 + case unknownDataEvent case glucoseSensorDataEvent public var eventType: GlucoseEvent.Type { @@ -52,8 +53,12 @@ public enum GlucoseEventType: UInt8 { return TenSomethingGlucoseEvent.self case .nineteenSomething: return NineteenSomethingGlucoseEvent.self - case .glucoseSensorDataEvent: - return GlucoseSensorDataGlucoseEvent.self + default: + if rawValue < 20 { + return UnknownGlucoseEvent.self + } else { + return GlucoseSensorDataGlucoseEvent.self + } } } } diff --git a/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift b/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift new file mode 100644 index 000000000..074af38ff --- /dev/null +++ b/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift @@ -0,0 +1,35 @@ +// +// UnknownGlucoseEvent.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public struct UnknownGlucoseEvent : GlucoseEvent { + public let length: Int + public let rawData: Data + public var timestamp: DateComponents + public let op: String + + public init?(availableData: Data, pumpModel: PumpModel) { + length = 1 + + guard length <= availableData.count else { + return nil + } + + rawData = availableData.subdata(in: 0.. [GlucoseEvent] { @@ -77,6 +78,6 @@ public class GlucosePage { offset += event.length } - events = tempEvents + events = tempEvents.reversed() } } diff --git a/MinimedKitTests/GlucosePageTests.swift b/MinimedKitTests/GlucosePageTests.swift index 0cb46cad1..93d75d9be 100644 --- a/MinimedKitTests/GlucosePageTests.swift +++ b/MinimedKitTests/GlucosePageTests.swift @@ -48,6 +48,26 @@ class GlucosePageTests: XCTestCase { } } + func testUnknownRecords() { + let pumpModel = PumpModel.Model551 + do { + //05 is a currently unknown opcode + let pageData = Data(hexadecimalString: "051053b394081053b3940b014a60".leftPadding(toLength: 2048, withPad: "0"))! + let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + let events = page.events + + XCTAssertEqual(events.count, 4) + XCTAssertEqual((events.first as! UnknownGlucoseEvent).op, "05") + + } catch GlucosePage.GlucosePageError.invalidCRC { + XCTFail("page decoding threw invalid crc") + } catch GlucosePage.GlucosePageError.unknownEventType(let eventType) { + XCTFail("unknown event type" + String(eventType)) + } catch { + XCTFail("Unexpected exception...") + } + } + func testRelativeTimestamping() { let pumpModel = PumpModel.Model551 do { @@ -61,7 +81,7 @@ class GlucosePageTests: XCTestCase { // 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.first as! NineteenSomethingGlucoseEvent).timestamp, expectedTimestamp) + XCTAssertEqual((events.last as? NineteenSomethingGlucoseEvent)?.timestamp, expectedTimestamp) } catch GlucosePage.GlucosePageError.invalidCRC { XCTFail("page decoding threw invalid crc") @@ -79,7 +99,7 @@ class GlucosePageTests: XCTestCase { let events = page.events - let expectedEventNames: [String] = ["19-Something", "19-Something", "19-Something", "19-Something", "19-Something", "SensorTimestamp", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "CalBGForGH", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "CalBGForGH", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "CalBGForGH", "CalBGForGH", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "CalBGForGH", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "CalBGForGH", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "DataEnd", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "10-Something", "CalBGForGH"] + let expectedEventNames: [String] = ["CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorTimestamp", "19-Something", "19-Something", "19-Something", "19-Something", "19-Something"] let eventNames: [String] = events.map({ $0.dictionaryRepresentation["name"] as! String }) diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index 225140f71..676f6b8df 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -88,6 +88,7 @@ 43FF221C1CB9B9DE00024F30 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */; }; 541688DB1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */; }; 541688DD1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */; }; + 54A840D11DB85D0600B1F202 /* UnknownGlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54A840D01DB85D0600B1F202 /* UnknownGlucoseEvent.swift */; }; 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44721DB46A5200340EED /* GlucosePageTests.swift */; }; 54BC44751DB46B0A00340EED /* GlucosePage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44741DB46B0A00340EED /* GlucosePage.swift */; }; 54BC44781DB46C7D00340EED /* GlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */; }; @@ -507,6 +508,7 @@ 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponents.swift; sourceTree = ""; }; 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBodyTests.swift; sourceTree = ""; }; 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBody.swift; sourceTree = ""; }; + 54A840D01DB85D0600B1F202 /* UnknownGlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnknownGlucoseEvent.swift; sourceTree = ""; }; 54BC44721DB46A5200340EED /* GlucosePageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePageTests.swift; sourceTree = ""; }; 54BC44741DB46B0A00340EED /* GlucosePage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePage.swift; sourceTree = ""; }; 54BC44771DB46C7D00340EED /* GlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseEvent.swift; sourceTree = ""; }; @@ -954,6 +956,7 @@ 54BC448F1DB47C7400340EED /* SensorTimestampGlucoseEvent.swift */, 54BC44891DB47BA500340EED /* SensorWeakSignalGlucoseEvent.swift */, 54BC447F1DB4762200340EED /* TenSomethingGlucoseEvent.swift */, + 54A840D01DB85D0600B1F202 /* UnknownGlucoseEvent.swift */, ); path = GlucoseEvents; sourceTree = ""; @@ -2038,6 +2041,7 @@ 438D39221D19011700D40CA4 /* PlaceholderPumpEvent.swift in Sources */, C1EAD6B41C826B6D006DBA60 /* MessageBody.swift in Sources */, C1842C001C8FA45100DB42AC /* JournalEntryPumpLowReservoirPumpEvent.swift in Sources */, + 54A840D11DB85D0600B1F202 /* UnknownGlucoseEvent.swift in Sources */, C1842BCF1C8F9E5100DB42AC /* PumpAlarmPumpEvent.swift in Sources */, C1EAD6C61C826B92006DBA60 /* NSData.swift in Sources */, C1842C211C8FA45100DB42AC /* ChangeAlarmNotifyModePumpEvent.swift in Sources */, From 06f29171dfec0d625bc35860a2635c73586c9ae0 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 19 Oct 2016 22:28:04 -0400 Subject: [PATCH 12/18] Add pump ops for fetching recent glucose --- MinimedKit/TimestampedGlucoseEvent.swift | 34 ++++++ RileyLink.xcodeproj/project.pbxproj | 4 + RileyLinkKit/PumpOps.swift | 28 +++++ RileyLinkKit/PumpOpsSynchronous.swift | 102 +++++++++++++++++- .../RileyLinkDeviceTableViewController.swift | 22 ++++ 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 MinimedKit/TimestampedGlucoseEvent.swift diff --git a/MinimedKit/TimestampedGlucoseEvent.swift b/MinimedKit/TimestampedGlucoseEvent.swift new file mode 100644 index 000000000..ca7cfd69e --- /dev/null +++ b/MinimedKit/TimestampedGlucoseEvent.swift @@ -0,0 +1,34 @@ +// +// TimestampedGlucoseEvent.swift +// RileyLink +// +// Created by Timothy Mecklem on 10/19/16. +// Copyright © 2016 Pete Schwamb. All rights reserved. +// + +import Foundation + +public struct TimestampedGlucoseEvent { + public let glucoseEvent: GlucoseEvent + public let date: Date + + public func isMutable(atDate date: Date = Date()) -> Bool { + return false + } + + public init(glucoseEvent: GlucoseEvent, date: Date) { + self.glucoseEvent = glucoseEvent + self.date = date + } +} + + +extension TimestampedGlucoseEvent: DictionaryRepresentable { + public var dictionaryRepresentation: [String : Any] { + var dict = glucoseEvent.dictionaryRepresentation + + dict["timestamp"] = DateFormatter.ISO8601DateFormatter().string(from: date) + + return dict + } +} diff --git a/RileyLink.xcodeproj/project.pbxproj b/RileyLink.xcodeproj/project.pbxproj index 676f6b8df..d65a3fa68 100644 --- a/RileyLink.xcodeproj/project.pbxproj +++ b/RileyLink.xcodeproj/project.pbxproj @@ -88,6 +88,7 @@ 43FF221C1CB9B9DE00024F30 /* NSDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */; }; 541688DB1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */; }; 541688DD1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */; }; + 541688DF1DB82E72005B1891 /* TimestampedGlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 541688DE1DB82E72005B1891 /* TimestampedGlucoseEvent.swift */; }; 54A840D11DB85D0600B1F202 /* UnknownGlucoseEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54A840D01DB85D0600B1F202 /* UnknownGlucoseEvent.swift */; }; 54BC44731DB46A5200340EED /* GlucosePageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44721DB46A5200340EED /* GlucosePageTests.swift */; }; 54BC44751DB46B0A00340EED /* GlucosePage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BC44741DB46B0A00340EED /* GlucosePage.swift */; }; @@ -508,6 +509,7 @@ 43FF221B1CB9B9DE00024F30 /* NSDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponents.swift; sourceTree = ""; }; 541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBodyTests.swift; sourceTree = ""; }; 541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadCurrentGlucosePageMessageBody.swift; sourceTree = ""; }; + 541688DE1DB82E72005B1891 /* TimestampedGlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimestampedGlucoseEvent.swift; sourceTree = ""; }; 54A840D01DB85D0600B1F202 /* UnknownGlucoseEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnknownGlucoseEvent.swift; sourceTree = ""; }; 54BC44721DB46A5200340EED /* GlucosePageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePageTests.swift; sourceTree = ""; }; 54BC44741DB46B0A00340EED /* GlucosePage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucosePage.swift; sourceTree = ""; }; @@ -1003,6 +1005,7 @@ C1274F851D8242BE0002912B /* PumpRegion.swift */, C1EAD6D91C829104006DBA60 /* RFTools.swift */, 43B0ADC11D12454700AAD278 /* TimestampedHistoryEvent.swift */, + 541688DE1DB82E72005B1891 /* TimestampedGlucoseEvent.swift */, ); path = MinimedKit; sourceTree = ""; @@ -2027,6 +2030,7 @@ C1EAD6DE1C82B78C006DBA60 /* CRC8.swift in Sources */, C1EAD6C51C826B92006DBA60 /* Int.swift in Sources */, C1842C021C8FA45100DB42AC /* JournalEntryExerciseMarkerPumpEvent.swift in Sources */, + 541688DF1DB82E72005B1891 /* TimestampedGlucoseEvent.swift in Sources */, C1EAD6CB1C826B92006DBA60 /* MySentryAlertMessageBody.swift in Sources */, 54BC449A1DB47DFE00340EED /* NineteenSomethingGlucoseEvent.swift in Sources */, C15AF2AD1D74929D0031FC9D /* ChangeBolusWizardSetupPumpEvent.swift in Sources */, diff --git a/RileyLinkKit/PumpOps.swift b/RileyLinkKit/PumpOps.swift index 929f71e09..ead224d8e 100644 --- a/RileyLinkKit/PumpOps.swift +++ b/RileyLinkKit/PumpOps.swift @@ -143,6 +143,34 @@ public class PumpOps { } } + /** + Fetches glucose history entries which occurred on or after the specified date. + + History timestamps are reconciled with UTC based on the `timeZone` property of PumpState, as well as recorded clock change events. + + - parameter startDate: The earliest date of events to retrieve + - parameter completion: A closure called after the command is complete. This closure takes a single Result argument: + - success(events): An array of fetched history entries, in ascending order of insertion + - failure(error): An error describing why the command failed + + */ + public func getGlucoseHistoryEvents(since startDate: Date, completion: @escaping (Either<(events: [TimestampedGlucoseEvent], pumpModel: PumpModel), Error>) -> Void) { + device.runSession(withName: "Get glucose history events") { (session) -> Void in + NSLog("Glucose history fetching task started.") + let ops = PumpOpsSynchronous(pumpState: self.pumpState, session: session) + do { + let (events, pumpModel) = try ops.getGlucoseHistoryEvents(since: startDate) + DispatchQueue.main.async { () -> Void in + completion(.success(events: events, pumpModel: pumpModel)) + } + } catch let error { + DispatchQueue.main.async { () -> Void in + completion(.failure(error)) + } + } + } + } + /** Reads the pump's clock diff --git a/RileyLinkKit/PumpOpsSynchronous.swift b/RileyLinkKit/PumpOpsSynchronous.swift index 588add72c..f6832b19e 100644 --- a/RileyLinkKit/PumpOpsSynchronous.swift +++ b/RileyLinkKit/PumpOpsSynchronous.swift @@ -432,7 +432,7 @@ class PumpOpsSynchronous { try wakeup() let pumpModel = try getPumpModel() - + var events = [TimestampedHistoryEvent]() var timeAdjustmentInterval: TimeInterval = 0 @@ -544,6 +544,106 @@ class PumpOpsSynchronous { } return frameData as Data } + + internal func getGlucoseHistoryEvents(since startDate: Date) throws -> ([TimestampedGlucoseEvent], PumpModel) { + try wakeup() + + let pumpModel = try getPumpModel() + + var events = [TimestampedGlucoseEvent]() + + // Going to scan backwards in time through events, so event time should be monotonically decreasing. + let eventTimestampDeltaAllowance = TimeInterval(hours: 9) + + let currentGlucosePage = try readCurrentGlucosePage() + let startPage = Int(currentGlucosePage.pageNum) + let endPage = max(startPage - 1, 0) + + pages: for pageNum in stride(from: startPage, to: endPage, by: -1) { + NSLog("Fetching page %d", pageNum) + let pageData: Data + + do { + pageData = try getGlucosePage(UInt32(pageNum)) + } catch let error as PumpCommsError { + if case .unexpectedResponse(let response, from: _) = error, response.messageType == .emptyHistoryPage { + break pages + } else { + throw error + } + } + + var idx = 0 + let chunkSize = 256; + while idx < pageData.count { + let top = min(idx + chunkSize, pageData.count) + let range = Range(uncheckedBounds: (lower: idx, upper: top)) + NSLog(String(format: "GlucosePage %02d - (bytes %03d-%03d): ", pageNum, idx, top-1) + pageData.subdata(in: range).hexadecimalString) + idx = top + } + + let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + + for event in page.events.reversed() { + var timestamp = event.timestamp + timestamp.timeZone = pump.timeZone + + if let date = timestamp.date { + if date.timeIntervalSince(startDate) < -eventTimestampDeltaAllowance { + NSLog("Found event at (%@) to be more than %@s before startDate(%@)", date as NSDate, String(describing: eventTimestampDeltaAllowance), startDate as NSDate); + break pages + } else { + events.insert(TimestampedGlucoseEvent(glucoseEvent: event, date: date), at: 0) + } + } else { + events.insert(TimestampedGlucoseEvent(glucoseEvent: event, date: Date()), at: 0) + } + } + } + return (events, pumpModel) + } + + private func readCurrentGlucosePage() throws -> ReadCurrentGlucosePageMessageBody { + let readCurrentGlucosePageResponse: ReadCurrentGlucosePageMessageBody = try messageBody(to: .readCurrentGlucosePage) + + return readCurrentGlucosePageResponse + } + + private func getGlucosePage(_ pageNum: UInt32) throws -> Data { + var frameData = Data() + + let msg = makePumpMessage(to: .getGlucosePage, using: GetGlucosePageMessageBody(pageNum: pageNum)) + + let firstResponse = try runCommandWithArguments(msg, responseMessageType: .getGlucosePage) + + var expectedFrameNum = 1 + var curResp = firstResponse.messageBody as! GetGlucosePageMessageBody + + while(expectedFrameNum == curResp.frameNumber) { + frameData.append(curResp.frame) + expectedFrameNum += 1 + let msg = makePumpMessage(to: .pumpAck) + if !curResp.lastFrame { + guard let resp = try? sendAndListen(msg) else { + throw PumpCommsError.rfCommsFailure("Did not receive frame data from pump") + } + guard resp.packetType == .carelink && resp.messageType == .getGlucosePage else { + throw PumpCommsError.rfCommsFailure("Bad packet type or message type. Possible interference.") + } + curResp = resp.messageBody as! GetGlucosePageMessageBody + } else { + let cmd = SendPacketCmd() + cmd.packet = RFPacket(data: msg.txData) + session.doCmd(cmd, withTimeoutMs: expectedMaxBLELatencyMS) + break + } + } + + guard frameData.count == 1024 else { + throw PumpCommsError.rfCommsFailure("Short glucose history page: \(frameData.count) bytes. Expected 1024") + } + return frameData as Data + } internal func readPumpStatus() throws -> PumpStatus { let clockResp: ReadTimeCarelinkMessageBody = try messageBody(to: .readTime) diff --git a/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift b/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift index f9780be78..7fbdbbcf2 100644 --- a/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift +++ b/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift @@ -151,6 +151,7 @@ public class RileyLinkDeviceTableViewController: UITableViewController, TextFiel case changeTime case mySentryPair case dumpHistory + case fetchGlucose case getPumpModel case pressDownButton case readPumpStatus @@ -282,6 +283,9 @@ public class RileyLinkDeviceTableViewController: UITableViewController, TextFiel case .dumpHistory: cell.textLabel?.text = NSLocalizedString("Fetch Recent History", comment: "The title of the command to fetch recent history") + case .fetchGlucose: + cell.textLabel?.text = NSLocalizedString("Fetch Recent Glucose", comment: "The title of the command to fetch recent glucose") + case .getPumpModel: cell.textLabel?.text = NSLocalizedString("Get Pump Model", comment: "The title of the command to get pump model") @@ -452,6 +456,24 @@ public class RileyLinkDeviceTableViewController: UITableViewController, TextFiel } return NSLocalizedString("Fetching history…", comment: "Progress message for fetching pump history.") } + case .fetchGlucose: + vc = CommandResponseViewController { [unowned self] (completionHandler) -> String in + let calendar = Calendar(identifier: Calendar.Identifier.gregorian) + let oneDayAgo = calendar.date(byAdding: DateComponents(day: -1), to: Date()) + self.device.ops?.getGlucoseHistoryEvents(since: oneDayAgo!) { (response) -> Void in + switch response { + case .success(let (events, _)): + var responseText = String(format:"Found %d events since %@", events.count, oneDayAgo! as NSDate) + for event in events { + responseText += String(format:"\nEvent: %@", event.dictionaryRepresentation) + } + completionHandler(responseText) + case .failure(let error): + completionHandler(String(describing: error)) + } + } + return NSLocalizedString("Fetching glucose…", comment: "Progress message for fetching pump glucose.") + } case .getPumpModel: vc = CommandResponseViewController { [unowned self] (completionHandler) -> String in self.device.ops?.getPumpModel({ (response) in From cf0a853baa0e2fa9f14bf7da57966f6e405043df Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 19 Oct 2016 23:30:11 -0400 Subject: [PATCH 13/18] Fix incorrect matching of unknown glucose events Clean up test output a little to help in troubleshooting --- MinimedKit/GlucoseEventType.swift | 8 -------- MinimedKit/GlucosePage.swift | 7 ++++++- MinimedKitTests/GlucosePageTests.swift | 7 ++++--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/MinimedKit/GlucoseEventType.swift b/MinimedKit/GlucoseEventType.swift index dd002326a..e89d74335 100644 --- a/MinimedKit/GlucoseEventType.swift +++ b/MinimedKit/GlucoseEventType.swift @@ -22,8 +22,6 @@ public enum GlucoseEventType: UInt8 { case sensorCalFactor = 0x0f case tenSomething = 0x10 case nineteenSomething = 0x13 - case unknownDataEvent - case glucoseSensorDataEvent public var eventType: GlucoseEvent.Type { switch self { @@ -53,12 +51,6 @@ public enum GlucoseEventType: UInt8 { return TenSomethingGlucoseEvent.self case .nineteenSomething: return NineteenSomethingGlucoseEvent.self - default: - if rawValue < 20 { - return UnknownGlucoseEvent.self - } else { - return GlucoseSensorDataGlucoseEvent.self - } } } } diff --git a/MinimedKit/GlucosePage.swift b/MinimedKit/GlucosePage.swift index a9d798987..f6f806142 100644 --- a/MinimedKit/GlucosePage.swift +++ b/MinimedKit/GlucosePage.swift @@ -34,12 +34,17 @@ public class GlucosePage { func matchEvent(_ offset: Int) -> GlucoseEvent { let remainingData = pageData.subdata(in: offset..= 20 { + return GlucoseSensorDataGlucoseEvent(availableData: remainingData, pumpModel: pumpModel)! + } + return UnknownGlucoseEvent(availableData: remainingData, pumpModel: pumpModel)! } diff --git a/MinimedKitTests/GlucosePageTests.swift b/MinimedKitTests/GlucosePageTests.swift index 93d75d9be..7b4fa1ccf 100644 --- a/MinimedKitTests/GlucosePageTests.swift +++ b/MinimedKitTests/GlucosePageTests.swift @@ -101,9 +101,10 @@ class GlucosePageTests: XCTestCase { let expectedEventNames: [String] = ["CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "19-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "DataEnd", "CalBGForGH", "10-Something", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "CalBGForGH", "SensorCalFactor", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "GlucoseSensorData", "SensorTimestamp", "19-Something", "19-Something", "19-Something", "19-Something", "19-Something"] - let eventNames: [String] = events.map({ $0.dictionaryRepresentation["name"] as! String }) - - XCTAssertEqual(expectedEventNames, eventNames, "Decoded events should match decocare output") + for (index, values) in zip(expectedEventNames, events).enumerated() { + let eventName = values.1.dictionaryRepresentation["name"] as! String + XCTAssertEqual(values.0, eventName, "Decoded events don't match at index: \(index). Event: \(values.1.dictionaryRepresentation)") + } } catch GlucosePage.GlucosePageError.invalidCRC { XCTFail("page decoding threw invalid crc") From 6ce4626322b330d72a696967af8d6cf89316c4a0 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 20 Oct 2016 07:42:06 -0400 Subject: [PATCH 14/18] Remove unnecessary passing of pumpModel --- .../GlucoseEvents/BatteryChangeGlucoseEvent.swift | 2 +- .../GlucoseEvents/CalBGForGHGlucoseEvent.swift | 2 +- .../GlucoseEvents/DataEndGlucoseEvent.swift | 2 +- .../DateTimeChangeGlucoseEvent.swift | 2 +- MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift | 2 +- MinimedKit/GlucoseEvents/GlucoseEvent.swift | 2 +- .../GlucoseSensorDataGlucoseEvent.swift | 2 +- .../NineteenSomethingGlucoseEvent.swift | 2 +- .../SensorCalFactorGlucoseEvent.swift | 2 +- .../GlucoseEvents/SensorCalGlucoseEvent.swift | 2 +- .../GlucoseEvents/SensorStatusGlucoseEvent.swift | 2 +- .../GlucoseEvents/SensorSyncGlucoseEvent.swift | 2 +- .../SensorTimestampGlucoseEvent.swift | 2 +- .../SensorWeakSignalGlucoseEvent.swift | 2 +- .../GlucoseEvents/TenSomethingGlucoseEvent.swift | 2 +- .../GlucoseEvents/UnknownGlucoseEvent.swift | 2 +- MinimedKit/GlucosePage.swift | 8 ++++---- .../BatteryChangeGlucoseEventTests.swift | 3 +-- .../CalBGForGHGlucoseEventTests.swift | 3 +-- .../DateTimeChangeGlucoseEventTests.swift | 3 +-- .../GlucoseSensorDataGlucoseEventTests.swift | 3 +-- .../SensorCalFactorGlucoseEventTests.swift | 3 +-- .../SensorCalGlucoseEventTests.swift | 6 ++---- .../SensorStatusGlucoseEventTests.swift | 3 +-- .../SensorSyncGlucoseEventTests.swift | 3 +-- .../SensorTimestampGlucoseEventTests.swift | 3 +-- .../TenSomethingGlucoseEventTests.swift | 3 +-- MinimedKitTests/GlucosePageTests.swift | 15 +++++---------- RileyLinkKit/PumpOpsSynchronous.swift | 2 +- 29 files changed, 37 insertions(+), 53 deletions(-) diff --git a/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift index 298cc9d67..22af2c62f 100644 --- a/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/BatteryChangeGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct BatteryChangeGlucoseEvent : GlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift b/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift index 5b4d5c799..4383f1162 100644 --- a/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift @@ -14,7 +14,7 @@ public struct CalBGForGHGlucoseEvent : GlucoseEvent { public let timestamp: DateComponents public let amount: Int - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 6 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift b/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift index 30ba19b10..a01214fec 100644 --- a/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/DataEndGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct DataEndGlucoseEvent : RelativeTimestampedGlucoseEvent { public let rawData: Data public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 1 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/DateTimeChangeGlucoseEvent.swift b/MinimedKit/GlucoseEvents/DateTimeChangeGlucoseEvent.swift index b57b61f1d..76747caa2 100644 --- a/MinimedKit/GlucoseEvents/DateTimeChangeGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/DateTimeChangeGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct DateTimeChangeGlucoseEvent : GlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift b/MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift index 9d719c6d4..a0a2ddc63 100644 --- a/MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/Fokko7GlucoseEvent.swift @@ -13,7 +13,7 @@ public struct Fokko7GlucoseEvent : GlucoseEvent { public let rawData: Data public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 2 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/GlucoseEvent.swift b/MinimedKit/GlucoseEvents/GlucoseEvent.swift index 40f01895e..9085402e3 100644 --- a/MinimedKit/GlucoseEvents/GlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/GlucoseEvent.swift @@ -10,7 +10,7 @@ import Foundation public protocol GlucoseEvent : DictionaryRepresentable { - init?(availableData: Data, pumpModel: PumpModel) + init?(availableData: Data) var rawData: Data { get diff --git a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift index 7b6c174e7..664f1ca86 100644 --- a/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/GlucoseSensorDataGlucoseEvent.swift @@ -14,7 +14,7 @@ public struct GlucoseSensorDataGlucoseEvent : RelativeTimestampedGlucoseEvent { public let sgv: Int public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 1 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/NineteenSomethingGlucoseEvent.swift b/MinimedKit/GlucoseEvents/NineteenSomethingGlucoseEvent.swift index b10726641..bcab008d8 100644 --- a/MinimedKit/GlucoseEvents/NineteenSomethingGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/NineteenSomethingGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct NineteenSomethingGlucoseEvent : RelativeTimestampedGlucoseEvent { public let rawData: Data public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 1 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift index c0a893ad3..c9418335f 100644 --- a/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift @@ -14,7 +14,7 @@ public struct SensorCalFactorGlucoseEvent : ReferenceTimestampedGlucoseEvent { public let timestamp: DateComponents public let factor: Float - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 7 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift index 9ee96814c..2c46ecfa4 100644 --- a/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift @@ -14,7 +14,7 @@ public struct SensorCalGlucoseEvent : RelativeTimestampedGlucoseEvent { public let waiting: String public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 2 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift index ee5827ab3..ae0b86d5f 100644 --- a/MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct SensorStatusGlucoseEvent : GlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift index 7a29eba62..84521bc7a 100644 --- a/MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct SensorSyncGlucoseEvent : GlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorTimestampGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorTimestampGlucoseEvent.swift index 3657be1f4..85719e8e9 100644 --- a/MinimedKit/GlucoseEvents/SensorTimestampGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorTimestampGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct SensorTimestampGlucoseEvent : ReferenceTimestampedGlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/SensorWeakSignalGlucoseEvent.swift b/MinimedKit/GlucoseEvents/SensorWeakSignalGlucoseEvent.swift index 2c9690b03..b4d36e21c 100644 --- a/MinimedKit/GlucoseEvents/SensorWeakSignalGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/SensorWeakSignalGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct SensorWeakSignalGlucoseEvent : RelativeTimestampedGlucoseEvent { public let rawData: Data public var timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 1 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/TenSomethingGlucoseEvent.swift b/MinimedKit/GlucoseEvents/TenSomethingGlucoseEvent.swift index edca5a401..c492129d7 100644 --- a/MinimedKit/GlucoseEvents/TenSomethingGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/TenSomethingGlucoseEvent.swift @@ -13,7 +13,7 @@ public struct TenSomethingGlucoseEvent : ReferenceTimestampedGlucoseEvent { public let rawData: Data public let timestamp: DateComponents - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 5 guard length <= availableData.count else { diff --git a/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift b/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift index 074af38ff..f7088db47 100644 --- a/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift +++ b/MinimedKit/GlucoseEvents/UnknownGlucoseEvent.swift @@ -14,7 +14,7 @@ public struct UnknownGlucoseEvent : GlucoseEvent { public var timestamp: DateComponents public let op: String - public init?(availableData: Data, pumpModel: PumpModel) { + public init?(availableData: Data) { length = 1 guard length <= availableData.count else { diff --git a/MinimedKit/GlucosePage.swift b/MinimedKit/GlucosePage.swift index f6f806142..68132b014 100644 --- a/MinimedKit/GlucosePage.swift +++ b/MinimedKit/GlucosePage.swift @@ -17,7 +17,7 @@ public class GlucosePage { public let events: [GlucoseEvent] - public init(pageData: Data, pumpModel: PumpModel) throws { + public init(pageData: Data) throws { guard checkCRC16(pageData) else { events = [GlucoseEvent]() @@ -36,16 +36,16 @@ public class GlucosePage { let remainingData = pageData.subdata(in: offset..= 20 { - return GlucoseSensorDataGlucoseEvent(availableData: remainingData, pumpModel: pumpModel)! + return GlucoseSensorDataGlucoseEvent(availableData: remainingData)! } - return UnknownGlucoseEvent(availableData: remainingData, pumpModel: pumpModel)! + return UnknownGlucoseEvent(availableData: remainingData)! } func addTimestampsToEvents(startTimestamp: DateComponents, eventsNeedingTimestamp: [RelativeTimestampedGlucoseEvent]) -> [GlucoseEvent] { diff --git a/MinimedKitTests/GlucoseEvents/BatteryChangeGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/BatteryChangeGlucoseEventTests.swift index 86116b0da..e7af514b8 100644 --- a/MinimedKitTests/GlucoseEvents/BatteryChangeGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/BatteryChangeGlucoseEventTests.swift @@ -21,9 +21,8 @@ class BatteryChangeGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0a0bae0a0e")! - let subject = BatteryChangeGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = BatteryChangeGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2014, month: 2, day: 10, hour: 11, minute: 46) diff --git a/MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift index 2adc1ccba..3301e3f75 100644 --- a/MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift @@ -22,9 +22,8 @@ class CalBGForGHGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0e4f5b138fa0")! - let subject = CalBGForGHGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = CalBGForGHGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2015, month: 5, day: 19, hour: 15, minute: 27) diff --git a/MinimedKitTests/GlucoseEvents/DateTimeChangeGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/DateTimeChangeGlucoseEventTests.swift index e5b12b38f..ae3b72ea3 100644 --- a/MinimedKitTests/GlucoseEvents/DateTimeChangeGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/DateTimeChangeGlucoseEventTests.swift @@ -22,9 +22,8 @@ class DateTimeChangeGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0c0ad23e0e")! - let subject = DateTimeChangeGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = DateTimeChangeGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2014, month: 3, day: 30, hour: 10, minute: 18) diff --git a/MinimedKitTests/GlucoseEvents/GlucoseSensorDataGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/GlucoseSensorDataGlucoseEventTests.swift index d5983da2a..89654aa08 100644 --- a/MinimedKitTests/GlucoseEvents/GlucoseSensorDataGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/GlucoseSensorDataGlucoseEventTests.swift @@ -22,9 +22,8 @@ class GlucoseSensorDataGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "35")! - let subject = GlucoseSensorDataGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = GlucoseSensorDataGlucoseEvent(availableData: rawData)! XCTAssertEqual(subject.sgv, 106) } diff --git a/MinimedKitTests/GlucoseEvents/SensorCalFactorGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/SensorCalFactorGlucoseEventTests.swift index c4be3ff32..e5c45f70a 100644 --- a/MinimedKitTests/GlucoseEvents/SensorCalFactorGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/SensorCalFactorGlucoseEventTests.swift @@ -22,9 +22,8 @@ class SensorCalFactorGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0f4f67130f128c")! - let subject = SensorCalFactorGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorCalFactorGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2015, month: 5, day: 19, hour: 15, minute: 39) diff --git a/MinimedKitTests/GlucoseEvents/SensorCalGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/SensorCalGlucoseEventTests.swift index c6aba4c1a..2b14f66d1 100644 --- a/MinimedKitTests/GlucoseEvents/SensorCalGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/SensorCalGlucoseEventTests.swift @@ -22,17 +22,15 @@ class SensorCalGlucoseEventTests: XCTestCase { } func testDecodingMeterBgNow() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0300")! - let subject = SensorCalGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorCalGlucoseEvent(availableData: rawData)! XCTAssertEqual(subject.waiting, "meter_bg_now") } func testDecodingWaiting() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0301")! - let subject = SensorCalGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorCalGlucoseEvent(availableData: rawData)! XCTAssertEqual(subject.waiting, "waiting") } diff --git a/MinimedKitTests/GlucoseEvents/SensorStatusGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/SensorStatusGlucoseEventTests.swift index 985585a59..482863555 100644 --- a/MinimedKitTests/GlucoseEvents/SensorStatusGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/SensorStatusGlucoseEventTests.swift @@ -22,9 +22,8 @@ class SensorStatusGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0b0baf0a0e")! - let subject = SensorStatusGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorStatusGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2014, month: 2, day: 10, hour: 11, minute: 47) diff --git a/MinimedKitTests/GlucoseEvents/SensorSyncGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/SensorSyncGlucoseEventTests.swift index c58443010..8d2a49f56 100644 --- a/MinimedKitTests/GlucoseEvents/SensorSyncGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/SensorSyncGlucoseEventTests.swift @@ -27,9 +27,8 @@ class SensorSyncGlucoseEventTests: XCTestCase { } func testPerformanceExample() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "0d4d44330f")! - let subject = SensorSyncGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorSyncGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2015, month: 5, day: 19, hour: 13, minute: 04) diff --git a/MinimedKitTests/GlucoseEvents/SensorTimestampGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/SensorTimestampGlucoseEventTests.swift index 2755f85c5..e899bfb04 100644 --- a/MinimedKitTests/GlucoseEvents/SensorTimestampGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/SensorTimestampGlucoseEventTests.swift @@ -22,9 +22,8 @@ class SensorTimestampGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "088d9b5d0c")! - let subject = SensorTimestampGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = SensorTimestampGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2012, month: 10, day: 29, hour: 13, minute: 27) diff --git a/MinimedKitTests/GlucoseEvents/TenSomethingGlucoseEventTests.swift b/MinimedKitTests/GlucoseEvents/TenSomethingGlucoseEventTests.swift index ae590ed91..6a98b2dec 100644 --- a/MinimedKitTests/GlucoseEvents/TenSomethingGlucoseEventTests.swift +++ b/MinimedKitTests/GlucoseEvents/TenSomethingGlucoseEventTests.swift @@ -22,9 +22,8 @@ class TenSomethingGlucoseEventTests: XCTestCase { } func testDecoding() { - let pumpModel = PumpModel.Model551 let rawData = Data(hexadecimalString: "100bb40a0e")! - let subject = TenSomethingGlucoseEvent(availableData: rawData, pumpModel: pumpModel)! + let subject = TenSomethingGlucoseEvent(availableData: rawData)! let expectedTimestamp = DateComponents(calendar: Calendar.current, year: 2014, month: 2, day: 10, hour: 11, minute: 52) diff --git a/MinimedKitTests/GlucosePageTests.swift b/MinimedKitTests/GlucosePageTests.swift index 7b4fa1ccf..2a7faaa0d 100644 --- a/MinimedKitTests/GlucosePageTests.swift +++ b/MinimedKitTests/GlucosePageTests.swift @@ -22,9 +22,8 @@ class GlucosePageTests: XCTestCase { } func testGlucosePageCRC() { - let pumpModel = PumpModel.Model551 do { - let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C95")!, pumpModel: pumpModel) + let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C95")!) } catch GlucosePage.GlucosePageError.invalidCRC { XCTFail("page decoding threw invalid crc") } catch GlucosePage.GlucosePageError.unknownEventType(let eventType) { @@ -35,9 +34,8 @@ class GlucosePageTests: XCTestCase { } func testGlucosePageInvalidCRC() { - let pumpModel = PumpModel.Model551 do { - let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C94")!, pumpModel: pumpModel) + let _ = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C94")!) XCTFail("Should have thrown InvalidCRC") } catch GlucosePage.GlucosePageError.invalidCRC { // Happy path @@ -49,11 +47,10 @@ class GlucosePageTests: XCTestCase { } func testUnknownRecords() { - let pumpModel = PumpModel.Model551 do { //05 is a currently unknown opcode let pageData = Data(hexadecimalString: "051053b394081053b3940b014a60".leftPadding(toLength: 2048, withPad: "0"))! - let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + let page = try GlucosePage(pageData: pageData) let events = page.events XCTAssertEqual(events.count, 4) @@ -69,10 +66,9 @@ class GlucosePageTests: XCTestCase { } func testRelativeTimestamping() { - let pumpModel = PumpModel.Model551 do { let pageData = Data(hexadecimalString: "1028B61408131313131341BB".leftPadding(toLength: 2048, withPad: "0"))! - let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + let page = try GlucosePage(pageData: pageData) let events = page.events // a sensor timestamp followed by 5 "19-Something" relative timestamp records @@ -93,9 +89,8 @@ class GlucosePageTests: XCTestCase { } func testGlucosePageEventDecoding() { - let pumpModel = PumpModel.Model551 do { - let page = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C95")!, pumpModel: pumpModel) + let page = try GlucosePage(pageData: Data(hexadecimalString: "201E1D1C1A1817161718191B1D1B1C1F212527282A2E3235383C3FCD9086850E0E0000011006850E1043484857EB181006940E0F595E00000110069F0E106670757374767A7D7F8181817D7813726D6663605E5B554F4C4E4F4B484946423E3B38353234332D2B2B2B2C2E302F2F312F2D2D2E2D2C2D2D2A2B292700000110069F1310242323272B2C3235383C00000110069514103E3E3F404141444A4945413E3D3D3A35302C2823201D1B1C1D1A1A1B1C1E2123272B2F3231323235383B3F43484949474542779007A0000E41423D6C171007AD000F3C3D3D3B3C3E3F41474F545A626A6E700000011007860210717271727476797A7A7B7C7D7C7B777472706E6D6C6B6A6763605C5A6064636160605F5D5C5C5B5956545250555A585656555554565654504D4C4A484E515455555452504F4D4D504E4D4E505050504F4E4D4C4C4F50510000011007980910539290079C090E54554BC4141007A8090F4A4A49494C4C4D50515253524F53524F4D4C7590078E0B0E7590878E0B0E4A4947433BD0121007A30B0F373333312F2B26242423252526292A2A2A0000011007810D102A2A2828282B3337383A3F44474746484B4F52504B4846434343413D393500000110079F0F10312F2D2B27211E1D3F90078E100E1C1A191214100798100F181817181F20242A2E33383E41464B4B4F56595E000001100788121065686667660000011007A3121066686B6D6F727375777774707273706C6764615F5A575553453D39322C251F1A1C191614171D2124282C2E31353A3F4144484A4B4C4D50555B000001100790171061656A6665656768C99007B9170E0000011007B917106869659413100889000F625F5D5D5F5D5A56524B4E4E4C4B4847494B494A4D5051514F4E4C4E4E4D4B4A494A4B4B4B4B47474D4E4E4E4E4E4C4B4A46434B4D4C4B49490000011008B804104B4B4B4D515456565655545455595A5800000110088F0610595A5A595A595755555858575655545453525152534E4A474445484B4E4F13505251135113515051525154AF9088A6090E0000011008A6091052505594141008B3090F55565B5A58534F4B49134037132F28132320131A13181514151615171919191A1A1B1A1B1C1D202325242221225890088B0D0E00000110088C0D1023252AEC151008990D0F2B2B2E31393F3A3C41484E555D00000110089F0E1063696E73767A7C7D7F7E7F8085878689E59008B50F0E0000011008B50F10878979CD13100885100F76737272706D0000011008A71010696461605D5A57555352520000011008A11110504C4947484743434278900893120E0000011008941210423F3A3837D6131008A9120F33322F2E2F31322F2C2C2B2828292A2827243A90088E140E211C19181809141008A4140F191A19191028B6140813131313133C95")!) let events = page.events diff --git a/RileyLinkKit/PumpOpsSynchronous.swift b/RileyLinkKit/PumpOpsSynchronous.swift index f6832b19e..7952e5293 100644 --- a/RileyLinkKit/PumpOpsSynchronous.swift +++ b/RileyLinkKit/PumpOpsSynchronous.swift @@ -582,7 +582,7 @@ class PumpOpsSynchronous { idx = top } - let page = try GlucosePage(pageData: pageData, pumpModel: pumpModel) + let page = try GlucosePage(pageData: pageData) for event in page.events.reversed() { var timestamp = event.timestamp From f266ebbf41e9888470d9db92285018331b2afda4 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 20 Oct 2016 08:03:23 -0400 Subject: [PATCH 15/18] Remove timestamp delta allowance and use reference timestamps in check Glucose events can be out of order (and are frequently). Checking for only reference timestamps should catch all relevant events. --- RileyLinkKit/PumpOpsSynchronous.swift | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/RileyLinkKit/PumpOpsSynchronous.swift b/RileyLinkKit/PumpOpsSynchronous.swift index 7952e5293..04ff14d79 100644 --- a/RileyLinkKit/PumpOpsSynchronous.swift +++ b/RileyLinkKit/PumpOpsSynchronous.swift @@ -552,9 +552,6 @@ class PumpOpsSynchronous { var events = [TimestampedGlucoseEvent]() - // Going to scan backwards in time through events, so event time should be monotonically decreasing. - let eventTimestampDeltaAllowance = TimeInterval(hours: 9) - let currentGlucosePage = try readCurrentGlucosePage() let startPage = Int(currentGlucosePage.pageNum) let endPage = max(startPage - 1, 0) @@ -589,8 +586,8 @@ class PumpOpsSynchronous { timestamp.timeZone = pump.timeZone if let date = timestamp.date { - if date.timeIntervalSince(startDate) < -eventTimestampDeltaAllowance { - NSLog("Found event at (%@) to be more than %@s before startDate(%@)", date as NSDate, String(describing: eventTimestampDeltaAllowance), startDate as NSDate); + if date < startDate && event is ReferenceTimestampedGlucoseEvent { + NSLog("Found reference event at (%@) to be before startDate(%@)", date as NSDate, startDate as NSDate); break pages } else { events.insert(TimestampedGlucoseEvent(glucoseEvent: event, date: date), at: 0) From 8f6356f4ce8742eac829dd6a589f4c8491da24a2 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 20 Oct 2016 08:04:55 -0400 Subject: [PATCH 16/18] Skip UnknownGlucoseEvents rather than try to timestamp them --- RileyLinkKit/PumpOpsSynchronous.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/RileyLinkKit/PumpOpsSynchronous.swift b/RileyLinkKit/PumpOpsSynchronous.swift index 04ff14d79..dcc51f279 100644 --- a/RileyLinkKit/PumpOpsSynchronous.swift +++ b/RileyLinkKit/PumpOpsSynchronous.swift @@ -585,6 +585,10 @@ class PumpOpsSynchronous { var timestamp = event.timestamp timestamp.timeZone = pump.timeZone + if event is UnknownGlucoseEvent { + continue pages + } + if let date = timestamp.date { if date < startDate && event is ReferenceTimestampedGlucoseEvent { NSLog("Found reference event at (%@) to be before startDate(%@)", date as NSDate, startDate as NSDate); @@ -592,8 +596,6 @@ class PumpOpsSynchronous { } else { events.insert(TimestampedGlucoseEvent(glucoseEvent: event, date: date), at: 0) } - } else { - events.insert(TimestampedGlucoseEvent(glucoseEvent: event, date: Date()), at: 0) } } } From 01cdf70a10e8380ada0f3ecb4b5fd8fbfff7a202 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 20 Oct 2016 13:27:45 -0400 Subject: [PATCH 17/18] Use reversed() instead of custom reverseBytes() --- MinimedKit/Extensions/NSData.swift | 9 --------- MinimedKit/GlucosePage.swift | 2 +- MinimedKitTests/NSDataTests.swift | 7 ------- 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/MinimedKit/Extensions/NSData.swift b/MinimedKit/Extensions/NSData.swift index b91a95138..3699b4909 100644 --- a/MinimedKit/Extensions/NSData.swift +++ b/MinimedKit/Extensions/NSData.swift @@ -71,15 +71,6 @@ extension Data { */ } -extension Data { - func reverseBytes() -> Data { - let byteArray = self.withUnsafeBytes { - [UInt8](UnsafeBufferPointer(start: $0, count: self.count)) - } - return Data(bytes: byteArray.reversed()) - } -} - extension Data { init?(hexadecimalString: String) { guard let chars = hexadecimalString.cString(using: String.Encoding.utf8) else { diff --git a/MinimedKit/GlucosePage.swift b/MinimedKit/GlucosePage.swift index 68132b014..d364be501 100644 --- a/MinimedKit/GlucosePage.swift +++ b/MinimedKit/GlucosePage.swift @@ -25,7 +25,7 @@ public class GlucosePage { } //glucose page parsing happens in reverse byte order - let pageData = pageData.subdata(in: 0..<1022).reverseBytes() + let pageData = Data(pageData.subdata(in: 0..<1022).reversed()) var offset = 0 let length = pageData.count diff --git a/MinimedKitTests/NSDataTests.swift b/MinimedKitTests/NSDataTests.swift index 859f5e02b..08a3ff2d2 100644 --- a/MinimedKitTests/NSDataTests.swift +++ b/MinimedKitTests/NSDataTests.swift @@ -49,11 +49,4 @@ class NSDataTests: XCTestCase { data?.copyBytes(to: &bytes, count: 4) XCTAssertEqual([0xa2, 0x59, 0x40, 0x40], bytes) } - - func testReverseBytes() { - let data = Data(hexadecimalString: "as594041") - let reversedData = Data(hexadecimalString: "414059as") - - XCTAssertEqual(reversedData, data?.reverseBytes()) - } } From 258f2db1096b921ccd830a577c08dacb32ceeb6d Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Thu, 20 Oct 2016 22:20:28 -0400 Subject: [PATCH 18/18] Remove pumpModel from PumpOps --- RileyLinkKit/PumpOps.swift | 6 +++--- RileyLinkKit/PumpOpsSynchronous.swift | 6 ++---- RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/RileyLinkKit/PumpOps.swift b/RileyLinkKit/PumpOps.swift index ead224d8e..9510dac8c 100644 --- a/RileyLinkKit/PumpOps.swift +++ b/RileyLinkKit/PumpOps.swift @@ -154,14 +154,14 @@ public class PumpOps { - failure(error): An error describing why the command failed */ - public func getGlucoseHistoryEvents(since startDate: Date, completion: @escaping (Either<(events: [TimestampedGlucoseEvent], pumpModel: PumpModel), Error>) -> Void) { + public func getGlucoseHistoryEvents(since startDate: Date, completion: @escaping (Either<[TimestampedGlucoseEvent], Error>) -> Void) { device.runSession(withName: "Get glucose history events") { (session) -> Void in NSLog("Glucose history fetching task started.") let ops = PumpOpsSynchronous(pumpState: self.pumpState, session: session) do { - let (events, pumpModel) = try ops.getGlucoseHistoryEvents(since: startDate) + let events = try ops.getGlucoseHistoryEvents(since: startDate) DispatchQueue.main.async { () -> Void in - completion(.success(events: events, pumpModel: pumpModel)) + completion(.success(events)) } } catch let error { DispatchQueue.main.async { () -> Void in diff --git a/RileyLinkKit/PumpOpsSynchronous.swift b/RileyLinkKit/PumpOpsSynchronous.swift index dcc51f279..2ecdcfeab 100644 --- a/RileyLinkKit/PumpOpsSynchronous.swift +++ b/RileyLinkKit/PumpOpsSynchronous.swift @@ -545,11 +545,9 @@ class PumpOpsSynchronous { return frameData as Data } - internal func getGlucoseHistoryEvents(since startDate: Date) throws -> ([TimestampedGlucoseEvent], PumpModel) { + internal func getGlucoseHistoryEvents(since startDate: Date) throws -> [TimestampedGlucoseEvent] { try wakeup() - let pumpModel = try getPumpModel() - var events = [TimestampedGlucoseEvent]() let currentGlucosePage = try readCurrentGlucosePage() @@ -599,7 +597,7 @@ class PumpOpsSynchronous { } } } - return (events, pumpModel) + return events } private func readCurrentGlucosePage() throws -> ReadCurrentGlucosePageMessageBody { diff --git a/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift b/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift index 7fbdbbcf2..f410d36d5 100644 --- a/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift +++ b/RileyLinkKit/UI/RileyLinkDeviceTableViewController.swift @@ -462,7 +462,7 @@ public class RileyLinkDeviceTableViewController: UITableViewController, TextFiel let oneDayAgo = calendar.date(byAdding: DateComponents(day: -1), to: Date()) self.device.ops?.getGlucoseHistoryEvents(since: oneDayAgo!) { (response) -> Void in switch response { - case .success(let (events, _)): + case .success(let events): var responseText = String(format:"Found %d events since %@", events.count, oneDayAgo! as NSDate) for event in events { responseText += String(format:"\nEvent: %@", event.dictionaryRepresentation)