VersionChecker: fix message, add logging

This commit is contained in:
Milos Kozak 2023-08-24 16:51:03 +02:00
parent 921989be2b
commit def6c51fa2
4 changed files with 28 additions and 22 deletions

View file

@ -21,7 +21,7 @@ abstract class PluginsConstraintsModule {
@Module @Module
interface Bindings { interface Bindings {
@Binds fun bindProcessedDeviceStatusData(versionCheckerUtils: VersionCheckerUtilsImpl): VersionCheckerUtils @Binds fun bindVersionCheckerUtils(versionCheckerUtils: VersionCheckerUtilsImpl): VersionCheckerUtils
@Binds fun bindBgQualityCheck(bgQualityCheck: BgQualityCheckPlugin): BgQualityCheck @Binds fun bindBgQualityCheck(bgQualityCheck: BgQualityCheckPlugin): BgQualityCheck
@Binds fun bindsConstraints(constraintsImpl: info.nightscout.plugins.constraints.ConstraintsImpl): Constraints @Binds fun bindsConstraints(constraintsImpl: info.nightscout.plugins.constraints.ConstraintsImpl): Constraints
} }

View file

@ -44,7 +44,7 @@ class VersionCheckerPlugin @Inject constructor(
enum class GracePeriod(val warning: Long, val old: Long, val veryOld: Long) { enum class GracePeriod(val warning: Long, val old: Long, val veryOld: Long) {
RELEASE(30, 60, 90), RELEASE(30, 60, 90),
RC(1, 7, 14) RC(2, 7, 14)
} }
private val gracePeriod: GracePeriod private val gracePeriod: GracePeriod
@ -63,7 +63,7 @@ class VersionCheckerPlugin @Inject constructor(
override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
checkWarning() checkWarning()
versionCheckerUtils.triggerCheckVersion() versionCheckerUtils.triggerCheckVersion()
if (isOldVersion(gracePeriod.veryOld.daysToMillis())) if (lastCheckOlderThan(gracePeriod.veryOld.daysToMillis()))
value.set(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.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0) val endDate = sp.getLong(rh.gs(info.nightscout.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0)
if (endDate != 0L && dateUtil.now() > endDate) if (endDate != 0L && dateUtil.now() > endDate)
@ -72,7 +72,7 @@ class VersionCheckerPlugin @Inject constructor(
} }
override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> = override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> =
if (isOldVersion(gracePeriod.old.daysToMillis())) if (lastCheckOlderThan(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
@ -80,19 +80,19 @@ class VersionCheckerPlugin @Inject constructor(
private fun checkWarning() { private fun checkWarning() {
val now = dateUtil.now() val now = dateUtil.now()
if (!sp.contains(R.string.key_last_versionchecker_plugin_warning)) { if (!sp.contains(R.string.key_last_versionchecker_plugin_warning_timestamp)) {
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now)
return return
} }
if (isOldVersion(gracePeriod.warning.daysToMillis()) && shouldWarnAgain()) { if (lastCheckOlderThan(gracePeriod.warning.daysToMillis()) && shouldWarnAgain()) {
// store last notification time // store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now)
//notify //notify
val message = rh.gs( val message = rh.gs(
R.string.new_version_warning, R.string.new_version_warning,
((now - sp.getLong(R.string.key_last_time_this_version_detected_as_ok, now)) / 1L.daysToMillis().toDouble()).roundToInt(), ((now - sp.getLong(R.string.key_last_successful_version_check_timestamp, now)) / 1L.daysToMillis().toDouble()).roundToInt(),
gracePeriod.old, gracePeriod.old,
gracePeriod.veryOld gracePeriod.veryOld
) )
@ -102,7 +102,7 @@ class VersionCheckerPlugin @Inject constructor(
val endDate = sp.getLong(rh.gs(info.nightscout.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0) val endDate = sp.getLong(rh.gs(info.nightscout.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0)
if (endDate != 0L && dateUtil.now() > endDate && shouldWarnAgain()) { if (endDate != 0L && dateUtil.now() > endDate && shouldWarnAgain()) {
// store last notification time // store last notification time
sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now)
//notify //notify
uiInteraction.addNotification(Notification.VERSION_EXPIRE, rh.gs(R.string.application_expired), Notification.URGENT) uiInteraction.addNotification(Notification.VERSION_EXPIRE, rh.gs(R.string.application_expired), Notification.URGENT)
@ -110,10 +110,10 @@ class VersionCheckerPlugin @Inject constructor(
} }
private fun shouldWarnAgain() = private fun shouldWarnAgain() =
dateUtil.now() > sp.getLong(R.string.key_last_versionchecker_plugin_warning, 0) + WARN_EVERY dateUtil.now() > sp.getLong(R.string.key_last_versionchecker_plugin_warning_timestamp, 0) + WARN_EVERY
private fun isOldVersion(gracePeriod: Long): Boolean = private fun lastCheckOlderThan(gracePeriod: Long): Boolean =
dateUtil.now() > sp.getLong(R.string.key_last_time_this_version_detected_as_ok, 0) + gracePeriod dateUtil.now() > sp.getLong(R.string.key_last_successful_version_check_timestamp, 0) + gracePeriod
private fun Long.daysToMillis() = TimeUnit.DAYS.toMillis(this) private fun Long.daysToMillis() = TimeUnit.DAYS.toMillis(this)
} }

View file

@ -36,13 +36,13 @@ class VersionCheckerUtilsImpl @Inject constructor(
override fun triggerCheckVersion() { override fun triggerCheckVersion() {
if (!sp.contains(R.string.key_last_time_this_version_detected_as_ok)) { if (!sp.contains(R.string.key_last_successful_version_check_timestamp)) {
// 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(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now() - TimeUnit.DAYS.toMillis(30)) setLastCheckTimestamp(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(R.string.key_last_time_this_version_detected_as_ok, 0) + CHECK_EVERY) { if (dateUtil.now() > sp.getLong(R.string.key_last_successful_version_check_timestamp, 0) + CHECK_EVERY) {
checkVersion() checkVersion()
} }
} }
@ -78,6 +78,7 @@ class VersionCheckerUtilsImpl @Inject constructor(
val newVersionElements = newVersion.toNumberList() val newVersionElements = newVersion.toNumberList()
val currentVersionElements = currentVersion.toNumberList() val currentVersionElements = currentVersion.toNumberList()
aapsLogger.debug(LTag.CORE, "Compare versions: $currentVersion $currentVersionElements, $newVersion $newVersionElements")
if (newVersionElements.isNullOrEmpty()) { if (newVersionElements.isNullOrEmpty()) {
onVersionNotDetectable() onVersionNotDetectable()
return return
@ -106,15 +107,15 @@ class VersionCheckerUtilsImpl @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_as_ok, dateUtil.now()) setLastCheckTimestamp(dateUtil.now())
} }
private fun onSameVersionDetected() { private fun onSameVersionDetected() {
sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now()) setLastCheckTimestamp(dateUtil.now())
} }
private fun onVersionNotDetectable() { private fun onVersionNotDetectable() {
aapsLogger.debug(LTag.CORE, "fetch failed") aapsLogger.debug(LTag.CORE, "Fetch failed")
} }
private fun onNewVersionDetected(currentVersion: String, newVersion: String?) { private fun onNewVersionDetected(currentVersion: String, newVersion: String?) {
@ -135,6 +136,11 @@ class VersionCheckerUtilsImpl @Inject constructor(
} }
} }
private fun setLastCheckTimestamp(timestamp: Long) {
aapsLogger.debug(LTag.CORE, "Setting key_last_successful_version_check_timestamp ${dateUtil.dateAndTimeAndSecondsString(timestamp)}")
sp.putLong(R.string.key_last_successful_version_check_timestamp, timestamp)
}
private fun String?.toNumberList() = private fun String?.toNumberList() =
this?.numericVersionPart().takeIf { !it.isNullOrBlank() }?.split(".")?.map { it.toInt() } this?.numericVersionPart().takeIf { !it.isNullOrBlank() }?.split(".")?.map { it.toInt() }

View file

@ -20,11 +20,11 @@
<string name="old_version">old version</string> <string name="old_version">old version</string>
<string name="very_old_version">very old version</string> <string name="very_old_version">very old version</string>
<string name="application_expired">Application expired</string> <string name="application_expired">Application expired</string>
<string name="new_version_warning">New version for at least %1$d days available! Fallback to LGS after %2$d days, loop will be disabled after %3$d days</string> <string name="new_version_warning">New version has not been checked for at least %1$d days! Fallback to LGS after %2$d days, loop will be disabled after %3$d days. Restore internet connectivity!</string>
<string name="key_last_time_this_version_detected_as_ok" translatable="false">last_time_this_version_detected</string> <string name="key_last_successful_version_check_timestamp" translatable="false">last_successful_version_check_timestamp</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_timestamp" translatable="false">last_versionchecker_plugin_warning_timestamp</string>
<string name="key_last_revoked_certs_check" translatable="false">last_revoked_certs_check</string> <string name="key_last_revoked_certs_check" translatable="false">last_revoked_certs_check</string>
<string name="running_invalid_version">We have detected that you are running an invalid version. Loop disabled!</string> <string name="running_invalid_version">We have detected that you are running an invalid version. Loop disabled!</string>
<string name="versionavailable">Version %1$s available</string> <string name="versionavailable">Version %1$s available</string>