Dexcom Share fixes

This commit is contained in:
Johan Degraeve 2019-07-06 16:19:48 +02:00
parent eaa0fb5af3
commit 9e1b9e2825
4 changed files with 43 additions and 13 deletions

View File

@ -80,7 +80,7 @@ class DexcomShareUploadManager:NSObject {
if let keyPathEnum = UserDefaults.Key(rawValue: keyPath) {
switch keyPathEnum {
case UserDefaults.Key.dexcomShareAccountName, UserDefaults.Key.dexcomSharePassword, UserDefaults.Key.useUSDexcomShareurl :
case UserDefaults.Key.dexcomShareAccountName, UserDefaults.Key.dexcomSharePassword :
if (keyValueObserverTimeKeeper.verifyKey(forKey: keyPathEnum.rawValue, withMinimumDelayMilliSeconds: 200)) {
@ -107,7 +107,7 @@ class DexcomShareUploadManager:NSObject {
}
}
case UserDefaults.Key.uploadReadingstoDexcomShare :
case UserDefaults.Key.uploadReadingstoDexcomShare, UserDefaults.Key.dexcomShareSerialNumber, UserDefaults.Key.useUSDexcomShareurl :
// if changing to enabled, then do a credentials test and if ok start upload, if fail don't give warning, that's the only difference with previous cases
if (keyValueObserverTimeKeeper.verifyKey(forKey: keyPathEnum.rawValue, withMinimumDelayMilliSeconds: 200)) {
@ -162,24 +162,49 @@ class DexcomShareUploadManager:NSObject {
return
}
// create url
let uploadURL = url.appendingPathComponent(startRemoteMonitoringSessionPath + "?sessionID=" + dexcomShareSessionId + "&serialNumber=" + dexcomShareSerialNumber)
let startRemoteMonitoringSessionUrl = url.appendingPathComponent(startRemoteMonitoringSessionPath)
// create NSURLComponents instance with scheme, host, queryItems
guard let components = NSURLComponents(url: startRemoteMonitoringSessionUrl, resolvingAgainstBaseURL: false) else {
os_log("in startRemoteMonitoringSessionAndStartUpload, failed to create components", log: self.log, type: .error)
return
}
components.scheme = startRemoteMonitoringSessionUrl.scheme
components.host = startRemoteMonitoringSessionUrl.host
components.queryItems = [URLQueryItem(name: "sessionId", value: dexcomShareSessionId), URLQueryItem(name: "serialNumber", value: dexcomShareSerialNumber)]
guard let newUrl = components.url else {
os_log("in startRemoteMonitoringSessionAndStartUpload, failed to create newUrl", log: self.log, type: .error)
return
}
// create the request
var request = URLRequest(url: uploadURL)
var request = URLRequest(url: newUrl)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField:"Content-Type")
request.setValue("application/json", forHTTPHeaderField:"Accept")
request.setValue("Dexcom Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0", forHTTPHeaderField: "User-Agent")
// get shared URLSession
let sharedSession = URLSession.shared
// Create upload Task
let dataTask = sharedSession.uploadTask(with: request, from: nil, completionHandler: { (data, response, error) -> Void in
let dataTask = sharedSession.uploadTask(with: request, from: "".data(using: .utf8), completionHandler: { (data, response, error) -> Void in
os_log("in startRemoteMonitoringSessionAndStartUpload, in uploadTask completionHandler", log: self.log, type: .info)
// if ends without success then log the data when existing the scope
var success = false
defer {
if !success {
if let data = data {
if let dataAsString = String(bytes: data, encoding: .utf8) {
os_log(" data = %{public}@", log: self.log, type: .error, dataAsString)
}
}
}
}
// error cases
if let error = error {
os_log(" error = %{public}@", log: self.log, type: .error, error.localizedDescription)
@ -218,7 +243,7 @@ class DexcomShareUploadManager:NSObject {
} else if errorCode == "MonitoredReceiverNotAssigned" {
os_log("in uploadBgReadingsToDexcomShare, new login failed with error MonitoredReceiverNotAssigned", log: self.log, type: .error)
os_log(" new login failed with error MonitoredReceiverNotAssigned", log: self.log, type: .error)
DispatchQueue.main.async {
if let messageHandler = self.messageHandler {
messageHandler(Texts_DexcomShareTestResult.uploadErrorWarning, self.createMonitoredReceiverNotAssignedMessage(acount: dexcomShareAccountName, serialNumber: dexcomShareSerialNumber))
@ -458,9 +483,9 @@ class DexcomShareUploadManager:NSObject {
dataTask.resume()
} catch let error {
os_log(" failed to upload, error = %{public}@", log: self.log, type: .info, error.localizedDescription)
return
} catch let error {
os_log(" failed to upload, error = %{public}@", log: self.log, type: .info, error.localizedDescription)
return
}
}

View File

@ -8,4 +8,4 @@
"dexcomsharetestresult_monitored_receiver_sn_doesnotmatch" = "Dexcom Share Serial Number does not match the serial number for this account. Verify the Serial Number in the settings.";
"dexcomsharetestresult_monitored_receiver_not_assigned_1" = "It seems the transmitter id or serial number";
"dexcomsharetestresult_monitored_receiver_not_assigned_2" = "is not assigned to";
"dexcomsharetestresult_monitored_receiver_not_assigned_3" = "Use the official Dexcom app to register the transmitter (G5) or Share receiver (G4)";
"dexcomsharetestresult_monitored_receiver_not_assigned_3" = "Use the official Dexcom app to register the transmitter (G5) or Share receiver (G4)\r\n\r\nPossibly you're just using the wrong url, verify the setting 'Use US url?'";

View File

@ -45,7 +45,7 @@ class Texts_DexcomShareTestResult {
}()
static let monitoredReceiverNotAssigned3: String = {
return NSLocalizedString("dexcomsharetestresult_monitored_receiver_not_assigned_3", tableName: filename, bundle: Bundle.main, value: "Use the official Dexcom app to register the transmitter (G5) or Share receiver (G4)", comment: "If serial number is not assigned to the account, this is the 3rd part in a series of 3 to form a complete sentence")
return NSLocalizedString("dexcomsharetestresult_monitored_receiver_not_assigned_3", tableName: filename, bundle: Bundle.main, value: "Use the official Dexcom app to register the transmitter (G5) or Share receiver (G4)\r\n\r\nPossibly you're just using the wrong url, verify the setting 'Use US url?'", comment: "If serial number is not assigned to the account, this is the 3rd part in a series of 3 to form a complete sentence")
}()

View File

@ -46,14 +46,19 @@ class SettingsViewDexcomSettingsViewModel:SettingsViewModelProtocol {
guard let setting = Setting(rawValue: index) else { fatalError("Unexpected Section") }
switch setting {
case .uploadReadingstoDexcomShare:
return SettingsSelectedRowAction.nothing
case .dexcomShareAccountName:
return SettingsSelectedRowAction.askText(title: Texts_SettingsView.labelDexcomShareAccountName, message: Texts_SettingsView.giveDexcomShareAccountName, keyboardType: UIKeyboardType.alphabet, text: UserDefaults.standard.dexcomShareAccountName, placeHolder: nil, actionTitle: nil, cancelTitle: nil, actionHandler: {(accountName:String) in UserDefaults.standard.dexcomShareAccountName = accountName.toNilIfLength0()}, cancelHandler: nil)
case .dexcomSharePassword:
return SettingsSelectedRowAction.askText(title: Texts_Common.password, message: Texts_SettingsView.giveDexcomSharePassword, keyboardType: UIKeyboardType.alphabet, text: UserDefaults.standard.dexcomSharePassword, placeHolder: nil, actionTitle: nil, cancelTitle: nil, actionHandler: {(password:String) in UserDefaults.standard.dexcomSharePassword = password.toNilIfLength0()}, cancelHandler: nil)
case .useUSDexcomShareurl:
return SettingsSelectedRowAction.nothing
case .dexcomShareSerialNumber:
return SettingsSelectedRowAction.askText(title: Texts_SettingsView.labelDexcomShareSerialNumber, message: Texts_SettingsView.giveDexcomShareSerialNumber, keyboardType: UIKeyboardType.alphabet, text: UserDefaults.standard.dexcomShareSerialNumber, placeHolder: nil, actionTitle: nil, cancelTitle: nil, actionHandler: {(serialNumber:String) in