SetBasalProfileCommand, calculating steps: revert to Adrian's simpler version, add clarifying comments.

This commit is contained in:
Johannes Mockenhaupt 2017-11-24 21:03:49 +01:00
parent 9698863f90
commit 2d7a8a2e4d
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 10 additions and 87 deletions

View file

@ -76,7 +76,8 @@ public class SetBasalProfileCommand extends BaseCommand {
private long inputBasalRate(double requestedRate) {
double currentRate = scripter.readBlinkingValue(Double.class, MenuAttribute.BASAL_RATE);
log.debug("Current rate: " + currentRate + ", requested: " + requestedRate);
long steps = calculateRequiredSteps(currentRate, requestedRate);
// the pump changes steps size from 0.01 to 0.05 when crossing 1.00 U
long steps = stepsToOne(currentRate) - stepsToOne(requestedRate);
if (steps == 0) {
return 0;
}
@ -91,30 +92,14 @@ public class SetBasalProfileCommand extends BaseCommand {
return steps;
}
long calculateRequiredSteps(double currentRate, double requestedRate) {
long steps;
if (currentRate < 1 && requestedRate > 1) {
// going from below 1 to above 1, need both granularities
// calculate steps 0.x -> 1.0, calculate 1.0 -> 1+
long smallSteps = Math.round((1 - currentRate) / 0.01);
long bigSteps = Math.round((requestedRate - 1) / 0.05);
steps = smallSteps + bigSteps;
} else if (currentRate > 1 && requestedRate < 1) {
// going from above 1 to below 1, need both granularities
// calculate +1 -> 1.0, calculate 1.0 -> 0.x
long bigSteps = Math.round((currentRate - 1) / 0.05);
long smallSteps = Math.round((1 - requestedRate) / 0.01);
steps = (bigSteps + smallSteps) * -1;
} else if (currentRate < 1 && requestedRate <= 1) {
// staying below 1, finer granularity only
steps = Math.round((requestedRate - currentRate) / 0.01);
} else if (currentRate >= 1 && requestedRate >= 1) {
// staying above 1, coarser granularity only
steps = Math.round((requestedRate - currentRate) / 0.05);
} else {
throw new CommandException("Programmer doesn't know what he's doing");
}
return steps;
/**
* Steps required to go up to 1.0 (positive return value),
* or down to 1.0 (negative return value).
*/
private long stepsToOne(double rate) {
double change = (1.0 - rate);
if (rate > 1) return Math.round(change / 0.05);
return Math.round(change / 0.01);
}
private void verifyDisplayedRate(double requestedRate, long change) {

View file

@ -1,62 +0,0 @@
package de.jotomo.ruffyscripter.commands;
import org.junit.Test;
import de.jotomo.ruffy.spi.BasalProfile;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
public class SetBasalProfileCommandTest {
private SetBasalProfileCommand setBasalProfileCommand = new SetBasalProfileCommand(new BasalProfile());
@Test
public void belowOneToAboveOne() {
assertThat(
// 0.85 -> 1.00 = 15 + 1.00 -> 1.25 = 5 == 20
setBasalProfileCommand.calculateRequiredSteps(0.85, 1.25),
is(equalTo(20L)));
}
@Test
public void aboveOneToBelowOne() {
assertThat(
// 2.85 -> 1.00 = 37 + 1.00 -> 0.25 = 75 == -112
setBasalProfileCommand.calculateRequiredSteps(2.85, 0.25),
is(equalTo(-112L)));
}
@Test
public void belowOneToBelowOne() {
assertThat(
setBasalProfileCommand.calculateRequiredSteps(0.85, 0.25),
is(equalTo(-60L)));
}
@Test
public void aboveOneToAboveOne() {
assertThat(
setBasalProfileCommand.calculateRequiredSteps(2.85, 3.25),
is(equalTo(8L)));
}
@Test
public void greaterOrEqualIssuesAroundOne() {
assertThat(
setBasalProfileCommand.calculateRequiredSteps(0.99, 1.00),
is(equalTo(1L)));
assertThat(
setBasalProfileCommand.calculateRequiredSteps(1.00, 1.05),
is(equalTo(1L)));
assertThat(
setBasalProfileCommand.calculateRequiredSteps(1.10, 1.00),
is(equalTo(-2L)));
assertThat(
setBasalProfileCommand.calculateRequiredSteps(1.00, 1.10),
is(equalTo(2L)));
assertThat(
setBasalProfileCommand.calculateRequiredSteps(1.10, 0.98),
is(equalTo(-4L)));
}
}