getLastAutosensData improvement and logging

This commit is contained in:
Milos Kozak 2018-01-16 19:28:25 +01:00
parent 5a00b68f68
commit 7fdc146b91
9 changed files with 55 additions and 15 deletions

View file

@ -69,7 +69,7 @@ public class QuickWizardEntry {
return Profile.secondsFromMidnight() >= validFrom() && Profile.secondsFromMidnight() <= validTo();
}
public BolusWizard doCalc(Profile profile, TempTarget tempTarget, BgReading lastBG) {
public BolusWizard doCalc(Profile profile, TempTarget tempTarget, BgReading lastBG, boolean _synchronized) {
BolusWizard wizard = new BolusWizard();
//BG
@ -80,7 +80,12 @@ public class QuickWizardEntry {
// COB
double cob = 0d;
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData autosensData;
if (_synchronized)
autosensData = IobCobCalculatorPlugin.getLastAutosensDataSynchronized("QuickWizard COB");
else
autosensData = IobCobCalculatorPlugin.getLastAutosensData("QuickWizard COB");
if (autosensData != null && useCOB() == YES) {
cob = autosensData.cob;
}

View file

@ -547,7 +547,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
} else {
if (time > now) {
// data may not be calculated yet, use last data
return getLastAutosensData();
return getLastAutosensData("getAutosensData");
}
//log.debug(">>> getAutosensData Cache miss " + new Date(time).toLocaleString());
return null;
@ -556,13 +556,35 @@ public class IobCobCalculatorPlugin implements PluginBase {
}
@Nullable
public static AutosensData getLastAutosensData() {
if (autosensDataTable.size() < 1)
public static AutosensData getLastAutosensDataSynchronized(String reason) {
synchronized (dataLock) {
return getLastAutosensData(reason);
}
}
@Nullable
public static AutosensData getLastAutosensData(String reason) {
if (autosensDataTable.size() < 1) {
log.debug("AUTOSENSDATA null: autosensDataTable empty (" + reason + ")");
return null;
AutosensData data = autosensDataTable.valueAt(autosensDataTable.size() - 1);
}
AutosensData data = null;
try {
data = autosensDataTable.valueAt(autosensDataTable.size() - 1);
} catch (Exception e) {
// data can be processed on the background
// in this rare case better return null and do not block UI
// APS plugin should use getLastAutosensDataSynchronized where the blocking is not an issue
log.debug("AUTOSENSDATA null: Exception catched (" + reason + ")");
return null;
}
if (data.time < System.currentTimeMillis() - 11 * 60 * 1000) {
log.debug("AUTOSENSDATA null: data is old (" + reason + ")");
return null;
} else {
if (data == null)
log.debug("AUTOSENSDATA null: data == null (" + " " + reason + ")");
return data;
}
}

View file

@ -55,6 +55,7 @@ import info.nightscout.androidaps.events.EventRefreshOverview;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui;
@ -151,6 +152,18 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
});
}
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished e) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
calculateInsulin();
}
});
}
final private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
@ -459,7 +472,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
// COB
Double c_cob = 0d;
if (cobCheckbox.isChecked()) {
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("Wizard COB");
if(autosensData != null) {
c_cob = autosensData.cob;

View file

@ -640,7 +640,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
final QuickWizardEntry quickWizardEntry = OverviewPlugin.getPlugin().quickWizard.getActive();
if (quickWizardEntry != null && actualBg != null) {
quickWizardButton.setVisibility(View.VISIBLE);
final BolusWizard wizard = quickWizardEntry.doCalc(profile, tempTarget, actualBg);
final BolusWizard wizard = quickWizardEntry.doCalc(profile, tempTarget, actualBg, true);
final JSONObject boluscalcJSON = new JSONObject();
try {
@ -1135,7 +1135,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (quickWizardEntry != null && lastBG != null && pump.isInitialized() && !pump.isSuspended()) {
quickWizardButton.setVisibility(View.VISIBLE);
String text = quickWizardEntry.buttonText() + "\n" + DecimalFormatter.to0Decimal(quickWizardEntry.carbs()) + "g";
BolusWizard wizard = quickWizardEntry.doCalc(profile, tempTarget, lastBG);
BolusWizard wizard = quickWizardEntry.doCalc(profile, tempTarget, lastBG, false);
text += " " + DecimalFormatter.to2Decimal(wizard.calculatedTotalInsulin) + "U";
quickWizardButton.setText(text);
if (wizard.calculatedTotalInsulin <= 0)
@ -1201,7 +1201,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
// cob
if (cobView != null) { // view must not exists
String cobText = "";
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("Overview COB");
if (autosensData != null)
cobText = (int) autosensData.cob + " g";
cobView.setText(cobText);

View file

@ -118,7 +118,7 @@ public class SensitivityAAPSPlugin implements PluginBase, SensitivityInterface{
return new AutosensResult();
}
AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime);
AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already
if (current == null) {
log.debug("No autosens data available");
return new AutosensResult();

View file

@ -118,7 +118,7 @@ public class SensitivityOref0Plugin implements PluginBase, SensitivityInterface
return new AutosensResult();
}
AutosensData current = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData current = IobCobCalculatorPlugin.getLastAutosensData("SensitivityOref0"); // this is running inside lock already
if (current == null) {
log.debug("No current autosens data available");
return new AutosensResult();

View file

@ -116,7 +116,7 @@ public class SensitivityWeightedAveragePlugin implements PluginBase, Sensitivity
return new AutosensResult();
}
AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime);
AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already
if (current == null) {
if (Config.logAutosensData)
log.debug("No autosens data available");

View file

@ -251,7 +251,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
}
}
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensDataSynchronized("getMealData()");
if (autosensData != null) {
result.mealCOB = autosensData.cob;
}

View file

@ -654,7 +654,7 @@ public class WatchUpdaterService extends WearableListenerService implements
private String generateCOBString() {
String cobStringResult = "--";
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData();
AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("WatcherUpdaterService");
if (autosensData != null) {
cobStringResult = (int) autosensData.cob + "g";
}