Move command argument checking out of ctor into a dedicated method and check prior to execution.
This commit is contained in:
parent
6faa1614ac
commit
44ae79bd50
|
@ -4,6 +4,8 @@ import android.os.DeadObjectException;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.SystemClock;
|
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.IRTHandler;
|
||||||
import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
|
import org.monkey.d.ruffy.ruffy.driver.IRuffyService;
|
||||||
import org.monkey.d.ruffy.ruffy.driver.display.Menu;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.commands.Command;
|
import de.jotomo.ruffyscripter.commands.Command;
|
||||||
import de.jotomo.ruffyscripter.commands.CommandException;
|
import de.jotomo.ruffyscripter.commands.CommandException;
|
||||||
import de.jotomo.ruffyscripter.commands.CommandResult;
|
import de.jotomo.ruffyscripter.commands.CommandResult;
|
||||||
|
@ -156,6 +160,12 @@ public class RuffyScripter {
|
||||||
if (unrecoverableError != null) {
|
if (unrecoverableError != null) {
|
||||||
return new CommandResult().success(false).enacted(false).message(unrecoverableError);
|
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) {
|
synchronized (RuffyScripter.class) {
|
||||||
try {
|
try {
|
||||||
final RuffyScripter scripter = this;
|
final RuffyScripter scripter = this;
|
||||||
|
|
|
@ -7,6 +7,8 @@ import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
@ -20,6 +22,17 @@ public class BolusCommand implements Command {
|
||||||
this.bolus = bolus;
|
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
|
@Override
|
||||||
public CommandResult execute(RuffyScripter scripter) {
|
public CommandResult execute(RuffyScripter scripter) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -5,6 +5,9 @@ import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
|
||||||
// TODO robustness: can a TBR run out, whilst we're trying to cancel it?
|
// 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 {
|
public class CancelTbrCommand implements Command {
|
||||||
private static final Logger log = LoggerFactory.getLogger(CancelTbrCommand.class);
|
private static final Logger log = LoggerFactory.getLogger(CancelTbrCommand.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> validateArguments() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandResult execute(RuffyScripter scripter) {
|
public CommandResult execute(RuffyScripter scripter) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package de.jotomo.ruffyscripter.commands;
|
package de.jotomo.ruffyscripter.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
|
||||||
public interface Command {
|
public interface Command {
|
||||||
CommandResult execute(RuffyScripter ruffyScripter);
|
CommandResult execute(RuffyScripter ruffyScripter);
|
||||||
|
List<String> validateArguments();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package de.jotomo.ruffyscripter.commands;
|
package de.jotomo.ruffyscripter.commands;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
|
||||||
public class ReadPumpStateCommand implements Command {
|
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");
|
return new CommandResult().success(true).enacted(false).message("Returning pump state only");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> validateArguments() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ReadPumpStateCommand{}";
|
return "ReadPumpStateCommand{}";
|
||||||
|
|
|
@ -8,8 +8,9 @@ import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import de.jotomo.ruffyscripter.RuffyScripter;
|
import de.jotomo.ruffyscripter.RuffyScripter;
|
||||||
|
|
||||||
|
@ -22,26 +23,33 @@ public class SetTbrCommand implements Command {
|
||||||
public SetTbrCommand(long percentage, long duration) {
|
public SetTbrCommand(long percentage, long duration) {
|
||||||
this.percentage = percentage;
|
this.percentage = percentage;
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> validateArguments() {
|
||||||
|
List<String> violations = new ArrayList<>();
|
||||||
|
|
||||||
if (percentage % 10 != 0) {
|
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) {
|
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 (percentage != 100) {
|
||||||
if (duration % 15 != 0) {
|
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) {
|
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) {
|
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
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue