ComboPlugin: Extract fields into ComboPump.

This commit is contained in:
Johannes Mockenhaupt 2017-08-24 16:00:24 +02:00
parent 3fdac4b23e
commit 37523fbdd1
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 65 additions and 53 deletions

View file

@ -148,9 +148,9 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
statusText.setText(getPlugin().statusSummary); statusText.setText(getPlugin().getPump().stateSummary);
if (getPlugin().isInitialized()) { if (getPlugin().isInitialized()) {
PumpState ps = getPlugin().pumpState; PumpState ps = getPlugin().getPump().state;
if (ps != null) { if (ps != null) {
boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100; boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100;
if (tbrActive) { if (tbrActive) {
@ -183,16 +183,16 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
} }
} }
Command lastCmd = getPlugin().lastCmd; Command lastCmd = getPlugin().getPump().lastCmd;
if (lastCmd != null) { if (lastCmd != null) {
lastCmdText.setText(lastCmd.toString()); lastCmdText.setText(lastCmd.toString());
lastCmdTimeText.setText(getPlugin().lastCmdTime.toLocaleString()); lastCmdTimeText.setText(getPlugin().getPump().lastCmdTime.toLocaleString());
} else { } else {
lastCmdText.setText(""); lastCmdText.setText("");
lastCmdTimeText.setText(""); lastCmdTimeText.setText("");
} }
CommandResult lastCmdResult = getPlugin().lastCmdResult; CommandResult lastCmdResult = getPlugin().getPump().lastCmdResult;
if (lastCmdResult != null && lastCmdResult.message != null) { if (lastCmdResult != null && lastCmdResult.message != null) {
lastCmdResultText.setText(lastCmdResult.message); lastCmdResultText.setText(lastCmdResult.message);
lastCmdDurationText.setText(lastCmdResult.duration); lastCmdDurationText.setText(lastCmdResult.duration);

View file

@ -75,17 +75,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
private RuffyScripter ruffyScripter; private RuffyScripter ruffyScripter;
private ServiceConnection mRuffyServiceConnection; private ServiceConnection mRuffyServiceConnection;
// package-protected only so ComboFragment can access these private ComboPump pump = new ComboPump();
@NonNull
volatile String statusSummary = "Initializing";
@Nullable
volatile Command lastCmd;
@Nullable
volatile CommandResult lastCmdResult;
@NonNull
volatile Date lastCmdTime = new Date(0);
// TODO move into ComboPump? Accessor for fragment; more accessors? solves volatile issues;
volatile PumpState pumpState = new PumpState();
private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult(); private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult();
@ -129,6 +119,10 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
pumpDescription.isRefillingCapable = true; pumpDescription.isRefillingCapable = true;
} }
public ComboPump getPump() {
return pump;
}
/** /**
* The alerter frequently checks the result of the last executed command via the lastCmdResult * The alerter frequently checks the result of the last executed command via the lastCmdResult
* field and shows a notification with sound and vibration if an error occurred. * field and shows a notification with sound and vibration if an error occurred.
@ -146,15 +140,15 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
int id = 1000; int id = 1000;
long lastAlarmTime = 0; long lastAlarmTime = 0;
while (true) { while (true) {
Command localLastCmd = lastCmd; Command localLastCmd = pump.lastCmd;
CommandResult localLastCmdResult = lastCmdResult; CommandResult localLastCmdResult = pump.lastCmdResult;
if (localLastCmdResult != null && !localLastCmdResult.success) { if (localLastCmdResult != null && !localLastCmdResult.success) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long fiveMinutesSinceLastAlarm = lastAlarmTime + (5 * 60 * 1000) + (15 * 1000); long fiveMinutesSinceLastAlarm = lastAlarmTime + (5 * 60 * 1000) + (15 * 1000);
if (now > fiveMinutesSinceLastAlarm) { if (now > fiveMinutesSinceLastAlarm) {
log.error("Command failed: " + localLastCmd); log.error("Command failed: " + localLastCmd);
log.error("Command result: " + localLastCmdResult); log.error("Command result: " + localLastCmdResult);
PumpState localPumpState = pumpState; PumpState localPumpState = pump.state;
if (localPumpState != null && localPumpState.errorMsg != null) { if (localPumpState != null && localPumpState.errorMsg != null) {
log.warn("Pump is in error state, displaying; " + localPumpState.errorMsg); log.warn("Pump is in error state, displaying; " + localPumpState.errorMsg);
} }
@ -231,7 +225,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
} }
if (!boundSucceeded) { if (!boundSucceeded) {
statusSummary = "No connection to ruffy. Pump control not available."; pump.stateSummary = "No connection to ruffy. Pump control not available.";
} }
return true; return true;
} }
@ -308,12 +302,12 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
public boolean isInitialized() { public boolean isInitialized() {
// consider initialized when the pump's state was initially fetched, // consider initialized when the pump's state was initially fetched,
// after that lastCmd* variables will have values // after that lastCmd* variables will have values
return lastCmdTime.getTime() > 0; return pump.lastCmdTime.getTime() > 0;
} }
@Override @Override
public boolean isSuspended() { public boolean isSuspended() {
return pumpState != null && pumpState.suspended; return pump.state != null && pump.state.suspended;
} }
@Override @Override
@ -335,7 +329,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
@Override @Override
public Date lastDataTime() { public Date lastDataTime() {
return lastCmdTime; return pump.lastCmdTime;
} }
// this method is regularly called from info.nightscout.androidaps.receivers.KeepAliveReceiver // this method is regularly called from info.nightscout.androidaps.receivers.KeepAliveReceiver
@ -350,8 +344,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
} }
boolean notAUserRequest = !reason.toLowerCase().contains("user"); boolean notAUserRequest = !reason.toLowerCase().contains("user");
boolean wasRunAtLeastOnce = lastCmdTime.getTime() > 0; boolean wasRunAtLeastOnce = pump.lastCmdTime.getTime() > 0;
boolean ranWithinTheLastMinute = System.currentTimeMillis() < lastCmdTime.getTime() + 60 * 1000; boolean ranWithinTheLastMinute = System.currentTimeMillis() < pump.lastCmdTime.getTime() + 60 * 1000;
if (notAUserRequest && wasRunAtLeastOnce && ranWithinTheLastMinute) { if (notAUserRequest && wasRunAtLeastOnce && ranWithinTheLastMinute) {
log.debug("Not fetching state from pump, since we did already within the last 60 seconds"); log.debug("Not fetching state from pump, since we did already within the last 60 seconds");
} else { } else {
@ -468,11 +462,11 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
private CommandResult runCommand(Command command) { private CommandResult runCommand(Command command) {
if (ruffyScripter == null) { if (ruffyScripter == null) {
String msg = "No connection to ruffy. Pump control not available."; String msg = "No connection to ruffy. Pump control not available.";
statusSummary = msg; pump.stateSummary = msg;
return new CommandResult().message(msg); return new CommandResult().message(msg);
} }
statusSummary = "Executing " + command; pump.stateSummary = "Executing " + command;
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
CommandResult commandResult = ruffyScripter.runCommand(command); CommandResult commandResult = ruffyScripter.runCommand(command);
@ -481,17 +475,17 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
log.error("Exception received from pump", commandResult.exception); log.error("Exception received from pump", commandResult.exception);
} }
lastCmd = command; pump.lastCmd = command;
lastCmdTime = new Date(); pump.lastCmdTime = new Date();
lastCmdResult = commandResult; pump.lastCmdResult = commandResult;
pumpState = commandResult.state; pump.state = commandResult.state;
if (commandResult.success && commandResult.state.suspended) { if (commandResult.success && commandResult.state.suspended) {
statusSummary = "Suspended"; pump.stateSummary = "Suspended";
} else if (commandResult.success) { } else if (commandResult.success) {
statusSummary = "Idle"; pump.stateSummary = "Idle";
} else { } else {
statusSummary = "Error"; pump.stateSummary = "Error";
} }
MainApp.bus().post(new EventComboPumpUpdateGUI()); MainApp.bus().post(new EventComboPumpUpdateGUI());
@ -639,41 +633,41 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// TODO v2 add battery, reservoir info when we start reading that and clean up the code // TODO v2 add battery, reservoir info when we start reading that and clean up the code
@Override @Override
public JSONObject getJSONStatus() { public JSONObject getJSONStatus() {
if (lastCmdTime.getTime() + 5 * 60 * 1000L < System.currentTimeMillis()) { if (pump.lastCmdTime.getTime() + 5 * 60 * 1000L < System.currentTimeMillis()) {
return null; return null;
} }
try { try {
JSONObject pump = new JSONObject(); JSONObject pumpJson = new JSONObject();
JSONObject status = new JSONObject(); JSONObject statusJson = new JSONObject();
JSONObject extended = new JSONObject(); JSONObject extendedJson = new JSONObject();
status.put("status", statusSummary); statusJson.put("status", pump.stateSummary);
extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION); extendedJson.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION);
try { try {
extended.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName()); extendedJson.put("ActiveProfile", MainApp.getConfigBuilder().getProfileName());
} catch (Exception e) { } catch (Exception e) {
} }
status.put("timestamp", lastCmdTime); statusJson.put("timestamp", pump.lastCmdTime);
PumpState ps = pumpState; PumpState ps = pump.state;
if (ps != null) { if (ps != null) {
if (ps.tbrActive) { if (ps.tbrActive) {
extended.put("TempBasalAbsoluteRate", ps.tbrRate); extendedJson.put("TempBasalAbsoluteRate", ps.tbrRate);
extended.put("TempBasalPercent", ps.tbrPercent); extendedJson.put("TempBasalPercent", ps.tbrPercent);
extended.put("TempBasalRemaining", ps.tbrRemainingDuration); extendedJson.put("TempBasalRemaining", ps.tbrRemainingDuration);
} }
if (ps.errorMsg != null) { if (ps.errorMsg != null) {
extended.put("ErrorMessage", ps.errorMsg); extendedJson.put("ErrorMessage", ps.errorMsg);
} }
} }
// more info here .... look at dana plugin // more info here .... look at dana plugin
pump.put("status", status); pumpJson.put("status", statusJson);
pump.put("extended", extended); pumpJson.put("extended", extendedJson);
pump.put("clock", DateUtil.toISOString(lastCmdTime)); pumpJson.put("clock", DateUtil.toISOString(pump.lastCmdTime));
return pump; return pumpJson;
} catch (Exception e) { } catch (Exception e) {
log.warn("Failed to gather device status for upload", e); log.warn("Failed to gather device status for upload", e);
} }
@ -696,7 +690,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
@Override @Override
public String shortStatus(boolean veryShort) { public String shortStatus(boolean veryShort) {
// TODO trim for wear if veryShort==true // TODO trim for wear if veryShort==true
return statusSummary; return pump.stateSummary;
} }
@Override @Override

View file

@ -1,4 +1,22 @@
package info.nightscout.androidaps.plugins.PumpCombo; package info.nightscout.androidaps.plugins.PumpCombo;
public class ComboPump { import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Date;
import de.jotomo.ruffyscripter.PumpState;
import de.jotomo.ruffyscripter.commands.Command;
import de.jotomo.ruffyscripter.commands.CommandResult;
class ComboPump {
@NonNull
volatile String stateSummary = "Initializing";
@Nullable
volatile Command lastCmd;
@Nullable
volatile CommandResult lastCmdResult;
@NonNull
volatile Date lastCmdTime = new Date(0);
volatile PumpState state = new PumpState();
} }