Skip to content

Commit

Permalink
LibreReceiver: clear nfc reading age and other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Aug 3, 2022
1 parent a1bbca6 commit e13a76e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
76 changes: 41 additions & 35 deletions app/src/main/java/com/eveningoutpost/dexdrip/LibreReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
import static com.eveningoutpost.dexdrip.Home.get_engineering_mode;
import static com.eveningoutpost.dexdrip.Models.Libre2Sensor.Libre2Sensors;

import lombok.val;

/**
* Created by jamorham on 14/11/2016.
*/

public class LibreReceiver extends BroadcastReceiver {

private static final String TAG = "xdrip libre_receiver";
private static final boolean debug = false;
private static final String TAG = LibreReceiver.class.getSimpleName();
private static final boolean d = false;
private static SharedPreferences prefs;
private static final Object lock = new Object();
private static String libre_calc_doku="wait for next reading...";
private static long last_reading=0;
private static String libre_calc_doku = "wait for next reading...";
private static long last_reading = 0;

@Override
public void onReceive(final Context context, final Intent intent) {
if(DexCollectionType.getDexCollectionType() != DexCollectionType.LibreReceiver)
if (DexCollectionType.getDexCollectionType() != DexCollectionType.LibreReceiver)
return;
new Thread() {
@Override
Expand All @@ -54,16 +54,9 @@ public void run() {

Log.d(TAG, "libre onReceiver: " + intent.getAction());
JoH.benchmark(null);
// check source
if (prefs == null)
prefs = PreferenceManager.getDefaultSharedPreferences(context);

final Bundle bundle = intent.getExtras();
// BundleScrubber.scrub(bundle);
final String action = intent.getAction();



if (action == null) return;

switch (action) {
Expand All @@ -78,49 +71,59 @@ public void run() {

case Intents.LIBRE2_BG:
Libre2RawValue currentRawValue = processIntent(intent);
//JoH.dumpBundle(intent.getExtras(), TAG);

if (currentRawValue == null) return;
Log.v(TAG,"got bg reading: from sensor:"+currentRawValue.serial+" rawValue:"+currentRawValue.glucose+" at:"+currentRawValue.timestamp);

Log.v(TAG, "got bg reading: from sensor:" + currentRawValue.serial + " rawValue:" + currentRawValue.glucose + " at:" + currentRawValue.timestamp);
// period of 4.5 minutes to collect 5 readings
if(!BgReading.last_within_millis(45 * 6 * 1000 )) {
if (!BgReading.last_within_millis(DexCollectionType.getCurrentDeduplicationPeriod())) {
List<Libre2RawValue> smoothingValues = Libre2RawValue.last20Minutes();
smoothingValues.add(currentRawValue);
processValues(currentRawValue, smoothingValues, context);
}
currentRawValue.save();

clearNFCsensorAge();
break;

default:
Log.e(TAG, "Unknown action! " + action);
break;
}
} finally {
JoH.benchmark("NSEmulator process");
JoH.benchmark(TAG);
JoH.releaseWakeLock(wl);
}
} // lock
}
}.start();
}

private static void clearNFCsensorAge() {
val PREF_KEY = "nfc_sensor_age";
if (Pref.getInt(PREF_KEY, 0) != 0) {
Pref.setInt(PREF_KEY, 0); // clear any nfc related sensor age cached from another collector
}
}

private static Libre2RawValue processIntent(Intent intent) {
Bundle sas = intent.getBundleExtra("sas");
try {
if (sas != null)
saveSensorStartTime(sas.getBundle("currentSensor"), intent.getBundleExtra("bleManager").getString("sensorSerial"));
} catch (NullPointerException e) {
Log.e(TAG,"Null pointer exception in processIntent: " + e);
Log.e(TAG, "Null pointer exception in processIntent: " + e);
}
if (!intent.hasExtra("glucose") || !intent.hasExtra("timestamp") || !intent.hasExtra("bleManager")) {
Log.e(TAG,"Received faulty intent from LibreLink.");
Log.e(TAG, "Received faulty intent from LibreLink.");
return null;
}
double glucose = intent.getDoubleExtra("glucose", 0);
long timestamp = intent.getLongExtra("timestamp", 0);
last_reading = timestamp;
String serial = intent.getBundleExtra("bleManager").getString("sensorSerial");
if (serial == null) {
Log.e(TAG,"Received faulty intent from LibreLink.");
Log.e(TAG, "Received faulty intent from LibreLink.");
return null;
}
Libre2RawValue rawValue = new Libre2RawValue();
Expand All @@ -129,49 +132,50 @@ private static Libre2RawValue processIntent(Intent intent) {
rawValue.serial = serial;
return rawValue;
}

private static void processValues(Libre2RawValue currentValue, List<Libre2RawValue> smoothingValues, Context context) {
if (Sensor.currentSensor() == null) {
Sensor.create(currentValue.timestamp, currentValue.serial);

}

double value = calculateWeightedAverage(smoothingValues, currentValue.timestamp);

BgReading.bgReadingInsertLibre2(value, currentValue.timestamp,currentValue.glucose);
BgReading.bgReadingInsertLibre2(value, currentValue.timestamp, currentValue.glucose);
}

private static void saveSensorStartTime(Bundle sensor, String serial) {
if (sensor != null && sensor.containsKey("sensorStartTime")) {
long sensorStartTime = sensor.getLong("sensorStartTime");

Sensor last = Sensor.currentSensor();
if(last!=null) {
if (last != null) {
if (!last.uuid.equals(serial)) {
Sensor.stopSensor();
last = null;
}
}

if(last==null) {
Sensor.create(sensorStartTime,serial);
if (last == null) {
Sensor.create(sensorStartTime, serial);
}
}
}

private static long SMOOTHING_DURATION = TimeUnit.MINUTES.toMillis(25);


private static double calculateWeightedAverage(List<Libre2RawValue> rawValues, long now) {
double sum = 0;
double weightSum = 0;
DecimalFormat longformat = new DecimalFormat( "#,###,###,##0.00" );
DecimalFormat longformat = new DecimalFormat("#,###,###,##0.00");

libre_calc_doku="";
libre_calc_doku = "";
for (Libre2RawValue rawValue : rawValues) {
double weight = 1 - ((now - rawValue.timestamp) / (double) SMOOTHING_DURATION);
sum += rawValue.glucose * weight;
weightSum += weight;
libre_calc_doku += DateFormat.format("kk:mm:ss :",rawValue.timestamp) + " w:" + longformat.format(weight) +" raw: " + rawValue.glucose + "\n" ;
}
libre_calc_doku += DateFormat.format("kk:mm:ss :", rawValue.timestamp) + " w:" + longformat.format(weight) + " raw: " + rawValue.glucose + "\n";
}
return Math.round(sum / weightSum);
}

Expand All @@ -182,12 +186,14 @@ public static List<StatusItem> megaStatus() {
if (sensor != null) {
l.add(new StatusItem("Libre2 Sensor", sensor.uuid + "\nStart: " + DateFormat.format("dd.MM.yyyy kk:mm", sensor.started_at)));
}
String lastReading ="";
try {
lastReading = DateFormat.format("dd.MM.yyyy kk:mm:ss", last_reading).toString();
l.add(new StatusItem("Last Reading", lastReading));
} catch (Exception e) {
Log.e(TAG, "Error readlast: " + e);
if (last_reading > 0) {
String lastReading = "";
try {
lastReading = DateFormat.format("dd.MM.yyyy kk:mm:ss", last_reading).toString();
l.add(new StatusItem(xdrip.gs(R.string.last_reading), lastReading));
} catch (Exception e) {
Log.e(TAG, "Error readlast: " + e);
}
}
if (get_engineering_mode()) {
l.add(new StatusItem("Last Calc.", libre_calc_doku));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1714,4 +1714,5 @@
<string name="reduce_battery_and_network_overhead">Reduce battery and network overhead by using batch processing and excluding unnecessary data</string>
<string name="simplify_graphs_by_smoothing_out_irregularities">Simplify graphs by smoothing out irregularities</string>
<string name="graph_smoothing">Graph Smoothing</string>
<string name="last_reading">Last Reading</string>
</resources>

0 comments on commit e13a76e

Please sign in to comment.