Initial bits for Insight Pump

This commit is contained in:
Jamorham 2018-01-24 14:18:31 +00:00
parent f5a2c5bcd2
commit 7a4bc3b694
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
7 changed files with 603 additions and 0 deletions

View file

@ -192,6 +192,8 @@ dependencies {
compile "com.joanzapata.iconify:android-iconify-fontawesome:2.1.1"
compile "com.google.android.gms:play-services-wearable:7.5.0"
compile(name: "android-edittext-validator-v1.3.4-mod", ext: "aar")
compile(name: "sightparser-release", ext: "aar")
compile("com.google.android:flexbox:0.3.0") {
exclude group: "com.android.support"
}

View file

@ -54,6 +54,7 @@ import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
import info.nightscout.androidaps.plugins.PumpInsight.InsightPumpPlugin;
import info.nightscout.androidaps.plugins.PumpMDI.MDIPlugin;
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin;
@ -129,6 +130,7 @@ public class MainApp extends Application {
if (Config.DANAR) pluginsList.add(DanaRv2Plugin.getPlugin());
if (Config.DANAR) pluginsList.add(DanaRSPlugin.getPlugin());
pluginsList.add(CareportalPlugin.getPlugin());
if (Config.APS) pluginsList.add(InsightPumpPlugin.getPlugin());
if (Config.MDI) pluginsList.add(MDIPlugin.getPlugin());
if (Config.VIRTUALPUMP) pluginsList.add(VirtualPumpPlugin.getPlugin());
if (Config.APS) pluginsList.add(LoopPlugin.getPlugin());

View file

@ -0,0 +1,106 @@
package info.nightscout.androidaps.plugins.PumpInsight;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import com.crashlytics.android.Crashlytics;
public class InsightPumpFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(InsightPumpFragment.class);
TextView basaBasalRateView;
TextView tempBasalView;
TextView extendedBolusView;
TextView batteryView;
TextView reservoirView;
TextView statusView;
Connector connector = Connector.get();
private static Handler sLoopHandler = new Handler();
private static Runnable sRefreshLoop = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (sRefreshLoop == null) {
sRefreshLoop = new Runnable() {
@Override
public void run() {
updateGUI();
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
};
sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View view = inflater.inflate(R.layout.insightpump_fragment, container, false);
basaBasalRateView = (TextView) view.findViewById(R.id.insightpump_basabasalrate);
tempBasalView = (TextView) view.findViewById(R.id.insightpump_tempbasal);
extendedBolusView = (TextView) view.findViewById(R.id.insightpump_extendedbolus);
batteryView = (TextView) view.findViewById(R.id.insightpump_battery);
reservoirView = (TextView) view.findViewById(R.id.insightpump_reservoir);
statusView = (TextView) view.findViewById(R.id.insightpump_status);
return view;
} catch (Exception e) {
Crashlytics.logException(e);
}
return null;
}
@Subscribe
public void onStatusEvent(final EventInsightPumpUpdateGui ev) {
updateGUI();
}
@Override
protected void updateGUI() {
Activity activity = getActivity();
if (activity != null && basaBasalRateView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin();
basaBasalRateView.setText(insightPumpPlugin.getBaseBasalRate() + "U");
if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
tempBasalView.setText(MainApp.getConfigBuilder().getTempBasalFromHistory(System.currentTimeMillis()).toStringFull());
} else {
tempBasalView.setText("");
}
if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress()) {
extendedBolusView.setText(MainApp.getConfigBuilder().getExtendedBolusFromHistory(System.currentTimeMillis()).toString());
} else {
extendedBolusView.setText("");
}
batteryView.setText(insightPumpPlugin.batteryPercent + "%");
reservoirView.setText(insightPumpPlugin.reservoirInUnits + "U");
statusView.setText(connector.getLastStatusMessage());
}
});
}
}

View file

@ -0,0 +1,207 @@
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 sugar.free.sightparser.handling.ServiceConnectionCallback;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.StatusCallback;
import sugar.free.sightparser.pipeline.Status;
/**
* Created by jamorham on 23/01/2018.
*
* Connects to SightRemote app service using SightParser library
*
* SightRemote and SightParser created by Tebbe Ubben
*
* Original proof of concept SightProxy by jamorham
*
*/
public class Connector {
private static final String TAG = "InsightConnector";
private static final String COMPANION_APP_PACKAGE = "sugar.free.sightremote";
private static final String STATUS_RECEIVER = "sugar.free.sightparser.handling.StatusCallback";
private static volatile Connector instance;
private volatile SightServiceConnector serviceConnector;
private volatile Status lastStatus = null;
private volatile long lastStatusTime = -1;
private boolean companionAppInstalled = false;
private StatusCallback statusCallback = new StatusCallback() {
@Override
public void onStatusChange(Status status) {
synchronized (this) {
log("Status change: " + status);
lastStatus = status;
lastStatusTime = tsl();
switch (status) {
// TODO automated reactions to change in status
}
MainApp.bus().post(new EventInsightPumpUpdateGui());
}
}
};
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() {
log("On service connected");
serviceConnector.connect();
statusCallback.onStatusChange(safeGetStatus());
}
@Override
public void onServiceDisconnected() {
log("Disconnected from service");
}
};
private Connector() {
registerReceiver();
}
public static Connector get() {
if (instance == null) {
init_instance();
}
return instance;
}
private synchronized static void init_instance() {
if (instance == null) {
instance = new Connector();
}
}
private static long tsl() {
return System.currentTimeMillis();
}
private static boolean isCompanionAppInstalled() {
return checkPackageExists(MainApp.instance(), COMPANION_APP_PACKAGE);
}
private static boolean checkPackageExists(Context context, String packageName) {
try {
final PackageManager pm = context.getPackageManager();
final PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi.packageName.equals(packageName);
} catch (PackageManager.NameNotFoundException e) {
return false;
} catch (Exception e) {
Log.wtf(TAG, "Exception trying to determine packages! " + e);
return false;
}
}
public static void connectToPump() {
log("Attempting to connect to pump");
get().getServiceConnector().connect();
}
private static void log(String msg) {
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");
if (serviceConnector == null) {
companionAppInstalled = isCompanionAppInstalled();
if (companionAppInstalled) {
serviceConnector = new SightServiceConnector(MainApp.instance());
serviceConnector.addStatusCallback(statusCallback);
serviceConnector.setConnectionCallback(connectionCallback);
serviceConnector.connectToService();
log("Trying to connect");
} else {
log("Not trying init due to missing companion app");
}
}
}
public SightServiceConnector getServiceConnector() {
init();
return serviceConnector;
}
public String getCurrent() {
init();
return safeGetStatus().toString();
}
public Status safeGetStatus() {
if (isConnected()) return serviceConnector.getStatus();
return Status.DISCONNECTED;
}
public Status getLastStatus() {
return lastStatus;
}
public boolean isConnected() {
return serviceConnector != null && serviceConnector.isConnectedToService();
}
public boolean isPumpConnected() {
//return isConnected() && serviceConnector.isUseable();
return isConnected() && getLastStatus() == Status.CONNECTED;
}
public String getLastStatusMessage() {
if (!companionAppInstalled) {
return "Companion app does not appear to be installed!";
}
if (!isConnected()) {
return "Not connected to companion app!";
}
if (lastStatus == null) {
return "Unknown";
}
switch (lastStatus) {
default:
return lastStatus.toString();
}
}
public boolean lastStatusRecent() {
return true; // TODO evaluate whether current
}
}

View file

@ -0,0 +1,276 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.PumpInsight.InsightPumpFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/insightpump"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="Status"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="@string/pump_basebasalrate_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_basabasalrate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="@string/pump_tempbasal_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_tempbasal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="@string/virtualpump_extendedbolus_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_extendedbolus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="@string/pump_battery_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_battery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:paddingRight="5dp"
android:text="@string/pump_reservoir_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/insightpump_reservoir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>

View file

@ -861,5 +861,7 @@
<string name="combo_error_bolus_recovery_progress">Recovering from connection loss</string>
<string name="combo_reservoir_level_insufficient_for_bolus">Not enough insulin for bolus left in reservoir</string>
<string name="extendedbolusdeliveryerror">Extended bolus delivery error</string>
<string name="insightpump_shortname">Insight</string>
<string name="insightpump">Insight</string>
</resources>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="insightpump"
android:title="@string/insightpump">
</PreferenceCategory>
</PreferenceScreen>