Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/client/CareLinkClient.java
#	app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java
  • Loading branch information
benceszasz committed Oct 8, 2023
2 parents fad7dd8 + 741f8b0 commit f59aee0
Show file tree
Hide file tree
Showing 12 changed files with 888 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.os.PowerManager;

import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkAuthenticator;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkCredentialStore;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.CollectionServiceStarter;
Expand Down Expand Up @@ -47,50 +49,71 @@ public String getStatus() {
this.carelinkCountry = carelinkCountry;
this.carelinkPatient = carelinkPatient;
loginDataLooksOkay = !emptyString(carelinkUsername) && !emptyString(carelinkPassword) && carelinkCountry != null && !emptyString(carelinkCountry);
loginDataLooksOkay = true;
}

public static void resetInstance() {
UserError.Log.d(TAG, "Instance reset");
CollectionServiceStarter.restartCollectionServiceBackground();
}

public boolean doEverything() {
msg("Start download");
public void doEverything(boolean refreshToken, boolean downloadData) {
if (refreshToken)
this.refreshToken();
if (downloadData)
this.downloadData();
}

if (D) UserError.Log.e(TAG, "doEverything called");
if (loginDataLooksOkay) {
private void downloadData() {
msg("Start download");
if (checkCredentials()) {
try {
if (getCareLinkClient() != null) {
extendWakeLock(30_000);
backgroundProcessConnectData();
} else {
UserError.Log.d(TAG, "Cannot get data as ConnectClient is null");
return false;
UserError.Log.d(TAG, "Cannot get data as CareLinkClient is null");
msg("Download data failed!");
}
return true;
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception in getData() " + e);
releaseWakeLock();
return false;
msg("Download data failed!");
}
} else {
final String invalid = "Invalid CareLink login data!";
msg(invalid);
UserError.Log.e(TAG, invalid);
if (emptyString(carelinkUsername)) {
UserError.Log.e(TAG, "CareLink Username empty!");
}
if (emptyString(carelinkPassword)) {
UserError.Log.e(TAG, "CareLink Password empty!");
}
if (carelinkCountry == null) {
UserError.Log.e(TAG, "CareLink Country empty!");
} else if (!CountryUtils.isSupportedCountry(carelinkCountry)) {
UserError.Log.e(TAG, "CareLink Country not supported!");
}
}

private void refreshToken() {
msg("Start refreshing token");
if (checkCredentials()) {
try {
if (new CareLinkAuthenticator(CareLinkCredentialStore.getInstance().getCredential().country, CareLinkCredentialStore.getInstance()).refreshToken()) {
UserError.Log.d(TAG, "Login token renewed!");
msg(null);
} else {
UserError.Log.e(TAG, "Error renewing login token!");
msg("Login refresh failed! Will try again!");
}
} catch (Exception e) {
UserError.Log.e(TAG, "Error renewing login token: " + e.getMessage());
msg("Login refresh failed! Will try again!");
}
return false;
}
}

private boolean checkCredentials() {
// Not authenticated
if (CareLinkCredentialStore.getInstance().getAuthStatus() != CareLinkCredentialStore.AUTHENTICATED) {
msg("Not logged in! Please log in!");
return false;
// Token expired
} else if (CareLinkCredentialStore.getInstance().getExpiresIn() <= 0) {
msg("Login refresh expired! Please log in!");
return false;
// Credentials are all ok!
} else {
return true;
}
}

private void msg(final String msg) {
Expand Down Expand Up @@ -118,64 +141,44 @@ private void processConnectData() {
carelinkClient = getCareLinkClient();
//Get RecentData from CareLink client
if (carelinkClient != null) {
//Get data
try {
if (JoH.emptyString(this.carelinkPatient))
recentData = getCareLinkClient().getRecentData();
else
recentData = getCareLinkClient().getRecentData(this.carelinkPatient);
lastResponseCode = carelinkClient.getLastResponseCode();
} catch (Exception e) {
UserError.Log.e(TAG, "Exception in CareLink data download: " + e);
}

//Try twice in case of 401 error
for (int i = 0; i < 2; i++) {

//Get data
//Process data
if (recentData != null) {
UserError.Log.d(TAG, "Success get data!");
try {
if (JoH.emptyString(this.carelinkPatient))
recentData = getCareLinkClient().getRecentData();
else
recentData = getCareLinkClient().getRecentData(this.carelinkPatient);
lastResponseCode = carelinkClient.getLastResponseCode();
UserError.Log.d(TAG, "Start process data");
//Process CareLink data (conversion and update xDrip data)
CareLinkDataProcessor.processData(recentData, true);
UserError.Log.d(TAG, "ProcessData finished!");
//Update Service status
CareLinkFollowService.updateBgReceiveDelay();
msg(null);
} catch (Exception e) {
UserError.Log.e(TAG, "Exception in CareLink data download: " + e);
UserError.Log.e(TAG, "Exception in data processing: " + e);
msg("Data processing error!");
}

//Process data
if (recentData != null) {
UserError.Log.d(TAG, "Success get data!");
try {
UserError.Log.d(TAG, "Start process data");
//Process CareLink data (conversion and update xDrip data)
CareLinkDataProcessor.processData(recentData, true);
UserError.Log.d(TAG, "ProcessData finished!");
//Update Service status
CareLinkFollowService.updateBgReceiveDelay();
msg(null);
} catch (Exception e) {
UserError.Log.e(TAG, "Exception in data processing: " + e);
msg("Data processing error!");
}
//Data receive error
//Data receive error
} else {
if (carelinkClient.getLastResponseCode() == 401) {
UserError.Log.e(TAG, "CareLink login error! Response code: " + carelinkClient.getLastResponseCode());
msg("Login error!");
//login error
} else {
//first 401 error => TRY AGAIN, only debug log
if (carelinkClient.getLastResponseCode() == 401 && i == 0) {
UserError.Log.d(TAG, "Try get data again due to 401 response code." + getCareLinkClient().getLastErrorMessage());
//second 401 error => unauthorized error
} else if (carelinkClient.getLastResponseCode() == 401) {
UserError.Log.e(TAG, "CareLink login error! Response code: " + carelinkClient.getLastResponseCode());
msg("Login error!");
//login error
} else if (!getCareLinkClient().getLastLoginSuccess()) {
UserError.Log.e(TAG, "CareLink login error! Response code: " + carelinkClient.getLastResponseCode());
UserError.Log.e(TAG, "Error message: " + getCareLinkClient().getLastErrorMessage());
msg("Login error!");
//other error in download
} else {
UserError.Log.e(TAG, "CareLink download error! Response code: " + carelinkClient.getLastResponseCode());
UserError.Log.e(TAG, "Error message: " + getCareLinkClient().getLastErrorMessage());
msg("Data request error!");
}
UserError.Log.e(TAG, "CareLink download error! Response code: " + carelinkClient.getLastResponseCode());
UserError.Log.e(TAG, "Error message: " + getCareLinkClient().getLastErrorMessage());
msg("Download data failed!");
}

//Next try only for 401 error and first attempt
if (!(carelinkClient.getLastResponseCode() == 401 && i == 0))
break;

}

}

}
Expand All @@ -185,7 +188,8 @@ private CareLinkClient getCareLinkClient() {
if (careLinkClient == null) {
try {
UserError.Log.d(TAG, "Creating CareLinkClient");
careLinkClient = new CareLinkClient(carelinkUsername, carelinkPassword, carelinkCountry);
if (CareLinkCredentialStore.getInstance().getAuthStatus() == CareLinkCredentialStore.AUTHENTICATED)
careLinkClient = new CareLinkClient(CareLinkCredentialStore.getInstance());
} catch (Exception e) {
UserError.Log.e(TAG, "Error creating CareLinkClient", e);
}
Expand Down
Loading

0 comments on commit f59aee0

Please sign in to comment.