LoopPlugin injection

This commit is contained in:
Milos Kozak 2020-01-03 01:45:26 +01:00
parent 4fff1f42c1
commit c16131bfc8
11 changed files with 63 additions and 56 deletions

View file

@ -78,7 +78,7 @@ class LoopFragment : DaggerFragment() {
@Synchronized
fun updateGUI() {
if (loop_request == null) return
LoopPlugin.lastRun?.let {
loopPlugin.lastRun?.let {
loop_request.text = it.request?.toSpanned() ?: ""
loop_constraintsprocessed.text = it.constraintsProcessed?.toSpanned() ?: ""
loop_source.text = it.source ?: ""

View file

@ -11,7 +11,6 @@ import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import java.util.Date;
@ -19,6 +18,7 @@ import java.util.Date;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Lazy;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
@ -78,7 +78,7 @@ public class LoopPlugin extends PluginBase {
private final ConfigBuilderPlugin configBuilderPlugin;
private final TreatmentsPlugin treatmentsPlugin;
private final VirtualPumpPlugin virtualPumpPlugin;
private final ActionStringHandler actionStringHandler;
private final Lazy<ActionStringHandler> actionStringHandler;
private CompositeDisposable disposable = new CompositeDisposable();
@ -86,17 +86,6 @@ public class LoopPlugin extends PluginBase {
private long lastBgTriggeredRun = 0;
private static LoopPlugin loopPlugin;
@NonNull
@Deprecated
public static LoopPlugin getPlugin() {
if (loopPlugin == null) {
throw new IllegalStateException("Accessing LoopPlugin before first instantiation");
}
return loopPlugin;
}
private long loopSuspendedTill; // end of manual loop suspend
private boolean isSuperBolus;
private boolean isDisconnected;
@ -112,8 +101,7 @@ public class LoopPlugin extends PluginBase {
public Date lastOpenModeAccept;
}
@Deprecated
static public LastRun lastRun = null;
public LastRun lastRun = null;
@Inject
public LoopPlugin(
@ -127,7 +115,7 @@ public class LoopPlugin extends PluginBase {
ConfigBuilderPlugin configBuilderPlugin,
TreatmentsPlugin treatmentsPlugin,
VirtualPumpPlugin virtualPumpPlugin,
ActionStringHandler actionStringHandler
Lazy<ActionStringHandler> actionStringHandler // TODO Adrian use RxBus instead of Lazy
) {
super(new PluginDescription()
.mainType(PluginType.LOOP)
@ -137,7 +125,6 @@ public class LoopPlugin extends PluginBase {
.preferencesId(R.xml.pref_loop)
.description(R.string.description_loop)
);
this.loopPlugin = this; //TODO remove
this.aapsLogger = aapsLogger;
this.rxBus = rxBus;
this.sp = sp;
@ -475,13 +462,13 @@ public class LoopPlugin extends PluginBase {
rxBus.send(new EventNewOpenLoopNotification());
// Send to Wear
actionStringHandler.handleInitiate("changeRequest");
actionStringHandler.get().handleInitiate("changeRequest");
} else if (allowNotification) {
// dismiss notifications
NotificationManager notificationManager =
(NotificationManager) mainApp.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.notificationID);
actionStringHandler.handleInitiate("cancelChangeRequest");
actionStringHandler.get().handleInitiate("cancelChangeRequest");
}
}

View file

@ -27,6 +27,7 @@ import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.food_fragment.*
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList
class FoodFragment : DaggerFragment() {
@ -47,7 +48,7 @@ class FoodFragment : DaggerFragment() {
food_recyclerview.setHasFixedSize(true)
food_recyclerview.setLayoutManager(LinearLayoutManager(view.context))
food_recyclerview.adapter = RecyclerViewAdapter(foodPlugin.service.foodData)
food_recyclerview.adapter = RecyclerViewAdapter(foodPlugin.service?.foodData ?: ArrayList())
food_clearfilter.setOnClickListener {
food_filter.setText("")
@ -105,7 +106,7 @@ class FoodFragment : DaggerFragment() {
}
private fun loadData() {
unfiltered = foodPlugin.service.foodData
unfiltered = foodPlugin.service?.foodData ?: ArrayList()
}
private fun fillCategories() {
@ -200,7 +201,7 @@ class FoodFragment : DaggerFragment() {
if (food._id != null && food._id != "") {
NSUpload.removeFoodFromNS(food._id)
}
foodPlugin.service.delete(food)
foodPlugin.service?.delete(food)
}, null)
}
}

View file

@ -16,5 +16,10 @@ class FoodPlugin @Inject constructor() : PluginBase(PluginDescription()
.description(R.string.description_food)
) {
val service: FoodService = FoodService()
var service: FoodService? = null
override fun onStart() {
super.onStart()
service = FoodService()
}
}

View file

@ -38,7 +38,7 @@ class MaintenanceFragment : DaggerFragment() {
MainApp.getDbHelper().resetDatabases()
// should be handled by Plugin-Interface and
// additional service interface and plugin registry
foodPlugin.service.resetFood()
foodPlugin.service?.resetFood()
treatmentsPlugin.service.resetTreatments()
})
}

View file

@ -21,6 +21,9 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo;
@ -47,10 +50,16 @@ import info.nightscout.androidaps.utils.SP;
/**
* Created by mike on 26.05.2017.
*/
@Singleton
public class NSUpload {
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
private static LoopPlugin loopPlugin; // Ugly but temporary
@Inject
public NSUpload(LoopPlugin loopPlugin) {
this.loopPlugin = loopPlugin;
}
public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) {
try {
JSONObject data = new JSONObject();
@ -169,7 +178,8 @@ public class NSUpload {
DeviceStatus deviceStatus = new DeviceStatus();
try {
LoopPlugin.LastRun lastRun = LoopPlugin.lastRun;
if (loopPlugin == null) return; // TODO ugly - not initialized yet
LoopPlugin.LastRun lastRun = loopPlugin.lastRun;
if (lastRun != null && lastRun.lastAPSRun.getTime() > System.currentTimeMillis() - 300 * 1000L) {
// do not send if result is older than 1 min
APSResult apsResult = lastRun.request;

View file

@ -477,10 +477,9 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
private void setupChartMenu(View view) {
chartButton = (ImageButton) view.findViewById(R.id.overview_chartMenuButton);
chartButton.setOnClickListener(v -> {
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
boolean predictionsAvailable;
if (Config.APS)
predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
predictionsAvailable = loopPlugin.lastRun != null && loopPlugin.lastRun.request.hasPredictions;
else if (Config.NSCLIENT)
predictionsAvailable = true;
else
@ -923,9 +922,8 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
if (loopPlugin.isEnabled(PluginType.LOOP) && profile != null) {
loopPlugin.invoke("Accept temp button", false);
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (finalLastRun != null && finalLastRun.lastAPSRun != null && finalLastRun.constraintsProcessed.isChangeRequested()) {
OKDialog.showConfirmation(context, resourceHelper.gs(R.string.pump_tempbasal_label), finalLastRun.constraintsProcessed.toSpanned(), () -> {
if (loopPlugin.lastRun != null && loopPlugin.lastRun.lastAPSRun != null && loopPlugin.lastRun.constraintsProcessed.isChangeRequested()) {
OKDialog.showConfirmation(context, resourceHelper.gs(R.string.pump_tempbasal_label), loopPlugin.lastRun.constraintsProcessed.toSpanned(), () -> {
hideTempRecommendation();
clearNotification();
loopPlugin.acceptChangeRequest();
@ -1078,7 +1076,6 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
Constraint<Boolean> closedLoopEnabled = constraintChecker.isClosedLoopAllowed();
// open loop mode
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (Config.APS && pump.getPumpDescription().isTempBasalCapable) {
apsModeView.setVisibility(View.VISIBLE);
apsModeView.setBackgroundColor(resourceHelper.gc(R.color.ribbonDefault));
@ -1131,13 +1128,13 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
// **** Temp button ****
if (acceptTempLayout != null) {
boolean showAcceptButton = !closedLoopEnabled.value(); // Open mode needed
showAcceptButton = showAcceptButton && finalLastRun != null && finalLastRun.lastAPSRun != null; // aps result must exist
showAcceptButton = showAcceptButton && (finalLastRun.lastOpenModeAccept == null || finalLastRun.lastOpenModeAccept.getTime() < finalLastRun.lastAPSRun.getTime()); // never accepted or before last result
showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.isChangeRequested(); // change is requested
showAcceptButton = showAcceptButton && loopPlugin.lastRun != null && loopPlugin.lastRun.lastAPSRun != null; // aps result must exist
showAcceptButton = showAcceptButton && (loopPlugin.lastRun.lastOpenModeAccept == null || loopPlugin.lastRun.lastOpenModeAccept.getTime() < loopPlugin.lastRun.lastAPSRun.getTime()); // never accepted or before last result
showAcceptButton = showAcceptButton && loopPlugin.lastRun.constraintsProcessed.isChangeRequested(); // change is requested
if (showAcceptButton && pump.isInitialized() && !pump.isSuspended() && loopPlugin.isEnabled(PluginType.LOOP)) {
acceptTempLayout.setVisibility(View.VISIBLE);
acceptTempButton.setText(resourceHelper.gs(R.string.setbasalquestion) + "\n" + finalLastRun.constraintsProcessed);
acceptTempButton.setText(resourceHelper.gs(R.string.setbasalquestion) + "\n" + loopPlugin.lastRun.constraintsProcessed);
} else {
acceptTempLayout.setVisibility(View.GONE);
}
@ -1349,7 +1346,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
boolean predictionsAvailable;
if (Config.APS)
predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
predictionsAvailable = loopPlugin.lastRun != null && loopPlugin.lastRun.request.hasPredictions;
else if (Config.NSCLIENT)
predictionsAvailable = true;
else
@ -1403,7 +1400,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
if (finalPredictionsAvailable && sp.getBoolean("showprediction", false)) {
if (Config.APS)
apsResult = finalLastRun.constraintsProcessed;
apsResult = loopPlugin.lastRun.constraintsProcessed;
else
apsResult = NSDeviceStatus.getAPSResult();
int predHours = (int) (Math.ceil(apsResult.getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000));
@ -1454,7 +1451,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
}
// add target line
graphData.addTargetLine(fromTime, toTime, profile);
graphData.addTargetLine(fromTime, toTime, profile, loopPlugin.lastRun);
// **** NOW line ****
graphData.addNowLine(now);

View file

@ -243,7 +243,7 @@ public class GraphData {
addSeries(absoluteBasalsLineSeries);
}
public void addTargetLine(long fromTime, long toTime, Profile profile) {
public void addTargetLine(long fromTime, long toTime, Profile profile, LoopPlugin.LastRun lastRun) {
LineGraphSeries<DataPoint> targetsSeries;
Scale targetsScale = new Scale();
@ -252,8 +252,8 @@ public class GraphData {
List<DataPoint> targetsSeriesArray = new ArrayList<>();
double lastTarget = -1;
if (LoopPlugin.lastRun != null && LoopPlugin.lastRun.constraintsProcessed != null) {
APSResult apsResult = LoopPlugin.lastRun.constraintsProcessed;
if (lastRun != null && lastRun.constraintsProcessed != null) {
APSResult apsResult = lastRun.constraintsProcessed;
long latestPredictionsTime = apsResult.getLatestPredictionsTime();
if (latestPredictionsTime > toTime) {
toTime = latestPredictionsTime;

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.general.wear
import android.app.NotificationManager
import android.content.Context
import dagger.Lazy
import info.nightscout.androidaps.Config
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
@ -33,7 +32,11 @@ import info.nightscout.androidaps.plugins.pump.insight.LocalInsightPlugin
import info.nightscout.androidaps.plugins.treatments.CarbsGenerator
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.*
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.HardLimits
import info.nightscout.androidaps.utils.SafeParse
import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import info.nightscout.androidaps.utils.wizard.BolusWizard
@ -52,7 +55,7 @@ class ActionStringHandler @Inject constructor(
private val constraintChecker: ConstraintChecker,
private val profileFunction: ProfileFunction,
private val mainApp: MainApp,
private val loopPlugin: Lazy<LoopPlugin>,
private val loopPlugin: LoopPlugin,
private val wearPlugin: WearPlugin,
private val treatmentsPlugin: TreatmentsPlugin,
private val configBuilderPlugin: ConfigBuilderPlugin,
@ -67,6 +70,8 @@ class ActionStringHandler @Inject constructor(
private var lastSentTimestamp: Long = 0
private var lastConfirmActionString: String? = null
private var lastBolusWizard: BolusWizard? = null
// TODO Adrian use RxBus instead of Lazy + cross dependency
@Synchronized
fun handleInitiate(actionString: String) {
if (!sp.getBoolean("wearcontrol", false)) return
@ -284,7 +289,7 @@ class ActionStringHandler @Inject constructor(
} else if ("changeRequest" == act[0]) { ////////////////////////////////////////////// CHANGE REQUEST
rTitle = resourceHelper.gs(R.string.openloop_newsuggestion)
rAction = "changeRequest"
val finalLastRun = LoopPlugin.lastRun
val finalLastRun = loopPlugin.lastRun
rMessage += finalLastRun.constraintsProcessed
wearPlugin.requestChangeConfirmation(rTitle, rMessage, rAction)
lastSentTimestamp = System.currentTimeMillis()
@ -392,7 +397,7 @@ class ActionStringHandler @Inject constructor(
get() {
var ret = ""
// decide if enabled/disabled closed/open; what Plugin as APS?
if (loopPlugin.get().isEnabled(loopPlugin.get().getType())) {
if (loopPlugin.isEnabled(loopPlugin.getType())) {
ret += if (constraintChecker.isClosedLoopAllowed().value()) {
"CLOSED LOOP\n"
} else {
@ -400,9 +405,9 @@ class ActionStringHandler @Inject constructor(
}
val aps = configBuilderPlugin.activeAPS
ret += "APS: " + if (aps == null) "NO APS SELECTED!" else (aps as PluginBase).name
if (LoopPlugin.lastRun != null) {
if (LoopPlugin.lastRun.lastAPSRun != null) ret += "\nLast Run: " + DateUtil.timeString(LoopPlugin.lastRun.lastAPSRun)
if (LoopPlugin.lastRun.lastEnact != null) ret += "\nLast Enact: " + DateUtil.timeString(LoopPlugin.lastRun.lastEnact)
if (loopPlugin.lastRun != null) {
if (loopPlugin.lastRun.lastAPSRun != null) ret += "\nLast Run: " + DateUtil.timeString(loopPlugin.lastRun.lastAPSRun)
if (loopPlugin.lastRun.lastEnact != null) ret += "\nLast Enact: " + DateUtil.timeString(loopPlugin.lastRun.lastEnact)
}
} else {
ret += "LOOP DISABLED\n"
@ -502,7 +507,7 @@ class ActionStringHandler @Inject constructor(
} else if ("dismissoverviewnotification" == act[0]) {
rxBus.send(EventDismissNotification(SafeParse.stringToInt(act[1])))
} else if ("changeRequest" == act[0]) {
loopPlugin.get().acceptChangeRequest()
loopPlugin.acceptChangeRequest()
val notificationManager = mainApp.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(Constants.notificationID)
}

View file

@ -11,6 +11,7 @@ import dagger.android.DaggerBroadcastReceiver
import info.nightscout.androidaps.events.EventProfileNeedsUpdate
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
@ -29,6 +30,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var loopPlugin: LoopPlugin
private var lastReadStatus: Long = 0
private var lastRun: Long = 0
@ -107,7 +109,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
// sometimes keep alive broadcast stops
// as as workaround test if readStatus was requested before an alarm is generated
if (lastReadStatus != 0L && lastReadStatus > System.currentTimeMillis() - T.mins(5).msecs()) {
LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated)
LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated, loopPlugin.isDisconnected)
}
if (!pump.isThisProfileSet(profile) && !configBuilderPlugin.commandQueue.isRunning(Command.CommandType.BASALPROFILE)) {
rxBus.send(EventProfileNeedsUpdate())

View file

@ -34,12 +34,12 @@ public class LocalAlertUtils {
return T.mins(SP.getInt(MainApp.gs(R.string.key_pump_unreachable_threshold), 30)).msecs();
}
public static void checkPumpUnreachableAlarm(long lastConnection, boolean isStatusOutdated) {
public static void checkPumpUnreachableAlarm(long lastConnection, boolean isStatusOutdated, boolean isDisconnected) {
boolean alarmTimeoutExpired = lastConnection + pumpUnreachableThreshold() < System.currentTimeMillis();
boolean nextAlarmOccurrenceReached = SP.getLong("nextPumpDisconnectedAlarm", 0L) < System.currentTimeMillis();
if (Config.APS && SP.getBoolean(MainApp.gs(R.string.key_enable_pump_unreachable_alert), true)
&& isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached && !LoopPlugin.getPlugin().isDisconnected()) {
&& isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached && !isDisconnected) {
log.debug("Generating pump unreachable alarm. lastConnection: " + DateUtil.dateAndTimeString(lastConnection) + " isStatusOutdated: " + isStatusOutdated);
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.gs(R.string.pump_unreachable), Notification.URGENT);
n.soundId = R.raw.alarm;