fix processing automations

This commit is contained in:
Milos Kozak 2021-11-22 16:26:33 +01:00
parent 8c1b4f7534
commit 95deaf8b98
2 changed files with 44 additions and 46 deletions

View file

@ -574,7 +574,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
OKDialog.showConfirmation( OKDialog.showConfirmation(
context, context,
rh.gs(R.string.run_question, event.title), rh.gs(R.string.run_question, event.title),
{ handler.post { automationPlugin.processEvent(event, true) } } { handler.post { automationPlugin.processEvent(event) } }
) )
} }
binding.buttonsLayout.userButtonsLayout.addView(it) binding.buttonsLayout.userButtonsLayout.addView(it)

View file

@ -186,18 +186,18 @@ class AutomationPlugin @Inject constructor(
@Synchronized @Synchronized
private fun processActions() { private fun processActions() {
var userEventsEnabled = config.APS var commonEventsEnabled = true
if (config.APS) { if (loopPlugin.isSuspended || !(loopPlugin as PluginBase).isEnabled()) {
if (loopPlugin.isSuspended || !(loopPlugin as PluginBase).isEnabled()) { aapsLogger.debug(LTag.AUTOMATION, "Loop deactivated")
aapsLogger.debug(LTag.AUTOMATION, "Loop deactivated") executionLog.add(rh.gs(R.string.loopisdisabled))
executionLog.add(rh.gs(R.string.loopisdisabled)) rxBus.send(EventAutomationUpdateGui())
userEventsEnabled = false commonEventsEnabled = false
} }
val enabled = constraintChecker.isAutomationEnabled() val enabled = constraintChecker.isAutomationEnabled()
if (!enabled.value()) { if (!enabled.value()) {
executionLog.add(enabled.getMostLimitedReasons(aapsLogger)) executionLog.add(enabled.getMostLimitedReasons(aapsLogger))
userEventsEnabled = false rxBus.send(EventAutomationUpdateGui())
} commonEventsEnabled = false
} }
aapsLogger.debug(LTag.AUTOMATION, "processActions") aapsLogger.debug(LTag.AUTOMATION, "processActions")
@ -205,7 +205,7 @@ class AutomationPlugin @Inject constructor(
while (iterator.hasNext()) { while (iterator.hasNext()) {
val event = iterator.next() val event = iterator.next()
if (event.isEnabled && !event.userAction && event.shouldRun()) if (event.isEnabled && !event.userAction && event.shouldRun())
processEvent(event, userEventsEnabled) if (event.systemAction || commonEventsEnabled) processEvent(event)
} }
// we cannot detect connected BT devices // we cannot detect connected BT devices
// so let's collect all connection/disconnections between 2 runs of processActions() // so let's collect all connection/disconnections between 2 runs of processActions()
@ -216,40 +216,38 @@ class AutomationPlugin @Inject constructor(
storeToSP() // save last run time storeToSP() // save last run time
} }
fun processEvent(event: AutomationEvent, userEventsEnabled: Boolean) { fun processEvent(event: AutomationEvent) {
if (event.trigger.shouldRun() && event.getPreconditions().shouldRun()) { if (event.trigger.shouldRun() && event.getPreconditions().shouldRun()) {
if (event.systemAction || userEventsEnabled) { val actions = event.actions
val actions = event.actions for (action in actions) {
for (action in actions) { action.title = event.title
action.title = event.title if (action.isValid())
if (action.isValid()) action.doAction(object : Callback() {
action.doAction(object : Callback() { override fun run() {
override fun run() { val sb = StringBuilder()
val sb = StringBuilder() .append(dateUtil.timeString(dateUtil.now()))
sb.append(dateUtil.timeString(dateUtil.now())) .append(" ")
sb.append(" ") .append(if (result.success) "" else "")
sb.append(if (result.success) "" else "") .append(" <b>")
sb.append(" <b>") .append(event.title)
sb.append(event.title) .append(":</b> ")
sb.append(":</b> ") .append(action.shortDescription())
sb.append(action.shortDescription()) .append(": ")
sb.append(": ") .append(result.comment)
sb.append(result.comment) executionLog.add(sb.toString())
executionLog.add(sb.toString()) aapsLogger.debug(LTag.AUTOMATION, "Executed: $sb")
aapsLogger.debug(LTag.AUTOMATION, "Executed: $sb") rxBus.send(EventAutomationUpdateGui())
rxBus.send(EventAutomationUpdateGui()) }
} })
}) else {
else { executionLog.add("Invalid action: ${action.shortDescription()}")
executionLog.add("Invalid action: ${action.shortDescription()}") aapsLogger.debug(LTag.AUTOMATION, "Invalid action: ${action.shortDescription()}")
aapsLogger.debug(LTag.AUTOMATION, "Invalid action: ${action.shortDescription()}") rxBus.send(EventAutomationUpdateGui())
rxBus.send(EventAutomationUpdateGui())
}
} }
SystemClock.sleep(1100)
event.lastRun = dateUtil.now()
if (event.autoRemove) automationEvents.remove(event)
} }
SystemClock.sleep(1100)
event.lastRun = dateUtil.now()
if (event.autoRemove) automationEvents.remove(event)
} }
} }
@ -306,7 +304,7 @@ class AutomationPlugin @Inject constructor(
val iterator: MutableIterator<AutomationEvent> = automationEvents.iterator() val iterator: MutableIterator<AutomationEvent> = automationEvents.iterator()
while (iterator.hasNext()) { while (iterator.hasNext()) {
val event = iterator.next() val event = iterator.next()
if (event.userAction) list.add(event) if (event.userAction && event.isEnabled) list.add(event)
} }
return list return list
} }