Add RuffyCommands.readPumpState, cleanup, fix.

This commit is contained in:
Johannes Mockenhaupt 2017-10-18 18:29:29 +02:00
parent e4901f292f
commit ab4f46471b
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
10 changed files with 53 additions and 28 deletions

View file

@ -16,9 +16,8 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.jotomo.ruffy.spi.PumpState;
import de.jotomo.ruffy.spi.CommandResult;
import de.jotomo.ruffy.spi.PumpState;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI;
@ -111,7 +110,8 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
@Override
public void run() {
ComboPlugin plugin = ComboPlugin.getPlugin();
statusText.setText(plugin.getPump().state.getStateSummary());
if (plugin.getPump().lastCmdResult != null)
statusText.setText(plugin.getPump().state.getStateSummary());
if (plugin.isInitialized()) {
PumpState ps = plugin.getPump().state;
if (ps != null) {

View file

@ -274,8 +274,7 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (notAUserRequest && wasRunAtLeastOnce && ranWithinTheLastMinute) {
log.debug("Not fetching state from pump, since we did already within the last 60 seconds");
} else {
// TODO
// runCommand(new GetPumpStateCommand());
ruffyScripter.readPumpState();
}
}

View file

@ -52,6 +52,7 @@
android:gravity="start"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:text="Initializing"
android:textSize="14sp" />
</LinearLayout>
@ -460,7 +461,7 @@
android:textSize="20sp"
android:paddingLeft="5dp"
android:gravity="start"
android:text=" - " />
android:text="" />
</LinearLayout>

View file

@ -2,9 +2,7 @@ package de.jotomo.ruffy.spi;
import java.util.Date;
/**
* State representing the state of the MAIN_MENU, plus reservoir level (if requested).
*/
/** State displayed on the main screen of the pump. */
public class PumpState {
public Date timestamp = new Date();
public boolean tbrActive = false;

View file

@ -17,6 +17,8 @@ public interface RuffyCommands {
boolean isPumpBusy();
CommandResult readPumpState();
CommandResult readHistory(PumpHistoryRequest request);
CommandResult readBasalProfile(int number);

View file

@ -52,6 +52,11 @@ public class RuffyCommandsV1Impl implements RuffyCommands {
return delegate.isPumpBusy();
}
@Override
public CommandResult readPumpState() {
return delegate.readPumpState();
}
@Override
public CommandResult readHistory(PumpHistoryRequest request) {
return delegate.readHistory(request);

View file

@ -30,6 +30,7 @@ import de.jotomo.ruffyscripter.commands.Command;
import de.jotomo.ruffyscripter.commands.CommandException;
import de.jotomo.ruffyscripter.commands.GetPumpStateCommand;
import de.jotomo.ruffyscripter.commands.ReadBasalProfileCommand;
import de.jotomo.ruffyscripter.commands.ReadPumpStateCommand;
import de.jotomo.ruffyscripter.commands.SetBasalProfileCommand;
import de.jotomo.ruffyscripter.commands.SetTbrCommand;
import de.jotomo.ruffy.spi.BasalProfile;
@ -109,6 +110,8 @@ public class RuffyScripter implements RuffyCommands {
if (!boundSucceeded) {
log.error("No connection to ruffy. Pump control unavailable.");
} else {
started = true;
}
}
@ -223,16 +226,9 @@ public class RuffyScripter implements RuffyCommands {
return activeCmd != null;
}
public void unbind() {
/*
if (ruffyService != null)
try {
ruffyService.removeHandler(mHandler);
} catch (Exception e) {
// ignore
}
*/
this.ruffyService = null;
@Override
public CommandResult readPumpState() {
return runCommand(new ReadPumpStateCommand());
}
public void returnToMainMenu() {
@ -260,16 +256,17 @@ public class RuffyScripter implements RuffyCommands {
* Always returns a CommandResult, never throws
*/
public CommandResult runCommand(final Command cmd) {
log.debug("Attempting to run cmd: " + cmd);
if (unrecoverableError != null) {
return new CommandResult().success(false).enacted(false).message(unrecoverableError);
}
List<String> violations = cmd.validateArguments();
if (!violations.isEmpty()) {
return new CommandResult().message(Joiner.on("\n").join(violations)).state(readPumpState());
return new CommandResult().message(Joiner.on("\n").join(violations)).state(readPumpStateInternal());
}
synchronized (RuffyScripter.class) {
// synchronized (RuffyScripter.class) {
try {
activeCmd = cmd;
long connectStart = System.currentTimeMillis();
@ -317,7 +314,7 @@ public class RuffyScripter implements RuffyCommands {
return;
}
log.debug("Connection ready to execute cmd " + cmd);
PumpState pumpState = readPumpState();
PumpState pumpState = readPumpStateInternal();
log.debug("Pump state before running command: " + pumpState);
long cmdStartTime = System.currentTimeMillis();
cmd.setScripter(scripter);
@ -368,7 +365,7 @@ public class RuffyScripter implements RuffyCommands {
}
if (returnable.cmdResult.state == null) {
returnable.cmdResult.state = readPumpState();
returnable.cmdResult.state = readPumpStateInternal();
}
long connectDurationSec = (executionStart - connectStart) / 1000;
long executionDurationSec = (System.currentTimeMillis() - executionStart) / 1000;
@ -384,7 +381,7 @@ public class RuffyScripter implements RuffyCommands {
} finally {
activeCmd = null;
}
}
// }
}
/**
@ -436,7 +433,7 @@ public class RuffyScripter implements RuffyCommands {
/** This reads the state of the, which is whatever is currently displayed on the display,
* no actions are performed. */
public PumpState readPumpState() {
public PumpState readPumpStateInternal() {
PumpState state = new PumpState();
Menu menu = currentMenu;
if (menu == null) {

View file

@ -25,7 +25,7 @@ public class CancelTbrCommand extends BaseCommand {
public CommandResult execute() {
try {
scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
PumpState pumpState = scripter.readPumpState();
PumpState pumpState = scripter.readPumpStateInternal();
if (!pumpState.tbrActive) {
// log.debug("active temp basal 90s ago: " +
// MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis() - 90 * 1000));

View file

@ -1,5 +1,6 @@
package de.jotomo.ruffyscripter.commands;
import java.util.Collections;
import java.util.List;
import de.jotomo.ruffyscripter.RuffyScripter;
@ -14,12 +15,12 @@ public class ReadBasalProfileCommand implements Command {
@Override
public CommandResult execute() {
return null;
return new CommandResult().success(false).enacted(false);
}
@Override
public List<String> validateArguments() {
return null;
return Collections.emptyList();
}
@Override

View file

@ -0,0 +1,22 @@
package de.jotomo.ruffyscripter.commands;
import java.util.Collections;
import java.util.List;
import de.jotomo.ruffy.spi.CommandResult;
import de.jotomo.ruffyscripter.RuffyScripter;
public class ReadPumpStateCommand implements Command {
@Override
public CommandResult execute() {
return new CommandResult().success(true).enacted(false);
}
@Override
public List<String> validateArguments() {
return Collections.emptyList();
}
@Override
public void setScripter(RuffyScripter scripter) {}
}