DstHelperPlugin refactor

This commit is contained in:
Milos Kozak 2020-01-02 20:48:36 +01:00
parent 88994d1b54
commit 45afd5ec34
5 changed files with 101 additions and 100 deletions

View file

@ -133,6 +133,7 @@ public class MainApp extends DaggerApplication {
@Inject DanaRSPlugin danaRSPlugin;
@Inject DanaRv2Plugin danaRv2Plugin;
@Inject DanaRKoreanPlugin danaRKoreanPlugin;
@Inject DstHelperPlugin dstHelperPlugin;
@Inject InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin;
@Inject InsulinOrefRapidActingPlugin insulinOrefRapidActingPlugin;
@Inject InsulinOrefUltraRapidActingPlugin insulinOrefUltraRapidActingPlugin;
@ -267,12 +268,10 @@ public class MainApp extends DaggerApplication {
// if (engineeringMode) pluginsList.add(tidepoolPlugin);
pluginsList.add(maintenancePlugin);
pluginsList.add(automationPlugin);
pluginsList.add(dstHelperPlugin);
pluginsList.add(configBuilderPlugin);
pluginsList.add(DstHelperPlugin.getPlugin());
configBuilderPlugin.initialize();
}

View file

@ -1,96 +0,0 @@
package info.nightscout.androidaps.plugins.constraints.dstHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
public class DstHelperPlugin extends PluginBase implements ConstraintsInterface {
public static final int DISABLE_TIMEFRAME_HOURS = -3;
public static final int WARN_PRIOR_TIMEFRAME_HOURS = 12;
private static Logger log = LoggerFactory.getLogger(L.CONSTRAINTS);
static DstHelperPlugin plugin = null;
@Deprecated
public static DstHelperPlugin getPlugin() {
if (plugin == null)
plugin = new DstHelperPlugin();
return plugin;
}
public DstHelperPlugin() {
super(new PluginDescription()
.mainType(PluginType.CONSTRAINTS)
.neverVisible(true)
.alwaysEnabled(true)
.showInList(false)
.pluginName(R.string.dst_plugin_name)
);
}
public static boolean wasDST(Calendar now) {
Calendar ago = (Calendar) now.clone();
ago.add(Calendar.HOUR, DISABLE_TIMEFRAME_HOURS);
return now.get(Calendar.DST_OFFSET) != ago.get(Calendar.DST_OFFSET);
}
public static boolean willBeDST(Calendar now) {
Calendar ago = (Calendar) now.clone();
ago.add(Calendar.HOUR, WARN_PRIOR_TIMEFRAME_HOURS);
return now.get(Calendar.DST_OFFSET) != ago.get(Calendar.DST_OFFSET);
}
//Return false if time to DST change happened in the last 3 hours.
@Override
public Constraint<Boolean> isLoopInvocationAllowed(Constraint<Boolean> value) {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
if (pump == null || pump.canHandleDST()) {
log.debug("Pump can handle DST");
return value;
}
Calendar cal = Calendar.getInstance();
if (willBeDST(cal)) {
warnUser(Notification.DST_IN_24H, MainApp.gs(R.string.dst_in_24h_warning));
}
if (!value.value()) {
log.debug("Already not allowed - don't check further");
return value;
}
if (wasDST(cal)) {
LoopPlugin loopPlugin = LoopPlugin.getPlugin();
if (!loopPlugin.isSuspended()) {
warnUser(Notification.DST_LOOP_DISABLED, MainApp.gs(R.string.dst_loop_disabled_warning));
} else {
log.debug("Loop already suspended");
}
value.set(false, "DST in last 3 hours.", this);
}
return value;
}
private void warnUser(int id, String warningText) {
Notification notification = new Notification(id, warningText, Notification.LOW);
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
}
}

View file

@ -0,0 +1,96 @@
package info.nightscout.androidaps.plugins.constraints.dstHelper
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.interfaces.ConstraintsInterface
import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginDescription
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
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.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationWithAction
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class DstHelperPlugin @Inject constructor(): PluginBase(PluginDescription()
.mainType(PluginType.CONSTRAINTS)
.neverVisible(true)
.alwaysEnabled(true)
.showInList(false)
.pluginName(R.string.dst_plugin_name)
), ConstraintsInterface {
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var mainApp: MainApp
@Inject lateinit var sp : SP
@Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin
@Inject lateinit var loopPlugin: LoopPlugin
private val DISABLE_TIMEFRAME_HOURS = -3
private val WARN_PRIOR_TIMEFRAME_HOURS = 12
//Return false if time to DST change happened in the last 3 hours.
override fun isLoopInvocationAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
val pump = configBuilderPlugin.activePump
if (pump == null || pump.canHandleDST()) {
aapsLogger.debug(LTag.CONSTRAINTS, "Pump can handle DST")
return value
}
val cal = Calendar.getInstance()
if (willBeDST(cal)) {
val snoozedTo: Long = sp.getLong(R.string.key_snooze_dst_in24h, 0L)
if (snoozedTo == 0L || System.currentTimeMillis() > snoozedTo) {
val notification = NotificationWithAction(mainApp, Notification.DST_IN_24H, resourceHelper.gs(R.string.dst_in_24h_warning), Notification.LOW)
notification.action(R.string.snooze, Runnable {
sp.putLong(R.string.key_snooze_dst_in24h, System.currentTimeMillis() + T.hours(24).msecs())
})
rxBus.send(EventNewNotification(notification))
}
}
if (!value.value()) {
aapsLogger.debug(LTag.CONSTRAINTS, "Already not allowed - don't check further")
return value
}
if (wasDST(cal)) {
if (!loopPlugin.isSuspended) {
val snoozedTo: Long = sp.getLong(R.string.key_snooze_loopdisabled, 0L)
if (snoozedTo == 0L || System.currentTimeMillis() > snoozedTo) {
val notification = NotificationWithAction(mainApp, Notification.DST_LOOP_DISABLED, resourceHelper.gs(R.string.dst_loop_disabled_warning), Notification.LOW)
notification.action(R.string.snooze, Runnable {
sp.putLong(R.string.key_snooze_loopdisabled, System.currentTimeMillis() + T.hours(24).msecs())
})
rxBus.send(EventNewNotification(notification))
}
} else {
aapsLogger.debug(LTag.CONSTRAINTS, "Loop already suspended")
}
value.set(false, "DST in last 3 hours.", this)
}
return value
}
fun wasDST(now: Calendar): Boolean {
val ago = now.clone() as Calendar
ago.add(Calendar.HOUR, DISABLE_TIMEFRAME_HOURS)
return now[Calendar.DST_OFFSET] != ago[Calendar.DST_OFFSET]
}
fun willBeDST(now: Calendar): Boolean {
val ago = now.clone() as Calendar
ago.add(Calendar.HOUR, WARN_PRIOR_TIMEFRAME_HOURS)
return now[Calendar.DST_OFFSET] != ago[Calendar.DST_OFFSET]
}
}

View file

@ -72,7 +72,7 @@ class NotificationWithAction constructor(
}
}
fun action(buttonText: Int, action: Runnable?) {
fun action(buttonText: Int, action: Runnable) {
this.buttonText = buttonText
this.action = action
}

View file

@ -1689,5 +1689,7 @@
<string name="key_xdripstatus_showbgi" translatable="false">xdripstatus_showbgi</string>
<string name="key_wear_detailed_delta" translatable="false">wear_detailed_delta</string>
<string name="key_snoozedTo" translatable="false">snoozedTo</string>
<string name="key_snooze_dst_in24h" translatable="false">snooze_dst_in24h</string>
<string name="key_snooze_loopdisabled" translatable="false">snooze_loopdisabled</string>
</resources>