This commit is contained in:
Milos Kozak 2022-01-14 13:08:28 +01:00
parent f440394121
commit d7b822a436
6 changed files with 67 additions and 88 deletions

View file

@ -201,7 +201,7 @@ class OpenAPSSMBPlugin @Inject constructor(
} }
override fun isSuperBolusEnabled(value: Constraint<Boolean>): Constraint<Boolean> { override fun isSuperBolusEnabled(value: Constraint<Boolean>): Constraint<Boolean> {
value[aapsLogger] = false value.set(aapsLogger, false)
return value return value
} }
} }

View file

@ -57,52 +57,52 @@ class SafetyPlugin @Inject constructor(
* Constraints interface * Constraints interface
*/ */
override fun isLoopInvocationAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isLoopInvocationAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
if (!activePlugin.activePump.pumpDescription.isTempBasalCapable) value[aapsLogger, false, rh.gs(R.string.pumpisnottempbasalcapable)] = this if (!activePlugin.activePump.pumpDescription.isTempBasalCapable) value.set(aapsLogger, false, rh.gs(R.string.pumpisnottempbasalcapable), this)
return value return value
} }
override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
val mode = sp.getString(R.string.key_aps_mode, "open") val mode = sp.getString(R.string.key_aps_mode, "open")
if (mode == "open") value[aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences)] = this if (mode == "open") value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this)
if (!buildHelper.isEngineeringModeOrRelease()) { if (!buildHelper.isEngineeringModeOrRelease()) {
if (value.value()) { if (value.value()) {
val n = Notification(Notification.TOAST_ALARM, rh.gs(R.string.closed_loop_disabled_on_dev_branch), Notification.NORMAL) val n = Notification(Notification.TOAST_ALARM, rh.gs(R.string.closed_loop_disabled_on_dev_branch), Notification.NORMAL)
rxBus.send(EventNewNotification(n)) rxBus.send(EventNewNotification(n))
} }
value[aapsLogger, false, rh.gs(R.string.closed_loop_disabled_on_dev_branch)] = this value.set(aapsLogger, false, rh.gs(R.string.closed_loop_disabled_on_dev_branch), this)
} }
val pump = activePlugin.activePump val pump = activePlugin.activePump
if (!pump.isFakingTempsByExtendedBoluses && iobCobCalculator.getExtendedBolus(dateUtil.now()) != null) { if (!pump.isFakingTempsByExtendedBoluses && iobCobCalculator.getExtendedBolus(dateUtil.now()) != null) {
value[aapsLogger, false, rh.gs(R.string.closed_loop_disabled_with_eb)] = this value.set(aapsLogger, false, rh.gs(R.string.closed_loop_disabled_with_eb), this)
} }
return value return value
} }
override fun isAutosensModeEnabled(value: Constraint<Boolean>): Constraint<Boolean> { override fun isAutosensModeEnabled(value: Constraint<Boolean>): Constraint<Boolean> {
val enabled = sp.getBoolean(R.string.key_openapsama_useautosens, false) val enabled = sp.getBoolean(R.string.key_openapsama_useautosens, false)
if (!enabled) value[aapsLogger, false, rh.gs(R.string.autosensdisabledinpreferences)] = this if (!enabled) value.set(aapsLogger, false, rh.gs(R.string.autosensdisabledinpreferences), this)
return value return value
} }
override fun isSMBModeEnabled(value: Constraint<Boolean>): Constraint<Boolean> { override fun isSMBModeEnabled(value: Constraint<Boolean>): Constraint<Boolean> {
val enabled = sp.getBoolean(R.string.key_use_smb, false) val enabled = sp.getBoolean(R.string.key_use_smb, false)
if (!enabled) value[aapsLogger, false, rh.gs(R.string.smbdisabledinpreferences)] = this if (!enabled) value.set(aapsLogger, false, rh.gs(R.string.smbdisabledinpreferences), this)
val closedLoop = constraintChecker.isClosedLoopAllowed() val closedLoop = constraintChecker.isClosedLoopAllowed()
if (!closedLoop.value()) value[aapsLogger, false, rh.gs(R.string.smbnotallowedinopenloopmode)] = this if (!closedLoop.value()) value.set(aapsLogger, false, rh.gs(R.string.smbnotallowedinopenloopmode), this)
return value return value
} }
override fun isUAMEnabled(value: Constraint<Boolean>): Constraint<Boolean> { override fun isUAMEnabled(value: Constraint<Boolean>): Constraint<Boolean> {
val enabled = sp.getBoolean(R.string.key_use_uam, false) val enabled = sp.getBoolean(R.string.key_use_uam, false)
if (!enabled) value[aapsLogger, false, rh.gs(R.string.uamdisabledinpreferences)] = this if (!enabled) value.set(aapsLogger, false, rh.gs(R.string.uamdisabledinpreferences), this)
val oref1Enabled = sensitivityOref1Plugin.isEnabled() val oref1Enabled = sensitivityOref1Plugin.isEnabled()
if (!oref1Enabled) value[aapsLogger, false, rh.gs(R.string.uamdisabledoref1notselected)] = this if (!oref1Enabled) value.set(aapsLogger, false, rh.gs(R.string.uamdisabledoref1notselected), this)
return value return value
} }
override fun isAdvancedFilteringEnabled(value: Constraint<Boolean>): Constraint<Boolean> { override fun isAdvancedFilteringEnabled(value: Constraint<Boolean>): Constraint<Boolean> {
val bgSource = activePlugin.activeBgSource val bgSource = activePlugin.activeBgSource
if (!bgSource.advancedFilteringSupported()) value[aapsLogger, false, rh.gs(R.string.smbalwaysdisabled)] = this if (!bgSource.advancedFilteringSupported()) value.set(aapsLogger, false, rh.gs(R.string.smbalwaysdisabled), this)
return value return value
} }
@ -134,7 +134,7 @@ class SafetyPlugin @Inject constructor(
// do rounding // do rounding
if (pump.pumpDescription.tempBasalStyle == PumpDescription.ABSOLUTE) { if (pump.pumpDescription.tempBasalStyle == PumpDescription.ABSOLUTE) {
absoluteRate[aapsLogger] = Round.roundTo(absoluteRate.value(), pump.pumpDescription.tempAbsoluteStep) absoluteRate.set(aapsLogger, Round.roundTo(absoluteRate.value(), pump.pumpDescription.tempAbsoluteStep))
} }
return absoluteRate return absoluteRate
} }
@ -149,7 +149,7 @@ class SafetyPlugin @Inject constructor(
val pump = activePlugin.activePump val pump = activePlugin.activePump
var percentRateAfterConst = java.lang.Double.valueOf(absoluteConstraint.value() / currentBasal * 100).toInt() var percentRateAfterConst = java.lang.Double.valueOf(absoluteConstraint.value() / currentBasal * 100).toInt()
percentRateAfterConst = if (percentRateAfterConst < 100) Round.ceilTo(percentRateAfterConst.toDouble(), pump.pumpDescription.tempPercentStep.toDouble()).toInt() else Round.floorTo(percentRateAfterConst.toDouble(), pump.pumpDescription.tempPercentStep.toDouble()).toInt() percentRateAfterConst = if (percentRateAfterConst < 100) Round.ceilTo(percentRateAfterConst.toDouble(), pump.pumpDescription.tempPercentStep.toDouble()).toInt() else Round.floorTo(percentRateAfterConst.toDouble(), pump.pumpDescription.tempPercentStep.toDouble()).toInt()
percentRate[aapsLogger, percentRateAfterConst, String.format(rh.gs(R.string.limitingpercentrate), percentRateAfterConst, rh.gs(R.string.pumplimit))] = this percentRate.set(aapsLogger, percentRateAfterConst, String.format(rh.gs(R.string.limitingpercentrate), percentRateAfterConst, rh.gs(R.string.pumplimit)), this)
if (pump.pumpDescription.tempBasalStyle == PumpDescription.PERCENT) { if (pump.pumpDescription.tempBasalStyle == PumpDescription.PERCENT) {
val pumpLimit = pump.pumpDescription.pumpType.tbrSettings?.maxDose ?: 0.0 val pumpLimit = pump.pumpDescription.pumpType.tbrSettings?.maxDose ?: 0.0
percentRate.setIfSmaller(aapsLogger, pumpLimit.toInt(), String.format(rh.gs(R.string.limitingbasalratio), pumpLimit, rh.gs(R.string.pumplimit)), this) percentRate.setIfSmaller(aapsLogger, pumpLimit.toInt(), String.format(rh.gs(R.string.limitingbasalratio), pumpLimit, rh.gs(R.string.pumplimit)), this)

View file

@ -4,13 +4,13 @@ import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.BuildConfig
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.interfaces.*
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification 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.Notification
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.extensions.daysToMillis import info.nightscout.androidaps.utils.extensions.daysToMillis
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
@ -59,59 +59,55 @@ class VersionCheckerPlugin @Inject constructor(
checkWarning() checkWarning()
versionCheckerUtils.triggerCheckVersion() versionCheckerUtils.triggerCheckVersion()
if (isOldVersion(gracePeriod.veryOld.daysToMillis())) if (isOldVersion(gracePeriod.veryOld.daysToMillis()))
value[aapsLogger, false, rh.gs(R.string.very_old_version)] = this value.set(aapsLogger, false, rh.gs(R.string.very_old_version), this)
val endDate = sp.getLong(rh.gs(info.nightscout.androidaps.core.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0) val endDate = sp.getLong(rh.gs(R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0)
if (endDate != 0L && dateUtil.now() > endDate) if (endDate != 0L && dateUtil.now() > endDate)
value[aapsLogger, false, rh.gs(R.string.application_expired)] = this value.set(aapsLogger, false, rh.gs(R.string.application_expired), this)
return value return value
} }
private fun checkWarning() {
val now = System.currentTimeMillis()
if (!sp.contains(R.string.key_last_versionchecker_plugin_warning)) {
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
return
}
if (isOldVersion(gracePeriod.warning.daysToMillis()) && shouldWarnAgain(now)) {
// store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
//notify
val message = rh.gs(
R.string.new_version_warning,
((now - sp.getLong(R.string.key_last_time_this_version_detected, now)) / 1L.daysToMillis().toDouble()).roundToInt(),
gracePeriod.old,
gracePeriod.veryOld
)
val notification = Notification(Notification.OLD_VERSION, message, Notification.NORMAL)
rxBus.send(EventNewNotification(notification))
}
val endDate = sp.getLong(rh.gs(info.nightscout.androidaps.core.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0)
if (endDate != 0L && dateUtil.now() > endDate && shouldWarnAgain(now)) {
// store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
//notify
val notification = Notification(Notification.VERSION_EXPIRE, rh.gs(R.string.application_expired), Notification.URGENT)
rxBus.send(EventNewNotification(notification))
}
}
private fun shouldWarnAgain(now: Long) =
now > sp.getLong(R.string.key_last_versionchecker_plugin_warning, 0) + WARN_EVERY
override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> = override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> =
if (isOldVersion(gracePeriod.old.daysToMillis())) if (isOldVersion(gracePeriod.old.daysToMillis()))
maxIob.set(aapsLogger, 0.0, rh.gs(R.string.old_version), this) maxIob.set(aapsLogger, 0.0, rh.gs(R.string.old_version), this)
else else
maxIob maxIob
private fun isOldVersion(gracePeriod: Long): Boolean { private fun checkWarning() {
val now = System.currentTimeMillis() val now = dateUtil.now()
return now > sp.getLong(R.string.key_last_time_this_version_detected, 0) + gracePeriod
if (!sp.contains(R.string.key_last_versionchecker_plugin_warning)) {
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
return
}
if (isOldVersion(gracePeriod.warning.daysToMillis()) && shouldWarnAgain()) {
// store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
//notify
val message = rh.gs(
R.string.new_version_warning,
((now - sp.getLong(R.string.key_last_time_this_version_detected_as_ok, now)) / 1L.daysToMillis().toDouble()).roundToInt(),
gracePeriod.old,
gracePeriod.veryOld
)
rxBus.send(EventNewNotification(Notification(Notification.OLD_VERSION, message, Notification.NORMAL)))
}
val endDate = sp.getLong(rh.gs(R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0)
if (endDate != 0L && dateUtil.now() > endDate && shouldWarnAgain()) {
// store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now)
//notify
rxBus.send(EventNewNotification(Notification(Notification.VERSION_EXPIRE, rh.gs(R.string.application_expired), Notification.URGENT)))
} }
} }
private fun shouldWarnAgain() =
dateUtil.now() > sp.getLong(R.string.key_last_versionchecker_plugin_warning, 0) + WARN_EVERY
private fun isOldVersion(gracePeriod: Long): Boolean =
dateUtil.now() > sp.getLong(R.string.key_last_time_this_version_detected_as_ok, 0) + gracePeriod
}

View file

@ -17,14 +17,14 @@ class Constraint<T : Comparable<T>>(private var value: T) {
return originalValue return originalValue
} }
operator fun set(aapsLogger: AAPSLogger, value: T): Constraint<T> { fun set(aapsLogger: AAPSLogger, value: T): Constraint<T> {
this.value = value this.value = value
originalValue = value originalValue = value
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value $value") aapsLogger.debug(LTag.CONSTRAINTS, "Setting value $value")
return this return this
} }
operator fun set(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> { fun set(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint<T> {
aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]") aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]")
this.value = value this.value = value
addReason(reason, from) addReason(reason, from)

View file

@ -34,20 +34,13 @@ class VersionCheckerUtils @Inject constructor(
fun triggerCheckVersion() { fun triggerCheckVersion() {
if (!sp.contains(R.string.key_last_time_this_version_detected)) { if (!sp.contains(R.string.key_last_time_this_version_detected_as_ok)) {
// On a new installation, set it as 30 days old in order to warn that there is a new version. // On a new installation, set it as 30 days old in order to warn that there is a new version.
sp.putLong( sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now() - TimeUnit.DAYS.toMillis(30))
R.string.key_last_time_this_version_detected,
dateUtil.now() - TimeUnit.DAYS.toMillis(30)
)
} }
// If we are good, only check once every day. // If we are good, only check once every day.
if (dateUtil.now() > sp.getLong( if (dateUtil.now() > sp.getLong(R.string.key_last_time_this_version_detected_as_ok, 0) + CHECK_EVERY) {
R.string.key_last_time_this_version_detected,
0
) + CHECK_EVERY
) {
checkVersion() checkVersion()
} }
} }
@ -68,7 +61,7 @@ class VersionCheckerUtils @Inject constructor(
sp.putLong(rh.gs(R.string.key_app_expiration) + "_" + config.VERSION_NAME, endDate) sp.putLong(rh.gs(R.string.key_app_expiration) + "_" + config.VERSION_NAME, endDate)
} }
} }
if (endDate != 0L) onExpiredVersionDetected(config.VERSION_NAME, dateUtil.dateString(endDate)) if (endDate != 0L) onExpireDateDetected(config.VERSION_NAME, dateUtil.dateString(endDate))
} catch (e: IOException) { } catch (e: IOException) {
aapsLogger.error(LTag.CORE, "Github master version check error: $e") aapsLogger.error(LTag.CORE, "Github master version check error: $e")
@ -111,11 +104,11 @@ class VersionCheckerUtils @Inject constructor(
private fun onOlderVersionDetected() { private fun onOlderVersionDetected() {
aapsLogger.debug(LTag.CORE, "Version newer than master. Are you developer?") aapsLogger.debug(LTag.CORE, "Version newer than master. Are you developer?")
sp.putLong(R.string.key_last_time_this_version_detected, dateUtil.now()) sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now())
} }
private fun onSameVersionDetected() { private fun onSameVersionDetected() {
sp.putLong(R.string.key_last_time_this_version_detected, dateUtil.now()) sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now())
} }
private fun onVersionNotDetectable() { private fun onVersionNotDetectable() {
@ -126,26 +119,16 @@ class VersionCheckerUtils @Inject constructor(
val now = dateUtil.now() val now = dateUtil.now()
if (now > sp.getLong(R.string.key_last_versionchecker_warning, 0) + WARN_EVERY) { if (now > sp.getLong(R.string.key_last_versionchecker_warning, 0) + WARN_EVERY) {
aapsLogger.debug(LTag.CORE, "Version $currentVersion outdated. Found $newVersion") aapsLogger.debug(LTag.CORE, "Version $currentVersion outdated. Found $newVersion")
val notification = Notification( rxBus.send(EventNewNotification(Notification(Notification.NEW_VERSION_DETECTED, rh.gs(R.string.versionavailable, newVersion.toString()), Notification.LOW)))
Notification.NEW_VERSION_DETECTED,
rh.gs(R.string.versionavailable, newVersion.toString()),
Notification.LOW
)
rxBus.send(EventNewNotification(notification))
sp.putLong(R.string.key_last_versionchecker_warning, now) sp.putLong(R.string.key_last_versionchecker_warning, now)
} }
} }
private fun onExpiredVersionDetected(currentVersion: String, endDate: String?) { private fun onExpireDateDetected(currentVersion: String, endDate: String?) {
val now = dateUtil.now() val now = dateUtil.now()
if (now > sp.getLong(R.string.key_last_expired_versionchecker_warning, 0) + WARN_EVERY) { if (now > sp.getLong(R.string.key_last_expired_versionchecker_warning, 0) + WARN_EVERY) {
aapsLogger.debug(LTag.CORE, "Version $currentVersion expired.") aapsLogger.debug(LTag.CORE, rh.gs(R.string.version_expire, currentVersion, endDate))
val notification = Notification( rxBus.send(EventNewNotification(Notification(Notification.VERSION_EXPIRE, rh.gs(R.string.version_expire, currentVersion, endDate), Notification.LOW)))
Notification.VERSION_EXPIRE,
rh.gs(R.string.version_expire, currentVersion, endDate),
Notification.LOW
)
rxBus.send(EventNewNotification(notification))
sp.putLong(R.string.key_last_expired_versionchecker_warning, now) sp.putLong(R.string.key_last_expired_versionchecker_warning, now)
} }
} }

View file

@ -371,7 +371,7 @@
<string name="prefdecrypt_issue_wrong_algorithm">Unsupported or not specified encryption algorithm!</string> <string name="prefdecrypt_issue_wrong_algorithm">Unsupported or not specified encryption algorithm!</string>
<!-- VersionChecker --> <!-- VersionChecker -->
<string name="key_last_time_this_version_detected" translatable="false">last_time_this_version_detected</string> <string name="key_last_time_this_version_detected_as_ok" translatable="false">last_time_this_version_detected</string>
<string name="key_last_versionchecker_warning" translatable="false">last_versionchecker_warning</string> <string name="key_last_versionchecker_warning" translatable="false">last_versionchecker_warning</string>
<string name="key_last_expired_versionchecker_warning" translatable="false">last_expired_version_checker_warning</string> <string name="key_last_expired_versionchecker_warning" translatable="false">last_expired_version_checker_warning</string>
<string name="key_last_versionchecker_plugin_warning" translatable="false">last_versionchecker_plugin_waring</string> <string name="key_last_versionchecker_plugin_warning" translatable="false">last_versionchecker_plugin_waring</string>