From 99b9b81d08a6194aec7e4ab7e9d2f9131945ad82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Sz=C3=A1sz?= <32592661+benceszasz@users.noreply.github.com> Date: Tue, 4 May 2021 08:44:46 +0200 Subject: [PATCH 1/3] Dump response in case of JSON exception --- .../carelink/cli/CareLinkClientCLI.java | 79 ++++++++++++------- .../carelink/client/CareLinkClient.java | 2 + 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/main/java/info/nightscout/medtronic/carelink/cli/CareLinkClientCLI.java b/src/main/java/info/nightscout/medtronic/carelink/cli/CareLinkClientCLI.java index 9d3c6b5..891b623 100644 --- a/src/main/java/info/nightscout/medtronic/carelink/cli/CareLinkClientCLI.java +++ b/src/main/java/info/nightscout/medtronic/carelink/cli/CareLinkClientCLI.java @@ -10,7 +10,6 @@ import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Calendar; -import java.util.Date; public class CareLinkClientCLI { @@ -24,6 +23,7 @@ public class CareLinkClientCLI { private static final String OPTION_WAIT = "w"; private static final String OPTION_ANONYM = "a"; private static final String OPTION_VERBOSE = "v"; + private static final String OPTION_JSON_EXCEPTION = "j"; private static Options generateOptions() { @@ -120,6 +120,15 @@ private static Options generateOptions() { .desc("Wait minutes between repeated calls.") .build()); + //j - Json exception + options.addOption( + Option.builder(OPTION_JSON_EXCEPTION) + .required(false) + .longOpt("jsonex") + .hasArg(false) + .desc("Dump response for data Json exception.") + .build()); + return options; } @@ -135,6 +144,7 @@ public static void main(String[] args) throws ParseException { boolean downloadRecentData; boolean verbose; boolean anonymize; + boolean dumpJsonException; Options options = generateOptions(); @@ -150,10 +160,11 @@ public static void main(String[] args) throws ParseException { CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); //Set params - verbose = (cmd.hasOption(OPTION_VERBOSE)) ? true : false; - downloadSession = (cmd.hasOption(OPTION_SESSION)) ? true : false; - downloadRecentData = (cmd.hasOption(OPTION_DATA)) ? true : false; - anonymize = (cmd.hasOption(OPTION_ANONYM)) ? true : false; + verbose = cmd.hasOption(OPTION_VERBOSE); + downloadSession = cmd.hasOption(OPTION_SESSION); + downloadRecentData = cmd.hasOption(OPTION_DATA); + anonymize = cmd.hasOption(OPTION_ANONYM); + dumpJsonException = cmd.hasOption(OPTION_JSON_EXCEPTION); folder = (cmd.hasOption(OPTION_OUTPUT)) ? cmd.getOptionValue(OPTION_OUTPUT) : null; repeat = (cmd.hasOption(OPTION_REPEAT)) ? Integer.parseInt(cmd.getOptionValue(OPTION_REPEAT)) : 1; wait = (cmd.hasOption(OPTION_WAIT)) ? Integer.parseInt(cmd.getOptionValue(OPTION_WAIT)) : 1; @@ -164,12 +175,10 @@ public static void main(String[] args) throws ParseException { downloadSession, downloadRecentData, anonymize, folder, - repeat, wait); - } catch (MissingOptionException exMiss) { - System.out.println(exMiss.getMessage()); - System.out.println("Run without options to get usage info!"); - } catch (UnrecognizedOptionException exUnrec) { - System.out.println(exUnrec.getMessage()); + repeat, wait, + dumpJsonException); + } catch (MissingOptionException| UnrecognizedOptionException exOption) { + System.out.println(exOption.getMessage()); System.out.println("Run without options to get usage info!"); } catch (Exception ex) { System.out.println(ex.getMessage()); @@ -179,9 +188,10 @@ public static void main(String[] args) throws ParseException { } - private static void callCareLinkClient(boolean verbose, String username, String password, String country, Boolean downloadSessionInfo, Boolean downloadData, boolean anonymize, String folder, int repeat, int wait){ + private static void callCareLinkClient(boolean verbose, String username, String password, String country, Boolean downloadSessionInfo, Boolean downloadData, boolean anonymize, String folder, int repeat, int wait, boolean dumpJsonException){ CareLinkClient client = null; + RecentData recentData = null; client = new CareLinkClient(username, password, country); if(verbose)printLog("Client created!"); @@ -201,14 +211,29 @@ private static void callCareLinkClient(boolean verbose, String username, String if(downloadData) { try { for(int j = 0; j < 2; j++) { - writeJson(client.getRecentData(), folder, "data", anonymize, verbose); - //writeResponse(client.getLastResponseBody(), folder, "dataresponse"); + recentData = client.getRecentData(); + //Auth error if(client.getLastResponseCode() == 401) { printLog("GetRecentData login error (response code 401). Trying again in 1 sec!"); Thread.sleep(1000); } - else { + //Get success + else if(client.getLastResponseCode() == 200) { + //Data OK + if(client.getLastDataSuccess()) { + writeJson(recentData, folder, "data", anonymize, verbose); + //Data error + } else { + printLog("Data exception: " + (client.getLastErrorMessage() == null ? "no details available" : client.getLastErrorMessage())); + if(dumpJsonException){ + writeFile(client.getLastResponseBody(), folder, "dataex", verbose); + } + } + //STOP!!! break; + } else { + printLog("Error, response code: " + String.valueOf(client.getLastResponseCode()) + " Trying again in 1 sec!"); + Thread.sleep(1000); } } } catch (Exception ex) { @@ -231,22 +256,17 @@ private static void callCareLinkClient(boolean verbose, String username, String protected static void writeJson(Object object, String folder, String name, boolean anonymize, boolean verbose){ - FileWriter writer = null; - SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss"); - String filename = name + "-" + sdfDate.format(Calendar.getInstance().getTime()) + ".json"; + String content; - if(anonymize) + //Anonymize data + if(anonymize) { anonymizeData(object); + } + //Convert JSON to string and write to file try { - if(folder == null) - writer = new FileWriter(filename); - else - writer = new FileWriter(Paths.get(folder, filename).toAbsolutePath().toString()); - new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").setPrettyPrinting().create().toJson(object, writer); - writer.flush(); - writer.close(); - if (verbose) printLog(name + " saved!"); + content = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").setPrettyPrinting().create().toJson(object); + writeFile(content, folder, name, verbose); } catch (Exception ex) { printLog("Error during save of " + name + " . Details: " + ex.getClass().getName() + " - " + ex.getMessage()); } @@ -254,7 +274,7 @@ protected static void writeJson(Object object, String folder, String name, boole } - protected static void writeResponse(String response, String folder, String name){ + protected static void writeFile(String content, String folder, String name, boolean verbose){ FileWriter writer = null; SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss"); @@ -265,9 +285,10 @@ protected static void writeResponse(String response, String folder, String name) writer = new FileWriter(filename); else writer = new FileWriter(Paths.get(folder, filename).toAbsolutePath().toString()); - writer.write(response); + writer.write(content); writer.flush(); writer.close(); + if (verbose) printLog(name + " saved!"); } catch (Exception ex) { printLog("Error during save of " + name + " . Details: " + ex.getClass().getName() + " - " + ex.getMessage()); } diff --git a/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java b/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java index ffa542c..7daff7d 100644 --- a/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java +++ b/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java @@ -156,6 +156,7 @@ protected boolean executeLoginProcedure() { lastLoginSuccess = false; loginInProcess = true; + lastErrorMessage = null; try { // Clear cookies @@ -428,6 +429,7 @@ protected T getData(HttpUrl url, RequestBody requestBody, Class dataClass Object data = null; this.lastDataSuccess = false; + this.lastErrorMessage = null; // Get auth token String authToken = this.getAuthorizationToken(); From 8c17094e8d6014c9fa2f2b07c23e1e46ff5248f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Sz=C3=A1sz?= <32592661+benceszasz@users.noreply.github.com> Date: Tue, 4 May 2021 08:46:03 +0200 Subject: [PATCH 2/3] General BLE device family to support 770G --- .../nightscout/medtronic/carelink/client/CareLinkClient.java | 2 +- .../nightscout/medtronic/carelink/message/MonitorData.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java b/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java index 7daff7d..287739f 100644 --- a/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java +++ b/src/main/java/info/nightscout/medtronic/carelink/client/CareLinkClient.java @@ -122,7 +122,7 @@ public RecentData getRecentData() { // Force login to get basic info if(getAuthorizationToken() != null) { - if (CountryUtils.isUS(carelinkCountry) || sessionMonitorData.isBleX()) + if (CountryUtils.isUS(carelinkCountry) || sessionMonitorData.isBle()) return this.getConnectDisplayMessage(this.sessionProfile.username, this.sessionUser.getUserRole(), sessionCountrySettings.blePereodicDataEndpoint); else diff --git a/src/main/java/info/nightscout/medtronic/carelink/message/MonitorData.java b/src/main/java/info/nightscout/medtronic/carelink/message/MonitorData.java index 08c6726..207c99e 100644 --- a/src/main/java/info/nightscout/medtronic/carelink/message/MonitorData.java +++ b/src/main/java/info/nightscout/medtronic/carelink/message/MonitorData.java @@ -4,8 +4,8 @@ public class MonitorData { public String deviceFamily; - public boolean isBleX() { - return deviceFamily.contains("BLE_X"); + public boolean isBle() { + return deviceFamily.contains("BLE"); } } From 2c7c513bda481f43821326ea1710c9b5c5d0f6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Sz=C3=A1sz?= <32592661+benceszasz@users.noreply.github.com> Date: Tue, 4 May 2021 08:46:28 +0200 Subject: [PATCH 3/3] Add supported device list to readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 4fd8e4c..01d58b2 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ Java library, which can be used for retrieving data from Medtronic CareLink of o ## Status **The development is in a very early stage!** +## Supported devices +- Medtronic Guardian Connect CGM +- Medtronic MiniMed 770G pump +- Medtronic MiniMed 780G pump +- Other Medtronic MiniMed 7xxG pumps??? + ## Features - Login to CareLink and provide access token for CareLink API calls - Some basic CareLink APIs: get user data, get user profile, get country settings, get last 24 hours, get recent data from CareLink Cloud