Skip to content

Commit

Permalink
Merge pull request #186 from ps2/dev
Browse files Browse the repository at this point in the history
v0.8.0
  • Loading branch information
ps2 authored Aug 4, 2016
2 parents 45b422f + 3fefcc9 commit 4c13ed4
Show file tree
Hide file tree
Showing 42 changed files with 1,007 additions and 207 deletions.
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.7.1</string>
<string>0.8.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
3 changes: 3 additions & 0 deletions MinimedKit/MessageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum MessageType: UInt8 {
case GetPumpModel = 0x8d
case ReadTempBasal = 0x98
case ReadSettings = 0xc0
case ReadPumpStatus = 0xce

var bodyType: MessageBody.Type {
switch self {
Expand Down Expand Up @@ -59,6 +60,8 @@ public enum MessageType: UInt8 {
return GetBatteryCarelinkMessageBody.self
case .ReadRemainingInsulin:
return ReadRemainingInsulinMessageBody.self
case .ReadPumpStatus:
return ReadPumpStatusMessageBody.self
default:
return UnknownMessageBody.self
}
Expand Down
28 changes: 20 additions & 8 deletions MinimedKit/Messages/GetBatteryCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,37 @@

import Foundation

public enum BatteryStatus {
case Low
case Normal
case Unknown(rawVal: UInt8)

init(statusByte: UInt8) {
switch statusByte {
case 1:
self = .Low
case 0:
self = .Normal
default:
self = .Unknown(rawVal: statusByte)
}
}
}

public class GetBatteryCarelinkMessageBody: CarelinkLongMessageBody {
public let status: String
public let status: BatteryStatus
public let volts: Double

public required init?(rxData: NSData) {
guard rxData.length == self.dynamicType.length else {
status = ""
volts = 0
status = .Unknown(rawVal: 0)
super.init(rxData: rxData)
return nil
}

volts = Double(Int(rxData[2] as UInt8) << 8 + Int(rxData[3] as UInt8)) / 100.0

if rxData[1] as UInt8 > 0 {
status = "Low"
} else {
status = "Normal"
}
status = BatteryStatus(statusByte: rxData[1] as UInt8)

super.init(rxData: rxData)
}
Expand Down
27 changes: 27 additions & 0 deletions MinimedKit/Messages/ReadPumpStatusMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ReadPumpStatusMessageBody.swift
// RileyLink
//
// Created by Pete Schwamb on 7/31/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public class ReadPumpStatusMessageBody: CarelinkLongMessageBody {

public let bolusing: Bool
public let suspended: Bool

public required init?(rxData: NSData) {
guard rxData.length == self.dynamicType.length else {
return nil
}

bolusing = (rxData[2] as UInt8) > 0
suspended = (rxData[3] as UInt8) > 0

super.init(rxData: rxData)
}

}
2 changes: 1 addition & 1 deletion MinimedKit/Messages/ReadRemainingInsulinMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import UIKit
import Foundation

public class ReadRemainingInsulinMessageBody: CarelinkLongMessageBody {

Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEventType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public enum PumpEventType: UInt8 {
case DeleteOtherDeviceID = 0x82
case ChangeCaptureEventEnable = 0x83

var eventType: PumpEvent.Type {
public var eventType: PumpEvent.Type {
switch self {
case .BolusNormal:
return BolusNormalPumpEvent.self
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEvents/ChangeTimePumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct ChangeTimePumpEvent: TimestampedPumpEvent {
oldTimestamp = NSDateComponents(pumpEventData: availableData, offset: 2)
timestamp = NSDateComponents(pumpEventData: availableData, offset: 9)
}

public var dictionaryRepresentation: [String: AnyObject] {
return [
"_type": "ChangeTime",
Expand Down
39 changes: 35 additions & 4 deletions MinimedKit/PumpEvents/PumpAlarmPumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,41 @@

import Foundation

public enum PumpAlarmType {
case BatteryOutLimitExceeded
case NoDelivery
case BatteryDepleted
case DeviceReset
case ReprogramError
case EmptyReservoir
case UnknownType(rawType: UInt8)

init(rawType: UInt8) {
switch rawType {
case 3:
self = .BatteryOutLimitExceeded
case 4:
self = .NoDelivery
case 5:
self = .BatteryDepleted
case 16:
self = .DeviceReset
case 61:
self = .ReprogramError
case 62:
self = .EmptyReservoir
default:
self = .UnknownType(rawType: rawType)
}
}
}

public struct PumpAlarmPumpEvent: TimestampedPumpEvent {
public let length: Int
public let rawData: NSData
public let timestamp: NSDateComponents
let rawType: Int
public let alarmType: PumpAlarmType

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

Expand All @@ -23,14 +52,16 @@ public struct PumpAlarmPumpEvent: TimestampedPumpEvent {

rawData = availableData[0..<length]

rawType = Int(availableData[1] as UInt8)
alarmType = PumpAlarmType(rawType: availableData[1] as UInt8)

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

public var dictionaryRepresentation: [String: AnyObject] {

return [
"_type": "AlarmPump",
"rawType": rawType,
"alarm": "\(self.alarmType)",
]
}
}
15 changes: 15 additions & 0 deletions MinimedKitTests/HistoryPageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ class HistoryPageTests: XCTestCase {
XCTAssertEqual(NSDateComponents(gregorianYear: 2016, month: 7, day: 15, hour: 20, minute: 2, second: 20), alarm.timestamp)
}

func testPumpAlarms() throws {
let pumpModel = PumpModel.Model723

let page = try HistoryPage(pageData: NSData(hexadecimalString: "5c0800b8c11ce0c101013c013c00000059eb567a1033004bf5165a100016014bf5165a10330079cc175a1000160179cc175a10330079e0175a1000160179e0175a10330b70ef175a1000160170ef175a10061503680040600107060a2084004060010706110411114040a1070c150a4600010706050309004040a1071a0005410001070c0505410001071a0119410001076400304100010764001e4200010717003542000107180040c0001b10070000000001870000006e01870500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021004fc0001b10030000000056c1201b107b0079c1001b1000440033006cc7005b100016016cc7005b10330068dc005b1000160168dc005b10338568e1005b1000160168e1005b1033c56aeb005b100016016aeb005b1033b168f0005b1000160168f0005b1033b968f5005b1000160168f5005b1033be68fa005b1000160168fa005b1033bc69c3015b1000160169c3015b1033c169c8015b1000160169c8015b1033b868cd015b1000160168cd015b1033ae69d2015b1000160169d2015b1033ad69d7015b1000160169d7015b1033c368dc015b1000160168dc015b1033c868e1015b1000160168e1015b1033c169e6015b1000160169e6015b1033b668eb015b1000160168eb015b1033be68f0015b1000160168f0015b1033a468f5015b1000160168f5015b1033ab68fa015b1000160168fa015b1033a869c3025b1000160169c3025b10338968c8025b1000160168c8025b10336c68cd025b1000160168cd025b10336469d2025b1000160169d2025b10336d68d7025b1000160168d7025b10337a68dc025b1000160168dc025b10338968e1025b1000160168e1025b10339968e6025b1000160168e6025b10339268eb025b1000160168eb025b10338168f0025b1000160168f0025b10338369f5025b1000160169f5025b10337768fa025b1000160168fa025b10337668c3035b1000160168c3035b10338068c8035b1000160168c8035b10336768cd035b1000160168cd035b10330069d2035b1000160069d2035b107b0069d2031b10004400336968d7035b1000160168d7035b10338668dc035b1000160168dc035b10337b6ae1035b100016016ae1035b10338668e6035b1000160168e6035b10339668eb035b1000160168eb035b1033c069f0035b1000160169f0035b1033b769f5035b1000160169f5035b10339c69fa035b1000160169fa035b10330068c3045b1000160068c3045b107b0068c3041b10004400338368d2045b1000160168d2045b1033ac68d7045b1000160168d7045b10338769dc045b1000160169dc045b10335f68e1045b1000160168e1045b10330069e6045b1000160069e6045b100000000000bf1d")!, pumpModel: pumpModel)
let events = page.events

XCTAssertEqual(125, events.count)

let alarm = events[9] as! PumpAlarmPumpEvent
XCTAssertEqual("UnknownType(21)", "\(alarm.alarmType)")
XCTAssertEqual(NSDateComponents(gregorianYear: 2007, month: 1, day: 1, hour: 0, minute: 0, second: 0), alarm.timestamp)

let batteryDepletedAlarm = events[13] as! PumpAlarmPumpEvent
XCTAssertEqual("BatteryDepleted", "\(batteryDepletedAlarm.alarmType)")
XCTAssertEqual(NSDateComponents(gregorianYear: 2007, month: 1, day: 1, hour: 0, minute: 0, second: 0), batteryDepletedAlarm.timestamp)

}
}
2 changes: 1 addition & 1 deletion MinimedKitTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>0.7.1</string>
<string>0.8.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
43 changes: 43 additions & 0 deletions NightscoutUploadKit/DeviceStatus/BatteryStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// BatteryStatus.swift
// RileyLink
//
// Created by Pete Schwamb on 7/28/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public enum BatteryIndicator: String {
case Low = "low"
case Normal = "normal"
}

public struct BatteryStatus {
let percent: Int?
let voltage: Double?
let status: BatteryIndicator?

public init(percent: Int? = nil, voltage: Double? = nil, status: BatteryIndicator? = nil) {
self.percent = percent
self.voltage = voltage
self.status = status
}

public var dictionaryRepresentation: [String: AnyObject] {
var rval = [String: AnyObject]()

if let percent = percent {
rval["percent"] = percent
}
if let voltage = voltage {
rval["voltage"] = voltage
}

if let status = status {
rval["status"] = status.rawValue
}

return rval
}
}
30 changes: 30 additions & 0 deletions NightscoutUploadKit/DeviceStatus/COBStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// COBStatus.swift
// RileyLink
//
// Created by Pete Schwamb on 8/2/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public struct COBStatus {
let cob: Double
let timestamp: NSDate

public init(cob: Double, timestamp: NSDate) {
self.cob = cob // grams
self.timestamp = timestamp
}

public var dictionaryRepresentation: [String: AnyObject] {

var rval = [String: AnyObject]()

rval["timestamp"] = TimeFormat.timestampStrFromDate(timestamp)
rval["cob"] = cob

return rval
}

}
48 changes: 48 additions & 0 deletions NightscoutUploadKit/DeviceStatus/DeviceStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// DeviceStatus.swift
// RileyLink
//
// Created by Pete Schwamb on 7/26/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public struct DeviceStatus {
let device: String
let timestamp: NSDate
let pumpStatus: PumpStatus?
let uploaderStatus: UploaderStatus?
let loopStatus: LoopStatus?

public init(device: String, timestamp: NSDate, pumpStatus: PumpStatus? = nil, uploaderStatus: UploaderStatus? = nil, loopStatus: LoopStatus? = nil) {
self.device = device
self.timestamp = timestamp
self.pumpStatus = pumpStatus
self.uploaderStatus = uploaderStatus
self.loopStatus = loopStatus
}

public var dictionaryRepresentation: [String: AnyObject] {
var rval = [String: AnyObject]()

rval["device"] = device
rval["created_at"] = TimeFormat.timestampStrFromDate(timestamp)

if let pump = pumpStatus {
rval["pump"] = pump.dictionaryRepresentation
}

if let uploader = uploaderStatus {
rval["uploader"] = uploader.dictionaryRepresentation
}

if let loop = loopStatus {
// Would like to change this to avoid confusion about whether or not this was uploaded from Loop or openaps
rval["openaps"] = loop.dictionaryRepresentation
}

return rval
}
}

38 changes: 38 additions & 0 deletions NightscoutUploadKit/DeviceStatus/IOBStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// IOBStatus.swift
// RileyLink
//
// Created by Pete Schwamb on 7/28/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

public struct IOBStatus {
let timestamp: NSDate
let iob: Double? // basal iob + bolus iob: can be negative
let basalIOB: Double? // does not include bolus iob

public init(timestamp: NSDate, iob: Double? = nil, basalIOB: Double? = nil) {
self.timestamp = timestamp
self.iob = iob
self.basalIOB = basalIOB
}

public var dictionaryRepresentation: [String: AnyObject] {

var rval = [String: AnyObject]()

rval["timestamp"] = TimeFormat.timestampStrFromDate(timestamp)

if let iob = iob {
rval["iob"] = iob
}

if let basalIOB = basalIOB {
rval["basaliob"] = basalIOB
}

return rval
}
}
Loading

0 comments on commit 4c13ed4

Please sign in to comment.