renamed goToMainMenuScreen to gotToMainTypeMenu to make it clevrer that Menu.isMaintype is meant, deactivates getBasal for now

This commit is contained in:
Sandra Keßler 2017-08-12 14:15:04 +02:00
parent d89b914bc2
commit 389eae93c5
3 changed files with 85 additions and 152 deletions

View file

@ -452,7 +452,7 @@ public class RuffyScripter {
return true; return true;
} }
public boolean goToMainMenuScreen(MenuType screen, long timeout) public boolean goToMainTypeScreen(MenuType screen, long timeout)
{ {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while((currentMenu == null || currentMenu.getType()!=screen) && start+timeout>System.currentTimeMillis()) while((currentMenu == null || currentMenu.getType()!=screen) && start+timeout>System.currentTimeMillis())

View file

@ -2,30 +2,17 @@ package de.jotomo.ruffyscripter.commands;
import android.util.Log; import android.util.Log;
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.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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; 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.GetBasalCommand.State.BEFORE;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.MAIN;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_1_MENU;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.BASAL_OVERVIEW;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.READ_BASAL;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.ERROR;
import static de.jotomo.ruffyscripter.commands.GetBasalCommand.State.AFTER;
public class GetBasalCommand implements Command { public class GetBasalCommand implements Command {
private static final Logger log = LoggerFactory.getLogger(GetBasalCommand.class); private static final Logger log = LoggerFactory.getLogger(GetBasalCommand.class);
@ -40,148 +27,94 @@ public class GetBasalCommand implements Command {
return violations; return violations;
} }
enum State { // private void tick()
BEFORE, // {
MAIN, // switch (state)
BASAL_1_MENU, // {
BASAL_OVERVIEW, // case BEFORE:
READ_BASAL, // if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU)
ERROR, // {
AFTER, // updateState(MAIN,120);
}; // lastMenu = MenuType.MAIN_MENU;
private State lastState,state; // log.debug("found MAIN_MENU -> state:MAIN");
private long last; // retries=3;
private long timeout; // }
private Thread timeoutThread = new Thread() // break;
{ // case MAIN:
@Override // if(retries>0)
public void run() { // if(scripter.goToMainTypeScreen(MenuType.BASAL_1_MENU,30000))
while(state != ERROR && state!=AFTER) { // {
if (timeout + last < System.currentTimeMillis()) { // if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000))
lastState = state; // {
state = ERROR; // updateState(BASAL_OVERVIEW, 30);
log.debug("timeout reached -> state:ERROR"); // retries=0;
} // }
tick(); // }
try { // else
Thread.sleep(100); // retries--;
} catch (InterruptedException e) { // else
e.printStackTrace(); // updateState(ERROR,30);
} // break;
} // case BASAL_OVERVIEW:
tick(); // if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1)
} // {
}; // basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL);
private void updateState(State newState,long timeoutSec) // if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000))
{ // {
lastState = state; // updateState(READ_BASAL,30);
state = newState; // retries = 96;
last = System.currentTimeMillis(); // }
timeout = timeoutSec*1000; // }
} // break;
private MenuType lastMenu; // case READ_BASAL:
private int retries = 0; // if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) {
private double basalTotal = 0; // Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
private Map<Integer,Double> rate = new HashMap<>(); // MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START);
// if (rateObj instanceof Double) {
private void tick() // rate.put(time.getHour(), (Double) rateObj);
{ // }
switch (state) // boolean complete = true;
{ // for (int t = 0; t < 24; t++) {
case BEFORE: // if (rate.get(t) == null)
if(scripter.currentMenu!=null && scripter.currentMenu.getType()==MenuType.MAIN_MENU) // complete = false;
{ // }
updateState(MAIN,120); // if (retries > 0) {
lastMenu = MenuType.MAIN_MENU; // if (complete) {
log.debug("found MAIN_MENU -> state:MAIN"); // scripter.pressBackKey();
retries=3; // updateState(AFTER, 30);
} // } else {
break; // retries--;
case MAIN: // scripter.pressMenuKey();
if(retries>0) // scripter.waitScreen(250);
if(scripter.goToMainMenuScreen(MenuType.BASAL_1_MENU,30000)) // }
{ // } else {
if(scripter.enterMenu(MenuType.BASAL_1_MENU,MenuType.BASAL_TOTAL, RuffyScripter.Key.CHECK,2000)) // updateState(ERROR, 30);
{ // }
updateState(BASAL_OVERVIEW, 30); // }
retries=0; // break;
} // case ERROR:
} // case AFTER:
else // scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000);
retries--; // synchronized(GetBasalCommand.this) {
else // GetBasalCommand.this.notify();
updateState(ERROR,30); // }
break; // break;
case BASAL_OVERVIEW: // }
if(scripter.currentMenu.getType()==MenuType.BASAL_TOTAL && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL)!=null && (Integer)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_SELECTED)==1) // }
{
basalTotal = (Double)scripter.currentMenu.getAttribute(MenuAttribute.BASAL_TOTAL);
if(scripter.enterMenu(MenuType.BASAL_TOTAL,MenuType.BASAL_SET, RuffyScripter.Key.MENU,3000))
{
updateState(READ_BASAL,30);
retries = 96;
}
}
break;
case READ_BASAL:
if(scripter.currentMenu.getType()==MenuType.BASAL_SET && scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START)!=null) {
Object rateObj = scripter.currentMenu.getAttribute(MenuAttribute.BASAL_RATE);
MenuTime time = (MenuTime) scripter.currentMenu.getAttribute(MenuAttribute.BASAL_START);
if (rateObj instanceof Double) {
rate.put(time.getHour(), (Double) rateObj);
}
boolean complete = true;
for (int t = 0; t < 24; t++) {
if (rate.get(t) == null)
complete = false;
}
if (retries > 0) {
if (complete) {
scripter.pressBackKey();
updateState(AFTER, 30);
} else {
retries--;
scripter.pressMenuKey();
scripter.waitScreen(250);
}
} else {
updateState(ERROR, 30);
}
}
break;
case ERROR:
case AFTER:
scripter.goToMainMenuScreen(MenuType.MAIN_MENU,2000);
synchronized(GetBasalCommand.this) {
GetBasalCommand.this.notify();
}
break;
}
}
@Override @Override
public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) { public CommandResult execute(RuffyScripter scripter, PumpState initialPumpState) {
state = BEFORE; try {
this.scripter = scripter; Map<Integer,Double> rate = new HashMap<>();
updateState(BEFORE,120);
timeoutThread.start();
try { for(int i = 0; i < 24;i++)
synchronized (this) {
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
return new CommandResult().success(false).message("failed to wait: "+e.getMessage());
}
if(state==AFTER)
{ {
for(int i = 0; i < 24;i++) Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i));
{
Log.v("BASAL_RATE","BASAL_RATE from "+String.format("%02d",i)+":00 = "+rate.get(i));
}
return new CommandResult().success(true).enacted(true).message("Basal Rate was read");
} }
return new CommandResult().success(false).message("failed with state: "+state+" from: "+lastState); } catch (Exception e) {
log.error("failed to get basal",e);
return new CommandResult().success(false).message("failed to get basal: "+e.getMessage());
}
return new CommandResult().success(true).enacted(true).message("Basal Rate was read");
} }
@Override @Override

View file

@ -62,7 +62,7 @@ public class SetTbrCommand implements Command {
try { try {
log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU"); log.debug("1. going from "+scripter.currentMenu+" to TBR_MENU");
int retries = 5; int retries = 5;
while(!scripter.goToMainMenuScreen(TBR_MENU,3000)) while(!scripter.goToMainTypeScreen(TBR_MENU,3000))
{ {
retries--; retries--;
if(retries==0) if(retries==0)