Remove duplication in ruffyscripter commands.

This commit is contained in:
Johannes Mockenhaupt 2018-01-30 23:16:27 +01:00
parent 1e22599979
commit a8cca7fcea
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 37 additions and 58 deletions

View file

@ -1,11 +1,21 @@
package info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.commands; package info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.commands;
import android.support.annotation.NonNull;
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.BolusType;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuDate;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.CommandResult; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.CommandResult;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.PumpWarningCodes; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.PumpWarningCodes;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.RuffyScripter; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.RuffyScripter;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus;
public abstract class BaseCommand implements Command { public abstract class BaseCommand implements Command {
// RS will inject itself here // RS will inject itself here
@ -46,4 +56,30 @@ public abstract class BaseCommand implements Command {
public CommandResult getResult() { public CommandResult getResult() {
return result; return result;
} }
@NonNull
protected Bolus readBolusRecord() {
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_DATA);
BolusType bolusType = (BolusType) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_TYPE);
boolean isValid = bolusType == BolusType.NORMAL;
Double bolus = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS);
long recordDate = readRecordDate();
return new Bolus(recordDate, bolus, isValid);
}
protected long readRecordDate() {
MenuDate date = (MenuDate) scripter.getCurrentMenu().getAttribute(MenuAttribute.DATE);
MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME);
int year = Calendar.getInstance().get(Calendar.YEAR);
if (date.getMonth() > Calendar.getInstance().get(Calendar.MONTH) + 1) {
year -= 1;
}
Calendar calendar = Calendar.getInstance();
calendar.set(year, date.getMonth() - 1, date.getDay(), time.getHour(), time.getMinute(), 0);
// round to second
return calendar.getTimeInMillis() - calendar.getTimeInMillis() % 1000;
}
} }

View file

@ -20,8 +20,6 @@ import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.PumpHi
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Tbr; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Tbr;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Tdd; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Tdd;
// Note: TBRs are added to history only after they've completed running
// TODO remove duplication
public class ReadHistoryCommand extends BaseCommand { public class ReadHistoryCommand extends BaseCommand {
private static Logger log = LoggerFactory.getLogger(ReadHistoryCommand.class); private static Logger log = LoggerFactory.getLogger(ReadHistoryCommand.class);
@ -78,7 +76,7 @@ public class ReadHistoryCommand extends BaseCommand {
} }
} }
// tdd history // tdd history (TBRs are added to history only after they've completed running)
scripter.pressMenuKey(); scripter.pressMenuKey();
scripter.verifyMenuIsDisplayed(MenuType.DAILY_DATA); scripter.verifyMenuIsDisplayed(MenuType.DAILY_DATA);
if (request.tddHistory != PumpHistoryRequest.SKIP) { if (request.tddHistory != PumpHistoryRequest.SKIP) {
@ -234,16 +232,6 @@ public class ReadHistoryCommand extends BaseCommand {
} }
} }
@NonNull
private Bolus readBolusRecord() {
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_DATA);
BolusType bolusType = (BolusType) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_TYPE);
boolean isValid = bolusType == BolusType.NORMAL;
Double bolus = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS);
long recordDate = readRecordDate();
return new Bolus(recordDate, bolus, isValid);
}
private void readAlertRecords(long requestedTime) { private void readAlertRecords(long requestedTime) {
int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD); int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_RECORD);
int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD); int totalRecords = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.TOTAL_RECORD);
@ -277,22 +265,6 @@ public class ReadHistoryCommand extends BaseCommand {
return new PumpAlert(recordDate, warningCode, errorCode, message); return new PumpAlert(recordDate, warningCode, errorCode, message);
} }
private long readRecordDate() {
MenuDate date = (MenuDate) scripter.getCurrentMenu().getAttribute(MenuAttribute.DATE);
MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME);
int year = Calendar.getInstance().get(Calendar.YEAR);
if (date.getMonth() > Calendar.getInstance().get(Calendar.MONTH) + 1) {
year -= 1;
}
Calendar calendar = Calendar.getInstance();
calendar.set(year, date.getMonth() - 1, date.getDay(), time.getHour(), time.getMinute(), 0);
// round to second
return calendar.getTimeInMillis() - calendar.getTimeInMillis() % 1000;
}
@Override @Override
public String toString() { public String toString() {
return "ReadHistoryCommand{" + return "ReadHistoryCommand{" +

View file

@ -1,15 +1,9 @@
package info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.commands; package info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.commands;
import android.support.annotation.NonNull;
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.BolusType;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuDate;
import org.monkey.d.ruffy.ruffy.driver.display.menu.MenuTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus;
@ -32,29 +26,6 @@ public class ReadQuickInfoCommand extends BaseCommand {
result.success = true; result.success = true;
} }
// TODO deduplicate -> ReadHistoryCommand
@NonNull
private Bolus readBolusRecord() {
scripter.verifyMenuIsDisplayed(MenuType.BOLUS_DATA);
BolusType bolusType = (BolusType) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS_TYPE);
boolean isValid = bolusType == BolusType.NORMAL;
Double bolus = (Double) scripter.getCurrentMenu().getAttribute(MenuAttribute.BOLUS);
long recordDate = readRecordDate();
return new Bolus(recordDate, bolus, isValid);
}
private long readRecordDate() {
MenuDate date = (MenuDate) scripter.getCurrentMenu().getAttribute(MenuAttribute.DATE);
MenuTime time = (MenuTime) scripter.getCurrentMenu().getAttribute(MenuAttribute.TIME);
int currentMonth = new Date().getMonth() + 1;
int currentYear = new Date().getYear() + 1900;
if (currentMonth == 1 && date.getMonth() == 12) {
currentYear -= 1;
}
return new Date(currentYear - 1900, date.getMonth() - 1, date.getDay(), time.getHour(), time.getMinute()).getTime();
}
@Override @Override
public boolean needsRunMode() { public boolean needsRunMode() {
return false; return false;