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

Add predicted iob data to broadcastservice #2294

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 = "";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this always sets the variables to an empty string which does not change till line 569. Maybe you should initialize the strings to "" in line 84/85?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a place where the variable resets in case if the iob data would not be updated in BgGraphBuilder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in case if settings.isDisplayGraph() is false, the predictedIOB and predictedBWP always would be empty. Maybe it would be better to transfer iob only if graph is enabled.

Copy link
Collaborator

@tolot27 tolot27 Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only location where predictedIOB and predictedBWP are set to likely non-empty value is in the statusReceiver onReceive event. The next use is bundle.putString("predict.IOB", predictedIOB); but right before the if condition these two variables will be emptied. Hence, an empty string is always passed to bundle.putString. That's my main concern. Did I oversee anything?

Copy link
Contributor Author

@bigdigital bigdigital Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. the variables are filled via statusReceiver by receiving the broadcast messages. And those broadcast messages can be triggered by bgGraphBuilder.defaultLines(false) function which calls addBgReadingValues(simple) and the last one function calls Home.updateStatusLine("iob", df.format(iob.iob)) . So when the code reaches to the bundle.putString , the iob and wpb values would be filled with the valid data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tolot27 as i understand this pr was stuck because the "bwp" and "iob" broadcast data which is generated in BgGraphBuilder transfers with help of sendBroadcast finction which is runs asynchronously. Maybe to resolve this problem we can change sendBroadcast in updateStatusLine to sendBroadcastSync which should block the execution of the thread .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using the keystore data like is done with bwp_last_insulin in PebbleDisplayTrend ? I could easily add more keystore fields if this doesn't include everything needed. Then you don't need to worry about asynchronous broadcasts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I guess this should help to transfer data across different parts of the application. It is enough to add bwp_update data (but maybe better to divide the current string inot two separate variables with the carb and insulin info). And a separate keystore for "iob" which is declared by this line Home.updateStatusLine("iob", df.format(iob.iob));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added both, let me know if this helps: 094b09c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Now works fine, added appropriate code changes

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;
}

}