Merge pull request #716 from jamorham/dev
Improve Insight Connector life-cycle handling
This commit is contained in:
commit
31f7670f43
|
@ -92,6 +92,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
private PumpDescription pumpDescription = new PumpDescription();
|
||||
private double basalRate = 0;
|
||||
private Connector connector;
|
||||
private volatile boolean connector_enabled = false;
|
||||
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
|
||||
|
||||
@Override
|
||||
|
@ -120,7 +121,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
};
|
||||
|
||||
private InsightPumpPlugin() {
|
||||
log("InsightPumpPlugin");
|
||||
log("InsightPumpPlugin instantiated");
|
||||
pumpDescription.isBolusCapable = true;
|
||||
pumpDescription.bolusStep = 0.05d; // specification says 0.05U up to 2U then 0.1U @ 2-5U 0.2U @ 10-20U 0.5U 10-20U (are these just UI restrictions?)
|
||||
|
||||
|
@ -144,12 +145,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
pumpDescription.basalMinimumRate = 0.02d;
|
||||
|
||||
pumpDescription.isRefillingCapable = true;
|
||||
//pumpDescription.storesCarbInfo = false; // uncomment when PumpDescription updated to include this
|
||||
//pumpDescription.storesCarbInfo = false;
|
||||
|
||||
this.connector = Connector.get();
|
||||
this.connector.init();
|
||||
|
||||
log("back from init");
|
||||
}
|
||||
|
||||
|
||||
|
@ -181,6 +178,31 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
MainApp.bus().post(e);
|
||||
}
|
||||
|
||||
private void enableConnector() {
|
||||
if (!connector_enabled) {
|
||||
synchronized (this) {
|
||||
if (!connector_enabled) {
|
||||
log("Instantiating connector");
|
||||
connector_enabled = true;
|
||||
this.connector = Connector.get();
|
||||
this.connector.init();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void disableConnector() {
|
||||
if (connector_enabled) {
|
||||
synchronized (this) {
|
||||
if (connector_enabled) {
|
||||
log("Shutting down connector");
|
||||
Connector.get().shutdown();
|
||||
connector_enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFragmentClass() {
|
||||
return InsightPumpFragment.class.getName();
|
||||
|
@ -231,7 +253,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
|
||||
@Override
|
||||
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
|
||||
if (type == PUMP) this.fragmentEnabled = fragmentEnabled;
|
||||
if (type == PUMP) {
|
||||
if (fragmentEnabled) {
|
||||
enableConnector();
|
||||
} else {
|
||||
disableConnector();
|
||||
}
|
||||
this.fragmentEnabled = fragmentEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -471,21 +500,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
|||
|
||||
if (percent_amount > 250) percent_amount = 250;
|
||||
|
||||
// TODO this is experimental and not enabled due to preference option
|
||||
// ignore TBR changes if the setting is enabled and they are not 0% but are +- 30% of where we are now or we've been running is TBR < 15 minutes
|
||||
if ((percent_amount != 0)
|
||||
&& (SP.getBoolean("insight_dont_change_active_tbr", false)
|
||||
&& MainApp.getConfigBuilder().isTempBasalInProgress())) {
|
||||
final TemporaryBasal temporaryBasalFromHistory = MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis());
|
||||
if ((temporaryBasalFromHistory != null) && (temporaryBasalFromHistory.isInProgress())) {
|
||||
// only change if it is +- 30% from where we are now or we have done more than 15 minutes already
|
||||
if ((Math.abs(temporaryBasalFromHistory.percentRate - percent_amount) <= 30)
|
||||
|| (temporaryBasalFromHistory.getRealDuration() < 15)) {
|
||||
log("Refusing to change TBR due to Insight plugin setting: " + percent_amount + " vs " + temporaryBasalFromHistory.percentRate + " running for: " + temporaryBasalFromHistory.getRealDuration());
|
||||
return new PumpEnactResult().enacted(false).success(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), percent_amount, durationInMinutes);
|
||||
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
|
||||
|
|
|
@ -229,7 +229,6 @@ public class Connector {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static synchronized void delayedDisconnectionThread() {
|
||||
if (keepAliveActive()) {
|
||||
if (!disconnect_thread_running) {
|
||||
|
@ -239,7 +238,7 @@ public class Connector {
|
|||
public void run() {
|
||||
final PowerManager.WakeLock wl = Helpers.getWakeLock("insight-disconnection-timer", 600000);
|
||||
try {
|
||||
while (keepAliveActive()) {
|
||||
while (disconnect_thread_running && keepAliveActive()) {
|
||||
if (Helpers.ratelimit("insight-expiry-notice", 5)) {
|
||||
log("Staying connected thread expires: " + Helpers.dateTimeText(stayConnectedTill));
|
||||
}
|
||||
|
@ -249,8 +248,13 @@ public class Connector {
|
|||
//
|
||||
}
|
||||
}
|
||||
log("Sending the real delayed disconnect");
|
||||
get().getServiceConnector().disconnect();
|
||||
|
||||
if (disconnect_thread_running) {
|
||||
log("Sending the real delayed disconnect");
|
||||
get().getServiceConnector().disconnect();
|
||||
} else {
|
||||
log("Disconnect thread already terminating");
|
||||
}
|
||||
} finally {
|
||||
Helpers.releaseWakeLock(wl);
|
||||
disconnect_thread_running = false;
|
||||
|
@ -267,6 +271,39 @@ public class Connector {
|
|||
return (long) (Helpers.roundDouble(((double) t * 100) / total, 0));
|
||||
}
|
||||
|
||||
public synchronized void shutdown() {
|
||||
if (instance != null) {
|
||||
log("Attempting to shut down connector");
|
||||
try {
|
||||
disconnect_thread_running = false;
|
||||
try {
|
||||
instance.serviceConnector.setConnectionCallback(null);
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
try {
|
||||
instance.serviceConnector.removeStatusCallback(statusCallback);
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
try {
|
||||
instance.serviceConnector.disconnect();
|
||||
} catch (Exception e) {
|
||||
log("Exception disconnecting: " + e);
|
||||
}
|
||||
try {
|
||||
instance.serviceConnector.disconnectFromService();
|
||||
} catch (Exception e) {
|
||||
log("Excpetion disconnecting service: " + e);
|
||||
}
|
||||
instance.serviceConnector = null;
|
||||
instance = null;
|
||||
} catch (Exception e) {
|
||||
log("Exception shutting down: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("AccessStaticViaInstance")
|
||||
private synchronized void initializeHistoryReceiver() {
|
||||
if (historyReceiver == null) {
|
||||
|
@ -494,17 +531,24 @@ public class Connector {
|
|||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventFeatureRunning ev) {
|
||||
if (SP.getBoolean("insight_preemptive_connect", true)) {
|
||||
switch (ev.getFeature()) {
|
||||
case WIZARD:
|
||||
log("Wizard feature detected, preconnecting to pump");
|
||||
connectToPump(120 * 1000);
|
||||
break;
|
||||
case MAIN:
|
||||
log("Main feature detected, preconnecting to pump");
|
||||
connectToPump(30 * 1000);
|
||||
break;
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isConnected()) {
|
||||
if (SP.getBoolean("insight_preemptive_connect", true)) {
|
||||
switch (ev.getFeature()) {
|
||||
case WIZARD:
|
||||
log("Wizard feature detected, preconnecting to pump");
|
||||
connectToPump(120 * 1000);
|
||||
break;
|
||||
case MAIN:
|
||||
log("Main feature detected, preconnecting to pump");
|
||||
connectToPump(30 * 1000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,6 @@ public class QueueThread extends Thread {
|
|||
|
||||
this.queue = queue;
|
||||
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
||||
//mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "QueueThread");
|
||||
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "QueueThread");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue