Skip to content

Commit

Permalink
NightScout: note editing sync improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed May 30, 2017
1 parent 6dcfa99 commit 005c0c8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
58 changes: 34 additions & 24 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/Treatments.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.Services.SyncService;
import com.eveningoutpost.dexdrip.UtilityModels.NightscoutUploader;
import com.eveningoutpost.dexdrip.UtilityModels.UndoRedo;
import com.eveningoutpost.dexdrip.UtilityModels.UploaderQueue;
import com.eveningoutpost.dexdrip.xdrip;
Expand Down Expand Up @@ -149,41 +150,50 @@ public static synchronized Treatments create_note(String note, long timestamp, d

boolean is_new = false;
// find treatment
Treatments Treatment = byTimestamp(timestamp, 60 * 1000 * 5);

Treatments treatment = byTimestamp(timestamp, 60 * 1000 * 5);
// if unknown create
if (Treatment == null) {
Treatment = new Treatments();
if (treatment == null) {
treatment = new Treatments();
Log.d(TAG, "Creating new treatment entry for note");
is_new = true;

Treatment.eventType = "<none>";
Treatment.carbs = 0;
Treatment.insulin = 0;
Treatment.notes = note;
Treatment.timestamp = timestamp;
Treatment.created_at = DateUtil.toISOString(timestamp);
Treatment.uuid = suggested_uuid != null ? suggested_uuid : UUID.randomUUID().toString();
treatment.eventType = "<none>";
treatment.carbs = 0;
treatment.insulin = 0;
treatment.notes = note;
treatment.timestamp = timestamp;
treatment.created_at = DateUtil.toISOString(timestamp);
treatment.uuid = suggested_uuid != null ? suggested_uuid : UUID.randomUUID().toString();

} else {
if (Treatment.notes == null) Treatment.notes = "";
Log.d(TAG, "Found existing treatment for note: " + Treatment.uuid + " distance:" + Long.toString(timestamp - Treatment.timestamp) + " " + Treatment.notes);
if (treatment.notes == null) treatment.notes = "";
Log.d(TAG, "Found existing treatment for note: " + treatment.uuid + ((suggested_uuid != null) ? " vs suggested: " + suggested_uuid : "") + " distance:" + Long.toString(timestamp - treatment.timestamp) + " " + treatment.notes);
if (treatment.notes.contains(note)) {
Log.d(TAG, "Suggested note update already present - skipping");
return null;
}
// append existing note or treatment
if (Treatment.notes.length() > 0) Treatment.notes += " \u2192 ";
Treatment.notes += note;
if (treatment.notes.length() > 0) treatment.notes += " \u2192 ";
treatment.notes += note;
Log.d(TAG, "Final notes: " + treatment.notes);
}

if (position > 0) {
Treatment.enteredBy = XDRIP_TAG + " pos:" + JoH.qs(position, 2);
} else {
Treatment.enteredBy = XDRIP_TAG;
// if ((treatment.enteredBy == null) || (!treatment.enteredBy.contains(NightscoutUploader.VIA_NIGHTSCOUT_TAG))) {
// tag it as from xdrip if it isn't being synced from nightscout right now to allow local updates to nightscout sourced notes
if (suggested_uuid == null) {
if (position > 0) {
treatment.enteredBy = XDRIP_TAG + " pos:" + JoH.qs(position, 2);
} else {
treatment.enteredBy = XDRIP_TAG;
}
}

treatment.save();

Treatment.save();
pushTreatmentSync(Treatment, is_new, suggested_uuid);
if (is_new) UndoRedo.addUndoTreatment(Treatment.uuid);
return Treatment;
pushTreatmentSync(treatment, is_new, suggested_uuid);
if (is_new) UndoRedo.addUndoTreatment(treatment.uuid);

return treatment;
}

public static synchronized Treatments SensorStart(long timestamp) {
Expand All @@ -205,7 +215,7 @@ private static void pushTreatmentSync(Treatments treatment) {
pushTreatmentSync(treatment, true, null); // new entry by default
}

private static void pushTreatmentSync(Treatments treatment, boolean is_new, String suggested_uuid) {
private static void pushTreatmentSync(Treatments treatment, boolean is_new, String suggested_uuid) {;
if (Home.get_master_or_follower()) GcmActivity.pushTreatmentAsync(treatment);

if (!(Home.getPreferencesBoolean("cloud_storage_api_enable", false) || Home.getPreferencesBoolean("cloud_storage_mongodb_enable", false))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -81,6 +82,8 @@ public class NightscoutUploader {
public static final String VIA_NIGHTSCOUT_TAG = "via Nightscout";

private static int failurecount = 0;
private static HashSet<String> bad_uuids = new HashSet<>();
private static HashSet<String> bad_bloodtest_uuids = new HashSet<>();
private Context mContext;
private Boolean enableRESTUpload;
private Boolean enableMongoUpload;
Expand Down Expand Up @@ -294,6 +297,10 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
// TODO if we are using upsert then we should favour _id over uuid!?
final String uuid = (tr.has("uuid") && (tr.getString("uuid") != null)) ? tr.getString("uuid") : UUID.nameUUIDFromBytes(tr.getString("_id").getBytes("UTF-8")).toString();
final String nightscout_id = (tr.getString("_id") == null) ? uuid : tr.getString("_id");
if (bad_uuids.contains(nightscout_id)) {
Log.d(TAG, "Skipping previously baulked uuid: " + nightscout_id);
continue;
}
if (d) Log.d(TAG, "event: " + etype + "_id: "+nightscout_id+" uuid:" + uuid);

boolean from_xdrip = false;
Expand All @@ -309,6 +316,10 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
try {
if (!from_xdrip) {
if (tr.getString("glucoseType").equals("Finger")) {
if (bad_bloodtest_uuids.contains(nightscout_id)) {
Log.d(TAG, "Skipping baulked bloodtest nightscout id: " + nightscout_id);
continue;
}
final BloodTest existing = BloodTest.byUUID(uuid);
if (existing == null) {
final long timestamp = DateUtil.tolerantFromISODateString(tr.getString("created_at")).getTime();
Expand All @@ -323,6 +334,7 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
Log.ueh(TAG, "Received new Bloodtest data from Nightscout: " + BgGraphBuilder.unitized_string_with_units_static(mgdl) + " @ " + JoH.dateTimeText(timestamp));
} else {
Log.d(TAG, "Error creating bloodtest record");
bad_bloodtest_uuids.add(nightscout_id);
}
} else {
if (d)
Expand Down Expand Up @@ -357,7 +369,7 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
} catch (JSONException e) {
// Log.d(TAG, "json processing: " + e);
}
if ((notes != null) && ((notes.equals("AndroidAPS started") || notes.equals("null"))))
if ((notes != null) && ((notes.equals("AndroidAPS started") || notes.equals("null") || (notes.equals("Bolus Std")))))
notes = null;

if ((carbs > 0) || (insulin > 0) || (notes != null)) {
Expand All @@ -374,8 +386,14 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
final Treatments t;
if ((carbs > 0) || (insulin > 0)) {
t = Treatments.create(carbs, insulin, timestamp, nightscout_id);
if (notes != null) t.notes = notes;
} else {
t = Treatments.create_note(notes, timestamp, -1, nightscout_id);
if (t == null) {
Log.d(TAG, "Create note baulked and returned null, so skipping");
bad_uuids.add(nightscout_id);
continue;
}
}

//t.uuid = nightscout_id; // replace with nightscout uuid
Expand All @@ -384,7 +402,7 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
} catch (JSONException e) {
t.enteredBy = VIA_NIGHTSCOUT_TAG;
}
if (notes != null) t.notes = notes;

t.save();
// sync again!
// pushTreatmentSync(t, false);
Expand All @@ -396,13 +414,17 @@ private synchronized boolean doRESTtreatmentDownload(SharedPreferences prefs) {
if (notes == null) notes = "";
if (existing.notes == null) existing.notes = "";
if ((existing.carbs != carbs) || (existing.insulin != insulin) || ((existing.timestamp / Constants.SECOND_IN_MS) != (timestamp / Constants.SECOND_IN_MS))
|| (!existing.notes.equals(notes))) {
|| (!existing.notes.contains(notes))) {
Log.ueh(TAG, "Treatment changes from Nightscout: " + carbs + " Insulin: " + insulin + " timestamp: " + JoH.dateTimeText(timestamp) + " " + notes + " " + " vs " + existing.carbs + " " + existing.insulin + " " + JoH.dateTimeText(existing.timestamp) + " " + existing.notes);
existing.carbs = carbs;
existing.insulin = insulin;
existing.timestamp = timestamp;
existing.created_at = DateUtil.toISOString(timestamp);
existing.notes = notes;
if (existing.notes.length() > 0) {
existing.notes += " \u2192 " + notes;
} else {
existing.notes = notes;
}
existing.save();
new_data = true;
}
Expand Down

0 comments on commit 005c0c8

Please sign in to comment.