From a8cca7fcea81fa14284f36e1a6939749e6e880cb Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Tue, 30 Jan 2018 23:16:27 +0100 Subject: [PATCH] Remove duplication in ruffyscripter commands. --- .../ruffyscripter/commands/BaseCommand.java | 36 +++++++++++++++++++ .../commands/ReadHistoryCommand.java | 30 +--------------- .../commands/ReadQuickInfoCommand.java | 29 --------------- 3 files changed, 37 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/BaseCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/BaseCommand.java index 623a3bb51f..5cc3e98b23 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/BaseCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/BaseCommand.java @@ -1,11 +1,21 @@ 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.List; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.CommandResult; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.PumpWarningCodes; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.RuffyScripter; +import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus; public abstract class BaseCommand implements Command { // RS will inject itself here @@ -46,4 +56,30 @@ public abstract class BaseCommand implements Command { public CommandResult getResult() { 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; + + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadHistoryCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadHistoryCommand.java index 4e94caf750..a558fbddcd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadHistoryCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadHistoryCommand.java @@ -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.Tdd; -// Note: TBRs are added to history only after they've completed running -// TODO remove duplication public class ReadHistoryCommand extends BaseCommand { 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.verifyMenuIsDisplayed(MenuType.DAILY_DATA); 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) { int record = (int) scripter.getCurrentMenu().getAttribute(MenuAttribute.CURRENT_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); } - 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 public String toString() { return "ReadHistoryCommand{" + diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadQuickInfoCommand.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadQuickInfoCommand.java index 5e84878f70..b1e80c4e61 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadQuickInfoCommand.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ruffyscripter/commands/ReadQuickInfoCommand.java @@ -1,15 +1,9 @@ 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.ArrayList; -import java.util.Date; import java.util.List; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus; @@ -32,29 +26,6 @@ public class ReadQuickInfoCommand extends BaseCommand { 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 public boolean needsRunMode() { return false;