Automation: catch instantiation errors

This commit is contained in:
Milos Kozak 2022-06-20 11:34:12 +02:00
parent dc0b6ac9f7
commit 6b6ba15852
2 changed files with 57 additions and 53 deletions

View file

@ -72,7 +72,7 @@ class WizardDialog : DaggerDialogFragment() {
private var wizard: BolusWizard? = null private var wizard: BolusWizard? = null
private var calculatedPercentage = 100.0 private var calculatedPercentage = 100.0
private var calculatedCorrection = 0.0 private var calculatedCorrection = 0.0
private var correctionPercent = false private var usePercentage = false
private var carbsPassedIntoWizard = 0.0 private var carbsPassedIntoWizard = 0.0
private var notesPassedIntoWizard = "" private var notesPassedIntoWizard = ""
private var okClicked: Boolean = false // one shot guards private var okClicked: Boolean = false // one shot guards
@ -157,7 +157,7 @@ class WizardDialog : DaggerDialogFragment() {
?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher
) )
if (correctionPercent) { if (usePercentage) {
calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher)
binding.correctionInput.value = calculatedPercentage binding.correctionInput.value = calculatedPercentage
@ -176,6 +176,7 @@ class WizardDialog : DaggerDialogFragment() {
handler.post { initDialog() } handler.post { initDialog() }
calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
binding.percentUsed.text = rh.gs(R.string.format_percent, sp.getInt(R.string.key_boluswizard_percentage, 100)) binding.percentUsed.text = rh.gs(R.string.format_percent, sp.getInt(R.string.key_boluswizard_percentage, 100))
binding.percentUsed.visibility = (sp.getInt(R.string.key_boluswizard_percentage, 100) != 100 || usePercentage).toVisibility()
// ok button // ok button
binding.okcancel.ok.setOnClickListener { binding.okcancel.ok.setOnClickListener {
if (okClicked) { if (okClicked) {
@ -227,8 +228,8 @@ class WizardDialog : DaggerDialogFragment() {
run { run {
sp.putBoolean(rh.gs(R.string.key_wizard_correction_percent), isChecked) sp.putBoolean(rh.gs(R.string.key_wizard_correction_percent), isChecked)
binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname) binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname)
correctionPercent = binding.correctionPercent.isChecked usePercentage = binding.correctionPercent.isChecked
if (correctionPercent) { if (usePercentage) {
binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher)
binding.correctionInput.customContentDescription = rh.gs(R.string.a11_correction_percentage) binding.correctionInput.customContentDescription = rh.gs(R.string.a11_correction_percentage)
} else { } else {
@ -239,7 +240,7 @@ class WizardDialog : DaggerDialogFragment() {
binding.correctionInput.customContentDescription = rh.gs(R.string.a11_correction_units) binding.correctionInput.customContentDescription = rh.gs(R.string.a11_correction_units)
} }
binding.correctionInput.updateA11yDescription() binding.correctionInput.updateA11yDescription()
binding.correctionInput.value = if (correctionPercent) calculatedPercentage else Round.roundTo(calculatedCorrection, bolusStep) binding.correctionInput.value = if (usePercentage) calculatedPercentage else Round.roundTo(calculatedCorrection, bolusStep)
} }
} }
// profile // profile
@ -320,8 +321,8 @@ class WizardDialog : DaggerDialogFragment() {
private fun loadCheckedStates() { private fun loadCheckedStates() {
binding.bgTrendCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_trend_bg, false) binding.bgTrendCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_trend_bg, false)
binding.cobCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_cob, false) binding.cobCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_cob, false)
correctionPercent = sp.getBoolean(R.string.key_wizard_correction_percent, false) usePercentage = sp.getBoolean(R.string.key_wizard_correction_percent, false)
binding.correctionPercent.isChecked = correctionPercent binding.correctionPercent.isChecked = usePercentage
} }
private fun valueToUnitsToString(value: Double, units: String): String = private fun valueToUnitsToString(value: Double, units: String): String =
@ -370,8 +371,6 @@ class WizardDialog : DaggerDialogFragment() {
binding.iobInsulin.text = rh.gs(R.string.formatinsulinunits, -bolusIob.iob - basalIob.basaliob) binding.iobInsulin.text = rh.gs(R.string.formatinsulinunits, -bolusIob.iob - basalIob.basaliob)
calculateInsulin() calculateInsulin()
binding.percentUsed.visibility = (sp.getInt(R.string.key_boluswizard_percentage, 100) != 100 || correctionPercent).toVisibility()
} }
} }

View file

@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProv
import info.nightscout.androidaps.services.LastLocationDataContainer import info.nightscout.androidaps.services.LastLocationDataContainer
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import org.json.JSONObject import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
@ -78,50 +79,54 @@ abstract class Trigger(val injector: HasAndroidInjector) {
.toString() .toString()
fun instantiate(obj: JSONObject): Trigger { fun instantiate(obj: JSONObject): Trigger {
val type = obj.getString("type") try {
val data = obj.getJSONObject("data") val type = obj.getString("type")
//val clazz = Class.forName(type).kotlin val data = obj.getJSONObject("data")
//return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() ?: "") //val clazz = Class.forName(type).kotlin
return when (type) { //return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() ?: "")
TriggerAutosensValue::class.java.name, // backward compatibility return when (type) {
TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(data.toString()) TriggerAutosensValue::class.java.name, // backward compatibility
TriggerBg::class.java.name, TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(data.toString())
TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data.toString()) TriggerBg::class.java.name,
TriggerBolusAgo::class.java.name, TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data.toString())
TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(data.toString()) TriggerBolusAgo::class.java.name,
TriggerBTDevice::class.java.name, TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(data.toString())
TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(data.toString()) TriggerBTDevice::class.java.name,
TriggerIob::class.java.name, TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(data.toString())
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString()) TriggerIob::class.java.name,
TriggerCOB::class.java.name, TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString())
TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString()) TriggerCOB::class.java.name,
TriggerConnector::class.java.name, TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString())
TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data.toString()) TriggerConnector::class.java.name,
TriggerDelta::class.java.name, TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data.toString())
TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString()) TriggerDelta::class.java.name,
TriggerDummy::class.java.name, TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString())
TriggerDummy::class.java.simpleName -> TriggerDummy(injector).fromJSON(data.toString()) TriggerDummy::class.java.name,
TriggerIob::class.java.name, TriggerDummy::class.java.simpleName -> TriggerDummy(injector).fromJSON(data.toString())
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString()) TriggerIob::class.java.name,
TriggerLocation::class.java.name, TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString())
TriggerLocation::class.java.simpleName -> TriggerLocation(injector).fromJSON(data.toString()) TriggerLocation::class.java.name,
TriggerProfilePercent::class.java.name, TriggerLocation::class.java.simpleName -> TriggerLocation(injector).fromJSON(data.toString())
TriggerProfilePercent::class.java.simpleName -> TriggerProfilePercent(injector).fromJSON(data.toString()) TriggerProfilePercent::class.java.name,
TriggerPumpLastConnection::class.java.name, TriggerProfilePercent::class.java.simpleName -> TriggerProfilePercent(injector).fromJSON(data.toString())
TriggerPumpLastConnection::class.java.simpleName -> TriggerPumpLastConnection(injector).fromJSON(data.toString()) TriggerPumpLastConnection::class.java.name,
TriggerRecurringTime::class.java.name, TriggerPumpLastConnection::class.java.simpleName -> TriggerPumpLastConnection(injector).fromJSON(data.toString())
TriggerRecurringTime::class.java.simpleName -> TriggerRecurringTime(injector).fromJSON(data.toString()) TriggerRecurringTime::class.java.name,
TriggerTempTarget::class.java.name, TriggerRecurringTime::class.java.simpleName -> TriggerRecurringTime(injector).fromJSON(data.toString())
TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON(data.toString()) TriggerTempTarget::class.java.name,
TriggerTempTargetValue::class.java.name, TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON(data.toString())
TriggerTempTargetValue::class.java.simpleName -> TriggerTempTargetValue(injector).fromJSON(data.toString()) TriggerTempTargetValue::class.java.name,
TriggerTime::class.java.name, TriggerTempTargetValue::class.java.simpleName -> TriggerTempTargetValue(injector).fromJSON(data.toString())
TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data.toString()) TriggerTime::class.java.name,
TriggerTimeRange::class.java.name, TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data.toString())
TriggerTimeRange::class.java.simpleName -> TriggerTimeRange(injector).fromJSON(data.toString()) TriggerTimeRange::class.java.name,
TriggerWifiSsid::class.java.name, TriggerTimeRange::class.java.simpleName -> TriggerTimeRange(injector).fromJSON(data.toString())
TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON(data.toString()) TriggerWifiSsid::class.java.name,
else -> TriggerConnector(injector) TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON(data.toString())
else -> TriggerConnector(injector)
}
} catch (e: Exception) {
aapsLogger.error(LTag.AUTOMATION, "Error parsing $obj")
} }
} }