Skip to content

Commit

Permalink
Merge pull request #326 from NightscoutFoundation/tzachi-libre-trend4
Browse files Browse the repository at this point in the history
Tzachi libre trend4
  • Loading branch information
jamorham authored Mar 2, 2018
2 parents 9e9ad76 + 57c9455 commit 734988f
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 58 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_display_qrcode"
android:noHistory="true" />
<activity
android:name=".utils.LibreTrendGraph"
android:configChanges="orientation|screenSize"
android:label="@string/libre_15_minutes_graph"
android:noHistory="true" />
<activity
android:name=".UtilityModels.SendFeedBack"
android:configChanges="orientation|screenSize"
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import com.eveningoutpost.dexdrip.Models.Calibration;
import com.eveningoutpost.dexdrip.Models.HeartRate;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.LibreBlock;
import com.eveningoutpost.dexdrip.Models.StepCounter;
import com.eveningoutpost.dexdrip.Models.ProcessInitialDataQuality;
import com.eveningoutpost.dexdrip.Models.Sensor;
import com.eveningoutpost.dexdrip.Models.StepCounter;
Expand Down Expand Up @@ -112,6 +114,7 @@
import com.eveningoutpost.dexdrip.utils.DatabaseUtil;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.eveningoutpost.dexdrip.utils.DisplayQRCode;
import com.eveningoutpost.dexdrip.utils.LibreTrendGraph;
import com.eveningoutpost.dexdrip.utils.Preferences;
import com.eveningoutpost.dexdrip.utils.SdcardImportExport;
import com.eveningoutpost.dexdrip.wearintegration.WatchUpdaterService;
Expand Down Expand Up @@ -1068,6 +1071,10 @@ public void testFeature(MenuItem x) {
public void viewEventLog(MenuItem x) {
startActivity(new Intent(this, ErrorsActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("events", ""));
}

public void ShowLibreTrend(MenuItem x) {
startActivity(new Intent(this, LibreTrendGraph.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("events", ""));
}

private boolean hideTreatmentButtonsIfAllDone() {
if ((btnBloodGlucose.getVisibility() == View.INVISIBLE) &&
Expand Down Expand Up @@ -2956,7 +2963,6 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);


//wear integration
if (!Pref.getBoolean("wear_sync", false)) {
menu.removeItem(R.id.action_open_watch_settings);
Expand Down Expand Up @@ -2990,6 +2996,14 @@ public boolean onCreateOptionsMenu(Menu menu) {

menu.findItem(R.id.showreminders).setVisible(Pref.getBoolean("plus_show_reminders", true));

LibreBlock libreBlock = null;
if(DexCollectionType.hasLibre()) {
libreBlock = LibreBlock.getLatestForTrend();
}
if(libreBlock == null ) {
menu.findItem(R.id.libreLastMinutes).setVisible(false);
}

return super.onCreateOptionsMenu(menu);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,16 @@ public void run() {
public static void processReadingDataTransferObject(ReadingData.TransferObject object) {
Log.i(TAG, "Data that was recieved from librealarm is " + HexDump.dumpHexString(object.data.raw_data));
// Save raw block record (we start from block 0)
LibreBlock.createAndSave("LibreAlarm", object.data.raw_data, 0);
long now = JoH.tsl();
LibreBlock.createAndSave("LibreAlarm", now, object.data.raw_data, 0);

if(Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
if(object.data.raw_data == null) {
Log.e(TAG, "Please update LibreAlarm to use OOP algorithm");
JoH.static_toast_long("Please update LibreAlarm to use OOP algorithm");
return;
}
LibreOOPAlgorithm.SendData(object.data.raw_data);
LibreOOPAlgorithm.SendData(object.data.raw_data, now);
return;
}
CalculateFromDataTransferObject(object, use_raw_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,36 +813,6 @@ public static List<BgReading> latest(int number, boolean is_follower) {
}
}

public static List<BgReading> latest_till(long till, int number) {
return latest_till(till, number, Home.get_follower());
}

public static List<BgReading> latest_till(long till, int number, boolean is_follower) {
if (is_follower) {
// exclude sensor information when working as a follower
return new Select()
.from(BgReading.class)
.where("calculated_value != 0")
.where("raw_data != 0") // TODO XXX
.orderBy("timestamp desc")
.limit(number)
.execute();
} else {
Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
return null;
}
return new Select()
.from(BgReading.class)
.where("Sensor = ? ", sensor.getId())
.where("calculated_value != 0")
.where("raw_data != 0")
.orderBy("timestamp desc")
.limit(number)
.execute();
}
}

public static boolean isDataStale() {
final BgReading last = lastNoSenssor();
if (last == null) return true;
Expand Down Expand Up @@ -1077,8 +1047,8 @@ public static void bgReadingInsertFromInt(int value, long timestamp, boolean do_
bgr.find_slope();
if (do_notification) {
xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al
BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), true, !do_notification); // pebble and widget
}
BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), false, !do_notification); // pebble and widget
} else {
Log.d(TAG, "Ignoring duplicate bgr record due to timestamp: " + timestamp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.provider.BaseColumns;

import java.text.DecimalFormat;

import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.google.gson.annotations.Expose;

/**
Expand All @@ -21,6 +25,7 @@ public class LibreBlock extends PlusModel {
"ALTER TABLE LibreBlock ADD COLUMN blockbytes BLOB;",
"ALTER TABLE LibreBlock ADD COLUMN bytestart INTEGER;",
"ALTER TABLE LibreBlock ADD COLUMN byteend INTEGER;",
"ALTER TABLE LibreBlock ADD COLUMN calculatedbg REAL;",
"CREATE INDEX index_LibreBlock_timestamp on LibreBlock(timestamp);",
"CREATE INDEX index_LibreBlock_bytestart on LibreBlock(bytestart);",
"CREATE INDEX index_LibreBlock_byteend on LibreBlock(byteend);"
Expand All @@ -47,16 +52,20 @@ public class LibreBlock extends PlusModel {
@Column(name = "blockbytes")
public byte[] blockbytes;

@Expose
@Column(name = "calculatedbg")
public double calculated_bg;

// if you are indexing by block then just * 8 to get byte start
public static LibreBlock createAndSave(String reference, byte[] blocks, int byte_start) {
final LibreBlock lb = create(reference, blocks, byte_start);
public static LibreBlock createAndSave(String reference, long timestamp, byte[] blocks, int byte_start) {
final LibreBlock lb = create(reference, timestamp, blocks, byte_start);
if (lb != null) {
lb.save();
}
return lb;
}

public static LibreBlock create(String reference, byte[] blocks, int byte_start) {
public static LibreBlock create(String reference, long timestamp, byte[] blocks, int byte_start) {
if (reference == null || blocks == null) {
UserError.Log.e(TAG, "Cannot save block will null data");
return null;
Expand All @@ -66,10 +75,43 @@ public static LibreBlock create(String reference, byte[] blocks, int byte_start)
lb.blockbytes = blocks;
lb.byte_start = byte_start;
lb.byte_end = byte_start + blocks.length;
lb.timestamp = JoH.tsl();
lb.timestamp = timestamp;
return lb;
}

public static LibreBlock getLatestForTrend() {

return new Select()
.from(LibreBlock.class)
.where("bytestart == 0")
.where("byteend >= 344")
.orderBy("timestamp desc")
.executeSingle();
}

public static LibreBlock getForTimestamp(long timestamp) {

final double margin = (3 * 1000);
final DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(1);

return new Select()
.from(LibreBlock.class)
.where("timestamp >= " + df.format(timestamp-margin))
.where("timestamp <= " + df.format(timestamp + margin))
.executeSingle();
}

public static void UpdateBgVal(long timestamp, double calculated_value) {
LibreBlock libreBlock = getForTimestamp(timestamp);
if(libreBlock == null) {
return;
}
Log.e(TAG, "Updating bg for timestamp " + timestamp);
libreBlock.calculated_bg = calculated_value;
libreBlock.save();
}

private static final boolean d = false;

public static void updateDB() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class LibreOOPAlgorithm {
private static final String TAG = "LibreOOPAlgorithm";

static public void SendData(byte[] fullData) {
static public void SendData(byte[] fullData, long timestamp) {
if(fullData == null) {
Log.e(TAG, "SendData called with null data");
return;
Expand All @@ -45,7 +45,7 @@ static public void SendData(byte[] fullData) {
Intent intent = new Intent(Intents.XDRIP_PLUS_LIBRE_DATA);
Bundle bundle = new Bundle();
bundle.putByteArray(Intents.LIBRE_DATA_BUFFER, fullData);
bundle.putLong(Intents.LIBRE_DATA_TIMESTAMP, JoH.tsl());
bundle.putLong(Intents.LIBRE_DATA_TIMESTAMP, timestamp);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
xdrip.getAppContext().sendBroadcast(intent);
Expand All @@ -57,10 +57,14 @@ static public void HandleData(String oopData) {
OOPResults oOPResults = null;
try {
final Gson gson = new GsonBuilder().create();
OOPResults []oOPResultsArray = null;
oOPResultsArray = gson.fromJson(oopData, OOPResults[].class);
if(oOPResultsArray.length > 0) {
oOPResults = oOPResultsArray[0];
OOPResultsContainer oOPResultsContainer = gson.fromJson(oopData, OOPResultsContainer.class);

if(oOPResultsContainer.Message != null) {
Log.e(TAG, "recieved a message from oop algorithm:" + oOPResultsContainer.Message);
}

if(oOPResultsContainer.oOPResultsArray.length > 0) {
oOPResults = oOPResultsContainer.oOPResultsArray[0];
} else {
Log.e(TAG, "oOPResultsArray exists, but size is zero");
return;
Expand Down Expand Up @@ -113,8 +117,6 @@ static public void HandleData(String oopData) {
glucoseData.glucoseLevelRaw = (int)(oOPResults.currentBg * factor);
libreAlarmObject.data.history.add(glucoseData);



Log.e(TAG, "HandleData Created the following object " + libreAlarmObject.toString());
LibreAlarmReceiver.CalculateFromDataTransferObject(libreAlarmObject, use_raw);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HistoricBg {
public double bg;
}

public class OOPResults {
class OOPResults {
double currentBg;
int currentTime;
int currenTrend;
Expand All @@ -25,4 +25,21 @@ String toGson() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}
}

public class OOPResultsContainer {
OOPResultsContainer() {
oOPResultsArray = new OOPResults[0];
version = 1;
}

String toGson() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}

int version;
String Message;

OOPResults[] oOPResultsArray;
}
26 changes: 22 additions & 4 deletions app/src/main/java/com/eveningoutpost/dexdrip/NFCReaderX.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import com.eveningoutpost.dexdrip.utils.DexCollectionType;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -234,12 +236,12 @@ protected void onPostExecute(Tag tag) {
if (!NFCReaderX.useNFC()) return;
if (succeeded) {
final String tagId = bytesToHexString(tag.getId());

long now = JoH.tsl();
// Save raw block record (we start from block 0)
LibreBlock.createAndSave(tagId, data, 0);
LibreBlock.createAndSave(tagId, now, data, 0);

if(Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
LibreOOPAlgorithm.SendData(data);
LibreOOPAlgorithm.SendData(data, now);
} else {
mResult = parseData(0, tagId, data);
new Thread() {
Expand All @@ -263,7 +265,7 @@ public void run() {
Home.staticBlockUI(context, false);
}
}


@Override
protected Tag doInBackground(Tag... params) {
Expand Down Expand Up @@ -627,4 +629,20 @@ public void run() {
}, 1000);
}
}

static public ReadingData getTrend(LibreBlock libreBlock) {
if(libreBlock.byte_start != 0 || libreBlock.byte_end < 344) {
Log.i(TAG, "libreBlock exists but does not have enough data " + libreBlock.timestamp);
return null;
}
ReadingData result = parseData(0, "", libreBlock.blockbytes);
if(result.trend.size() == 0 || result.trend.get(0).glucoseLevelRaw == 0) {
Log.i(TAG, "libreBlock exists but no trend data exists, or first value is zero " + libreBlock.timestamp);
return null;
}

// TODO: verify checksum
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.eveningoutpost.dexdrip.Models.BgReading;
import com.eveningoutpost.dexdrip.Models.Calibration;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.LibreBlock;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.NewDataObserver;
import com.eveningoutpost.dexdrip.Services.SyncService;
Expand Down Expand Up @@ -229,6 +230,7 @@ public static void handleNewBgReading(BgReading bgReading, String operation_type

if (!quick) {
NewDataObserver.newBgReading(bgReading, is_follower);
LibreBlock.UpdateBgVal(bgReading.timestamp, bgReading.calculated_value);
}

if ((!is_follower) && (prefs.getBoolean("plus_follow_master", false))) {
Expand Down
Loading

0 comments on commit 734988f

Please sign in to comment.