Skip to content

Commit

Permalink
Nightscout: Support for direct upload of Treatments + Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 16, 2016
1 parent bd652b3 commit 0d83955
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
android:value="41" />
<meta-data
android:name="AA_MODELS"
android:value="com.eveningoutpost.dexdrip.Models.ActiveBgAlert,com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice,com.eveningoutpost.dexdrip.Models.AlertType,com.eveningoutpost.dexdrip.Models.BgReading,com.eveningoutpost.dexdrip.Models.BgReading,com.eveningoutpost.dexdrip.Models.Calibration,com.eveningoutpost.dexdrip.Models.Calibration,com.eveningoutpost.dexdrip.Models.CalibrationRequest,com.eveningoutpost.dexdrip.Models.Sensor,com.eveningoutpost.dexdrip.Models.TransmitterData,com.eveningoutpost.dexdrip.Models.Treatments,com.eveningoutpost.dexdrip.Models.UserError,com.eveningoutpost.dexdrip.Models.UserNotification,com.eveningoutpost.dexdrip.ShareModels.Models,com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue,com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue,com.eveningoutpost.dexdrip.UtilityModels.SensorSendQueue,com.eveningoutpost.dexdrip.Models.HeartRate,com.eveningoutpost.dexdrip.Models.PebbleMovement"/>
android:value="com.eveningoutpost.dexdrip.Models.ActiveBgAlert,com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice,com.eveningoutpost.dexdrip.Models.AlertType,com.eveningoutpost.dexdrip.Models.BgReading,com.eveningoutpost.dexdrip.Models.BgReading,com.eveningoutpost.dexdrip.Models.Calibration,com.eveningoutpost.dexdrip.Models.Calibration,com.eveningoutpost.dexdrip.Models.CalibrationRequest,com.eveningoutpost.dexdrip.Models.Sensor,com.eveningoutpost.dexdrip.Models.TransmitterData,com.eveningoutpost.dexdrip.Models.Treatments,com.eveningoutpost.dexdrip.Models.UserError,com.eveningoutpost.dexdrip.Models.UserNotification,com.eveningoutpost.dexdrip.ShareModels.Models,com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue,com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue,com.eveningoutpost.dexdrip.UtilityModels.SensorSendQueue,com.eveningoutpost.dexdrip.Models.HeartRate,com.eveningoutpost.dexdrip.Models.PebbleMovement,com.eveningoutpost.dexdrip.UtilityModels.UploaderQueue"/>

<provider
android:name="com.activeandroid.content.ContentProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.Services.SyncService;
import com.eveningoutpost.dexdrip.UtilityModels.UndoRedo;
import com.eveningoutpost.dexdrip.UtilityModels.UploaderQueue;
import com.eveningoutpost.dexdrip.xdrip;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
Expand Down Expand Up @@ -112,8 +114,9 @@ public static synchronized Treatments create(final double carbs, final double in
Treatment.created_at = DateUtil.toISOString(timestamp);
Treatment.uuid = UUID.randomUUID().toString();
Treatment.save();
GcmActivity.pushTreatmentAsync(Treatment);
NSClientChat.pushTreatmentAsync(Treatment);
// GcmActivity.pushTreatmentAsync(Treatment);
// NSClientChat.pushTreatmentAsync(Treatment);
pushTreatmentSync(Treatment);
UndoRedo.addUndoTreatment(Treatment.uuid);
return Treatment;
}
Expand Down Expand Up @@ -176,9 +179,12 @@ public static synchronized Treatments create_note(String note, long timestamp, d
return Treatment;
}

public static void pushTreatmentSync(Treatments Treatment) {
GcmActivity.pushTreatmentAsync(Treatment);
NSClientChat.pushTreatmentAsync(Treatment);
public static void pushTreatmentSync(Treatments treatment) {
GcmActivity.pushTreatmentAsync(treatment);
NSClientChat.pushTreatmentAsync(treatment);
if (UploaderQueue.newEntry("insert",treatment) != null) {
SyncService.startSyncService(3000); // sync in 3 seconds
}
}

// This shouldn't be needed but it seems it is
Expand Down Expand Up @@ -223,6 +229,13 @@ public static Treatments byuuid(String uuid) {
.executeSingle();
}

public static Treatments byid(long id) {
return new Select()
.from(Treatments.class)
.where("_ID = ?", id)
.executeSingle();
}

public static Treatments byTimestamp(long timestamp) {
return byTimestamp(timestamp, 1500);
}
Expand All @@ -246,6 +259,7 @@ public static void delete_all(boolean from_interactive) {
new Delete()
.from(Treatments.class)
.execute();
// not synced with uploader queue - should we?
}

public static Treatments delete_last() {
Expand All @@ -261,9 +275,12 @@ public static void delete_by_uuid(String uuid, boolean from_interactive) {
Treatments thistreat = byuuid(uuid);
if (thistreat != null) {

UploaderQueue.newEntry("delete",thistreat);
if (from_interactive) {
GcmActivity.push_delete_treatment(thistreat);
SyncService.startSyncService(3000); // sync in 3 seconds
}

thistreat.delete();
Home.staticRefreshBGCharts();
}
Expand All @@ -278,6 +295,7 @@ public static Treatments delete_last(boolean from_interactive) {
//GoogleDriveInterface gdrive = new GoogleDriveInterface();
//gdrive.deleteTreatmentAtRemote(thistreat.uuid);
}
UploaderQueue.newEntry("delete",thistreat);
thistreat.delete();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue;
import com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue;
import com.eveningoutpost.dexdrip.UtilityModels.UploaderQueue;

import static com.eveningoutpost.dexdrip.UtilityModels.UpdateActivity.checkForAnUpdate;

Expand All @@ -19,11 +22,29 @@ public DailyIntentService() {
@Override
protected void onHandleIntent(Intent intent) {
final PowerManager.WakeLock wl = JoH.getWakeLock("DailyIntentService", 120000);

// prune old database records
try {
UserError.cleanup();
} catch (Exception e) {
//
}
try {
BgSendQueue.cleanQueue();
} catch (Exception e) {
//
}
try {
CalibrationSendQueue.cleanQueue();
} catch (Exception e) {
//
}
try {
UploaderQueue.cleanQueue();
} catch (Exception e) {
//
}

try {
checkForAnUpdate(getApplicationContext());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.UtilityModels.MongoSendTask;
import com.eveningoutpost.dexdrip.xdrip;
Expand Down Expand Up @@ -41,18 +42,24 @@ public void attemptSend() {

public void setRetryTimer() {
if (enableRESTUpload || enableMongoUpload) { //Check for any upload type being enabled
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
long wakeTime = calendar.getTimeInMillis() + (1000 * 60 * 6);
// make it up on the next BG reading or retry if the reading doesn't materialize
// TODO THIS NEEDS UPDATING
PendingIntent serviceIntent = PendingIntent.getService(this, 0, new Intent(this, SyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);
alarm.set(AlarmManager.RTC_WAKEUP, wakeTime, serviceIntent);
final PendingIntent serviceIntent = PendingIntent.getService(this, 0, new Intent(this, SyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);
JoH.wakeUpIntent(this,(1000 * 60 * 6),serviceIntent); // TODO use static method below instead
}
}

public void syncToMongoDb() {
private void syncToMongoDb() {
// TODO does this need locking?
MongoSendTask task = new MongoSendTask(getApplicationContext());
task.executeOnExecutor(xdrip.executor);
}

public static void startSyncService(long delay) {
Log.d("SyncService", "static starting Sync service delay: " + delay);
if (delay == 0) {
xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), SyncService.class));
} else {
final PendingIntent serviceIntent = PendingIntent.getService(xdrip.getAppContext(), 0, new Intent(xdrip.getAppContext(), SyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);
JoH.wakeUpIntent(xdrip.getAppContext(), delay, serviceIntent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.BestGlucose;
import com.eveningoutpost.dexdrip.GcmActivity;
Expand Down Expand Up @@ -75,6 +76,15 @@ public static List<BgSendQueue> mongoQueue() {
.execute();
}

public static List<BgSendQueue> cleanQueue() {
return new Delete()
.from(BgSendQueue.class)
.where("mongo_success = ?", true)
.where("operation_type = ?", "create")
.limit(5000)
.execute();
}

private static void addToQueue(BgReading bgReading, String operation_type) {
BgSendQueue bgSendQueue = new BgSendQueue();
bgSendQueue.operation_type = operation_type;
Expand All @@ -99,7 +109,10 @@ public static void handleNewBgReading(BgReading bgReading, String operation_type
"sendQueue");
wakeLock.acquire();
try {
if (!is_follower) addToQueue(bgReading, operation_type);
if (!is_follower) {
addToQueue(bgReading, operation_type);
//UploaderQueue.newEntry("insert",bgReading);
}

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.Models.Calibration;

Expand Down Expand Up @@ -45,6 +46,14 @@ public static List<CalibrationSendQueue> mongoQueue() {
.limit(20)
.execute();
}

public static List<CalibrationSendQueue> cleanQueue() {
return new Delete()
.from(CalibrationSendQueue.class)
.where("mongo_success = ?", true)
.execute();
}

public static void addToQueue(Calibration calibration, Context context) {
CalibrationSendQueue calibrationSendQueue = new CalibrationSendQueue();
calibrationSendQueue.calibration = calibration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.content.Context;
import android.os.AsyncTask;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.Treatments;
import com.eveningoutpost.dexdrip.Models.UserError.Log;

import com.eveningoutpost.dexdrip.Models.BgReading;
Expand All @@ -28,37 +31,40 @@ public MongoSendTask(Context pContext) {
context = pContext;
}

public Void doInBackground(String... urls) {
try {
List<BgReading> bgReadings = new ArrayList<BgReading>();
List<Calibration> calibrations = new ArrayList<Calibration>();
for (CalibrationSendQueue job : calibrationsQueue) {
calibrations.add(job.calibration);
}
for (BgSendQueue job : bgsQueue) {
bgReadings.add(job.bgReading);
}

if(bgReadings.size() + calibrations.size() > 0) {
Log.i(TAG, "uploader.upload called " + bgReadings.size());
NightscoutUploader uploader = new NightscoutUploader(context);
boolean uploadStatus = uploader.upload(bgReadings, calibrations, calibrations);
if (uploadStatus) {
for (CalibrationSendQueue calibration : calibrationsQueue) {
calibration.markMongoSuccess();
}
for (BgSendQueue bgReading : bgsQueue) {
bgReading.markMongoSuccess();
}
public Void doInBackground(String... urls) {
try {
List<BgReading> bgReadings = new ArrayList<BgReading>();
List<Calibration> calibrations = new ArrayList<Calibration>();
for (CalibrationSendQueue job : calibrationsQueue) {
calibrations.add(job.calibration);
}
for (BgSendQueue job : bgsQueue) {
bgReadings.add(job.bgReading);
}
if ((bgReadings.size() > 0) || (calibrations.size() > 0)
|| (Home.getPreferencesBooleanDefaultFalse("cloud_storage_mongodb_enable")
&& (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), UploaderQueue.MONGO_DIRECT, 1).size() > 0))
|| (Home.getPreferencesBooleanDefaultFalse("cloud_storage_api_enable")
&& (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), UploaderQueue.NIGHTSCOUT_RESTAPI, 1).size() > 0))) {
Log.i(TAG, "uploader.upload called " + bgReadings.size());
NightscoutUploader uploader = new NightscoutUploader(context);
boolean uploadStatus = uploader.upload(bgReadings, calibrations, calibrations);
if (uploadStatus) {
for (CalibrationSendQueue calibration : calibrationsQueue) {
calibration.markMongoSuccess();
}
for (BgSendQueue bgReading : bgsQueue) {
bgReading.markMongoSuccess();
}
}
} catch (Exception e) {
Log.e(TAG, "caught exception", e);
this.exception = e;
return null;
}
} catch (Exception e) {
Log.e(TAG, "caught exception", e);
this.exception = e;
return null;
}
return null;
}

// protected void onPostExecute(RSSFeed feed) {
// // TODO: check this.exception
Expand Down
Loading

0 comments on commit 0d83955

Please sign in to comment.