Skip to content
Snippets Groups Projects
Commit a8793fa2 authored by Lennart Goedhart's avatar Lennart Goedhart
Browse files

- Make the NightscoutUploadIntentService a wakeful service

- Move the Nightscout upload to the MedtronicCnlIntentService, rather than firing from the MainActivity
parent 1c230ad0
Branches
Tags
No related merge requests found
......@@ -324,10 +324,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
initialPoll, MedtronicCnlIntentService.POLL_PERIOD_MS, pending);
}
private void uploadCgmData() {
startService(mNightscoutUploadService);
}
private void stopCgmService() {
Log.i(TAG, "stopCgmService called");
......@@ -452,7 +448,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
.equalTo("pumpMac", MainActivity.activePumpMac)
.findFirst();
if (pump != null & pump.isValid()) {
if (pump != null && pump.isValid()) {
mActivePump = pump;
}
}
......@@ -485,19 +481,21 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
TextView textViewTrend = (TextView) findViewById(R.id.textview_trend);
TextView textViewIOB = (TextView) findViewById(R.id.textview_iob);
// Get the most recently written CGM record.
// Get the most recently written CGM record for the active pump.
PumpStatusEvent pumpStatusData = null;
PumpInfo pump = getActivePump();
if (pump != null & pump.isValid()) {
pumpStatusData = pump.getPumpHistory().last();
}
// FIXME - grab the last item from the activePump's getPumpHistory
RealmResults<PumpStatusEvent> results =
mRealm.where(PumpStatusEvent.class)
.findAllSorted("eventDate", Sort.ASCENDING);
PumpStatusEvent pumpRecord = null;
if (results.size() > 0) {
pumpRecord = results.last();
}
if (pumpRecord == null) {
if (pumpStatusData == null) {
return;
}
......@@ -509,33 +507,22 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
String sgvString, units;
if (prefs.getBoolean("mmolxl", false)) {
float fBgValue = (float) pumpRecord.getSgv();
float fBgValue = (float) pumpStatusData.getSgv();
sgvString = df.format(fBgValue / 18.016f);
units = "mmol/L";
Log.d(TAG, "mmolxl true --> " + sgvString);
} else {
sgvString = String.valueOf(pumpRecord.getSgv());
sgvString = String.valueOf(pumpStatusData.getSgv());
units = "mg/dL";
Log.d(TAG, "mmolxl false --> " + sgvString);
}
textViewBg.setText(sgvString);
textViewUnits.setText(units);
textViewBgTime.setText(DateUtils.getRelativeTimeSpanString(pumpRecord.getEventDate().getTime()));
textViewTrend.setText(Html.fromHtml(renderTrendHtml(pumpRecord.getCgmTrend())));
textViewIOB.setText(String.format(Locale.getDefault(), "%.2f", pumpRecord.getActiveInsulin()));
/*
// Open Realm because we're in a different thread
Realm realm = Realm.getDefaultInstance();
if (MainActivity.mActivePump != null && MainActivity.mActivePump.isValid()) {
PumpInfo pump = MainActivity.mActivePump;
long pumpMac = pump.getPumpMac();
CgmStatusEvent cgmData = MainActivity.mActivePump.getCgmHistory().last();
}
realm.close();
*/
textViewBgTime.setText(DateUtils.getRelativeTimeSpanString(pumpStatusData.getEventDate().getTime()));
textViewTrend.setText(Html.fromHtml(renderTrendHtml(pumpStatusData.getCgmTrend())));
textViewIOB.setText(String.format(Locale.getDefault(), "%.2f", pumpStatusData.getActiveInsulin()));
// TODO - waiting for MPAndroidCharts 3.0.0. This will fix:
// Date support
......@@ -580,20 +567,13 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
PumpInfo pump = getActivePump();
if (pump != null & pump.isValid()) {
long pumpMac = pump.getPumpMac();
if (pump != null && pump.isValid()) {
pumpStatusData = pump.getPumpHistory().last();
} else {
return;
}
if (pumpStatusData != null) {
Log.d(TAG, "It's working yo");
}
PumpStatusEvent record = mRealm.where(PumpStatusEvent.class)
.findAll()
.last();
long nextPoll = record.getEventDate().getTime() + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS + MedtronicCnlIntentService.POLL_PERIOD_MS;
long nextPoll = pumpStatusData.getEventDate().getTime() + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS + MedtronicCnlIntentService.POLL_PERIOD_MS;
startCgmService(nextPoll);
Log.d(TAG, "Next Poll at " + new Date(nextPoll).toString());
......@@ -617,9 +597,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
});
}
// TODO - handle isOffline in NightscoutUploadIntentService?
uploadCgmData();
refreshDisplay();
}
}
......
package info.nightscout.android.medtronic.service;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
......@@ -25,6 +27,7 @@ import info.nightscout.android.medtronic.message.UnexpectedMessageException;
import info.nightscout.android.model.medtronicNg.ContourNextLinkInfo;
import info.nightscout.android.model.medtronicNg.PumpInfo;
import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
import info.nightscout.android.upload.nightscout.NightscoutUploadReceiver;
import io.realm.Realm;
import io.realm.RealmResults;
......@@ -267,10 +270,19 @@ public class MedtronicCnlIntentService extends IntentService {
realm.close();
}
// TODO - set status if offline or Nightscout not reachable
uploadToNightscout();
MedtronicCnlAlarmReceiver.completeWakefulIntent(intent);
}
}
private void uploadToNightscout() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent receiverIntent = new Intent(this, NightscoutUploadReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiverIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000L, pendingIntent);
}
private boolean hasUsbHostFeature() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST);
}
......
......@@ -87,7 +87,7 @@ public class PumpStatusEvent extends RealmObject {
this.reservoirAmount = reservoirAmount;
}
public boolean isRecentBolusWizard() {
public boolean hasRecentBolusWizard() {
return recentBolusWizard;
}
......
......@@ -85,6 +85,8 @@ public class NightscoutUploadIntentService extends IntentService {
} catch (Exception e) {
Log.e(TAG, "ERROR uploading data!!!!!", e);
}
NightscoutUploadReceiver.completeWakefulIntent(intent);
}
private void doRESTUpload(SharedPreferences prefs, RealmResults<PumpStatusEvent> records) {
......@@ -185,7 +187,7 @@ public class NightscoutUploadIntentService extends IntentService {
try {
// FIXME - Change this to bulk uploads
populateV1APIEntry(json, record);
populateSgvEntry(json, record);
} catch (Exception e) {
Log.w(TAG, "Unable to populate entry", e);
continue;
......@@ -264,7 +266,7 @@ public class NightscoutUploadIntentService extends IntentService {
httpclient.execute(post, responseHandler);
}
private void populateV1APIEntry(JSONObject json, PumpStatusEvent pumpRecord) throws Exception {
private void populateSgvEntry(JSONObject json, PumpStatusEvent pumpRecord) throws Exception {
// TODO replace with Retrofit/EntriesSerializer
json.put("sgv", pumpRecord.getSgv());
json.put("direction", EntriesSerializer.getDirectionString(pumpRecord.getCgmTrend()));
......@@ -272,7 +274,17 @@ public class NightscoutUploadIntentService extends IntentService {
json.put("type", "sgv");
json.put("date", pumpRecord.getEventDate().getTime());
json.put("dateString", pumpRecord.getEventDate());
}
private void populateMbgEntry(JSONObject json, PumpStatusEvent pumpRecord) throws Exception {
if(pumpRecord.hasRecentBolusWizard()) {
// TODO replace with Retrofit/EntriesSerializer
json.put("type", "mbg");
json.put("mbg", pumpRecord.getBolusWizardBGL());
json.put("device", pumpRecord.getDeviceName());
json.put("date", pumpRecord.getEventDate().getTime());
json.put("dateString", pumpRecord.getEventDate());
}
}
private boolean isOnline() {
......
package info.nightscout.android.upload.nightscout;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
/**
* Created by lgoedhart on 14/07/2016.
*/
public class NightscoutUploadReceiver extends WakefulBroadcastReceiver {
private static final String TAG = NightscoutUploadReceiver.class.getSimpleName();
@Override
public void onReceive(final Context context, Intent intent) {
// Start the IntentService
Log.d(TAG, "Received broadcast message");
Intent service = new Intent(context, NightscoutUploadIntentService.class);
startWakefulService(context, service);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment