Skip to content

Commit

Permalink
First version to support CareLink server with multiple follow enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
benceszasz committed May 13, 2023
1 parent 0ca5770 commit 684efc8
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CareLinkFollowDownloader {
private String carelinkUsername;
private String carelinkPassword;
private String carelinkCountry;
private String carelinkPatient;

private CareLinkClient carelinkClient;

Expand All @@ -37,10 +38,11 @@ public int getLastResponseCode(){
return lastResponseCode;
}

CareLinkFollowDownloader(String carelinkUsername, String carelinkPassword, String carelinkCountry) {
CareLinkFollowDownloader(String carelinkUsername, String carelinkPassword, String carelinkCountry, String carelinkPatient) {
this.carelinkUsername = carelinkUsername;
this.carelinkPassword = carelinkPassword;
this.carelinkCountry = carelinkCountry;
this.carelinkPatient = carelinkPatient;
loginDataLooksOkay = !emptyString(carelinkUsername) && !emptyString(carelinkPassword) && carelinkCountry != null && !emptyString(carelinkCountry);
}

Expand Down Expand Up @@ -118,7 +120,10 @@ private void processCareLinkData() {

//Get data
try {
recentData = getCareLinkClient().getRecentData();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
downloader = new CareLinkFollowDownloader(
Pref.getString("clfollow_user", ""),
Pref.getString("clfollow_pass", ""),
Pref.getString("clfollow_country", "").toLowerCase()
Pref.getString("clfollow_country", "").toLowerCase(),
Pref.getString("clfollow_patient", "")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public Profile getSessionProfile() {
public CountrySettings getSessionCountrySettings() {
return sessionCountrySettings;
}
protected Boolean sessionM2MEnabled;
public boolean getSessionM2MEnabled(){
return sessionM2MEnabled;
}
protected Patient[] sessionPatients;
public Patient[] getSessionPatients(){
return sessionPatients;
}
protected MonitorData sessionMonitorData;
public MonitorData getSessionMonitorData() {
return sessionMonitorData;
Expand Down Expand Up @@ -103,19 +111,45 @@ public CareLinkClient(String carelinkUsername, String carelinkPassword, String c
*/
public RecentData getRecentData() {

//Use default patient username
return this.getRecentData(this.getDefaultPatientUsername());

}

public RecentData getRecentData(String patientUsername) {

// Force login to get basic info
if(getAuthorizationToken() != null) {
if (CountryUtils.isUS(carelinkCountry) || sessionMonitorData.isBle())
if (getAuthorizationToken() != null) {
// M2M
if (this.sessionM2MEnabled)
return this.getM2MPatientData(patientUsername);
// Non M2M (old logic)
else if (sessionMonitorData.isBle())
return this.getConnectDisplayMessage(this.sessionProfile.username, this.sessionUser.getUserRole(),
sessionCountrySettings.blePereodicDataEndpoint);
else
return this.getLast24Hours();
}
else {
} else {
return null;
}

}

public String getDefaultPatientUsername() {
// Force login to get basic info
if (getAuthorizationToken() != null) {
if (this.sessionUser != null)
if (this.sessionUser.isCarePartner())
if (this.sessionPatients != null && this.sessionPatients.length > 0)
return this.sessionPatients[0].username;
else
return null;
else
return this.sessionProfile.username;
else
return null;
} else
return null;
}

// Get CareLink server address
Expand Down Expand Up @@ -166,7 +200,11 @@ protected boolean executeLoginProcedure() {
this.sessionUser = this.getMyUser();
this.sessionProfile = this.getMyProfile();
this.sessionCountrySettings = this.getMyCountrySettings();
this.sessionMonitorData = this.getMonitorData();
this.sessionM2MEnabled = this.getM2MEnabled().value;
if(!this.sessionM2MEnabled)
this.sessionMonitorData = this.getMonitorData();
if(this.sessionUser.isCarePartner() && this.sessionM2MEnabled)
this.sessionPatients = this.getM2MPatients();

} catch (Exception e) {
lastErrorMessage = e.getClass().getSimpleName() + ":" + e.getMessage();
Expand All @@ -176,7 +214,8 @@ protected boolean executeLoginProcedure() {
}

// Set login success if everything was ok:
if(this.sessionUser != null && this.sessionProfile != null && this.sessionCountrySettings != null && this.sessionMonitorData != null)
if(this.sessionUser != null && this.sessionProfile != null && this.sessionCountrySettings != null && this.sessionM2MEnabled != null
&& (this.sessionM2MEnabled || this.sessionMonitorData != null))
lastLoginSuccess = true;
//Clear cookies, session infos if error occured during login process
else {
Expand All @@ -185,6 +224,7 @@ protected boolean executeLoginProcedure() {
this.sessionProfile = null;
this.sessionCountrySettings = null;
this.sessionMonitorData = null;
this.sessionPatients = null;
}

return lastLoginSuccess;
Expand Down Expand Up @@ -322,6 +362,16 @@ public CountrySettings getMyCountrySettings() {

}

// M2M Enabled
public M2MEnabled getM2MEnabled() {
return this.getData(this.careLinkServer(), "patient/configuration/system/personal.cp.m2m.enabled", null, null, M2MEnabled.class);
}

// M2M Patients
public Patient[] getM2MPatients() {
return this.getData(this.careLinkServer(), "patient/m2m/links/patients", null, null, Patient[].class);
}

// Classic last24hours webapp data
public RecentData getLast24Hours() {

Expand Down Expand Up @@ -373,6 +423,27 @@ public RecentData getConnectDisplayMessage(String username, String role, String

}

// M2M data
public RecentData getM2MPatientData(String patientUsername) {

Map<String, String> queryParams = null;

//Patient username is mandantory!
if(patientUsername == null || patientUsername.isEmpty())
return null;

queryParams = new HashMap<String, String>();
queryParams.put("cpSerialNumber", "NONE");
queryParams.put("msgType", "last24hours");
queryParams.put("requestTime", String.valueOf(System.currentTimeMillis()));

RecentData recentData = this.getData(this.careLinkServer(), "/patient/m2m/connect/data/gc/patients/" + patientUsername, queryParams, null, RecentData.class);
if (recentData != null)
correctTimeInRecentData(recentData);
return recentData;

}

// Helper methods
// Response parsing
protected String extractResponseData(String respBody, String groupRegex, int groupIndex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eveningoutpost.dexdrip.cgm.carelinkfollow.message;

public class M2MEnabled {

public boolean value;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.eveningoutpost.dexdrip.cgm.carelinkfollow.message;

public class Patient {

public String firstName;
public String lastName;
public String status;
public String username;
public boolean notificationsAllowed;
public String nickname;
public String lastDeviceFamily;
public boolean patientUsesConnect;

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ public void run() {
new Runnable() {
@Override
public void run() {
Home.staticRefreshBGCharts();
CareLinkFollowService.resetInstanceAndInvalidateSession();
CollectionServiceStarter.restartCollectionServiceBackground();
textSettingDialog(activity,
"clfollow_patient", "CareLink Patient",
"Enter CareLink Patient (optional)",
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
new Runnable() {
@Override
public void run() {
Home.staticRefreshBGCharts();
CareLinkFollowService.resetInstanceAndInvalidateSession();
CollectionServiceStarter.restartCollectionServiceBackground();
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
final Preference carelinkFollowUser = findPreference("clfollow_user");
final Preference carelinkFollowPass = findPreference("clfollow_pass");
final Preference carelinkFollowCountry = findPreference("clfollow_country");
final Preference carelinkFollowPatient = findPreference("clfollow_patient");
final Preference carelinkFollowGracePeriod = findPreference("clfollow_grace_period");
final Preference carelinkFollowPollInterval = findPreference("clfollow_poll_interval");
final Preference carelinkFollowDownloadFingerBGs = findPreference("clfollow_download_finger_bgs");
Expand All @@ -1207,6 +1208,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
collectionCategory.addPreference(carelinkFollowUser);
collectionCategory.addPreference(carelinkFollowPass);
collectionCategory.addPreference(carelinkFollowCountry);
collectionCategory.addPreference(carelinkFollowPatient);
collectionCategory.addPreference(carelinkFollowGracePeriod);
collectionCategory.addPreference(carelinkFollowPollInterval);
collectionCategory.addPreference(carelinkFollowDownloadFingerBGs);
Expand All @@ -1227,6 +1229,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
carelinkFollowUser.setOnPreferenceChangeListener(carelinkFollowListener);
carelinkFollowPass.setOnPreferenceChangeListener(carelinkFollowListener);
carelinkFollowCountry.setOnPreferenceChangeListener(carelinkFollowListener);
carelinkFollowPatient.setOnPreferenceChangeListener(carelinkFollowListener);
carelinkFollowGracePeriod.setOnPreferenceChangeListener(carelinkFollowListener);
carelinkFollowPollInterval.setOnPreferenceChangeListener(carelinkFollowListener);
//carelinkFollowDownloadFingerBGs.setOnPreferenceChangeListener(carelinkFollowListener);
Expand All @@ -1242,6 +1245,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
collectionCategory.removePreference(carelinkFollowUser);
collectionCategory.removePreference(carelinkFollowPass);
collectionCategory.removePreference(carelinkFollowCountry);
collectionCategory.removePreference(carelinkFollowPatient);
collectionCategory.removePreference(carelinkFollowGracePeriod);
collectionCategory.removePreference(carelinkFollowPollInterval);
collectionCategory.removePreference(carelinkFollowDownloadFingerBGs);
Expand Down Expand Up @@ -1582,6 +1586,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
if (collectionType != DexCollectionType.CLFollow) {
try {
collectionCategory.removePreference(carelinkFollowCountry);
collectionCategory.removePreference(carelinkFollowPatient);
collectionCategory.removePreference(carelinkFollowPass);
collectionCategory.removePreference(carelinkFollowUser);
collectionCategory.removePreference(carelinkFollowGracePeriod);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,8 @@
<string name="summary_clfollow_pass">CareLink login password</string>
<string name="title_clfollow_country">CareLink Country</string>
<string name="summary_clfollow_country">CareLink country code</string>
<string name="title_clfollow_patient">CareLink Patient Username</string>
<string name="summary_clfollow_patient">CareLink patient username</string>
<string name="title_clfollow_lang">CareLink Language</string>
<string name="summary_clfollow_lang">CareLink language code</string>
<string name="title_clfollow_grace_period">Grace Period</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/pref_data_source.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
android:key="clfollow_country"
android:summary="@string/summary_clfollow_country"
android:title="@string/title_clfollow_country" />
<EditTextPreference
android:defaultValue=""
android:inputType="textNoSuggestions|textVisiblePassword"
android:key="clfollow_patient"
android:maxLines="1"
android:singleLine="true"
android:summary="@string/summary_clfollow_patient"
android:title="@string/title_clfollow_patient" />
<EditTextPreference
android:defaultValue="30"
android:inputType="number"
Expand Down

0 comments on commit 684efc8

Please sign in to comment.