Fix connection handshake for DanaR pumps

This commit is contained in:
Milos Kozak 2018-08-21 17:25:20 +02:00
parent cd90bab877
commit 32d9a8b4d6
17 changed files with 121 additions and 13 deletions

View file

@ -10,9 +10,10 @@ import info.nightscout.androidaps.R;
public class EventPumpStatusChanged extends Event {
public static final int CONNECTING = 0;
public static final int CONNECTED = 1;
public static final int PERFORMING = 2;
public static final int DISCONNECTING = 3;
public static final int DISCONNECTED = 4;
public static final int HANDSHAKING = 2;
public static final int PERFORMING = 3;
public static final int DISCONNECTING = 4;
public static final int DISCONNECTED = 5;
public int sStatus = DISCONNECTED;
public int sSecondsElapsed = 0;
@ -47,6 +48,8 @@ public class EventPumpStatusChanged extends Event {
public String textStatus() {
if (sStatus == CONNECTING)
return String.format(MainApp.gs(R.string.danar_history_connectingfor), sSecondsElapsed);
else if (sStatus == HANDSHAKING)
return MainApp.gs(R.string.handshaking);
else if (sStatus == CONNECTED)
return MainApp.gs(R.string.connected);
else if (sStatus == PERFORMING)

View file

@ -11,11 +11,13 @@ import info.nightscout.androidaps.data.PumpEnactResult;
*/
public interface PumpInterface {
boolean isInitialized();
boolean isSuspended();
boolean isBusy();
boolean isConnected();
boolean isConnecting();
boolean isInitialized(); // true if pump status has been read and is ready to accept commands
boolean isSuspended(); // true if suspended (not delivering insulin)
boolean isBusy(); // if true pump is not ready to accept commands right now
boolean isConnected(); // true if BT connection is established
boolean isConnecting(); // true if BT connection is in progress
boolean isHandshakeInProgress(); // true if BT is connected but initial handshake is still in progress
void finishHandshaking(); // set initial handshake completed
void connect(String reason);
void disconnect(String reason);

View file

@ -250,6 +250,15 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint
return false;
}
@Override
public boolean isHandshakeInProgress() {
return false;
}
@Override
public void finishHandshaking() {
}
@Override
public void connect(String reason) {
// ruffyscripter establishes a connection as needed.

View file

@ -175,6 +175,16 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
return pump.lastConnection > 0 && pump.isExtendedBolusEnabled && pump.maxBasal > 0;
}
@Override
public boolean isHandshakeInProgress() {
return sExecutionService != null && sExecutionService.isHandshakeInProgress();
}
@Override
public void finishHandshaking() {
sExecutionService.finishHandshaking();
}
@Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value();

View file

@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
/**
@ -34,6 +35,9 @@ public class MsgInitConnStatusOption extends MessageBase {
if (L.isEnabled(L.PUMPCOMM))
log.debug("Pump password: " + DanaRPump.getInstance().password);
}
// This is last message of initial sequence
ConfigBuilderPlugin.getPlugin().getActivePump().finishHandshaking();
}
}

View file

@ -58,7 +58,8 @@ public abstract class AbstractDanaRExecutionService extends Service {
protected DanaRPump mDanaRPump = DanaRPump.getInstance();
protected Treatment mBolusingTreatment = null;
protected Boolean mConnectionInProgress = false;
protected boolean mConnectionInProgress = false;
protected boolean mHandshakeInProgress = false;
protected AbstractSerialIOThread mSerialIOThread;
@ -131,6 +132,15 @@ public abstract class AbstractDanaRExecutionService extends Service {
return mConnectionInProgress;
}
public boolean isHandshakeInProgress() {
return isConnected() && mHandshakeInProgress;
}
public void finishHandshaking() {
mHandshakeInProgress = false;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED, 0));
}
public void disconnect(String from) {
if (mSerialIOThread != null)
mSerialIOThread.disconnect(from);

View file

@ -111,6 +111,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService {
return;
new Thread(() -> {
mHandshakeInProgress = false;
mConnectionInProgress = true;
getBTSocketForSelectedPump();
if (mRfcommSocket == null || mBTDevice == null) {
@ -132,7 +133,8 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED, 0));
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
mConnectionInProgress = false;

View file

@ -179,6 +179,16 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin {
return pump.lastConnection > 0 && pump.maxBasal > 0 && !pump.isConfigUD && !pump.isEasyModeEnabled && pump.isExtendedBolusEnabled;
}
@Override
public boolean isHandshakeInProgress() {
return sExecutionService != null && sExecutionService.isHandshakeInProgress();
}
@Override
public void finishHandshaking() {
sExecutionService.finishHandshaking();
}
@Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value();

View file

@ -115,6 +115,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService {
return;
new Thread(() -> {
mHandshakeInProgress = false;
mConnectionInProgress = true;
getBTSocketForSelectedPump();
if (mRfcommSocket == null || mBTDevice == null) {
@ -136,7 +137,8 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED, 0));
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
mConnectionInProgress = false;

View file

@ -221,6 +221,15 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
return danaRSService != null && danaRSService.isConnecting();
}
@Override
public boolean isHandshakeInProgress() {
return false;
}
@Override
public void finishHandshaking() {
}
@Override
public void disconnect(String from) {
if (L.isEnabled(L.PUMP))

View file

@ -144,6 +144,16 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
return DanaRPump.getInstance().lastConnection > 0 && DanaRPump.getInstance().maxBasal > 0;
}
@Override
public boolean isHandshakeInProgress() {
return sExecutionService != null && sExecutionService.isHandshakeInProgress();
}
@Override
public void finishHandshaking() {
sExecutionService.finishHandshaking();
}
@Override
public void switchAllowed(ConfigBuilderFragment.PluginViewHolder.PluginSwitcher pluginSwitcher, FragmentActivity context) {
boolean allowHardwarePump = SP.getBoolean("allow_hardware_pump", false);

View file

@ -134,6 +134,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService {
return;
new Thread(() -> {
mHandshakeInProgress = false;
mConnectionInProgress = true;
getBTSocketForSelectedPump();
if (mRfcommSocket == null || mBTDevice == null) {
@ -155,7 +156,8 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService {
mSerialIOThread.disconnect("Recreate SerialIOThread");
}
mSerialIOThread = new SerialIOThread(mRfcommSocket);
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED, 0));
mHandshakeInProgress = true;
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0));
}
mConnectionInProgress = false;

View file

@ -259,6 +259,15 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
return Connector.get().isPumpConnecting();
}
@Override
public boolean isHandshakeInProgress() {
return false;
}
@Override
public void finishHandshaking() {
}
@Override
public void connect(String reason) {
log("InsightPlugin::connect()");

View file

@ -88,6 +88,15 @@ public class MDIPlugin extends PluginBase implements PumpInterface {
return false;
}
@Override
public boolean isHandshakeInProgress() {
return false;
}
@Override
public void finishHandshaking() {
}
@Override
public void connect(String reason) {
}

View file

@ -147,6 +147,15 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface {
return false;
}
@Override
public boolean isHandshakeInProgress() {
return false;
}
@Override
public void finishHandshaking() {
}
@Override
public void connect(String reason) {
if (!Config.NSCLIENT)

View file

@ -101,6 +101,14 @@ public class QueueThread extends Thread {
}
}
if (pump.isHandshakeInProgress()) {
if (L.isEnabled(L.PUMPQUEUE))
log.debug("handshaking " + secondsElapsed);
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, (int) secondsElapsed));
SystemClock.sleep(100);
continue;
}
if (pump.isConnecting()) {
if (L.isEnabled(L.PUMPQUEUE))
log.debug("connecting " + secondsElapsed);
@ -109,7 +117,6 @@ public class QueueThread extends Thread {
continue;
}
if (!pump.isConnected()) {
if (L.isEnabled(L.PUMPQUEUE))
log.debug("connect");

View file

@ -1187,6 +1187,7 @@
<string name="setupwizard_preferred_aps_mode">Preferred APS mode</string>
<string name="treatments_wizard_total_label">Total</string>
<string name="calculation_short">Calc</string>
<string name="handshaking">Handshaking</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>