From 44ae79bd50ea11d8f0f4dc013c3b81c3f4820a31 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Mon, 17 Jul 2017 10:27:39 +0200 Subject: [PATCH] Move command argument checking out of ctor into a dedicated method and check prior to execution. --- .../jotomo/ruffyscripter/RuffyScripter.java | 10 +++++++++ .../ruffyscripter/commands/BolusCommand.java | 17 ++++++++++++-- .../commands/CancelTbrCommand.java | 8 +++++++ .../ruffyscripter/commands/Command.java | 3 +++ .../commands/ReadPumpStateCommand.java | 8 +++++++ .../ruffyscripter/commands/SetTbrCommand.java | 22 +++++++++++++------ 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 2522241875..e2c9025f02 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -4,6 +4,8 @@ import android.os.DeadObjectException; import android.os.RemoteException; import android.os.SystemClock; +import com.google.common.base.Joiner; + import org.monkey.d.ruffy.ruffy.driver.IRTHandler; import org.monkey.d.ruffy.ruffy.driver.IRuffyService; import org.monkey.d.ruffy.ruffy.driver.display.Menu; @@ -13,6 +15,8 @@ import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import de.jotomo.ruffyscripter.commands.Command; import de.jotomo.ruffyscripter.commands.CommandException; import de.jotomo.ruffyscripter.commands.CommandResult; @@ -156,6 +160,12 @@ public class RuffyScripter { if (unrecoverableError != null) { return new CommandResult().success(false).enacted(false).message(unrecoverableError); } + + List violations = cmd.validateArguments(); + if (!violations.isEmpty()) { + return new CommandResult().message(Joiner.on("\n").join(violations)); + } + synchronized (RuffyScripter.class) { try { final RuffyScripter scripter = this; diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java index fdcd0e404a..494595077e 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/BolusCommand.java @@ -7,6 +7,8 @@ import org.monkey.d.ruffy.ruffy.driver.display.MenuType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import de.jotomo.ruffyscripter.RuffyScripter; @@ -20,6 +22,17 @@ public class BolusCommand implements Command { this.bolus = bolus; } + @Override + public List validateArguments() { + List violations = new ArrayList<>(); + + if (bolus > 0 && bolus < 25) { + violations.add("Requested bolus " + bolus + " out of limits (0-25)"); + } + + return violations; + } + @Override public CommandResult execute(RuffyScripter scripter) { try { @@ -37,7 +50,7 @@ public class BolusCommand implements Command { scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU, "Pump did not return to MAIN_MEU from BOLUS_ENTER to deliver bolus. " - + "Check pump manually, the bolus might not have been delivered."); + + "Check pump manually, the bolus might not have been delivered."); // wait for bolus delivery to complete Double bolusRemaining = (Double) scripter.currentMenu.getAttribute(MenuAttribute.BOLUS_REMAINING); @@ -53,7 +66,7 @@ public class BolusCommand implements Command { // make sure no alert (occlusion, cartridge empty) has occurred. scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU, "Bolus delivery did not complete as expected. " - + "Check pump manually, the bolus might not have been delivered."); + + "Check pump manually, the bolus might not have been delivered."); return new CommandResult().success(true).enacted(true) .message(String.format(Locale.US, "Delivered %02.1f U", bolus)); diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/CancelTbrCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/CancelTbrCommand.java index 043a2de388..27110c7893 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/CancelTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/CancelTbrCommand.java @@ -5,6 +5,9 @@ import org.monkey.d.ruffy.ruffy.driver.display.MenuType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; +import java.util.List; + import de.jotomo.ruffyscripter.RuffyScripter; // TODO robustness: can a TBR run out, whilst we're trying to cancel it? @@ -13,6 +16,11 @@ import de.jotomo.ruffyscripter.RuffyScripter; public class CancelTbrCommand implements Command { private static final Logger log = LoggerFactory.getLogger(CancelTbrCommand.class); + @Override + public List validateArguments() { + return Collections.emptyList(); + } + @Override public CommandResult execute(RuffyScripter scripter) { try { diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/Command.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/Command.java index 4763d5d521..0061060814 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/Command.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/Command.java @@ -1,7 +1,10 @@ package de.jotomo.ruffyscripter.commands; +import java.util.List; + import de.jotomo.ruffyscripter.RuffyScripter; public interface Command { CommandResult execute(RuffyScripter ruffyScripter); + List validateArguments(); } diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/ReadPumpStateCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/ReadPumpStateCommand.java index abbadd0d83..ddac5c65d3 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/ReadPumpStateCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/ReadPumpStateCommand.java @@ -1,5 +1,8 @@ package de.jotomo.ruffyscripter.commands; +import java.util.Collections; +import java.util.List; + import de.jotomo.ruffyscripter.RuffyScripter; public class ReadPumpStateCommand implements Command { @@ -8,6 +11,11 @@ public class ReadPumpStateCommand implements Command { return new CommandResult().success(true).enacted(false).message("Returning pump state only"); } + @Override + public List validateArguments() { + return Collections.emptyList(); + } + @Override public String toString() { return "ReadPumpStateCommand{}"; diff --git a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java index d298fca334..90d118fe83 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/commands/SetTbrCommand.java @@ -8,8 +8,9 @@ import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; -import java.util.Objects; import de.jotomo.ruffyscripter.RuffyScripter; @@ -22,26 +23,33 @@ public class SetTbrCommand implements Command { public SetTbrCommand(long percentage, long duration) { this.percentage = percentage; this.duration = duration; + } + + @Override + public List validateArguments() { + List violations = new ArrayList<>(); if (percentage % 10 != 0) { - throw new IllegalArgumentException("TBR percentage must be set in 10% steps"); + violations.add("TBR percentage must be set in 10% steps"); } if (percentage < 0 || percentage > 500) { - throw new IllegalArgumentException("TBR percentage must be within 0-500%"); + violations.add("TBR percentage must be within 0-500%"); } if (percentage != 100) { if (duration % 15 != 0) { - throw new IllegalArgumentException("TBR duration can only be set in 15 minute steps"); + violations.add("TBR duration can only be set in 15 minute steps"); } if (duration > 60 * 24) { - throw new IllegalArgumentException("Maximum TBR duration is 24 hours"); + violations.add("Maximum TBR duration is 24 hours"); } } if (percentage == 0 && duration > 120) { - throw new IllegalArgumentException("Max allowed zero-temp duration is 2h"); + violations.add("Max allowed zero-temp duration is 2h"); } + + return violations; } @Override @@ -72,7 +80,7 @@ public class SetTbrCommand implements Command { scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU, "Pump did not return to MAIN_MEU after setting TBR. " + - "Check pump manually, the TBR might not have been set/cancelled."); + "Check pump manually, the TBR might not have been set/cancelled."); // check main menu shows the same values we just set if (percentage == 100) {