More pieces for Insight Pump

This commit is contained in:
Jamorham 2018-01-24 16:51:05 +00:00
parent 7a4bc3b694
commit 979dc2d5e2
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
6 changed files with 131 additions and 31 deletions

View file

@ -85,7 +85,7 @@ public class InsightPumpFragment extends SubscriberFragment {
@Override
public void run() {
InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin();
basaBasalRateView.setText(insightPumpPlugin.getBaseBasalRate() + "U");
basaBasalRateView.setText(insightPumpPlugin.getBaseBasalRateString() + "U");
if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
tempBasalView.setText(MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()).toStringFull());
} else {

View file

@ -0,0 +1,44 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import sugar.free.sightparser.applayer.AppLayerMessage;
import sugar.free.sightparser.applayer.remote_control.ChangeTBRMessage;
import sugar.free.sightparser.applayer.remote_control.SetTBRMessage;
import sugar.free.sightparser.applayer.status.CurrentBasalMessage;
import sugar.free.sightparser.applayer.status.CurrentTBRMessage;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.TaskRunner;
// by Tebbe Ubben
public class AbsoluteTBRTaskRunner extends TaskRunner {
private float absolute;
private int amount;
private int duration;
public AbsoluteTBRTaskRunner(SightServiceConnector serviceConnector, float absolute, int duration) {
super(serviceConnector);
this.absolute = absolute;
}
@Override
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
if (message == null) return new CurrentBasalMessage();
else if (message instanceof CurrentBasalMessage) {
float currentBasal = ((CurrentBasalMessage) message).getCurrentBasalAmount();
amount = (int) (100F / currentBasal * absolute);
amount = ((int) amount / 10) * 10;
if (amount > 250) amount = 250;
return new CurrentTBRMessage();
} else if (message instanceof CurrentTBRMessage) {
SetTBRMessage setTBRMessage;
if (((CurrentTBRMessage) message).getPercentage() == 100)
setTBRMessage = new SetTBRMessage();
else setTBRMessage = new ChangeTBRMessage();
setTBRMessage.setAmount((short) amount);
setTBRMessage.setDuration((short) duration);
return setTBRMessage;
} else if (message instanceof SetTBRMessage) finish(amount);
return null;
}
}

View file

@ -0,0 +1,38 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import sugar.free.sightparser.applayer.AppLayerMessage;
import sugar.free.sightparser.applayer.remote_control.CancelBolusMessage;
import sugar.free.sightparser.applayer.status.ActiveBolusesMessage;
import sugar.free.sightparser.applayer.status.BolusType;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.TaskRunner;
// by Tebbe Ubben
public class CancelBolusTaskRunner extends TaskRunner {
private BolusType bolusType;
public CancelBolusTaskRunner(SightServiceConnector serviceConnector, BolusType bolusType) {
super(serviceConnector);
this.bolusType = bolusType;
}
@Override
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
if (message == null) return new ActiveBolusesMessage();
else if (message instanceof ActiveBolusesMessage) {
ActiveBolusesMessage bolusesMessage = (ActiveBolusesMessage) message;
CancelBolusMessage cancelBolusMessage = new CancelBolusMessage();
if (bolusesMessage.getBolus1().getBolusType() == bolusType)
cancelBolusMessage.setBolusId(bolusesMessage.getBolus1().getBolusID());
else if (bolusesMessage.getBolus2().getBolusType() == bolusType)
cancelBolusMessage.setBolusId(bolusesMessage.getBolus2().getBolusID());
else if (bolusesMessage.getBolus3().getBolusType() == bolusType)
cancelBolusMessage.setBolusId(bolusesMessage.getBolus3().getBolusID());
else finish(null);
return cancelBolusMessage;
} else if (message instanceof CancelBolusMessage) finish(null);
return null;
}
}

View file

@ -1,16 +1,13 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.util.Log;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
import sugar.free.sightparser.handling.ServiceConnectionCallback;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.StatusCallback;
@ -44,7 +41,7 @@ public class Connector {
synchronized (this) {
log("Status change: " + status);
lastStatus = status;
lastStatusTime = tsl();
lastStatusTime = Helpers.tsl();
switch (status) {
// TODO automated reactions to change in status
}
@ -53,19 +50,6 @@ public class Connector {
}
};
private BroadcastReceiver statusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
log("Receiving broadcast!");
final String str = intent.getStringExtra("STATUS_MESSAGE");
try {
statusCallback.onStatusChange(str);
} catch (RemoteException e) {
log("Remote exception: " + e);
}
}
};
private ServiceConnectionCallback connectionCallback = new ServiceConnectionCallback() {
@Override
public void onServiceConnected() {
@ -81,7 +65,6 @@ public class Connector {
};
private Connector() {
registerReceiver();
}
public static Connector get() {
@ -97,9 +80,6 @@ public class Connector {
}
}
private static long tsl() {
return System.currentTimeMillis();
}
private static boolean isCompanionAppInstalled() {
return checkPackageExists(MainApp.instance(), COMPANION_APP_PACKAGE);
@ -127,14 +107,6 @@ public class Connector {
android.util.Log.e("PUMPPUMP", msg);
}
private void registerReceiver() {
try {
MainApp.instance().unregisterReceiver(statusReceiver);
} catch (Exception e) {
//
}
MainApp.instance().registerReceiver(statusReceiver, new IntentFilter(STATUS_RECEIVER));
}
public synchronized void init() {
log("init");

View file

@ -0,0 +1,9 @@
package info.nightscout.androidaps.plugins.PumpInsight.events;
import info.nightscout.androidaps.events.EventUpdateGui;
/**
* Created by jamorham on 23/01/2018.
*/
public class EventInsightPumpUpdateGui extends EventUpdateGui {
}

View file

@ -0,0 +1,37 @@
package info.nightscout.androidaps.plugins.PumpInsight.utils;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jamorham on 24/01/2018.
*
* Useful utility methods from xDrip+
*
*/
public class Helpers {
private static final String TAG = "InsightHelpers";
private static final Map<String, Long> rateLimits = new HashMap<>();
// return true if below rate limit
public static synchronized boolean ratelimit(String name, int seconds) {
// check if over limit
if ((rateLimits.containsKey(name)) && (tsl() - rateLimits.get(name) < (seconds * 1000))) {
Log.d(TAG, name + " rate limited: " + seconds + " seconds");
return false;
}
// not over limit
rateLimits.put(name, tsl());
return true;
}
public static long tsl() {
return System.currentTimeMillis();
}
}