Move command argument checking out of ctor into a dedicated method and check prior to execution.

This commit is contained in:
Johannes Mockenhaupt 2017-07-17 10:27:39 +02:00
parent 6faa1614ac
commit 44ae79bd50
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
6 changed files with 59 additions and 9 deletions

View file

@ -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<String> violations = cmd.validateArguments();
if (!violations.isEmpty()) {
return new CommandResult().message(Joiner.on("\n").join(violations));
}
synchronized (RuffyScripter.class) {
try {
final RuffyScripter scripter = this;

View file

@ -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<String> validateArguments() {
List<String> 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));

View file

@ -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<String> validateArguments() {
return Collections.emptyList();
}
@Override
public CommandResult execute(RuffyScripter scripter) {
try {

View file

@ -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<String> validateArguments();
}

View file

@ -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<String> validateArguments() {
return Collections.emptyList();
}
@Override
public String toString() {
return "ReadPumpStateCommand{}";

View file

@ -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<String> validateArguments() {
List<String> 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) {