Skip to content

Commit

Permalink
Merge pull request #2 from benceszasz/dev
Browse files Browse the repository at this point in the history
BLE for 770G, JsonEx, Readme devices
  • Loading branch information
benceszasz authored May 4, 2021
2 parents fd94c70 + 2c7c513 commit 8b4ba69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CareLinkClientCLI {

Expand All @@ -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() {
Expand Down Expand Up @@ -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;

}
Expand All @@ -135,6 +144,7 @@ public static void main(String[] args) throws ParseException {
boolean downloadRecentData;
boolean verbose;
boolean anonymize;
boolean dumpJsonException;


Options options = generateOptions();
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -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!");
Expand All @@ -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) {
Expand All @@ -231,30 +256,25 @@ 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());
}

}


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");
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -156,6 +156,7 @@ protected boolean executeLoginProcedure() {

lastLoginSuccess = false;
loginInProcess = true;
lastErrorMessage = null;

try {
// Clear cookies
Expand Down Expand Up @@ -428,6 +429,7 @@ protected <T> T getData(HttpUrl url, RequestBody requestBody, Class<T> dataClass
Object data = null;

this.lastDataSuccess = false;
this.lastErrorMessage = null;

// Get auth token
String authToken = this.getAuthorizationToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

0 comments on commit 8b4ba69

Please sign in to comment.