From 684efc85b3def4964125930c50cc08f4f18ff4ed Mon Sep 17 00:00:00 2001 From: benceszasz Date: Sat, 13 May 2023 22:53:12 +0200 Subject: [PATCH] First version to support CareLink server with multiple follow enabled --- .../CareLinkFollowDownloader.java | 9 +- .../carelinkfollow/CareLinkFollowService.java | 3 +- .../carelinkfollow/client/CareLinkClient.java | 83 +++++++++++++++++-- .../carelinkfollow/message/M2MEnabled.java | 7 ++ .../cgm/carelinkfollow/message/Patient.java | 14 ++++ .../dexdrip/utils/DexCollectionHelper.java | 15 +++- .../dexdrip/utils/Preferences.java | 5 ++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/pref_data_source.xml | 8 ++ 9 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/M2MEnabled.java create mode 100644 app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/Patient.java diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowDownloader.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowDownloader.java index 54e8236..d3f49ec 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowDownloader.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowDownloader.java @@ -19,6 +19,7 @@ public class CareLinkFollowDownloader { private String carelinkUsername; private String carelinkPassword; private String carelinkCountry; + private String carelinkPatient; private CareLinkClient carelinkClient; @@ -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); } @@ -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); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowService.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowService.java index 8ebd83e..a354884 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowService.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/CareLinkFollowService.java @@ -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", "") ); } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/client/CareLinkClient.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/client/CareLinkClient.java index fdc0e52..7c0d731 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/client/CareLinkClient.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/client/CareLinkClient.java @@ -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; @@ -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 @@ -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(); @@ -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 { @@ -185,6 +224,7 @@ protected boolean executeLoginProcedure() { this.sessionProfile = null; this.sessionCountrySettings = null; this.sessionMonitorData = null; + this.sessionPatients = null; } return lastLoginSuccess; @@ -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() { @@ -373,6 +423,27 @@ public RecentData getConnectDisplayMessage(String username, String role, String } + // M2M data + public RecentData getM2MPatientData(String patientUsername) { + + Map queryParams = null; + + //Patient username is mandantory! + if(patientUsername == null || patientUsername.isEmpty()) + return null; + + queryParams = new HashMap(); + 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) { diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/M2MEnabled.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/M2MEnabled.java new file mode 100644 index 0000000..5faa49f --- /dev/null +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/M2MEnabled.java @@ -0,0 +1,7 @@ +package com.eveningoutpost.dexdrip.cgm.carelinkfollow.message; + +public class M2MEnabled { + + public boolean value; + +} diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/Patient.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/Patient.java new file mode 100644 index 0000000..256fa6d --- /dev/null +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/carelinkfollow/message/Patient.java @@ -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; + +} diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionHelper.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionHelper.java index 3373c34..c920d46 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionHelper.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionHelper.java @@ -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(); + } + }); } }); } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java index d6f468f..2e71769 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java @@ -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"); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8d90cfa..342f639 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1329,6 +1329,8 @@ CareLink login password CareLink Country CareLink country code + CareLink Patient Username + CareLink patient username CareLink Language CareLink language code Grace Period diff --git a/app/src/main/res/xml/pref_data_source.xml b/app/src/main/res/xml/pref_data_source.xml index cb92723..21a201f 100644 --- a/app/src/main/res/xml/pref_data_source.xml +++ b/app/src/main/res/xml/pref_data_source.xml @@ -164,6 +164,14 @@ android:key="clfollow_country" android:summary="@string/summary_clfollow_country" android:title="@string/title_clfollow_country" /> +