-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNightscoutUploader.java
1419 lines (1253 loc) · 70.9 KB
/
NightscoutUploader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.eveningoutpost.dexdrip.UtilityModels;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Base64;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.MegaStatus;
import com.eveningoutpost.dexdrip.Models.BgReading;
import com.eveningoutpost.dexdrip.Models.BloodTest;
import com.eveningoutpost.dexdrip.Models.Calibration;
import com.eveningoutpost.dexdrip.Models.DateUtil;
import com.eveningoutpost.dexdrip.Models.HeartRate;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.StepCounter;
import com.eveningoutpost.dexdrip.Models.TransmitterData;
import com.eveningoutpost.dexdrip.Models.Treatments;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.Models.LibreBlock;
import com.eveningoutpost.dexdrip.Services.ActivityRecognizedService;
import com.eveningoutpost.dexdrip.utils.CipherUtils;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.eveningoutpost.dexdrip.utils.Mdns;
import com.eveningoutpost.dexdrip.xdrip;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.WriteResult;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteConcern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import okhttp3.CipherSuite;
import okhttp3.Handshake;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.TlsVersion;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;
import static com.eveningoutpost.dexdrip.UtilityModels.OkHttpWrapper.enableTls12OnPreLollipop;
/**
* THIS CLASS WAS BUILT BY THE NIGHTSCOUT GROUP FOR THEIR NIGHTSCOUT ANDROID UPLOADER
* https://github.com/nightscout/android-uploader/
* I have modified this class to make it fit my needs
* Modifications include field remappings and lists instead of arrays
* A DTO would probably be a better future implementation
* -Emma Black
*/
public class NightscoutUploader {
private static final String TAG = NightscoutUploader.class.getSimpleName();
private static final int SOCKET_TIMEOUT = 60000;
private static final int CONNECTION_TIMEOUT = 30000;
private static final boolean d = false;
private static final boolean USE_GZIP = true; // conditional inside interceptor
public static final String VIA_NIGHTSCOUT_LOADER_TAG = "Nightscout Loader";
public static long last_success_time = -1;
public static long last_exception_time = -1;
public static int last_exception_count = 0;
public static String last_exception;
public static final String VIA_NIGHTSCOUT_TAG = "via Nightscout";
private static boolean notification_shown = false;
private static final String LAST_SUCCESS_TREATMENT_DOWNLOAD = "NS-Last-Treatment-Download-Modified";
private static final String ETAG = "ETAG";
private static int failurecount = 0;
private Context mContext;
private Boolean enableRESTUpload;
private Boolean enableMongoUpload;
private SharedPreferences prefs;
private OkHttpClient client;
public interface NightscoutService {
@POST("entries")
Call<ResponseBody> upload(@Header("api-secret") String secret, @Body RequestBody body);
@POST("entries")
Call<ResponseBody> upload(@Body RequestBody body);
@POST("devicestatus")
Call<ResponseBody> uploadDeviceStatus(@Body RequestBody body);
@POST("devicestatus")
Call<ResponseBody> uploadDeviceStatus(@Header("api-secret") String secret, @Body RequestBody body);
@GET("status.json")
Call<ResponseBody> getStatus(@Header("api-secret") String secret);
@POST("treatments")
Call<ResponseBody> uploadTreatments(@Header("api-secret") String secret, @Body RequestBody body);
@PUT("treatments")
Call<ResponseBody> upsertTreatments(@Header("api-secret") String secret, @Body RequestBody body);
@GET("treatments")
// retrofit2/okhttp3 could do the if-modified-since natively using cache
Call<ResponseBody> downloadTreatments(@Header("api-secret") String secret, @Header("BROKEN-If-Modified-Since") String ifmodified);
@GET("treatments.json")
Call<ResponseBody> findTreatmentByUUID(@Header("api-secret") String secret, @Query("find[uuid]") String uuid);
@DELETE("treatments/{id}")
Call<ResponseBody> deleteTreatment(@Header("api-secret") String secret, @Path("id") String id);
@POST("activity")
Call<ResponseBody> uploadActivity(@Header("api-secret") String secret, @Body RequestBody body);
}
private class UploaderException extends RuntimeException {
int code;
public UploaderException (String message, int code) {
super(message);
this.code = code;
}
}
public NightscoutUploader(Context context) {
mContext = context;
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
final OkHttpClient.Builder okHttp3Builder = enableTls12OnPreLollipop(new OkHttpClient.Builder());
if (UserError.ExtraLogTags.shouldLogTag(TAG, android.util.Log.VERBOSE)) {
okHttp3Builder.addInterceptor(new SSLHandshakeInterceptor());
}
if (USE_GZIP) okHttp3Builder.addInterceptor(new GzipRequestInterceptor());
okHttp3Builder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
okHttp3Builder.writeTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
okHttp3Builder.readTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
client = okHttp3Builder.build();
enableRESTUpload = prefs.getBoolean("cloud_storage_api_enable", false);
enableMongoUpload = prefs.getBoolean("cloud_storage_mongodb_enable", false);
}
public static void launchDownloadRest() {
if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")
&& Pref.getBooleanDefaultFalse("cloud_storage_api_download_enable")) {
if (JoH.ratelimit("cloud_treatment_download", 60)) {
final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
uploader.downloadRest(500);
}
}
}
public static boolean isNightscoutCompatible(String url) {
final String vers = getNightscoutVersion(url);
return !(vers.startsWith("0.8") || vers.startsWith("0.7") || vers.startsWith("0.6"));
}
public static String getNightscoutVersion(String url) {
try {
final String store_marker = "nightscout-status-poll-" + url;
final JSONObject status = new JSONObject(PersistentStore.getString(store_marker));
return status.getString("version");
} catch (Exception e) {
return "Unknown";
}
}
private void doStatusUpdate(NightscoutService nightscoutService, String url, String hashedSecret) {
final String store_marker = "nightscout-status-poll-" + url;
final String old_data = PersistentStore.getString(store_marker);
int retry_secs = (old_data.length() == 0) ? 20 : 86400;
if (old_data.equals("error")) retry_secs = 3600;
if (JoH.pratelimit("poll-nightscout-status-" + url, retry_secs)) {
try {
final Response<ResponseBody> r;
r = nightscoutService.getStatus(hashedSecret).execute();
if ((r != null) && (r.isSuccessful())) {
final String response = r.body().string();
if (d) Log.d(TAG, "Status Response: " + response);
// TODO do we need to parse json here or should we just store string?
final JSONObject tr = new JSONObject(response);
if (d) Log.d(TAG, url + " " + tr.toString());
PersistentStore.setString(store_marker, tr.toString());
checkGzipSupport(r);
} else {
PersistentStore.setString(store_marker, "error");
Log.d(TAG, "Failure to get status data from: " + url + " " + ((r != null) ? r.message() : ""));
}
} catch (JSONException e) {
Log.e(TAG, "Got json exception in status update parsing: " + e);
} catch (IOException e) {
Log.e(TAG, "Got exception attempting status update: " + e);
}
}
}
public static String uuid_to_id(String uuid) {
if (uuid.length() == 24) return uuid; // already converted
if (uuid.length() < 24) {
// convert non-standard uuids to compatible ones
return CipherUtils.getMD5(uuid).substring(0,24);
}
return uuid.replaceAll("-", "").substring(0, 24);
}
public boolean downloadRest(final long sleep) {
new Thread(new Runnable() {
@Override
public void run() {
final PowerManager.WakeLock wl = JoH.getWakeLock("ns-download-rest", 180000);
try {
try {
if (sleep > 0) Thread.sleep(sleep);
} catch (InterruptedException e) {
//
}
if (doRESTtreatmentDownload(prefs)) {
Home.staticRefreshBGCharts();
}
} finally {
JoH.releaseWakeLock(wl);
}
}
}).start();
return true;
}
public boolean uploadRest(List<BgReading> glucoseDataSets, List<BloodTest> meterRecords, List<Calibration> calRecords) {
boolean apiStatus = false;
if (enableRESTUpload) {
long start = System.currentTimeMillis();
Log.i(TAG, String.format("Starting upload of %s record using a REST API", glucoseDataSets.size()));
apiStatus = doRESTUpload(prefs, glucoseDataSets, meterRecords, calRecords);
Log.i(TAG, String.format("Finished upload of %s record using a REST API in %s ms result: %b", glucoseDataSets.size(), System.currentTimeMillis() - start, apiStatus));
if (prefs.getBoolean("cloud_storage_api_download_enable", false)) {
start = System.currentTimeMillis();
final boolean substatus = doRESTtreatmentDownload(prefs);
if (substatus) {
Home.staticRefreshBGCharts();
}
Log.i(TAG, String.format("Finished download using a REST API in %s ms result: %b", System.currentTimeMillis() - start, substatus));
}
}
return apiStatus;
}
public boolean uploadMongo(List<BgReading> glucoseDataSets, List<Calibration> meterRecords, List<Calibration> calRecords, List<TransmitterData> transmittersData, List<LibreBlock> libreBlock) {
boolean mongoStatus = false;
if (enableMongoUpload) {
double start = new Date().getTime();
mongoStatus = doMongoUpload(prefs, glucoseDataSets, meterRecords, calRecords, transmittersData, libreBlock);
Log.i(TAG, String.format("Finished upload of %s record using a Mongo in %s ms result: %b",
glucoseDataSets.size() + meterRecords.size() + calRecords.size() + transmittersData.size() + libreBlock.size(), System.currentTimeMillis() - start, mongoStatus));
}
return mongoStatus;
}
private String TryResolveName(String baseURI) {
Log.d(TAG, "Resolveing name" );
URI uri;
try {
uri = new URI(baseURI);
} catch (URISyntaxException e) {
Log.e(TAG, "Error URISyntaxException for the base URL", e);
return baseURI;
}
String host = uri.getHost();
Log.d(TAG, "host = " + host);
// Host has either to end with .local, or be one word (with no dots) for us to try and resolve it.
if (host == null || (host.contains(".") && (!host.endsWith(".local")))) {
return baseURI;
}
// So, we need to resolve this name
String fullHost = host;
try {
if(!fullHost.endsWith(".local")) {
fullHost += ".local";
}
String ip = Mdns.genericResolver(fullHost);
if(ip == null) {
Log.d(TAG, "Recieved null resolving " + fullHost);
return baseURI;
}
// Resolve succeeded, replace host, with the resovled address.
String newUri = baseURI.replace(host, ip);
Log.d(TAG, "Returning new uri " + newUri);
return newUri;
} catch (UnknownHostException e) {
Log.w(TAG, "UnknownHostException error nanme not resovled" + fullHost);
return baseURI;
}
}
private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
final String baseURLSettings = prefs.getString("cloud_storage_api_base", "");
final ArrayList<String> baseURIs = new ArrayList<>();
boolean new_data = false;
Log.d(TAG, "doRESTtreatmentDownload() starting run");
try {
for (String baseURLSetting : baseURLSettings.split(" ")) {
String baseURL = baseURLSetting.trim();
if (baseURL.isEmpty()) continue;
baseURIs.add(baseURL + (baseURL.endsWith("/") ? "" : "/"));
}
} catch (Exception e) {
Log.e(TAG, "Unable to process API Base URL: " + e);
return false;
}
// process a list of base uris
for (String baseURI : baseURIs) {
try {
baseURI = TryResolveName(baseURI);
int apiVersion = 0;
URI uri = new URI(baseURI);
if ((uri.getHost().startsWith("192.168.")) && prefs.getBoolean("skip_lan_uploads_when_no_lan", true) && (!JoH.isLANConnected())) {
Log.d(TAG, "Skipping Nighscout download from: " + uri.getHost() + " due to no LAN connection");
continue;
}
if (uri.getPath().endsWith("/v1/")) apiVersion = 1;
String baseURL;
String secret = uri.getUserInfo();
if ((secret == null || secret.isEmpty()) && apiVersion == 0) {
baseURL = baseURI;
} else if ((secret == null || secret.isEmpty())) {
throw new Exception("Starting with API v1, a pass phase is required");
} else if (apiVersion > 0) {
baseURL = baseURI.replaceFirst("//[^@]+@", "//");
} else {
throw new Exception("Unexpected baseURI: " + baseURI);
}
final Retrofit retrofit = new Retrofit.Builder().baseUrl(baseURL).client(client).build();
final NightscoutService nightscoutService = retrofit.create(NightscoutService.class);
final String checkurl = retrofit.baseUrl().url().toString();
if (!isNightscoutCompatible(checkurl)) {
Log.e(TAG, "Nightscout version: " + getNightscoutVersion(checkurl) + " on " + checkurl + " is not compatible with the Rest-API download feature!");
continue;
}
if (apiVersion == 1) {
final String hashedSecret = Hashing.sha1().hashBytes(secret.getBytes(Charsets.UTF_8)).toString();
final Response<ResponseBody> r;
if (hashedSecret != null) {
doStatusUpdate(nightscoutService, retrofit.baseUrl().url().toString(), hashedSecret); // update status if needed
final String LAST_MODIFIED_KEY = LAST_SUCCESS_TREATMENT_DOWNLOAD + CipherUtils.getMD5(uri.toString()); // per uri marker
String last_modified_string = PersistentStore.getString(LAST_MODIFIED_KEY);
if (last_modified_string.equals("")) last_modified_string = JoH.getRFC822String(0);
final long request_start = JoH.tsl();
r = nightscoutService.downloadTreatments(hashedSecret, last_modified_string).execute();
if ((r != null) && (r.raw().networkResponse().code() == HttpURLConnection.HTTP_NOT_MODIFIED)) {
Log.d(TAG, "Treatments on " + uri.getHost() + ":" + uri.getPort() + " not modified since: " + last_modified_string);
continue; // skip further processing of this url
}
if ((r != null) && (r.isSuccessful())) {
last_modified_string = r.raw().header("Last-Modified", JoH.getRFC822String(request_start));
final String this_etag = r.raw().header("Etag", "");
if (this_etag.length() > 0) {
// older versions of nightscout don't support if-modified-since so check the etag for duplication
if (this_etag.equals(PersistentStore.getString(ETAG + LAST_MODIFIED_KEY))) {
Log.d(TAG, "Skipping Treatments on " + uri.getHost() + ":" + uri.getPort() + " due to etag duplicate: " + this_etag);
continue;
}
PersistentStore.setString(ETAG + LAST_MODIFIED_KEY, this_etag);
}
final String response = r.body().string();
if (d) Log.d(TAG, "Response: " + response);
new_data = NightscoutTreatments.processTreatmentResponse(response);
PersistentStore.setString(LAST_MODIFIED_KEY, last_modified_string);
checkGzipSupport(r);
} else {
Log.d(TAG, "Failed to get treatments from the base URL");
}
} else {
Log.d(TAG, "Old api version not supported");
}
}
} catch (Exception e) {
String msg = "Unable to do REST API Download " + e + e.getMessage();
handleRestFailure(msg);
}
}
Log.d(TAG, "doRESTtreatmentDownload() finishing run");
return new_data;
}
private boolean doRESTUpload(SharedPreferences prefs, List<BgReading> glucoseDataSets, List<BloodTest> meterRecords, List<Calibration> calRecords) {
String baseURLSettings = prefs.getString("cloud_storage_api_base", "");
ArrayList<String> baseURIs = new ArrayList<String>();
try {
for (String baseURLSetting : baseURLSettings.split(" ")) {
String baseURL = baseURLSetting.trim();
if (baseURL.isEmpty()) continue;
baseURIs.add(baseURL + (baseURL.endsWith("/") ? "" : "/"));
}
} catch (Exception e) {
Log.e(TAG, "Unable to process API Base URL: "+e);
return false;
}
boolean any_successes = false;
for (String baseURI : baseURIs) {
try {
baseURI = TryResolveName(baseURI);
int apiVersion = 0;
URI uri = new URI(baseURI);
if ((uri.getHost().startsWith("192.168.")) && prefs.getBoolean("skip_lan_uploads_when_no_lan", true) && (!JoH.isLANConnected()))
{
Log.d(TAG,"Skipping Nighscout upload to: "+uri.getHost()+" due to no LAN connection");
continue;
}
if (uri.getPath().endsWith("/v1/")) apiVersion = 1;
String baseURL;
String secret = uri.getUserInfo();
if ((secret == null || secret.isEmpty()) && apiVersion == 0) {
baseURL = baseURI;
} else if ((secret == null || secret.isEmpty())) {
throw new Exception("Starting with API v1, a pass phase is required");
} else if (apiVersion > 0) {
baseURL = baseURI.replaceFirst("//[^@]+@", "//");
} else {
throw new Exception("Unexpected baseURI: "+baseURI);
}
final Retrofit retrofit = new Retrofit.Builder().baseUrl(baseURL).client(client).build();
final NightscoutService nightscoutService = retrofit.create(NightscoutService.class);
if (apiVersion == 1) {
String hashedSecret = Hashing.sha1().hashBytes(secret.getBytes(Charsets.UTF_8)).toString();
doStatusUpdate(nightscoutService, retrofit.baseUrl().url().toString(), hashedSecret); // update status if needed
doRESTUploadTo(nightscoutService, hashedSecret, glucoseDataSets, meterRecords, calRecords);
} else {
doLegacyRESTUploadTo(nightscoutService, glucoseDataSets);
}
any_successes = true;
last_success_time = JoH.tsl();
last_exception_count = 0;
} catch (Exception e) {
String msg = "Unable to do REST API Upload: " + e.getMessage() + " marking record: " + (any_successes ? "succeeded" : "failed");
handleRestFailure(msg);
}
}
return any_successes;
}
private void doLegacyRESTUploadTo(NightscoutService nightscoutService, List<BgReading> glucoseDataSets) throws Exception {
for (BgReading record : glucoseDataSets) {
Response<ResponseBody> r = nightscoutService.upload(populateLegacyAPIEntry(record)).execute();
if (!r.isSuccessful()) throw new UploaderException(r.message(), r.code());
}
try {
postDeviceStatus(nightscoutService, null);
} catch (Exception e) {
Log.e(TAG, "Ignoring legacy devicestatus post exception: " + e);
}
}
private void doRESTUploadTo(NightscoutService nightscoutService, String secret, List<BgReading> glucoseDataSets, List<BloodTest> meterRecords, List<Calibration> calRecords) throws Exception {
final JSONArray array = new JSONArray();
for (BgReading record : glucoseDataSets) {
populateV1APIBGEntry(array, record);
}
for (BloodTest record : meterRecords) {
populateV1APIMeterReadingEntry(array, record);
}
for (Calibration record : calRecords) {
final BloodTest dupe = BloodTest.getForPreciseTimestamp(record.timestamp, 60000);
if (dupe == null) {
populateV1APIMeterReadingEntry(array, record); // also add calibrations as meter records
} else {
Log.d(TAG, "Found duplicate blood test entry for this calibration record: " + record.bg + " vs " + dupe.mgdl + " mg/dl");
}
populateV1APICalibrationEntry(array, record);
}
if (array.length() > 0) {//KS
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), array.toString());
final Response<ResponseBody> r = nightscoutService.upload(secret, body).execute();
if (!r.isSuccessful()) throw new UploaderException(r.message(), r.code());
checkGzipSupport(r);
try {
postDeviceStatus(nightscoutService, secret);
} catch (Exception e) {
Log.e(TAG, "Ignoring devicestatus post exception: " + e);
}
}
try {
if (Pref.getBooleanDefaultFalse("send_treatments_to_nightscout")) {
postTreatments(nightscoutService, secret);
} else {
Log.d(TAG,"Skipping treatment upload due to preference disabled");
}
} catch (Exception e) {
Log.e(TAG, "Exception uploading REST API treatments: ", e);
if (e.getMessage().equals("Not Found")) {
final String msg = "Please ensure careportal plugin is enabled on nightscout for treatment upload!";
Log.wtf(TAG, msg);
Home.toaststaticnext(msg);
handleRestFailure(msg);
}
}
// TODO we may want to check nightscout version before trying to upload!!
// TODO in the future we may want to merge these in to a single post
if (Pref.getBooleanDefaultFalse("use_pebble_health") && (Home.get_engineering_mode())) {
try {
postHeartRate(nightscoutService, secret);
postStepsCount(nightscoutService, secret);
postMotionTracking(nightscoutService, secret);
} catch (Exception e) {
if (JoH.ratelimit("heartrate-upload-exception", 3600)) {
Log.e(TAG, "Exception uploading REST API heartrate: " + e.getMessage());
}
}
}
}
private static synchronized void handleRestFailure(String msg) {
last_exception = msg;
last_exception_time = JoH.tsl();
last_exception_count++;
if (last_exception_count > 5) {
if (Pref.getBooleanDefaultFalse("warn_nightscout_failures")) {
if (JoH.ratelimit("nightscout-error-notification", 1800)) {
notification_shown = true;
JoH.showNotification("Nightscout Failure", "REST-API upload to Nightscout has failed " + last_exception_count
+ " times. With message: " + last_exception + " " + ((last_success_time > 0) ? "Last succeeded: " + JoH.dateTimeText(last_success_time) : ""),
MegaStatus.getStatusPendingIntent("Uploaders"), Constants.NIGHTSCOUT_ERROR_NOTIFICATION_ID, NotificationChannels.NIGHTSCOUT_UPLOADER_CHANNEL, true, true, null, null, msg);
}
} else {
Log.e(TAG, "Cannot alert for nightscout failures as preference setting is disabled");
}
} else {
if (notification_shown) {
JoH.cancelNotification(Constants.NIGHTSCOUT_ERROR_NOTIFICATION_ID);
notification_shown = false;
}
}
Log.e(TAG, msg);
}
private String getDeviceString(BgReading record) {
String withMethod = "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel");
if (Pref.getBooleanDefaultFalse("nightscout_device_append_source_info") &&
record.source_info != null &&
record.source_info.length() > 0) {
return withMethod + " " + record.source_info;
}
return withMethod;
}
private void populateV1APIBGEntry(JSONArray array, BgReading record) throws Exception {
JSONObject json = new JSONObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
json.put("device", getDeviceString(record));
if (record != null) {//KS
json.put("date", record.timestamp);
json.put("dateString", format.format(record.timestamp));
if(prefs.getBoolean("cloud_storage_api_use_best_glucose", false)){
json.put("sgv", (int) record.getDg_mgdl());
try {
json.put("delta", new BigDecimal(record.getDg_slope() * 5 * 60 * 1000).setScale(3, BigDecimal.ROUND_HALF_UP));
} catch (NumberFormatException e) {
UserError.Log.e(TAG, "Problem calculating delta from getDg_slope() for Nightscout REST Upload, skipping");
}
json.put("direction", record.getDg_deltaName());
} else {
json.put("sgv", (int) record.calculated_value);
try {
json.put("delta", new BigDecimal(record.currentSlope() * 5 * 60 * 1000).setScale(3, BigDecimal.ROUND_HALF_UP)); // jamorham for automation
} catch (NumberFormatException e) {
UserError.Log.e(TAG, "Problem calculating delta from currentSlope() for Nightscout REST Upload, skipping");
}
json.put("direction", record.slopeName());
}
json.put("type", "sgv");
json.put("filtered", record.ageAdjustedFiltered() * 1000);
json.put("unfiltered", record.usedRaw() * 1000);
json.put("rssi", 100);
json.put("noise", record.noiseValue());
json.put("sysTime", format.format(record.timestamp));
array.put(json);
}
else
Log.e(TAG, "doRESTUploadTo BG record is null.");
}
private RequestBody populateLegacyAPIEntry(BgReading record) throws Exception {
JSONObject json = new JSONObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
json.put("device", getDeviceString(record));
json.put("date", record.timestamp);
json.put("dateString", format.format(record.timestamp));
json.put("sgv", (int)record.calculated_value);
json.put("direction", record.slopeName());
return RequestBody.create(MediaType.parse("application/json"), json.toString());
}
private void populateV1APIMeterReadingEntry(JSONArray array, Calibration record) throws Exception {
if (record == null) {
Log.e(TAG, "Received null calibration record in populateV1ApiMeterReadingEntry !");
return;
}
JSONObject json = new JSONObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
json.put("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"));
json.put("type", "mbg");
json.put("date", record.timestamp);
json.put("dateString", format.format(record.timestamp));
json.put("mbg", record.bg);
json.put("sysTime", format.format(record.timestamp));
array.put(json);
}
private void populateV1APIMeterReadingEntry(JSONArray array, BloodTest record) throws Exception {
if (record == null) {
Log.e(TAG, "Received null bloodtest record in populateV1ApiMeterReadingEntry !");
return;
}
JSONObject json = new JSONObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
json.put("device", record.source);
json.put("type", "mbg");
json.put("date", record.timestamp);
json.put("dateString", format.format(record.timestamp));
json.put("mbg", record.mgdl);
json.put("sysTime", format.format(record.timestamp));
array.put(json);
}
private void populateV1APICalibrationEntry(JSONArray array, Calibration record) throws Exception {
if (record == null) {
Log.e(TAG, "Received null calibration record in populateV1ApiCalibrationEntry !");
return;
}
//do not upload undefined slopes
if(record.slope == 0d) return;
JSONObject json = new JSONObject();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
json.put("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"));
json.put("type", "cal");
json.put("date", record.timestamp);
json.put("dateString", format.format(record.timestamp));
if(record.check_in) {
json.put("slope", (record.first_slope));
json.put("intercept", ((record.first_intercept)));
json.put("scale", record.first_scale);
} else {
json.put("slope", (1000/record.slope));
json.put("intercept", ((record.intercept * -1000) / (record.slope)));
json.put("scale", 1);
}
json.put("sysTime", format.format(record.timestamp));
array.put(json);
}
private void populateV1APITreatmentEntry(JSONArray array, Treatments treatment) throws Exception {
if (treatment == null) return;
if (treatment.enteredBy != null && ((treatment.enteredBy.endsWith(VIA_NIGHTSCOUT_TAG)) || (treatment.enteredBy.contains(VIA_NIGHTSCOUT_LOADER_TAG)))) return; // don't send back to nightscout what came from there
final JSONObject record = new JSONObject();
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
record.put("timestamp", treatment.timestamp);
record.put("eventType", treatment.eventType);
record.put("enteredBy", treatment.enteredBy);
record.put("notes", treatment.notes);
record.put("uuid", treatment.uuid);
record.put("carbs", treatment.carbs);
record.put("insulin", treatment.insulin);
if (treatment.insulinJSON != null) {
record.put("insulinInjections", treatment.insulinJSON);
}
record.put("created_at", treatment.created_at);
record.put("sysTime", format.format(treatment.timestamp));
array.put(record);
}
private void postTreatments(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing treatments for RESTAPI");
final long THIS_QUEUE = UploaderQueue.NIGHTSCOUT_RESTAPI;
final List<UploaderQueue> tups = UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE);
if (tups != null) {
JSONArray insert_array = new JSONArray();
JSONArray upsert_array = new JSONArray();
for (UploaderQueue up : tups) {
if ((up.action.equals("insert") || (up.action.equals("update")))) {
final Treatments treatment = Treatments.byid(up.reference_id);
if (up.action.equals("insert")) {
//populateV1APITreatmentEntry(insert_array, treatment);
// TODO always use singular upserts for now
populateV1APITreatmentEntry(upsert_array, treatment);
} else if (up.action.equals("update")) {
populateV1APITreatmentEntry(upsert_array, treatment);
}
} else if (up.action.equals("delete")) {
if (up.reference_uuid != null) {
if (apiSecret != null) {
// do we already have a nightscout style reference id
String this_id = up.reference_uuid.length() == 24 ? up.reference_uuid : null;
Response<ResponseBody> lookup = null;
if (this_id == null) {
// look up the _id to delete as we can't use find with delete action nor can we specify our own _id on submission circa nightscout 0.9.2
lookup = nightscoutService.findTreatmentByUUID(apiSecret, up.reference_uuid).execute();
}
// throw an exception if we failed lookup
if ((this_id == null) && (lookup != null) && !lookup.isSuccessful()) {
throw new UploaderException(lookup.message(), lookup.code());
} else {
// parse the result
if (this_id == null) {
try {
final String response = lookup.body().string();
final JSONArray jsonArray = new JSONArray(response);
final JSONObject tr = (JSONObject) jsonArray.get(0); // can only be one
this_id = tr.getString("_id");
} catch (Exception e) {
Log.e(TAG, "Got exception parsing treatment lookup response: " + e);
}
}
// is the id valid now?
if ((this_id != null) && (this_id.length() == 24)) {
final Response<ResponseBody> r = nightscoutService.deleteTreatment(apiSecret, this_id).execute();
if (!r.isSuccessful()) {
throw new UploaderException(r.message(), r.code());
} else {
up.completed(THIS_QUEUE);
Log.d(TAG, "Success for RESTAPI treatment delete: " + up.reference_uuid + " _id: " + this_id);
}
} else {
Log.wtf(TAG, "Couldn't find a reference _id for uuid: " + up.reference_uuid + " got: " + this_id);
up.completed(THIS_QUEUE); // don't retry
}
}
} else {
Log.wtf(TAG, "Cannot delete treatments without api secret being set");
}
}
} else {
Log.wtf(TAG, "Unsupported operation type for treatment: " + up.action);
up.completed(THIS_QUEUE); // don't retry it
}
}
// handle insert types
if (insert_array.length() != 0) {
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), insert_array.toString());
final Response<ResponseBody> r;
if (apiSecret != null) {
r = nightscoutService.uploadTreatments(apiSecret, body).execute();
if (!r.isSuccessful()) {
throw new UploaderException(r.message(), r.code());
} else {
Log.d(TAG, "Success for RESTAPI treatment insert upload");
for (UploaderQueue up : tups) {
if (up.action.equals("insert")) {
up.completed(THIS_QUEUE); // approve all types for this queue
}
}
checkGzipSupport(r);
}
} else {
Log.wtf(TAG, "Cannot upload treatments without api secret being set");
}
}
// handle upsert types
if (upsert_array.length() != 0) {
for (int i = 0; i < upsert_array.length(); i++) {
JSONObject item = (JSONObject) upsert_array.get(i);
final String match_uuid = item.getString("uuid");
item.put("_id", uuid_to_id(match_uuid));
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), item.toString());
final Response<ResponseBody> r;
if (apiSecret != null) {
r = nightscoutService.upsertTreatments(apiSecret, body).execute();
if (!r.isSuccessful()) {
throw new UploaderException(r.message(), r.code());
} else {
Log.d(TAG, "Success for RESTAPI treatment upsert upload: " + match_uuid);
for (UploaderQueue up : tups) {
if (d) Log.d(TAG, "upsert: " + match_uuid + " / " + up.reference_uuid + " " + up.action + " " + up.reference_id);
if ((up.action.equals("update") || (up.action.equals("insert")))
&& (up.reference_uuid.equals(match_uuid) || (uuid_to_id(up.reference_uuid).equals(match_uuid)))) {
if( d) Log.d(TAG, "upsert: matched");
up.completed(THIS_QUEUE); // approve all types for this queue
break;
}
}
checkGzipSupport(r);
}
} else {
Log.wtf(TAG, "Cannot upload treatments without api secret being set");
return;
}
}
// if we got this far without exception then mark everything as completed to fix harmless erroneous queue entries
for (UploaderQueue up : tups) {
if (d) Log.d(TAG, "Marking all items completed");
up.completed(THIS_QUEUE);
}
}
}
}
private static int activityErrorCount = 0;
private static final int MAX_ACTIVITY_RECORDS = 500;
private void postHeartRate(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing heartrate for RESTAPI");
if (apiSecret != null) {
final String STORE_COUNTER = "nightscout-rest-heartrate-synced-time";
final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
final List<HeartRate> readings = HeartRate.latestForGraph((MAX_ACTIVITY_RECORDS / Math.min(1, Math.max(activityErrorCount, MAX_ACTIVITY_RECORDS / 10))), syncedTillTime);
final JSONArray data = new JSONArray();
//final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
//format.setTimeZone(TimeZone.getDefault());
long highest_timestamp = 0;
if (readings.size() > 0) {
for (HeartRate reading : readings) {
final JSONObject json = new JSONObject();
json.put("type", "hr-bpm");
json.put("timeStamp", reading.timestamp);
//json.put("dateString", format.format(reading.timestamp));
json.put("created_at", DateUtil.toISOString(reading.timestamp));
json.put("bpm", reading.bpm);
if (reading.accuracy != 1) json.put("accuracy", reading.accuracy);
data.put(json);
highest_timestamp = Math.max(highest_timestamp, reading.timestamp);
}
// send to nightscout - update counter
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), data.toString());
Response<ResponseBody> r;
r = nightscoutService.uploadActivity(apiSecret, body).execute();
if (!r.isSuccessful()) {
activityErrorCount++;
if (JoH.ratelimit("heartrate-unable-upload",3600)) {
UserError.Log.e(TAG, "Unable to upload heart-rate data to Nightscout - check nightscout version");
}
throw new UploaderException(r.message(), r.code());
} else {
PersistentStore.setLong(STORE_COUNTER, highest_timestamp);
UserError.Log.d(TAG, "Updating heartrate synced record count (success) " + JoH.dateTimeText(highest_timestamp) + " Processed: " + readings.size() + " records");
checkGzipSupport(r);
}
}
} else {
UserError.Log.e(TAG, "Api secret is null");
}
}
private void postStepsCount(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing steps for RESTAPI");
final String STORE_COUNTER = "nightscout-rest-steps-synced-time";
if (apiSecret != null) {
final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
final List<StepCounter> readings = StepCounter.latestForGraph((MAX_ACTIVITY_RECORDS / Math.min(1, Math.max(activityErrorCount, MAX_ACTIVITY_RECORDS / 10))), syncedTillTime);
final JSONArray data = new JSONArray();
//final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
//format.setTimeZone(TimeZone.getDefault());
long highest_timestamp = 0;
if (readings.size() > 0) {
for (StepCounter reading : readings) {
final JSONObject json = new JSONObject();
json.put("type", "steps-total");
json.put("timeStamp", reading.timestamp);
//json.put("dateString", format.format(reading.timestamp));
json.put("created_at", DateUtil.toISOString(reading.timestamp));
json.put("steps", reading.metric);
data.put(json);
highest_timestamp = Math.max(highest_timestamp, reading.timestamp);
}
// send to nightscout - update counter
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), data.toString());
Response<ResponseBody> r;
r = nightscoutService.uploadActivity(apiSecret, body).execute();
if (!r.isSuccessful()) {
activityErrorCount++;
UserError.Log.e(TAG, "Unable to upload steps data to Nightscout - check nightscout version");
throw new UploaderException(r.message(), r.code());
} else {
PersistentStore.setLong(STORE_COUNTER, highest_timestamp);
UserError.Log.e(TAG, "Updating steps synced record count (success) " + JoH.dateTimeText(highest_timestamp) + " Processed: " + readings.size() + " records");
checkGzipSupport(r);
}
}
} else {
UserError.Log.e(TAG, "Api secret is null");
}
}
private void postMotionTracking(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing motion tracking for RESTAPI");
final String STORE_COUNTER = "nightscout-rest-motion-synced-time";
if (apiSecret != null) {
final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
final ArrayList<ActivityRecognizedService.motionData> readings = ActivityRecognizedService.getForGraph(syncedTillTime, JoH.tsl());
int counter = 0;
final JSONArray data = new JSONArray();
//final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
//format.setTimeZone(TimeZone.getDefault());
long highest_timestamp = 0;
if (readings.size() > 0) {
for (ActivityRecognizedService.motionData reading : readings) {
counter++;
if (counter > (MAX_ACTIVITY_RECORDS / Math.min(1, Math.max(activityErrorCount, MAX_ACTIVITY_RECORDS / 10)))) break;
final JSONObject json = new JSONObject();
json.put("type", "motion-class");
json.put("timeStamp", reading.timestamp);
//json.put("dateString", format.format(reading.timestamp));
json.put("created_at", DateUtil.toISOString(reading.timestamp));
json.put("class", reading.toPrettyType());
data.put(json);
highest_timestamp = Math.max(highest_timestamp, reading.timestamp);
}
// send to nightscout - update counter