BG viewer

This commit is contained in:
Milos Kozak 2017-12-15 01:03:31 +01:00
parent a05c1f947e
commit 8cb4921bab
13 changed files with 337 additions and 28 deletions

View file

@ -117,10 +117,8 @@ public class DataService extends IntentService {
handleNewDataFromDexcomG5(intent);
}
} else if (Intents.ACTION_NEW_SGV.equals(action)) {
// always handle SGV if NS-Client is the source
if (nsClientEnabled) {
handleNewDataFromNSClient(intent);
}
// always backfill SGV from NS
handleNewDataFromNSClient(intent);
// Objectives 0
ObjectivesPlugin.bgIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();

View file

@ -53,6 +53,7 @@ public class BgReading implements DataPointWithLabelInterface {
value = sgv.getMgdl();
raw = sgv.getFiltered() != null ? sgv.getFiltered() : value;
direction = sgv.getDirection();
_id = sgv.getId();
}
public Double valueToUnits(String units) {

View file

@ -367,6 +367,15 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return false;
}
public void update(BgReading bgReading) {
bgReading.date = roundDateToSec(bgReading.date);
try {
getDaoBgReadings().update(bgReading);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void scheduleBgChange() {
class PostRunnable implements Runnable {
public void run() {
@ -397,7 +406,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
QueryBuilder<BgReading, Long> queryBuilder = daoBgReadings.queryBuilder();
queryBuilder.orderBy("date", false);
queryBuilder.limit(1L);
queryBuilder.where().gt("value", 38);
queryBuilder.where().gt("value", 38).and().eq("isValid", true);
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgList = daoBgReadings.query(preparedQuery);
@ -435,7 +444,24 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
queryBuilder.orderBy("date", ascending);
Where where = queryBuilder.where();
where.ge("date", mills).and().gt("value", 38);
where.ge("date", mills).and().gt("value", 38).and().eq("isValid", true);
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
return new ArrayList<BgReading>();
}
public List<BgReading> getAllBgreadingsDataFromTime(long mills, boolean ascending) {
try {
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
List<BgReading> bgReadings;
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
queryBuilder.orderBy("date", ascending);
Where where = queryBuilder.where();
where.ge("date", mills);
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;

View file

@ -63,5 +63,6 @@ public class NSSgv {
public Long getMills () { return getLongOrNull("mills"); }
public String getDevice () { return getStringOrNull("device"); }
public String getDirection () { return getStringOrNull("direction"); }
public String getId () { return getStringOrNull("_id"); }
}

View file

@ -0,0 +1,172 @@
package info.nightscout.androidaps.plugins.SourceDexcomG5;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.crashlytics.android.Crashlytics;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.NSUpload;
/**
* Created by mike on 16.10.2017.
*/
public class BGSourceFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(BGSourceFragment.class);
RecyclerView recyclerView;
Profile profile;
final long MILLS_TO_THE_PAST = 12 * 60 * 60 * 1000L;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View view = inflater.inflate(R.layout.bgsource_fragment, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.bgsource_recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
long now = System.currentTimeMillis();
RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false));
recyclerView.setAdapter(adapter);
profile = ConfigBuilderPlugin.getActiveProfileInterface().getProfile().getDefaultProfile();
return view;
} catch (Exception e) {
Crashlytics.logException(e);
}
return null;
}
@Subscribe
@SuppressWarnings("unused")
public void onStatusEvent(final EventNewBG ev) {
updateGUI();
}
@Override
protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
long now = System.currentTimeMillis();
recyclerView.swapAdapter(new BGSourceFragment.RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true);
}
});
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.BgReadingsViewHolder> {
List<BgReading> bgReadings;
RecyclerViewAdapter(List<BgReading> bgReadings) {
this.bgReadings = bgReadings;
}
@Override
public BgReadingsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bgsource_item, viewGroup, false);
return new BgReadingsViewHolder(v);
}
@Override
public void onBindViewHolder(BgReadingsViewHolder holder, int position) {
BgReading bgReading = bgReadings.get(position);
holder.ns.setVisibility(NSUpload.isIdValid(bgReading._id) ? View.VISIBLE : View.GONE);
holder.invalid.setVisibility(!bgReading.isValid ? View.VISIBLE : View.GONE);
holder.date.setText(DateUtil.dateAndTimeString(bgReading.date));
holder.value.setText(bgReading.valueToUnitsToString(profile.getUnits()));
holder.direction.setText(bgReading.directionToSymbol());
holder.remove.setTag(bgReading);
}
@Override
public int getItemCount() {
return bgReadings.size();
}
class BgReadingsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView date;
TextView value;
TextView direction;
TextView invalid;
TextView ns;
TextView remove;
BgReadingsViewHolder(View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.bgsource_date);
value = (TextView) itemView.findViewById(R.id.bgsource_value);
direction = (TextView) itemView.findViewById(R.id.bgsource_direction);
invalid = (TextView) itemView.findViewById(R.id.invalid_sign);
ns = (TextView) itemView.findViewById(R.id.ns_sign);
remove = (TextView) itemView.findViewById(R.id.bgsource_remove);
remove.setOnClickListener(this);
remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
@Override
public void onClick(View v) {
final BgReading bgReading = (BgReading) v.getTag();
switch (v.getId()) {
case R.id.bgsource_remove:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(MainApp.sResources.getString(R.string.confirmation));
builder.setMessage(MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(bgReading.date) + "\n" + bgReading.valueToUnitsToString(profile.getUnits()));
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final String _id = bgReading._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeFoodFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
bgReading.isValid = false;
MainApp.getDbHelper().update(bgReading);
updateGUI();
}
});
builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null);
builder.show();
break;
}
}
}
}
}

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface {
private boolean fragmentEnabled = false;
private boolean fragmentVisible = false;
private static SourceDexcomG5Plugin plugin = null;
@ -23,7 +24,7 @@ public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return null;
return BGSourceFragment.class.getName();
}
@Override
@ -49,7 +50,7 @@ public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface {
@Override
public boolean isVisibleInTabs(int type) {
return false;
return Config.G5UPLOADER || type == BGSOURCE && fragmentVisible;
}
@Override
@ -59,7 +60,7 @@ public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface {
@Override
public boolean hasFragment() {
return false;
return true;
}
@Override
@ -74,7 +75,7 @@ public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface {
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == BGSOURCE) this.fragmentVisible = fragmentVisible;
}
@Override

View file

@ -4,12 +4,14 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.SourceDexcomG5.BGSourceFragment;
/**
* Created by mike on 05.08.2016.
*/
public class SourceGlimpPlugin implements PluginBase, BgSourceInterface {
private boolean fragmentEnabled = false;
private boolean fragmentVisible = false;
private static SourceGlimpPlugin plugin = null;
@ -21,7 +23,7 @@ public class SourceGlimpPlugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return null;
return BGSourceFragment.class.getName();
}
@Override
@ -47,7 +49,7 @@ public class SourceGlimpPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean isVisibleInTabs(int type) {
return false;
return type == BGSOURCE && fragmentVisible;
}
@Override
@ -57,7 +59,7 @@ public class SourceGlimpPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean hasFragment() {
return false;
return true;
}
@Override
@ -72,7 +74,7 @@ public class SourceGlimpPlugin implements PluginBase, BgSourceInterface {
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == BGSOURCE) this.fragmentVisible = fragmentVisible;
}
@Override

View file

@ -4,12 +4,14 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.SourceDexcomG5.BGSourceFragment;
/**
* Created by mike on 05.08.2016.
*/
public class SourceMM640gPlugin implements PluginBase, BgSourceInterface {
private boolean fragmentEnabled = false;
private boolean fragmentVisible = false;
private static SourceMM640gPlugin plugin = null;
@ -21,7 +23,7 @@ public class SourceMM640gPlugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return null;
return BGSourceFragment.class.getName();
}
@Override
@ -47,7 +49,7 @@ public class SourceMM640gPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean isVisibleInTabs(int type) {
return false;
return type == BGSOURCE && fragmentVisible;
}
@Override
@ -57,7 +59,7 @@ public class SourceMM640gPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean hasFragment() {
return false;
return true;
}
@Override
@ -72,7 +74,7 @@ public class SourceMM640gPlugin implements PluginBase, BgSourceInterface {
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == BGSOURCE) this.fragmentVisible = fragmentVisible;
}
@Override

View file

@ -5,12 +5,14 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.SourceDexcomG5.BGSourceFragment;
/**
* Created by mike on 05.08.2016.
*/
public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
private boolean fragmentEnabled = true;
private boolean fragmentVisible = false;
private static SourceNSClientPlugin plugin = null;
@ -22,7 +24,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return null;
return BGSourceFragment.class.getName();
}
@Override
@ -32,7 +34,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
@Override
public String getName() {
return MainApp.instance().getString(R.string.nsclient);
return MainApp.instance().getString(R.string.nsclientbg);
}
@Override
@ -49,7 +51,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean isVisibleInTabs(int type) {
return false;
return type == BGSOURCE && fragmentVisible;
}
@Override
@ -59,7 +61,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean hasFragment() {
return false;
return true;
}
@Override
@ -74,7 +76,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface {
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == BGSOURCE) this.fragmentVisible = fragmentVisible;
}
@Override

View file

@ -4,12 +4,16 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.SourceDexcomG5.BGSourceFragment;
/**
* Created by mike on 05.08.2016.
*/
public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
private boolean fragmentEnabled = false;
private boolean fragmentVisible = false;
private static SourceXdripPlugin plugin = null;
public static SourceXdripPlugin getPlugin() {
@ -20,11 +24,9 @@ public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
@Override
public String getFragmentClass() {
return null;
return BGSourceFragment.class.getName();
}
private boolean fragmentEnabled = false;
@Override
public int getType() {
return PluginBase.BGSOURCE;
@ -48,7 +50,7 @@ public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean isVisibleInTabs(int type) {
return false;
return type == BGSOURCE && fragmentVisible;
}
@Override
@ -58,7 +60,7 @@ public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
@Override
public boolean hasFragment() {
return false;
return true;
}
@Override
@ -73,6 +75,7 @@ public class SourceXdripPlugin implements PluginBase, BgSourceInterface {
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == BGSOURCE) this.fragmentVisible = fragmentVisible;
}
@Override

View file

@ -0,0 +1,22 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.nightscout.androidaps.plugins.SourceDexcomG5.BGSourceFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/bgsource_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>

View file

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/bgsource_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/cardColorBackground"
card_view:cardCornerRadius="6dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/bgsource_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="16:55"
android:textStyle="bold" />
<TextView
android:id="@+id/bgsource_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="@+id/bgsource_direction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="10dp"
android:text="-" />
<TextView
android:id="@+id/ns_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:text="NS"
android:textAlignment="viewEnd"
android:textColor="@color/colorSetTempButton" />
<TextView
android:id="@+id/invalid_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:text="@string/invalid"
android:textColor="@android:color/holo_red_light" />
<TextView
android:id="@+id/bgsource_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="5dp"
android:paddingStart="10dp"
android:text="@string/overview_quickwizard_item_remove_button"
android:textAlignment="viewEnd"
android:textColor="@android:color/holo_orange_light" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>

View file

@ -788,5 +788,6 @@
<string name="dexcomg5_xdripupload_title">Send BG data to xDrip+</string>
<string name="key_dexcomg5_xdripupload" translatable="false">dexcomg5_xdripupload</string>
<string name="dexcomg5_xdripupload_summary">In xDrip+ select 640g/Eversense data source</string>
<string name="nsclientbg">NSClient BG</string>
</resources>