Skip to content

Commit

Permalink
NightscoutFoundation#2294 - Add predicted iob data to broadcastservice
Browse files Browse the repository at this point in the history
  • Loading branch information
Sedlmayer authored and Sedlmayer committed Sep 18, 2022
1 parent 9d6007d commit dce493e
Showing 1 changed file with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.os.Parcelable;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.eveningoutpost.dexdrip.BestGlucose;
import com.eveningoutpost.dexdrip.Models.Accuracy;
Expand All @@ -29,6 +31,7 @@
import com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer;
import com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
import com.eveningoutpost.dexdrip.UtilityModels.Intents;
import com.eveningoutpost.dexdrip.UtilityModels.Pref;
import com.eveningoutpost.dexdrip.UtilityModels.PumpStatus;
import com.eveningoutpost.dexdrip.stats.StatsResult;
Expand Down Expand Up @@ -76,6 +79,11 @@ public class BroadcastService extends Service {
protected String TAG = this.getClass().getSimpleName();
protected Map<String, BroadcastModel> broadcastEntities;

private BroadcastReceiver statusReceiver;

private String predictedBWP;
private String predictedIOB;

/**
* The receiver listening {@link ACTION_WATCH_COMMUNICATION_RECEIVER} action.
* Every Receiver command requires {@link Const.INTENT_PACKAGE_KEY}
Expand Down Expand Up @@ -209,7 +217,6 @@ public void onReceive(Context context, Intent intent) {
}
};


@Override
public IBinder onBind(Intent intent) {
return null;
Expand All @@ -227,6 +234,25 @@ public void onCreate() {
registerReceiver(broadcastReceiver, new IntentFilter(ACTION_WATCH_COMMUNICATION_RECEIVER));

JoH.startService(BroadcastService.class, Const.INTENT_FUNCTION_KEY, Const.CMD_START);

statusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String bwp = intent.getStringExtra("bwp");
if (bwp != null) {
predictedBWP = bwp;
} else {
final String iob = intent.getStringExtra("iob");
if (iob != null) {
predictedIOB = iob;
}
}
}
};

LocalBroadcastManager.getInstance(this).registerReceiver(statusReceiver,
new IntentFilter(Intents.HOME_STATUS_ACTION));

super.onCreate();
}

Expand All @@ -235,6 +261,12 @@ public void onDestroy() {
UserError.Log.e(TAG, "killing service");
broadcastEntities.clear();
unregisterReceiver(broadcastReceiver);

try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(statusReceiver);
} catch (Exception e) {
Log.e(TAG, "Exception unregistering broadcast receiver: " + e);
}
super.onDestroy();
}

Expand Down Expand Up @@ -495,6 +527,9 @@ protected Bundle prepareBgBundle(BroadcastModel broadcastModel) {
bundle.putLong("treatment.timeStamp", treatment.timestamp);
}

predictedIOB = "";
predictedBWP = "";

if (settings.isDisplayGraph()) {
long graphStartOffset = settings.getGraphStart();
long graphEndOffset = settings.getGraphEnd();
Expand All @@ -513,7 +548,7 @@ protected Bundle prepareBgBundle(BroadcastModel broadcastModel) {
bundle.putDouble("lowMark", JoH.tolerantParseDouble(prefs.getString("lowValue", "70"), 70));

BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(xdrip.getAppContext(), start, end);
bgGraphBuilder.defaultLines(true); // simple mode
bgGraphBuilder.defaultLines(false); // not simple mode in order to receive simulated data

bundle.putParcelable("graph.lowLine", new GraphLine(bgGraphBuilder.lowLine()));
bundle.putParcelable("graph.highLine", new GraphLine(bgGraphBuilder.highLine()));
Expand All @@ -530,7 +565,11 @@ protected Bundle prepareBgBundle(BroadcastModel broadcastModel) {
bundle.putParcelable("graph.cob", new GraphLine(treatments[6])); //cobValues
bundle.putParcelable("graph.polyBg", new GraphLine(treatments[7])); //poly predict ;
}

bundle.putString("predict.IOB", predictedIOB);
bundle.putString("predict.BWP", predictedBWP);
}
return bundle;
}
}

}

0 comments on commit dce493e

Please sign in to comment.