88import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .ClearedNotification ;
99import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .CountrySettings ;
1010import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .DataUpload ;
11+ import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .DisplayMessage ;
1112import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .Marker ;
1213import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .MonitorData ;
1314import com .eveningoutpost .dexdrip .cgm .carelinkfollow .message .Profile ;
@@ -53,6 +54,9 @@ public class CareLinkClient {
5354 protected String carelinkCountry ;
5455 protected static final String CARELINK_CONNECT_SERVER_EU = "carelink.minimed.eu" ;
5556 protected static final String CARELINK_CONNECT_SERVER_US = "carelink.minimed.com" ;
57+ protected static final String CARELINK_CLOUD_SERVER_EU = "clcloud.minimed.eu" ;
58+ protected static final String CARELINK_CLOUD_SERVER_US = "clcloud.minimed.com" ;
59+ protected static final String API_PATH_DISPLAY_MESSAGE = "connect/carepartner/v11/display/message" ;
5660 protected static final String CARELINK_LANGUAGE_EN = "en" ;
5761 protected static final String CARELINK_AUTH_TOKEN_COOKIE_NAME = "auth_tmp_token" ;
5862 protected static final String CARELINK_TOKEN_VALIDTO_COOKIE_NAME = "c_token_valid_to" ;
@@ -197,6 +201,12 @@ protected String careLinkServer() {
197201 return CARELINK_CONNECT_SERVER_EU ;
198202 }
199203
204+ protected String cloudServer () {
205+ if (this .carelinkCountry .equals ("us" ))
206+ return CARELINK_CLOUD_SERVER_US ;
207+ else
208+ return CARELINK_CLOUD_SERVER_EU ;
209+ }
200210
201211 //Wrapper for common request of recent data (last 24 hours)
202212 public RecentData getRecentData () {
@@ -619,10 +629,14 @@ public RecentData getLast24Hours() {
619629 // Periodic data from CareLink Cloud
620630 public RecentData getConnectDisplayMessage (String username , String role , String patientUsername , String endpointUrl ) {
621631
622- RequestBody requestBody = null ;
623- Gson gson = null ;
624- JsonObject userJson = null ;
632+ RequestBody requestBody ;
633+ Gson gson ;
634+ JsonObject userJson ;
625635 RecentData recentData = null ;
636+ DisplayMessage displayMessage ;
637+ boolean useNewEndpoint ;
638+ HttpUrl newEndpointUrl ;
639+
626640
627641 // Build user json for request
628642 userJson = new JsonObject ();
@@ -635,10 +649,33 @@ public RecentData getConnectDisplayMessage(String username, String role, String
635649
636650 requestBody = RequestBody .create (MediaType .get ("application/json; charset=utf-8" ), gson .toJson (userJson ));
637651
652+ //use new v11 endpoint outside US
653+ useNewEndpoint = !this .carelinkCountry .equals ("us" ) ? true : false ;
654+
655+ //new endpoint url
656+ newEndpointUrl = new HttpUrl .Builder ()
657+ .scheme ("https" )
658+ .host (this .cloudServer ())
659+ .addPathSegments (API_PATH_DISPLAY_MESSAGE )
660+ .build ();
661+
662+ //get data and correct time
638663 try {
639- recentData = this .getData (HttpUrl .parse (endpointUrl ), requestBody , RecentData .class );
640- if (recentData != null )
641- correctTimeInRecentData (recentData );
664+ //Use old data format for old endpoint
665+ if (!useNewEndpoint ) {
666+ recentData = this .getData (HttpUrl .parse (endpointUrl ), requestBody , RecentData .class );
667+ if (recentData != null ) {
668+ correctTimeInRecentData (recentData );
669+ }
670+ }
671+ //Use new data format outside US
672+ else {
673+ displayMessage = this .getData (newEndpointUrl , requestBody , DisplayMessage .class );
674+ if (displayMessage != null && displayMessage .patientData != null ) {
675+ correctTimeInDisplayMessage (displayMessage );
676+ recentData = displayMessage .patientData ;
677+ }
678+ }
642679 } catch (Exception e ) {
643680 lastErrorMessage = e .getClass ().getSimpleName () + ":" + e .getMessage ();
644681 }
@@ -668,6 +705,11 @@ public RecentData getM2MPatientData(String patientUsername) {
668705 }
669706
670707 // General data request for API calls
708+
709+ protected <T > T getData (String host , String path , RequestBody requestBody , Class <T > dataClass ) {
710+ return this .getData (new HttpUrl .Builder ().scheme ("https" ).host (host ).addPathSegments (path ).build (), requestBody , dataClass );
711+ }
712+
671713 protected <T > T getData (HttpUrl url , RequestBody requestBody , Class <T > dataClass ) {
672714
673715 Request .Builder requestBuilder = null ;
@@ -774,6 +816,65 @@ protected void addHttpHeaders(Request.Builder requestBuilder, RequestType type,
774816
775817 }
776818
819+ protected void correctTimeInDisplayMessage (DisplayMessage displayMessage ) {
820+
821+ boolean timezoneMissing = false ;
822+ String offsetString = null ;
823+ RecentData recentData = null ;
824+
825+ recentData = displayMessage .patientData ;
826+
827+ //time data is available to check and correct time if needed
828+ if (recentData .lastConduitDateTime != null && recentData .lastConduitDateTime .getTime () > 1
829+ && recentData .lastConduitUpdateServerDateTime > 1 ) {
830+
831+ //Correct times if server <> device > 26 mins => possibly different time zones
832+ int diffInHour = (int ) Math .round (((recentData .lastConduitUpdateServerDateTime - recentData .lastConduitDateTime .getTime ()) / 3600000D ));
833+ if (diffInHour != 0 && diffInHour < 26 ) {
834+
835+ recentData .lastConduitDateTime = shiftDateByHours (recentData .lastConduitDateTime , diffInHour );
836+
837+ //Sensor glucose
838+ if (recentData .sgs != null ) {
839+ for (SensorGlucose sg : recentData .sgs ) {
840+ if (sg .timestamp != null )
841+ sg .timestamp = shiftDateByHours (sg .timestamp , diffInHour );
842+ }
843+ }
844+
845+ //Markers
846+ if (recentData .markers != null ) {
847+ for (Marker marker : recentData .markers ) {
848+ if (marker .timestamp != null )
849+ marker .timestamp = shiftDateByHours (marker .timestamp , diffInHour );
850+ }
851+ }
852+ //Notifications
853+ if (recentData .notificationHistory != null ) {
854+ if (recentData .notificationHistory .clearedNotifications != null ) {
855+ for (ClearedNotification notification : recentData .notificationHistory .clearedNotifications ) {
856+ if (notification .dateTime != null ) {
857+ notification .dateTime = shiftDateByHours (notification .dateTime , diffInHour );
858+ notification .triggeredDateTime = shiftDateByHours (notification .triggeredDateTime , diffInHour );
859+ }
860+ }
861+ }
862+ if (recentData .notificationHistory .activeNotifications != null ) {
863+ for (ActiveNotification notification : recentData .notificationHistory .activeNotifications ) {
864+ if (notification .dateTime != null )
865+ notification .dateTime = shiftDateByHours (notification .dateTime , diffInHour );
866+ }
867+ }
868+ }
869+
870+ }
871+
872+ }
873+
874+ displayMessage .patientData = recentData ;
875+
876+ }
877+
777878 protected void correctTimeInRecentData (RecentData recentData ) {
778879
779880 boolean timezoneMissing = false ;
0 commit comments