missing stuf :/

This commit is contained in:
Sandra Keßler 2017-08-04 17:58:28 +02:00
parent f790ce6f20
commit 37a3270a16

View file

@ -1,5 +1,7 @@
package de.jotomo.ruffyscripter.commands; package de.jotomo.ruffyscripter.commands;
import android.util.Log;
import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute; import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute;
import org.monkey.d.ruffy.ruffy.driver.display.MenuType; import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime; import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
@ -7,71 +9,45 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import de.jotomo.ruffyscripter.PumpState; import de.jotomo.ruffyscripter.PumpState;
import de.jotomo.ruffyscripter.RuffyScripter; import de.jotomo.ruffyscripter.RuffyScripter;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.AFTER; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BEFORE;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.BEFORE; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.MAIN;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.ERROR; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_1_MENU;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.MAIN; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_OVERVIEW;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.READ_BASAL;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TBR; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.ERROR;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.SET_TIME; import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.AFTER;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TBR;
import static de.jotomo.ruffyscripter.commands.SetTbrCommand.State.TIME;
public class SetTbrCommand implements Command { public class GetBasalCommand implements Command {
private static final Logger log = LoggerFactory.getLogger(SetTbrCommand.class); private static final Logger log = LoggerFactory.getLogger(GetBasalCommand.class);
private final long percentage;
private final long duration;
private RuffyScripter scripter; private RuffyScripter scripter;
public SetTbrCommand(long percentage, long duration) { public GetBasalCommand() {}
this.percentage = percentage;
this.duration = duration;
}
@Override @Override
public List<String> validateArguments() { public List<String> validateArguments() {
List<String> violations = new ArrayList<>(); List<String> violations = new ArrayList<>();
if (percentage % 10 != 0) {
violations.add("TBR percentage must be set in 10% steps");
}
if (percentage < 0 || percentage > 500) {
violations.add("TBR percentage must be within 0-500%");
}
if (percentage != 100) {
if (duration % 15 != 0) {
violations.add("TBR duration can only be set in 15 minute steps");
}
if (duration > 60 * 24) {
violations.add("Maximum TBR duration is 24 hours");
}
}
if (percentage == 0 && duration > 120) {
violations.add("Max allowed zero-temp duration is 2h");
}
return violations; return violations;
} }
enum State { enum State {
BEFORE, BEFORE,
MAIN, MAIN,
TBR, BASAL_1_MENU,
SET_TBR, BASAL_OVERVIEW,
TIME, READ_BASAL,
SET_TIME, ERROR,
SET,
AFTER, AFTER,
ERROR
}; };
private State lastState,state; private State lastState,state;
private long last; private long last;
@ -105,6 +81,9 @@ public class SetTbrCommand implements Command {
} }
private MenuType lastMenu; private MenuType lastMenu;
private int retries = 0; private int retries = 0;
private double basalTotal = 0;
private Map<Integer,Double> rate = new HashMap<>();
private void tick() private void tick()
{ {
switch (state) switch (state)
@ -120,147 +99,61 @@ public class SetTbrCommand implements Command {
break; break;
case MAIN: case MAIN:
if(retries>0) if(retries>0)
if(scripter.goToMainMenuScreen(MenuType.TBR_MENU,30000)) if(scripter.goToMainMenuScreen(MenuType.BASAL_1_MENU,30000))
{ {
updateState(TBR,30); if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000));
retries=0; {
updateState(BASAL_OVERVIEW, 30);
retries=0;
}
} }
else else
retries--; retries--;
else else
updateState(ERROR,30); updateState(ERROR,30);
break; break;
case TBR: case BASAL_OVERVIEW:
if(scripter.enterMenu(MenuType.TBR_MENU,MenuType.TBR_SET, RuffyScripter.Key.CHECK,20000)) if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1)
{ {
updateState(SET_TBR,60); basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL);
retries = 10; if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000))
}
else
updateState(ERROR,30);
break;
case SET_TBR:
if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_SET)
{
Object percentageObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
if(percentageObj != null && percentageObj instanceof Double)
{ {
double currentPercentage = ((Double) percentageObj).doubleValue(); updateState(READ_BASAL,30);
retries = 96;
if(currentPercentage!=percentage)
{
if(retries>0) {
retries--;
int steps = (int) ((percentage - currentPercentage) / 10.0);
scripter.step(steps,(steps<0? RuffyScripter.Key.DOWN: RuffyScripter.Key.UP), 3000);
}
else
updateState(ERROR,30);
}
else
{
if(percentage==100)
{
scripter.pressCheckKey();
updateState(SET,30);
}
else {
updateState(TIME, 30);
}
retries=10;
}
} }
} }
else
{
updateState(ERROR,30);
}
break; break;
case TIME: case READ_BASAL:
if((scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION) || scripter.enterMenu(MenuType.TBR_SET,MenuType.TBR_DURATION, RuffyScripter.Key.MENU,20000)) if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) {
{ Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
updateState(SET_TIME,60); MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START);
retries = 10; if (rateObj instanceof Double) {
} rate.put(time.getHour(), (Double) rateObj);
else if(retries==0)
updateState(ERROR,30);
else retries--;
break;
case SET_TIME:
if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.TBR_DURATION)
{
Object durationObj = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
if(durationObj != null && durationObj instanceof MenuTime)
{
MenuTime time = (MenuTime) durationObj;
double currentDuration = (time.getHour()*60)+time.getMinute();
if(currentDuration!=duration)
{
if(retries>0) {
retries--;
int steps = (int)((currentDuration - duration)/15.0);
if(currentDuration+(steps*15)<duration)
steps++;
else if(currentDuration+(steps*15)>duration)
steps--;
scripter.step(steps,(steps>0? RuffyScripter.Key.UP: RuffyScripter.Key.DOWN), 3000);
}
else
updateState(ERROR,30);
}
else {
scripter.pressCheckKey();
updateState(SET, 30);
}
} }
} boolean complete = true;
else for (int t = 0; t < 24; t++) {
{ if (rate.get(t) == null)
updateState(ERROR,60); complete = false;
}
break;
case SET:
if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.WARNING_OR_ERROR)
{
lastMenu = scripter.currentMenu.getType();
scripter.pressCheckKey();
updateState(SET, 30);
}
else if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) {
Object setPercentage = scripter.currentMenu.getAttribute(MenuAttribute.TBR);
Object setDuration = scripter.currentMenu.getAttribute(MenuAttribute.RUNTIME);
if (setPercentage== null ||setDuration==null) {
if(percentage!=100)
{
updateState(ERROR,10);
}
else
{
if(lastMenu==MenuType.WARNING_OR_ERROR)
updateState(AFTER,10);
else
updateState(SET,10);
}
} }
else { if (retries > 0) {
double mmTbrPercentage = (Double) setPercentage; if (complete) {
MenuTime mmTbrDuration = (MenuTime) setDuration; scripter.pressBackKey();
// ... and be the same as what we set updateState(AFTER, 30);
// note that displayed duration might have already counted down, e.g. from 30 minutes to
// 29 minutes and 59 seconds, so that 29 minutes are displayed
int mmTbrDurationInMinutes = mmTbrDuration.getHour() * 60 + mmTbrDuration.getMinute();
if (mmTbrPercentage == percentage && mmTbrDurationInMinutes <= duration) {
updateState(AFTER, 10);
} else { } else {
updateState(ERROR, 10); retries--;
scripter.pressMenuKey();
scripter.waitScreen(250);
} }
} else {
updateState(ERROR, 30);
} }
} }
break; break;
case ERROR: case ERROR:
case AFTER: case AFTER:
synchronized(SetTbrCommand.this) { scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000);
SetTbrCommand.this.notify(); synchronized(GetBasalCommand.this) {
GetBasalCommand.this.notify();
} }
break; break;
} }
@ -282,20 +175,17 @@ public class SetTbrCommand implements Command {
} }
if(state==AFTER) if(state==AFTER)
{ {
if(percentage==100) for(int i = 0; i < 24;i++)
return new CommandResult().success(true).enacted(true).message("TBR was cancelled"); {
Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i));
return new CommandResult().success(true).enacted(true).message( }
String.format(Locale.US, "TBR set to %d%% for %d min", percentage, duration)); return new CommandResult().success(true).enacted(true).message("Basal Rate was read");
} }
return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState);
} }
@Override @Override
public String toString() { public String toString() {
return "SetTbrCommand{" + return "GetTbrCommand{}";
"percentage=" + percentage +
", duration=" + duration +
'}';
} }
} }