LoopFragment -> kotlin

This commit is contained in:
Milos Kozak 2019-08-05 14:12:39 +02:00
parent 663d6464d5
commit 89d2ff9de8
13 changed files with 186 additions and 199 deletions

View file

@ -1,145 +0,0 @@
package info.nightscout.androidaps.plugins.aps.loop;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui;
import info.nightscout.androidaps.plugins.common.SubscriberFragment;
import info.nightscout.androidaps.utils.FabricPrivacy;
public class LoopFragment extends SubscriberFragment {
@BindView(R.id.loop_run)
Button runNowButton;
@BindView(R.id.loop_lastrun)
TextView lastRunView;
@BindView(R.id.loop_lastenact)
TextView lastEnactView;
@BindView(R.id.loop_source)
TextView sourceView;
@BindView(R.id.loop_request)
TextView requestView;
@BindView(R.id.loop_constraintsprocessed)
TextView constraintsProcessedView;
@BindView(R.id.loop_constraints)
TextView constraintsView;
@BindView(R.id.loop_tbrsetbypump)
TextView tbrSetByPumpView;
@BindView(R.id.loop_smbsetbypump)
TextView smbSetByPumpView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View view = inflater.inflate(R.layout.loop_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
} catch (Exception e) {
FabricPrivacy.logException(e);
}
return null;
}
@OnClick(R.id.loop_run)
void onRunClick() {
lastRunView.setText(MainApp.gs(R.string.executing));
new Thread(() -> LoopPlugin.getPlugin().invoke("Loop button", true)).start();
}
@Subscribe
public void onStatusEvent(final EventLoopUpdateGui ev) {
updateGUI();
}
@Subscribe
public void onStatusEvent(final EventLoopSetLastRunGui ev) {
clearGUI();
final Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
synchronized (LoopFragment.this) {
if (lastRunView != null) lastRunView.setText(ev.text);
}
});
}
@Override
protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
synchronized (LoopFragment.this) {
if (!isBound()) return;
LoopPlugin.LastRun lastRun = LoopPlugin.lastRun;
if (lastRun != null) {
requestView.setText(lastRun.request != null ? lastRun.request.toSpanned() : "");
constraintsProcessedView.setText(lastRun.constraintsProcessed != null ? lastRun.constraintsProcessed.toSpanned() : "");
sourceView.setText(lastRun.source != null ? lastRun.source : "");
lastRunView.setText(lastRun.lastAPSRun != null && lastRun.lastAPSRun.getTime() != 0 ? lastRun.lastAPSRun.toLocaleString() : "");
lastEnactView.setText(lastRun.lastEnact != null && lastRun.lastEnact.getTime() != 0 ? lastRun.lastEnact.toLocaleString() : "");
tbrSetByPumpView.setText(lastRun.tbrSetByPump != null ? Html.fromHtml(lastRun.tbrSetByPump.toHtml()) : "");
smbSetByPumpView.setText(lastRun.smbSetByPump != null ? Html.fromHtml(lastRun.smbSetByPump.toHtml()) : "");
String constraints = "";
if (lastRun.constraintsProcessed != null) {
Constraint<Double> allConstraints = new Constraint<>(0d);
if (lastRun.constraintsProcessed.rateConstraint != null)
allConstraints.copyReasons(lastRun.constraintsProcessed.rateConstraint);
if (lastRun.constraintsProcessed.smbConstraint != null)
allConstraints.copyReasons(lastRun.constraintsProcessed.smbConstraint);
constraints = allConstraints.getMostLimitedReasons();
}
constraintsView.setText(constraints);
}
}
});
}
void clearGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
synchronized (LoopFragment.this) {
if (isBound()) {
requestView.setText("");
constraintsProcessedView.setText("");
sourceView.setText("");
lastRunView.setText("");
lastEnactView.setText("");
tbrSetByPumpView.setText("");
smbSetByPumpView.setText("");
}
}
});
}
boolean isBound() {
return requestView != null
&& constraintsProcessedView != null
&& sourceView != null
&& lastRunView != null
&& lastEnactView != null
&& tbrSetByPumpView != null
&& smbSetByPumpView != null
&& constraintsView != null
&& runNowButton != null;
}
}

View file

@ -0,0 +1,105 @@
package info.nightscout.androidaps.plugins.aps.loop
import android.os.Bundle
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.loop_fragment.*
class LoopFragment : Fragment() {
private var disposable: CompositeDisposable = CompositeDisposable()
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
add(disposable)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.loop_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loop_run.setOnClickListener {
loop_lastrun.text = MainApp.gs(R.string.executing)
Thread { LoopPlugin.getPlugin().invoke("Loop button", true) }.start()
}
disposable += RxBus
.toObservable(EventLoopUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
updateGUI()
}, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventLoopSetLastRunGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
clearGUI()
loop_lastrun.text = it.text
}, {
FabricPrivacy.logException(it)
})
}
override fun onStop() {
super.onStop()
disposable.clear()
}
fun updateGUI() {
val lastRun = LoopPlugin.lastRun
lastRun?.let {
loop_request.text = it.request?.toSpanned() ?: ""
loop_constraintsprocessed.text = it.constraintsProcessed?.toSpanned() ?: ""
loop_source.text = it.source ?: ""
loop_lastrun.text = it.lastAPSRun?.let { lastrun -> DateUtil.dateAndTimeString(lastrun.time) }
?: ""
loop_lastenact.text = it.lastAPSRun?.let { lastEnact -> DateUtil.dateAndTimeString(lastEnact.time) }
?: ""
loop_tbrsetbypump.text = it.tbrSetByPump?.let { tbrSetByPump -> Html.fromHtml(tbrSetByPump.toHtml()) }
?: ""
loop_smbsetbypump.text = it.smbSetByPump?.let { smbSetByPump -> Html.fromHtml(smbSetByPump.toHtml()) }
?: ""
val constraints =
it.constraintsProcessed?.let { constraintsProcessed ->
val allConstraints = Constraint(0.0)
constraintsProcessed.rateConstraint?.let { rateConstraint -> allConstraints.copyReasons(rateConstraint) }
constraintsProcessed.smbConstraint?.let { smbConstraint -> allConstraints.copyReasons(smbConstraint) }
allConstraints.mostLimitedReasons
} ?: ""
loop_constraints.text = constraints
}
}
internal fun clearGUI() {
loop_request.text = ""
loop_constraints.text = ""
loop_constraintsprocessed.text = ""
loop_source.text = ""
loop_lastrun.text = ""
loop_lastenact.text = ""
loop_tbrsetbypump.text = ""
loop_smbsetbypump.text = ""
}
}

View file

@ -10,6 +10,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
@ -47,6 +48,7 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui;
import info.nightscout.androidaps.plugins.aps.loop.events.EventNewOpenLoopNotification;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
@ -278,7 +280,7 @@ public class LoopPlugin extends PluginBase {
String message = MainApp.gs(R.string.loopdisabled) + "\n" + loopEnabled.getReasons();
if (L.isEnabled(L.APS))
log.debug(message);
MainApp.bus().post(new EventLoopSetLastRunGui(message));
RxBus.INSTANCE.send(new EventLoopSetLastRunGui(message));
return;
}
final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
@ -292,7 +294,7 @@ public class LoopPlugin extends PluginBase {
if (!ProfileFunctions.getInstance().isProfileValid("Loop")) {
if (L.isEnabled(L.APS))
log.debug(MainApp.gs(R.string.noprofileselected));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.noprofileselected)));
RxBus.INSTANCE.send(new EventLoopSetLastRunGui(MainApp.gs(R.string.noprofileselected)));
return;
}
@ -307,7 +309,7 @@ public class LoopPlugin extends PluginBase {
// Check if we have any result
if (result == null) {
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.noapsselected)));
RxBus.INSTANCE.send(new EventLoopSetLastRunGui(MainApp.gs(R.string.noapsselected)));
return;
}
@ -349,14 +351,14 @@ public class LoopPlugin extends PluginBase {
if (isSuspended()) {
if (L.isEnabled(L.APS))
log.debug(MainApp.gs(R.string.loopsuspended));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.loopsuspended)));
RxBus.INSTANCE.send(new EventLoopSetLastRunGui(MainApp.gs(R.string.loopsuspended)));
return;
}
if (pump.isSuspended()) {
if (L.isEnabled(L.APS))
log.debug(MainApp.gs(R.string.pumpsuspended));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.pumpsuspended)));
RxBus.INSTANCE.send(new EventLoopSetLastRunGui(MainApp.gs(R.string.pumpsuspended)));
return;
}
@ -372,7 +374,7 @@ public class LoopPlugin extends PluginBase {
lastRun.tbrSetByPump = waiting;
if (resultAfterConstraints.bolusRequested)
lastRun.smbSetByPump = waiting;
MainApp.bus().post(new EventLoopUpdateGui());
RxBus.INSTANCE.send(new EventLoopUpdateGui());
FabricPrivacy.getInstance().logCustom("APSRequest");
applyTBRRequest(resultAfterConstraints, profile, new Callback() {
@Override
@ -393,11 +395,11 @@ public class LoopPlugin extends PluginBase {
LoopPlugin.getPlugin().invoke("tempBasalFallback", allowNotification, true);
}).start();
}
MainApp.bus().post(new EventLoopUpdateGui());
RxBus.INSTANCE.send(new EventLoopUpdateGui());
}
});
}
MainApp.bus().post(new EventLoopUpdateGui());
RxBus.INSTANCE.send(new EventLoopUpdateGui());
}
});
} else {
@ -451,7 +453,7 @@ public class LoopPlugin extends PluginBase {
}
}
MainApp.bus().post(new EventLoopUpdateGui());
RxBus.INSTANCE.send(new EventLoopUpdateGui());
} finally {
if (L.isEnabled(L.APS))
log.debug("invoke end");

View file

@ -1,14 +0,0 @@
package info.nightscout.androidaps.plugins.aps.loop.events;
import info.nightscout.androidaps.events.EventUpdateGui;
/**
* Created by mike on 05.08.2016.
*/
public class EventLoopSetLastRunGui extends EventUpdateGui {
public String text = null;
public EventLoopSetLastRunGui(String text) {
this.text = text;
}
}

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.plugins.aps.loop.events
import info.nightscout.androidaps.events.EventUpdateGui
/**
* Created by mike on 05.08.2016.
*/
class EventLoopSetLastRunGui(val text: String) : EventUpdateGui()

View file

@ -1,9 +0,0 @@
package info.nightscout.androidaps.plugins.aps.loop.events;
import info.nightscout.androidaps.events.EventUpdateGui;
/**
* Created by mike on 05.08.2016.
*/
public class EventLoopUpdateGui extends EventUpdateGui {
}

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.plugins.aps.loop.events
import info.nightscout.androidaps.events.EventUpdateGui
/**
* Created by mike on 05.08.2016.
*/
class EventLoopUpdateGui : EventUpdateGui()

View file

@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog
import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged
import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
@ -56,13 +57,17 @@ class AutomationFragment : Fragment() {
sb.append("\n")
}
automation_logView.text = sb.toString()
}, {})
}, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventAutomationDataChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
eventListAdapter?.notifyDataSetChanged()
}, {})
}, {
FabricPrivacy.logException(it)
})
}
override fun onStop() {

View file

@ -21,6 +21,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutos
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.services.LocationService
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.SP
import info.nightscout.androidaps.utils.T
import io.reactivex.disposables.CompositeDisposable
@ -79,11 +80,15 @@ object AutomationPlugin : PluginBase(PluginDescription()
ctx.stopService(Intent(ctx, LocationService::class.java))
ctx.startService(Intent(ctx, LocationService::class.java))
}
}, {})
}, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventAutomationDataChanged::class.java)
.observeOn(Schedulers.io())
.subscribe({ storeToSP() }, {})
.subscribe({ storeToSP() }, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventLocationChange::class.java)
.observeOn(Schedulers.io())
@ -92,19 +97,27 @@ object AutomationPlugin : PluginBase(PluginDescription()
log.debug("Grabbed location: $it.location.latitude $it.location.longitude Provider: $it.location.provider")
processActions()
}
}, {})
}, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventChargingState::class.java)
.observeOn(Schedulers.io())
.subscribe({ processActions() }, {})
.subscribe({ processActions() }, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventNetworkChange::class.java)
.observeOn(Schedulers.io())
.subscribe({ processActions() }, {})
.subscribe({ processActions() }, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(Schedulers.io())
.subscribe({ processActions() }, {})
.subscribe({ processActions() }, {
FabricPrivacy.logException(it)
})
}
override fun onStop() {

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.plugins.general.automation.AutomationEvent
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin
import info.nightscout.androidaps.plugins.general.automation.events.*
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.ToastUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
@ -100,7 +101,9 @@ class EditEventDialog : DialogFragment() {
.subscribe({
actionListAdapter?.notifyDataSetChanged()
showPreconditions()
}, {})
}, {
FabricPrivacy.logException(it)
})
)
disposable.add(RxBus
.toObservable(EventAutomationAddAction::class.java)
@ -108,7 +111,9 @@ class EditEventDialog : DialogFragment() {
.subscribe({
event.addAction(it.action)
actionListAdapter?.notifyDataSetChanged()
}, {})
}, {
FabricPrivacy.logException(it)
})
)
disposable.add(RxBus
.toObservable(EventAutomationUpdateTrigger::class.java)
@ -116,7 +121,9 @@ class EditEventDialog : DialogFragment() {
.subscribe({
event.trigger = it.trigger
automation_triggerDescription.text = event.trigger.friendlyDescription()
}, {})
}, {
FabricPrivacy.logException(it)
})
)
disposable.add(RxBus
.toObservable(EventAutomationUpdateAction::class.java)
@ -124,7 +131,9 @@ class EditEventDialog : DialogFragment() {
.subscribe({
event.actions[it.position] = it.action
actionListAdapter?.notifyDataSetChanged()
}, {})
}, {
FabricPrivacy.logException(it)
})
)
}

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolDoUpload
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolResetData
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
@ -46,7 +47,9 @@ class TidepoolFragment : Fragment() {
tidepool_status.text = TidepoolUploader.connectionStatus.name
tidepool_log.text = TidepoolPlugin.textLog
tidepool_logscrollview.fullScroll(ScrollView.FOCUS_DOWN)
}, {})
}, {
FabricPrivacy.logException(it)
})
)
}

View file

@ -21,6 +21,7 @@ import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolU
import info.nightscout.androidaps.plugins.general.tidepool.utils.RateLimit
import info.nightscout.androidaps.receivers.ChargingStateReceiver
import info.nightscout.androidaps.receivers.NetworkChangeReceiver
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.SP
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.ToastUtils
@ -56,7 +57,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
.toObservable(EventTidepoolDoUpload::class.java)
.observeOn(Schedulers.io())
.subscribe({ doUpload() }, {
log.error(it.message)
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventTidepoolResetData::class.java)
@ -70,12 +71,14 @@ object TidepoolPlugin : PluginBase(PluginDescription()
TidepoolUploader.doLogin()
}
}, {
log.error(it.message)
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventTidepoolStatus::class.java)
.observeOn(Schedulers.io())
.subscribe({ event -> addToLog(event) }, {})
.subscribe({ event -> addToLog(event) }, {
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventNewBG::class.java)
.observeOn(Schedulers.io())
@ -90,7 +93,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
&& RateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt()))
doUpload()
}, {
log.error(it.message)
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventPreferenceChange::class.java)
@ -102,13 +105,13 @@ object TidepoolPlugin : PluginBase(PluginDescription()
)
TidepoolUploader.resetInstance()
}, {
log.error(it.message)
FabricPrivacy.logException(it)
})
disposable += RxBus
.toObservable(EventNetworkChange::class.java)
.observeOn(Schedulers.io())
.subscribe({}, {
log.error(it.message)
FabricPrivacy.logException(it)
}) // TODO start upload on wifi connect
}

View file

@ -39,10 +39,8 @@ public class DateUtil {
*
* @param isoDateString the iso date string
* @return the date
* @throws Exception the exception
*/
public static Date fromISODateString(String isoDateString)
throws Exception {
public static Date fromISODateString(String isoDateString) {
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
DateTime dateTime = DateTime.parse(isoDateString, parser);
@ -148,6 +146,7 @@ public class DateUtil {
}
public static String dateAndTimeString(long mills) {
if (mills == 0) return "";
return dateString(mills) + " " + timeString(mills);
}