Skip to content

Commit

Permalink
Removing iOS 8 fallbacks. Adding remote logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
loudnate committed Sep 12, 2015
1 parent 5b4ad91 commit ffeafcb
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ Carthage
# `pod install` in .travis.yml
#
Pods/

# Sensitive details about remote backup
RemoteSettings.plist
32 changes: 32 additions & 0 deletions MinimedKit/Messages/MySentryPumpStatusMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ public struct MySentryPumpStatusMessageBody: MessageBody {
}
}

public var dictionaryRepresentation: [String: AnyObject] {
let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

var dict: [String: AnyObject] = [
"glucoseTrend": String(glucoseTrend),
"pumpDate": dateFormatter.stringFromDate(pumpDate),
"reservoirRemaining": reservoirRemaining,
"iob": iob
]

switch glucose {
case .Active(glucose: let glucose):
dict["glucose"] = glucose
default:
dict["glucose"] = nil
}
dict["sensorStatus"] = String(glucose)

switch previousGlucose {
case .Active(glucose: let glucose):
dict["lastGlucose"] = glucose
default:
dict["lastGlucose"] = nil
}
dict["lastSensorStatus"] = String(previousGlucose)

return dict
}

public var txData: NSData {
return NSData()
}
Expand Down
12 changes: 6 additions & 6 deletions Naterade/Categories/HKDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import MinimedKit
import RileyLinkKit


@available(iOS 9.0, *)
extension HKDevice {
convenience init(rileyLinkDevice: RileyLinkDevice) {
// TODO: Don't hard-code this information here. Can we read firmware version from the pump?
self.init(
name: rileyLinkDevice.name,
manufacturer: "@ps2",
model: "RileyLink",
hardwareVersion: "1.0",
firmwareVersion: "0.0.1",
softwareVersion: "\(RileyLinkKitVersionNumber)/\(MinimedKitVersionNumber)/\(NSBundle.mainBundle().shortVersionString)",
manufacturer: "Medtronic",
model: "Revel",
hardwareVersion: "723",
firmwareVersion: "2.4A 1.1 0B 0B",
softwareVersion: "RileyLink: \(RileyLinkKitVersionNumber), MinimedKit: \(MinimedKitVersionNumber), Naterade: \(NSBundle.mainBundle().shortVersionString)",
localIdentifier: nil,
UDIDeviceIdentifier: nil
)
Expand Down
13 changes: 7 additions & 6 deletions Naterade/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Bluetooth is used to communicate with insulin pump and continuous glucose monitor devices</string>
<key>NSHealthShareUsageDescription</key>
<string>Glucose data on Apple Watch is retrieved from HealthKit</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Historical glucose data is stored securely in HealthKit for later analysis</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
Expand Down Expand Up @@ -50,6 +56,7 @@
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
Expand All @@ -58,11 +65,5 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSHealthUpdateUsageDescription</key>
<string>Historical glucose data is stored securely in HealthKit for later analysis</string>
<key>NSHealthShareUsageDescription</key>
<string>Glucose data on Apple Watch is retrieved from HealthKit</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Bluetooth is used to communicate with insulin pump and continuous glucose monitor devices</string>
</dict>
</plist>
60 changes: 60 additions & 0 deletions Naterade/Managers/DiagnosticLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// DiagnosticLogger.swift
// Naterade
//
// Created by Nathan Racklyeft on 9/10/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//

import Foundation
import MinimedKit


class DiagnosticLogger {
let APIKey: String
let APIHost: String
let APIPath: String

init?() {
if let
settingsPath = NSBundle.mainBundle().pathForResource("RemoteSettings", ofType: "plist"),
settings = NSDictionary(contentsOfFile: settingsPath)
{
APIKey = settings["APIKey"] as! String
APIHost = settings["APIHost"] as! String
APIPath = settings["APIPath"] as! String
} else {
APIKey = ""
APIHost = ""
APIPath = ""

return nil
}
}

func addMessage(message: MySentryPumpStatusMessageBody) {
if let messageData = try? NSJSONSerialization.dataWithJSONObject(message.dictionaryRepresentation, options: []),
let URL = NSURL(string: APIHost)?.URLByAppendingPathComponent(APIPath).URLByAppendingPathComponent("sentryMessage"),
components = NSURLComponents(URL: URL, resolvingAgainstBaseURL: true)
{
components.query = "apiKey=\(APIKey)"

if let URL = components.URL {
let request = NSMutableURLRequest(URL: URL)

request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: messageData) { (_, _, error) -> Void in
if let error = error {
NSLog("%s error: %@", __FUNCTION__, error)
}
}

task.resume()
}
}


}
}
40 changes: 14 additions & 26 deletions Naterade/Managers/PumpDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PumpDataManager {

// MARK: - Observed state

lazy var logger = DiagnosticLogger()

var rileyLinkManager: RileyLinkManager? {
switch state {
case .Ready(manager: let manager):
Expand Down Expand Up @@ -93,37 +95,24 @@ class PumpDataManager {
if status != latestPumpStatus {
latestPumpStatus = status

logger?.addMessage(status)

if let date = status.glucoseDate {
switch status.glucose {
case .Active(glucose: let value):
let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)!
let quantity = HKQuantity(unit: HKUnit(fromString: "mg/dL"), doubleValue: Double(value))

let sample: HKQuantitySample
if #available(iOS 9.0, *) {
sample = HKQuantitySample(
type: quantityType,
quantity: quantity,
startDate: date,
endDate: date,
device: HKDevice(rileyLinkDevice: device),
metadata: [
HKMetadataKeyWasUserEntered: false
]
)
} else {
sample = HKQuantitySample(
type: quantityType,
quantity: quantity,
startDate: date,
endDate: date,
metadata: [
HKMetadataKeyWasUserEntered: false,
HKMetadataKeyDeviceName: device.name ?? "",
HKMetadataKeyDeviceManufacturerName: "@ps2",
]
)
}
let sample = HKQuantitySample(
type: quantityType,
quantity: quantity,
startDate: date,
endDate: date,
device: HKDevice(rileyLinkDevice: device),
metadata: [
HKMetadataKeyWasUserEntered: false
]
)

if let store = healthStore where store.authorizationStatusForType(glucoseQuantityType) == .SharingAuthorized {
store.saveObject(sample, withCompletion: { (success, error) -> Void in
Expand Down Expand Up @@ -220,7 +209,6 @@ class PumpDataManager {
}

deinit {
// Unregistering observers necessary in iOS 8 only
rileyLinkManagerObserver = nil
rileyLinkDeviceObserver = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SettingsTableViewController: UITableViewController, PumpIDTableViewControl
}

deinit {
dataManagerObserver = nil // iOS 8 only
dataManagerObserver = nil
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
Expand Down

0 comments on commit ffeafcb

Please sign in to comment.