Skip to content

Commit

Permalink
Merge pull request #166 from ps2/dev
Browse files Browse the repository at this point in the history
Release 0.7.0
  • Loading branch information
ps2 committed Jul 20, 2016
2 parents eb054a3 + 7372237 commit 0c80001
Show file tree
Hide file tree
Showing 54 changed files with 1,656 additions and 183 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "loudnate/Crypto" "e0ef5b498f2c373d676135dabf5d1803b8558509"
github "loudnate/Crypto" "13fee45175b88629aeabe60b4b4fc3daf86fa0a3"
10 changes: 5 additions & 5 deletions MinimedKit/Extensions/NSDateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ extension NSDateComponents {
convenience init(mySentryBytes: [UInt8]) {
self.init()

hour = Int(mySentryBytes[0])
minute = Int(mySentryBytes[1])
second = Int(mySentryBytes[2])
hour = Int(mySentryBytes[0] & 0b00011111)
minute = Int(mySentryBytes[1] & 0b00111111)
second = Int(mySentryBytes[2] & 0b00111111)
year = Int(mySentryBytes[3]) + 2000
month = Int(mySentryBytes[4])
day = Int(mySentryBytes[5])
month = Int(mySentryBytes[4] & 0b00001111)
day = Int(mySentryBytes[5] & 0b00011111)

calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
}
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.6.0</string>
<string>0.7.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
9 changes: 9 additions & 0 deletions MinimedKit/Messages/MySentryPumpStatusMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public enum SensorReading {
}
}

public enum ClockType {
case TwentyFourHour
case TwelveHour
}


/**
Describes a status message sent periodically from the pump to any paired MySentry devices
Expand Down Expand Up @@ -113,6 +118,7 @@ public struct MySentryPumpStatusMessageBody: MessageBody, DictionaryRepresentabl
public let previousGlucose: SensorReading
public let sensorAgeHours: Int
public let sensorRemainingHours: Int
public let clockType: ClockType

public let nextSensorCalibrationDateComponents: NSDateComponents?

Expand All @@ -129,6 +135,9 @@ public struct MySentryPumpStatusMessageBody: MessageBody, DictionaryRepresentabl

let pumpDateComponents = NSDateComponents(mySentryBytes: rxData[2...7])

let hourByte: UInt8 = rxData[2]
clockType = ((hourByte & 0b10000000) > 0) ? .TwentyFourHour : .TwelveHour

guard let calendar = pumpDateComponents.calendar where pumpDateComponents.isValidDateInCalendar(calendar) else {
return nil
}
Expand Down
9 changes: 8 additions & 1 deletion MinimedKit/PumpEventType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public enum PumpEventType: UInt8 {
case Questionable3b = 0x3b
case ChangeParadigmLinkID = 0x3c
case BGReceived = 0x3f
case JournalEntryMealMarker = 0x40
case JournalEntryExerciseMarker = 0x41
case JournalEntryInsulinMarker = 0x42
case JournalEntryOtherMarker = 0x43
case ChangeSensorSetup2 = 0x50
case ChangeSensorRateOfChangeAlertSetup = 0x56
case ChangeBolusScrollStepSize = 0x57
Expand Down Expand Up @@ -124,6 +127,10 @@ public enum PumpEventType: UInt8 {
return BGReceivedPumpEvent.self
case .JournalEntryExerciseMarker:
return JournalEntryExerciseMarkerPumpEvent.self
case .JournalEntryInsulinMarker:
return JournalEntryInsulinMarkerPumpEvent.self
case .JournalEntryMealMarker:
return JournalEntryMealMarkerPumpEvent.self
case .ChangeSensorSetup2:
return ChangeSensorSetup2PumpEvent.self
case .ChangeSensorRateOfChangeAlertSetup:
Expand Down Expand Up @@ -181,7 +188,7 @@ public enum PumpEventType: UInt8 {
case .SelectBasalProfile:
return SelectBasalProfilePumpEvent.self
default:
return UnknownPumpEvent.self
return PlaceholderPumpEvent.self
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEvents/AlarmSensorPumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct AlarmSensorPumpEvent: TimestampedPumpEvent {

rawData = availableData[0..<length]

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)
timestamp = NSDateComponents(pumpEventData: availableData, offset: 3)
}

public var dictionaryRepresentation: [String: AnyObject] {
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEvents/CalBGForPHPumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct CalBGForPHPumpEvent: TimestampedPumpEvent {
}

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)
amount = ((d(6) & 0b10000000) << 1) + d(1)
amount = ((d(4) & 0b10000000) << 2) + ((d(6) & 0b10000000) << 1) + d(1)
}

public var dictionaryRepresentation: [String: AnyObject] {
Expand Down
10 changes: 9 additions & 1 deletion MinimedKit/PumpEvents/ChangeTimeFormatPumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public struct ChangeTimeFormatPumpEvent: TimestampedPumpEvent {
public let length: Int
public let rawData: NSData
public let timestamp: NSDateComponents

public let timeFormat: String

public init?(availableData: NSData, pumpModel: PumpModel) {
length = 7

Expand All @@ -23,11 +24,18 @@ public struct ChangeTimeFormatPumpEvent: TimestampedPumpEvent {
rawData = availableData[0..<length]

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)

func d(idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
}

timeFormat = d(1) == 1 ? "24hr" : "am_pm"
}

public var dictionaryRepresentation: [String: AnyObject] {
return [
"_type": "ChangeTimeFormat",
"timeFormat": timeFormat,
]
}
}
39 changes: 39 additions & 0 deletions MinimedKit/PumpEvents/JournalEntryInsulinMarkerPumpEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// JournalEntryInsulinMarkerPumpEvent.swift
// RileyLink
//
// Created by Pete Schwamb on 7/16/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public struct JournalEntryInsulinMarkerPumpEvent: TimestampedPumpEvent {
public let length: Int
public let rawData: NSData
public let timestamp: NSDateComponents
public let amount: Double

public init?(availableData: NSData, pumpModel: PumpModel) {
length = 8

guard length <= availableData.length else {
return nil
}

rawData = availableData[0..<length]

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)

let lowBits = rawData[1] as UInt8
let highBits = rawData[4] as UInt8
amount = Double((Int(highBits & 0b1100000) << 3) + Int(lowBits)) / 10.0
}

public var dictionaryRepresentation: [String: AnyObject] {
return [
"_type": "JournalEntryInsulinMarker",
"amount": amount,
]
}
}
54 changes: 54 additions & 0 deletions MinimedKit/PumpEvents/JournalEntryMealMarkerPumpEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// JournalEntryMealMarkerPumpEvent.swift
// RileyLink
//
// Created by Pete Schwamb on 7/14/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public struct JournalEntryMealMarkerPumpEvent: TimestampedPumpEvent {
public let length: Int
public let rawData: NSData
public let timestamp: NSDateComponents
public let carbohydrates: Double
public let carbUnits: CarbUnits

public enum CarbUnits: String {
case Exchanges
case Grams
}

public init?(availableData: NSData, pumpModel: PumpModel) {
length = 9

let useExchangesBit = ((availableData[8] as UInt8) >> 1) & 0b1
carbUnits = (useExchangesBit != 0) ? .Exchanges : .Grams

let carbHighBit = (availableData[8] as UInt8) & 0b1
let carbLowBits = availableData[7] as UInt8

if carbUnits == .Exchanges {
carbohydrates = Double(carbLowBits) / 10.0
} else {
carbohydrates = Double(Int(carbHighBit) << 8 + Int(carbLowBits))
}

guard length <= availableData.length else {
return nil
}

rawData = availableData[0..<length]

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)
}

public var dictionaryRepresentation: [String: AnyObject] {
return [
"_type": "JournalEntryMealMarker",
"carbohydrates": carbohydrates,
"carbUnits": carbUnits.rawValue,
]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// UnknownPumpEvent.swift
// PlaceholderPumpEvent.swift
// RileyLink
//
// Created by Nate Racklyeft on 6/20/16.
Expand All @@ -9,7 +9,7 @@
import Foundation


public struct UnknownPumpEvent: TimestampedPumpEvent {
public struct PlaceholderPumpEvent: TimestampedPumpEvent {
public let length: Int
public let rawData: NSData
public let timestamp: NSDateComponents
Expand All @@ -20,15 +20,21 @@ public struct UnknownPumpEvent: TimestampedPumpEvent {
guard length <= availableData.length else {
return nil
}

rawData = availableData[0..<length]

timestamp = NSDateComponents(pumpEventData: availableData, offset: 2)
}

public var dictionaryRepresentation: [String: AnyObject] {
let name: String
if let type = PumpEventType(rawValue: rawData[0] as UInt8) {
name = String(type).componentsSeparatedByString(".").last!
} else {
name = "UnknownPumpEvent(\(rawData[0] as UInt8))"
}

return [
"_type": "UnknownPumpEvent(\(rawData[0] as UInt8))",
"_type": name,
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEvents/PumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
public protocol PumpEvent : DictionaryRepresentable {

init?(availableData: NSData, pumpModel: PumpModel)

var rawData: NSData {
get
}
Expand Down
19 changes: 17 additions & 2 deletions MinimedKit/PumpModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public enum PumpModel: String {
case Model751 = "751"
case Model554 = "554"
case Model754 = "754"

var generation: Int {

private var size: Int {
return Int(rawValue)! / 100
}

private var generation: Int {
return Int(rawValue)! % 100
}

Expand All @@ -46,6 +50,17 @@ public enum PumpModel: String {
public var strokesPerUnit: Int {
return (generation >= 23) ? 40 : 10
}

var reservoirCapacity: Int {
switch size {
case 5:
return 176
case 7:
return 300
default:
fatalError("Unknown reservoir capacity for PumpModel.\(self)")
}
}
}


Expand Down
Loading

0 comments on commit 0c80001

Please sign in to comment.