better handle wrong profile
This commit is contained in:
parent
2b71bfdeb0
commit
ba9a5dc97b
|
@ -178,47 +178,48 @@ class ProfileSwitchDialog : DialogFragmentWithDate() {
|
||||||
actions.add(resourceHelper.gs(R.string.careportal_temporarytarget) + ": " + resourceHelper.gs(R.string.activity))
|
actions.add(resourceHelper.gs(R.string.careportal_temporarytarget) + ": " + resourceHelper.gs(R.string.activity))
|
||||||
|
|
||||||
activity?.let { activity ->
|
activity?.let { activity ->
|
||||||
val ps = profileFunction.buildProfileSwitch(profileStore, profileName, duration, percent, timeShift, eventTime)
|
val ps = profileFunction.buildProfileSwitch(profileStore, profileName, duration, percent, timeShift, eventTime) ?: return@let
|
||||||
val validity = ProfileSealed.PS(ps).isValid(resourceHelper.gs(R.string.careportal_profileswitch), activePlugin.activePump, config, resourceHelper, rxBus, hardLimits, false)
|
val validity = ProfileSealed.PS(ps).isValid(resourceHelper.gs(R.string.careportal_profileswitch), activePlugin.activePump, config, resourceHelper, rxBus, hardLimits, false)
|
||||||
if (validity.isValid)
|
if (validity.isValid)
|
||||||
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
||||||
profileFunction.createProfileSwitch(
|
if (profileFunction.createProfileSwitch(
|
||||||
profileStore,
|
profileStore,
|
||||||
profileName = profileName,
|
profileName = profileName,
|
||||||
durationInMinutes = duration,
|
durationInMinutes = duration,
|
||||||
percentage = percent,
|
percentage = percent,
|
||||||
timeShiftInHours = timeShift,
|
timeShiftInHours = timeShift,
|
||||||
timestamp = eventTime
|
timestamp = eventTime
|
||||||
)
|
)) {
|
||||||
uel.log(Action.PROFILE_SWITCH,
|
uel.log(Action.PROFILE_SWITCH,
|
||||||
Sources.ProfileSwitchDialog,
|
Sources.ProfileSwitchDialog,
|
||||||
notes,
|
notes,
|
||||||
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
|
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
|
||||||
ValueWithUnit.SimpleString(profileName),
|
ValueWithUnit.SimpleString(profileName),
|
||||||
ValueWithUnit.Percent(percent),
|
ValueWithUnit.Percent(percent),
|
||||||
ValueWithUnit.Hour(timeShift).takeIf { timeShift != 0 },
|
ValueWithUnit.Hour(timeShift).takeIf { timeShift != 0 },
|
||||||
ValueWithUnit.Minute(duration).takeIf { duration != 0 })
|
ValueWithUnit.Minute(duration).takeIf { duration != 0 })
|
||||||
if (percent == 90 && duration == 10) sp.putBoolean(R.string.key_objectiveuseprofileswitch, true)
|
if (percent == 90 && duration == 10) sp.putBoolean(R.string.key_objectiveuseprofileswitch, true)
|
||||||
if (isTT) {
|
if (isTT) {
|
||||||
disposable += repository.runTransactionForResult(
|
disposable += repository.runTransactionForResult(
|
||||||
InsertAndCancelCurrentTemporaryTargetTransaction(
|
InsertAndCancelCurrentTemporaryTargetTransaction(
|
||||||
timestamp = eventTime,
|
timestamp = eventTime,
|
||||||
duration = TimeUnit.MINUTES.toMillis(duration.toLong()),
|
duration = TimeUnit.MINUTES.toMillis(duration.toLong()),
|
||||||
reason = TemporaryTarget.Reason.ACTIVITY,
|
reason = TemporaryTarget.Reason.ACTIVITY,
|
||||||
lowTarget = Profile.toMgdl(target, profileFunction.getUnits()),
|
lowTarget = Profile.toMgdl(target, profileFunction.getUnits()),
|
||||||
highTarget = Profile.toMgdl(target, profileFunction.getUnits())
|
highTarget = Profile.toMgdl(target, profileFunction.getUnits())
|
||||||
|
)
|
||||||
|
).subscribe({ result ->
|
||||||
|
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted temp target $it") }
|
||||||
|
result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated temp target $it") }
|
||||||
|
}, {
|
||||||
|
aapsLogger.error(LTag.DATABASE, "Error while saving temporary target", it)
|
||||||
|
})
|
||||||
|
uel.log(
|
||||||
|
Action.TT, Sources.TTDialog, ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged }, ValueWithUnit.TherapyEventTTReason(
|
||||||
|
TemporaryTarget.Reason.ACTIVITY
|
||||||
|
), ValueWithUnit.fromGlucoseUnit(target, units.asText), ValueWithUnit.Minute(duration)
|
||||||
)
|
)
|
||||||
).subscribe({ result ->
|
}
|
||||||
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted temp target $it") }
|
|
||||||
result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated temp target $it") }
|
|
||||||
}, {
|
|
||||||
aapsLogger.error(LTag.DATABASE, "Error while saving temporary target", it)
|
|
||||||
})
|
|
||||||
uel.log(
|
|
||||||
Action.TT, Sources.TTDialog, ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged }, ValueWithUnit.TherapyEventTTReason(
|
|
||||||
TemporaryTarget.Reason.ACTIVITY
|
|
||||||
), ValueWithUnit.fromGlucoseUnit(target, units.asText), ValueWithUnit.Minute(duration)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -141,9 +141,8 @@ class ProfileFunctionImplementation @Inject constructor(
|
||||||
if (sp.getString(R.string.key_units, Constants.MGDL) == Constants.MGDL) GlucoseUnit.MGDL
|
if (sp.getString(R.string.key_units, Constants.MGDL) == Constants.MGDL) GlucoseUnit.MGDL
|
||||||
else GlucoseUnit.MMOL
|
else GlucoseUnit.MMOL
|
||||||
|
|
||||||
override fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch {
|
override fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch? {
|
||||||
val pureProfile = profileStore.getSpecificProfile(profileName)
|
val pureProfile = profileStore.getSpecificProfile(profileName) ?: return null
|
||||||
?: throw InvalidParameterSpecException(profileName)
|
|
||||||
return ProfileSwitch(
|
return ProfileSwitch(
|
||||||
timestamp = timestamp,
|
timestamp = timestamp,
|
||||||
basalBlocks = pureProfile.basalBlocks,
|
basalBlocks = pureProfile.basalBlocks,
|
||||||
|
@ -161,8 +160,8 @@ class ProfileFunctionImplementation @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long) {
|
override fun createProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): Boolean {
|
||||||
val ps = buildProfileSwitch(profileStore, profileName, durationInMinutes, percentage, timeShiftInHours, timestamp)
|
val ps = buildProfileSwitch(profileStore, profileName, durationInMinutes, percentage, timeShiftInHours, timestamp) ?: return false
|
||||||
disposable += repository.runTransactionForResult(InsertOrUpdateProfileSwitch(ps))
|
disposable += repository.runTransactionForResult(InsertOrUpdateProfileSwitch(ps))
|
||||||
.subscribe({ result ->
|
.subscribe({ result ->
|
||||||
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted ProfileSwitch $it") }
|
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted ProfileSwitch $it") }
|
||||||
|
@ -170,12 +169,13 @@ class ProfileFunctionImplementation @Inject constructor(
|
||||||
}, {
|
}, {
|
||||||
aapsLogger.error(LTag.DATABASE, "Error while saving ProfileSwitch", it)
|
aapsLogger.error(LTag.DATABASE, "Error while saving ProfileSwitch", it)
|
||||||
})
|
})
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int): Boolean {
|
override fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int): Boolean {
|
||||||
val profile = repository.getPermanentProfileSwitch(dateUtil.now()) ?: return false
|
val profile = repository.getPermanentProfileSwitch(dateUtil.now()) ?: return false
|
||||||
val profileStore = activePlugin.activeProfileSource.profile ?: return false
|
val profileStore = activePlugin.activeProfileSource.profile ?: return false
|
||||||
val ps = buildProfileSwitch(profileStore, profile.profileName, durationInMinutes, percentage, 0, dateUtil.now())
|
val ps = buildProfileSwitch(profileStore, profile.profileName, durationInMinutes, percentage, 0, dateUtil.now()) ?: return false
|
||||||
val validity = ProfileSealed.PS(ps).isValid(
|
val validity = ProfileSealed.PS(ps).isValid(
|
||||||
resourceHelper.gs(info.nightscout.androidaps.automation.R.string.careportal_profileswitch),
|
resourceHelper.gs(info.nightscout.androidaps.automation.R.string.careportal_profileswitch),
|
||||||
activePlugin.activePump,
|
activePlugin.activePump,
|
||||||
|
|
|
@ -605,11 +605,16 @@ class SmsCommunicatorPlugin @Inject constructor(
|
||||||
val finalPercentage = percentage
|
val finalPercentage = percentage
|
||||||
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = true, list[pIndex - 1] as String, finalPercentage) {
|
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = true, list[pIndex - 1] as String, finalPercentage) {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
profileFunction.createProfileSwitch(store, list[pIndex - 1] as String, 0, finalPercentage, 0, dateUtil.now())
|
if (profileFunction.createProfileSwitch(store, list[pIndex - 1] as String, 0, finalPercentage, 0, dateUtil.now())) {
|
||||||
val replyText = resourceHelper.gs(R.string.profileswitchcreated)
|
val replyText = resourceHelper.gs(R.string.profileswitchcreated)
|
||||||
sendSMS(Sms(receivedSms.phoneNumber, replyText))
|
sendSMS(Sms(receivedSms.phoneNumber, replyText))
|
||||||
uel.log(Action.PROFILE_SWITCH, Sources.SMS, resourceHelper.gs(R.string.profileswitchcreated),
|
uel.log(
|
||||||
ValueWithUnit.SimpleString(resourceHelper.gsNotLocalised(R.string.profileswitchcreated)))
|
Action.PROFILE_SWITCH, Sources.SMS, resourceHelper.gs(R.string.profileswitchcreated),
|
||||||
|
ValueWithUnit.SimpleString(resourceHelper.gsNotLocalised(R.string.profileswitchcreated))
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.invalidprofile)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,8 +62,8 @@ class ActionProfileSwitch(injector: HasAndroidInjector) : Action(injector) {
|
||||||
uel.log(UserEntry.Action.PROFILE_SWITCH, Sources.Automation, title,
|
uel.log(UserEntry.Action.PROFILE_SWITCH, Sources.Automation, title,
|
||||||
ValueWithUnit.SimpleString(inputProfileName.value),
|
ValueWithUnit.SimpleString(inputProfileName.value),
|
||||||
ValueWithUnit.Percent(100))
|
ValueWithUnit.Percent(100))
|
||||||
profileFunction.createProfileSwitch(profileStore, inputProfileName.value, 0, 100, 0, dateUtil.now())
|
val result = profileFunction.createProfileSwitch(profileStore, inputProfileName.value, 0, 100, 0, dateUtil.now())
|
||||||
callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run()
|
callback.result(PumpEnactResult(injector).success(result).comment(R.string.ok))?.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateDialog(root: LinearLayout) {
|
override fun generateDialog(root: LinearLayout) {
|
||||||
|
|
|
@ -52,10 +52,11 @@ class ActionProfileSwitchPercent(injector: HasAndroidInjector) : Action(injector
|
||||||
ValueWithUnit.Percent(pct.value.toInt()),
|
ValueWithUnit.Percent(pct.value.toInt()),
|
||||||
ValueWithUnit.Minute(duration.value)
|
ValueWithUnit.Minute(duration.value)
|
||||||
)
|
)
|
||||||
|
callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run()
|
||||||
} else {
|
} else {
|
||||||
aapsLogger.error(LTag.AUTOMATION, "Final profile not valid")
|
aapsLogger.error(LTag.AUTOMATION, "Final profile not valid")
|
||||||
|
callback.result(PumpEnactResult(injector).success(false).comment(R.string.ok))?.run()
|
||||||
}
|
}
|
||||||
callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateDialog(root: LinearLayout) {
|
override fun generateDialog(root: LinearLayout) {
|
||||||
|
|
|
@ -55,8 +55,9 @@ interface ProfileFunction {
|
||||||
* @param percentage 100 = no modification
|
* @param percentage 100 = no modification
|
||||||
* @param timeShiftInHours 0 = no modification
|
* @param timeShiftInHours 0 = no modification
|
||||||
* @param timestamp expected time
|
* @param timestamp expected time
|
||||||
|
* @return null if profile cannot be created from profile store
|
||||||
*/
|
*/
|
||||||
fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch
|
fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes:Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new circadian profile switch request based on provided profile
|
* Create a new circadian profile switch request based on provided profile
|
||||||
|
@ -67,8 +68,9 @@ interface ProfileFunction {
|
||||||
* @param percentage 100 = no modification
|
* @param percentage 100 = no modification
|
||||||
* @param timeShiftInHours 0 = no modification
|
* @param timeShiftInHours 0 = no modification
|
||||||
* @param timestamp expected time
|
* @param timestamp expected time
|
||||||
|
* @return true if profile was created from store
|
||||||
*/
|
*/
|
||||||
fun createProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long)
|
fun createProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): Boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new circadian profile switch request based on currently selected profile interface and default profile
|
* Create a new circadian profile switch request based on currently selected profile interface and default profile
|
||||||
|
|
Loading…
Reference in a new issue