Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libre2 sync raw #3371

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/GcmActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.eveningoutpost.dexdrip.models.Calibration;
import com.eveningoutpost.dexdrip.models.DesertSync;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.Libre2RawValue;
import com.eveningoutpost.dexdrip.models.RollCall;
import com.eveningoutpost.dexdrip.models.Sensor;
import com.eveningoutpost.dexdrip.models.Treatments;
Expand Down Expand Up @@ -293,6 +294,16 @@ public synchronized static void syncBGReading(BgReading bgReading) {
}
}

// Synchronization of Libre2 patched app received raw values
public synchronized static void syncLibre2RawReading(Libre2RawValue rawValue) {
if (rawValue == null) {
UserError.Log.wtf(TAG, "Cannot sync null libre2rawvalue - should never occur");
return;
}
Log.d(TAG, "syncLibre2RawReading called");
GcmActivity.sendMessage("l2rs", rawValue.toJSON());
}

// called only from interactive or evaluated new data
public synchronized static void syncBloodTests() {
Log.d(TAG, "syncBloodTests called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.eveningoutpost.dexdrip.models.Calibration;
import com.eveningoutpost.dexdrip.models.DesertSync;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.Libre2RawValue;
import com.eveningoutpost.dexdrip.models.LibreBlock;
import com.eveningoutpost.dexdrip.models.RollCall;
import com.eveningoutpost.dexdrip.models.Sensor;
Expand Down Expand Up @@ -562,6 +563,9 @@ public void run() {
}
} else if (action.equals("libreBlock") || action.equals("libreBlck")) {
HandleLibreBlock(payload);
} else if (action.equals("l2rs")) {
Libre2RawValue currentRawValue = Libre2RawValue.fromJSON(payload);
currentRawValue.save();
} else {
Log.e(TAG, "Received message action we don't know about: " + action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ public void run() {
processValues(currentRawValue, smoothingValues, smoothing_minutes, context);
}
currentRawValue.save();

clearNFCsensorAge();

// send raw values to sync+ followers
if (Pref.getBooleanDefaultFalse("plus_follow_master") // we are the master
&& !Pref.getBooleanDefaultFalse("plus_follower_save_power") // we don't want to save power
&& Pref.getBooleanDefaultFalse("Libre2_showRawGraph")) // we want to see raw values
GcmActivity.syncLibre2RawReading(currentRawValue);

break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package com.eveningoutpost.dexdrip.models;


import android.provider.BaseColumns;
import android.util.Log;

import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.utilitymodels.Constants;

import java.util.Date;
import java.util.List;

import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import org.json.JSONException;
import org.json.JSONObject;

@Table(name = "Libre2RawValue2", id = BaseColumns._ID)
public class Libre2RawValue extends PlusModel {
private static final String TAG = "libre2rawvalue";

static final String[] schema = {
"DROP TABLE Libre2RawValue;",
"CREATE TABLE Libre2RawValue2 (_id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, serial STRING, glucose REAL);",
"CREATE INDEX index_Libre2RawValue2_ts on Libre2RawValue2(ts);"
"DROP TABLE Libre2RawValue;",
"CREATE TABLE Libre2RawValue2 (_id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER, serial STRING, glucose REAL);",
"CREATE INDEX index_Libre2RawValue2_ts on Libre2RawValue2(ts);"
};

@Column(name = "serial", index = true)
Expand Down Expand Up @@ -69,4 +74,40 @@ public static List<Libre2RawValue> cleanup(final int retention_days) {
public static void updateDB() {
fixUpTable(schema, false);
}
}

public String toJSON() {
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("serial", serial);
jsonObject.put("timestamp", timestamp);
jsonObject.put("glucose", glucose);

return jsonObject.toString();
} catch (JSONException e) {
UserError.Log.wtf(TAG, "Error producing in toJSON: " + e);
if (Double.isNaN(glucose)) UserError.Log.e(TAG, "glucose is NaN");
return "";
}
}

public static Libre2RawValue fromJSON(String json) {
NewLibre2RawValue nl2rv = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(json, NewLibre2RawValue.class);
Libre2RawValue l2rv = new Libre2RawValue();
l2rv.serial = nl2rv.serial;
l2rv.glucose = nl2rv.glucose;
l2rv.timestamp = nl2rv.timestamp;
Log.d(TAG, "Object form json: " + l2rv.serial + ", " + l2rv.glucose + ", " + l2rv.timestamp);
return l2rv;
}
}

class NewLibre2RawValue {
@Expose
double glucose;

@Expose
long timestamp;

@Expose
String serial;
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ public BgGraphBuilder(Context context, long start, long end, int numValues, bool
loaded_start = start;
loaded_end = end;
bgReadings = BgReading.latestForGraph(numValues, start, end);
if (DexCollectionType.getDexCollectionType() == DexCollectionType.LibreReceiver)
Libre2RawValues = Libre2RawValue.latestForGraph(numValues, start, end);
if (DexCollectionType.getDexCollectionType() == DexCollectionType.LibreReceiver
|| (DexCollectionType.getDexCollectionType() == DexCollectionType.Follower && prefs.getBoolean("Libre2_showRawGraph",false)))
Libre2RawValues = Libre2RawValue.latestForGraph(numValues * 5, start, end);
plugin_adjusted = false;
smoother_adjusted = false;
} finally {
Expand Down Expand Up @@ -1343,7 +1344,8 @@ private synchronized void addBgReadingValues(final boolean simple) {
}

try {
if (DexCollectionType.getDexCollectionType() == DexCollectionType.LibreReceiver && prefs.getBoolean("Libre2_showRawGraph", false)) {
if ((DexCollectionType.getDexCollectionType() == DexCollectionType.LibreReceiver || DexCollectionType.getDexCollectionType () == DexCollectionType.Follower)
&& prefs.getBoolean("Libre2_showRawGraph", false)) {
for (final Libre2RawValue bgLibre : Libre2RawValues) {
if (bgLibre.glucose > 0) {
rawInterpretedValues.add(new HPointValue((double) (bgLibre.timestamp / FUZZER), (float) unitized(bgLibre.glucose)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.eveningoutpost.dexdrip.models;

import static com.google.common.truth.Truth.assertThat;

import com.eveningoutpost.dexdrip.RobolectricTestWithConfig;

import org.junit.Test;

import java.util.Date;

public class Libre2RawValueTest extends RobolectricTestWithConfig {

@Test
public void jsonParsingWorks() {
Libre2RawValue value = new Libre2RawValue();
value.serial = "Hello World";
value.timestamp = new Date().getTime();
value.glucose = 123.456;

String json = value.toJSON();

Libre2RawValue new_value = Libre2RawValue.fromJSON(json);

assertThat(value.serial).isEqualTo(new_value.serial);
assertThat(value.timestamp).isEqualTo(new_value.timestamp);
assertThat(value.glucose).isEqualTo(new_value.glucose);
}
}