Merge pull request #716 from jamorham/dev
Improve Insight Connector life-cycle handling
This commit is contained in:
commit
31f7670f43
3 changed files with 96 additions and 38 deletions
|
@ -92,6 +92,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
||||||
private PumpDescription pumpDescription = new PumpDescription();
|
private PumpDescription pumpDescription = new PumpDescription();
|
||||||
private double basalRate = 0;
|
private double basalRate = 0;
|
||||||
private Connector connector;
|
private Connector connector;
|
||||||
|
private volatile boolean connector_enabled = false;
|
||||||
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
|
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -120,7 +121,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
||||||
};
|
};
|
||||||
|
|
||||||
private InsightPumpPlugin() {
|
private InsightPumpPlugin() {
|
||||||
log("InsightPumpPlugin");
|
log("InsightPumpPlugin instantiated");
|
||||||
pumpDescription.isBolusCapable = true;
|
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?)
|
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.basalMinimumRate = 0.02d;
|
||||||
|
|
||||||
pumpDescription.isRefillingCapable = true;
|
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);
|
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
|
@Override
|
||||||
public String getFragmentClass() {
|
public String getFragmentClass() {
|
||||||
return InsightPumpFragment.class.getName();
|
return InsightPumpFragment.class.getName();
|
||||||
|
@ -231,7 +253,14 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
|
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
|
@Override
|
||||||
|
@ -471,21 +500,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
|
||||||
|
|
||||||
if (percent_amount > 250) percent_amount = 250;
|
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 SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), percent_amount, durationInMinutes);
|
||||||
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
|
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
|
||||||
|
|
|
@ -229,7 +229,6 @@ public class Connector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static synchronized void delayedDisconnectionThread() {
|
private static synchronized void delayedDisconnectionThread() {
|
||||||
if (keepAliveActive()) {
|
if (keepAliveActive()) {
|
||||||
if (!disconnect_thread_running) {
|
if (!disconnect_thread_running) {
|
||||||
|
@ -239,7 +238,7 @@ public class Connector {
|
||||||
public void run() {
|
public void run() {
|
||||||
final PowerManager.WakeLock wl = Helpers.getWakeLock("insight-disconnection-timer", 600000);
|
final PowerManager.WakeLock wl = Helpers.getWakeLock("insight-disconnection-timer", 600000);
|
||||||
try {
|
try {
|
||||||
while (keepAliveActive()) {
|
while (disconnect_thread_running && keepAliveActive()) {
|
||||||
if (Helpers.ratelimit("insight-expiry-notice", 5)) {
|
if (Helpers.ratelimit("insight-expiry-notice", 5)) {
|
||||||
log("Staying connected thread expires: " + Helpers.dateTimeText(stayConnectedTill));
|
log("Staying connected thread expires: " + Helpers.dateTimeText(stayConnectedTill));
|
||||||
}
|
}
|
||||||
|
@ -249,8 +248,13 @@ public class Connector {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (disconnect_thread_running) {
|
||||||
log("Sending the real delayed disconnect");
|
log("Sending the real delayed disconnect");
|
||||||
get().getServiceConnector().disconnect();
|
get().getServiceConnector().disconnect();
|
||||||
|
} else {
|
||||||
|
log("Disconnect thread already terminating");
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
Helpers.releaseWakeLock(wl);
|
Helpers.releaseWakeLock(wl);
|
||||||
disconnect_thread_running = false;
|
disconnect_thread_running = false;
|
||||||
|
@ -267,6 +271,39 @@ public class Connector {
|
||||||
return (long) (Helpers.roundDouble(((double) t * 100) / total, 0));
|
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")
|
@SuppressWarnings("AccessStaticViaInstance")
|
||||||
private synchronized void initializeHistoryReceiver() {
|
private synchronized void initializeHistoryReceiver() {
|
||||||
if (historyReceiver == null) {
|
if (historyReceiver == null) {
|
||||||
|
@ -494,6 +531,10 @@ public class Connector {
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onStatusEvent(final EventFeatureRunning ev) {
|
public void onStatusEvent(final EventFeatureRunning ev) {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (isConnected()) {
|
||||||
if (SP.getBoolean("insight_preemptive_connect", true)) {
|
if (SP.getBoolean("insight_preemptive_connect", true)) {
|
||||||
switch (ev.getFeature()) {
|
switch (ev.getFeature()) {
|
||||||
case WIZARD:
|
case WIZARD:
|
||||||
|
@ -508,3 +549,6 @@ public class Connector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -41,7 +41,6 @@ public class QueueThread extends Thread {
|
||||||
|
|
||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
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");
|
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "QueueThread");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue