DanaR Stats Activity

This commit is contained in:
viktoria 2017-01-02 18:04:45 +01:00
parent 262dae3ab3
commit e851d15aff
7 changed files with 392 additions and 0 deletions

View file

@ -36,6 +36,7 @@
<activity android:name=".AgreementActivity" />
<activity android:name=".plugins.DanaR.History.DanaRHistoryActivity" />
<activity android:name=".plugins.DanaRKorean.History.DanaRHistoryActivity" />
<activity android:name=".plugins.DanaR.History.DanaRStatsActivity" />
<activity android:name=".plugins.Overview.activities.QuickWizardListActivity">
<intent-filter>
<action android:name="info.nightscout.androidaps.plugins.Overview.activities.QuickWizardListActivity" />

View file

@ -29,6 +29,7 @@ import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRHistoryActivity;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRStatsActivity;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus;
import info.nightscout.utils.DateUtil;
@ -65,6 +66,8 @@ public class DanaRFragment extends Fragment implements FragmentBase {
TextView iobView;
Button viewProfileButton;
Button historyButton;
Button statsButton;
public DanaRFragment() {
if (sHandlerThread == null) {
@ -105,6 +108,8 @@ public class DanaRFragment extends Fragment implements FragmentBase {
iobView = (TextView) view.findViewById(R.id.danar_iob);
viewProfileButton = (Button) view.findViewById(R.id.danar_viewprofile);
historyButton = (Button) view.findViewById(R.id.danar_history);
statsButton = (Button) view.findViewById(R.id.danar_stats);
viewProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
@ -122,6 +127,13 @@ public class DanaRFragment extends Fragment implements FragmentBase {
}
});
statsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getContext(), DanaRStatsActivity.class));
}
});
btConnectionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View file

@ -0,0 +1,282 @@
package info.nightscout.androidaps.plugins.DanaR.History;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.DanaRHistoryRecord;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPercentageProfilePlugin;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus;
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.ToastUtils;
public class DanaRStatsActivity extends Activity {
private static Logger log = LoggerFactory.getLogger(DanaRStatsActivity.class);
private boolean mBounded;
private static ExecutionService mExecutionService;
private Handler mHandler;
private static HandlerThread mHandlerThread;
TextView statusView;
TextView mainView;
Button reloadButton;
LinearLayoutManager llm;
List<DanaRHistoryRecord> historyList = new ArrayList<>();
public DanaRStatsActivity() {
super();
mHandlerThread = new HandlerThread(DanaRStatsActivity.class.getSimpleName());
mHandlerThread.start();
this.mHandler = new Handler(mHandlerThread.getLooper());
}
@Override
public void onStart() {
super.onStart();
Intent intent = new Intent(this, ExecutionService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Override
protected void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
@Override
public void onStop() {
super.onStop();
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
log.debug("Service is disconnected");
mBounded = false;
mExecutionService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
log.debug("Service is connected");
mBounded = true;
ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service;
mExecutionService = mLocalBinder.getServiceInstance();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.danar_statsactivity);
statusView = (TextView) findViewById(R.id.danar_historystatus);
mainView = (TextView) findViewById(R.id.danar_stats_textview);
reloadButton = (Button) findViewById(R.id.danar_historyreload);
llm = new LinearLayoutManager(this);
statusView.setVisibility(View.GONE);
reloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mExecutionService.isConnected() || mExecutionService.isConnecting()) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.pumpbusy));
return;
}
mHandler.post(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
reloadButton.setVisibility(View.GONE);
statusView.setVisibility(View.VISIBLE);
}
});
mExecutionService.loadHistory(RecordTypes.RECORD_TYPE_DAILY);
loadDataFromDB(RecordTypes.RECORD_TYPE_DAILY);
runOnUiThread(new Runnable() {
@Override
public void run() {
reloadButton.setVisibility(View.VISIBLE);
statusView.setVisibility(View.GONE);
}
});
}
});
}
});
loadDataFromDB(RecordTypes.RECORD_TYPE_DAILY);
}
private void loadDataFromDB(byte type) {
try {
Dao<DanaRHistoryRecord, String> dao = MainApp.getDbHelper().getDaoDanaRHistory();
QueryBuilder<DanaRHistoryRecord, String> queryBuilder = dao.queryBuilder();
queryBuilder.orderBy("recordDate", false);
Where where = queryBuilder.where();
where.eq("recordCode", type);
queryBuilder.limit(10L);
PreparedQuery<DanaRHistoryRecord> preparedQuery = queryBuilder.prepare();
historyList = dao.query(preparedQuery);
} catch (SQLException e) {
e.printStackTrace();
historyList = new ArrayList<>();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mainView.setText("");
DateFormat df = new SimpleDateFormat("dd.MM. - ");
double magicNumber = 18d;
ProfileInterface pi = ConfigBuilderPlugin.getActiveProfile();
if (pi instanceof CircadianPercentageProfilePlugin){
magicNumber = ((CircadianPercentageProfilePlugin)pi).baseBasalSum();
}
magicNumber *=2;
int i = 0;
double sum = 0d;
double weighted03 = 0d;
double weighted05 = 0d;
double weighted07 = 0d;
String avg_string = "\n" + getString(R.string.danar_stats_avg);
String weighted_string = "\n\n" + getString(R.string.danar_stats_expweight);
for (DanaRHistoryRecord record: historyList) {
double tdd= record.getRecordDailyBolus() + record.getRecordDailyBasal();
mainView.append(df.format(new Date(record.getRecordDate())));
mainView.append(getString(R.string.danar_stats_basalrate) + DecimalFormatter.to2Decimal(record.getRecordDailyBasal()));
mainView.append(getString(R.string.danar_stats_bolus) + DecimalFormatter.to2Decimal(record.getRecordDailyBolus()));
mainView.append(getString(R.string.danar_stats_tdi) + DecimalFormatter.to2Decimal(tdd));
mainView.append(" " + Math.round(100*tdd/magicNumber) +"%");
mainView.append("\n");
sum = sum + tdd;
i++;
avg_string = avg_string + "\n " + i +": " + DecimalFormatter.to2Decimal(sum/i) + " " + Math.round(100*sum/i/magicNumber) +"%";
}
if (historyList.size()<3 || !(df.format(new Date(historyList.get(0).getRecordDate())).equals(df.format(new Date(System.currentTimeMillis() - 1000*60*60*24))))){
mainView.setBackgroundColor(Color.RED);
} else {
mainView.setBackgroundColor(Color.TRANSPARENT);
}
Collections.reverse(historyList);
i = 0;
for (DanaRHistoryRecord record: historyList) {
double tdd= record.getRecordDailyBolus() + record.getRecordDailyBasal();
if(i == 0 ) {
weighted03 = tdd;
weighted05 = tdd;
weighted07 = tdd;
} else {
weighted07 = (weighted07*0.3 + tdd*0.7);
weighted05 = (weighted05*0.5 + tdd*0.5);
weighted03 = (weighted03*0.7 + tdd*0.3);
}
i++;
}
weighted_string = weighted_string + "\n 0.3 " + DecimalFormatter.to2Decimal(weighted03) + " " + Math.round(100*weighted03/magicNumber) +"%";
weighted_string = weighted_string + "\n 0.5 " + DecimalFormatter.to2Decimal(weighted05) + " " + Math.round(100*weighted05/magicNumber) +"%";
weighted_string = weighted_string + "\n 0.7 " + DecimalFormatter.to2Decimal(weighted07) + " " + Math.round(100*weighted07/magicNumber) +"%";
mainView.append(avg_string);
mainView.append(weighted_string);
}
});
}
@Subscribe
public void onStatusEvent(final EventDanaRSyncStatus s) {
log.debug("EventDanaRSyncStatus: " + s.message);
runOnUiThread(
new Runnable() {
@Override
public void run() {
statusView.setText(s.message);
}
});
}
@Subscribe
public void onStatusEvent(final EventDanaRConnectionStatus c) {
runOnUiThread(
new Runnable() {
@Override
public void run() {
if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) {
statusView.setText(String.format(getString(R.string.danar_history_connectingfor), c.sSecondsElapsed));
log.debug("EventDanaRConnectionStatus: " + "Connecting for " + c.sSecondsElapsed + "s");
} else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) {
statusView.setText(MainApp.sResources.getString(R.string.connected));
log.debug("EventDanaRConnectionStatus: Connected");
} else {
statusView.setText(MainApp.sResources.getString(R.string.disconnected));
log.debug("EventDanaRConnectionStatus: Disconnected");
}
}
}
);
}
}

View file

@ -28,6 +28,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog;
import info.nightscout.androidaps.plugins.DanaR.History.DanaRStatsActivity;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus;
import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus;
import info.nightscout.androidaps.plugins.DanaRKorean.History.DanaRHistoryActivity;
@ -62,6 +63,8 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase {
TextView iobView;
Button viewProfileButton;
Button historyButton;
Button statsButton;
public DanaRKoreanFragment() {
if (sHandlerThread == null) {
@ -102,6 +105,8 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase {
iobView = (TextView) view.findViewById(R.id.danar_iob);
viewProfileButton = (Button) view.findViewById(R.id.danar_viewprofile);
historyButton = (Button) view.findViewById(R.id.danar_history);
statsButton = (Button) view.findViewById(R.id.danar_stats);
viewProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
@ -119,6 +124,13 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase {
}
});
statsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getContext(), DanaRStatsActivity.class));
}
});
btConnectionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View file

@ -269,6 +269,12 @@
android:text="@string/danar_history"
android:id="@+id/danar_history"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/danar_stats"
android:id="@+id/danar_stats"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

View file

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".plugins.DanaR.History.DanaRHistoryActivity">
<LinearLayout
android:id="@+id/danar_historytop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/loopmodeborder"
android:gravity="center_horizontal"
android:textColor="@color/cardColorBackground"
android:text="@string/danar_stats" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/danar_historytop"
android:layout_above="@+id/danar_historybuttons"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/danar_historystatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/danar_stats_textview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Da kommt was hin." />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/danar_historybuttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/danar_historyreload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/danar_historyreload" />
</LinearLayout>
</RelativeLayout>

View file

@ -398,4 +398,10 @@
<string name="minago">%dmin ago</string>
<string name="sms_minago">%dmin ago</string>
<string name="localprofile">Local Profile</string>
<string name="danar_stats">DanaR Stats</string>
<string name="danar_stats_avg">average</string>
<string name="danar_stats_expweight">exponentially weighted</string>
<string name="danar_stats_basalrate">Basalrate</string>
<string name="danar_stats_bolus">Bolus</string>
<string name="danar_stats_tdi">total daily insulin</string>
</resources>