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
No related branches found
No related tags found
No related merge requests found
...@@ -324,10 +324,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -324,10 +324,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
initialPoll, MedtronicCnlIntentService.POLL_PERIOD_MS, pending); initialPoll, MedtronicCnlIntentService.POLL_PERIOD_MS, pending);
} }
private void uploadCgmData() {
startService(mNightscoutUploadService);
}
private void stopCgmService() { private void stopCgmService() {
Log.i(TAG, "stopCgmService called"); Log.i(TAG, "stopCgmService called");
...@@ -452,7 +448,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -452,7 +448,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
.equalTo("pumpMac", MainActivity.activePumpMac) .equalTo("pumpMac", MainActivity.activePumpMac)
.findFirst(); .findFirst();
if (pump != null & pump.isValid()) { if (pump != null && pump.isValid()) {
mActivePump = pump; mActivePump = pump;
} }
} }
...@@ -485,19 +481,21 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -485,19 +481,21 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
TextView textViewTrend = (TextView) findViewById(R.id.textview_trend); TextView textViewTrend = (TextView) findViewById(R.id.textview_trend);
TextView textViewIOB = (TextView) findViewById(R.id.textview_iob); 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 // FIXME - grab the last item from the activePump's getPumpHistory
RealmResults<PumpStatusEvent> results = RealmResults<PumpStatusEvent> results =
mRealm.where(PumpStatusEvent.class) mRealm.where(PumpStatusEvent.class)
.findAllSorted("eventDate", Sort.ASCENDING); .findAllSorted("eventDate", Sort.ASCENDING);
PumpStatusEvent pumpRecord = null; if (pumpStatusData == null) {
if (results.size() > 0) {
pumpRecord = results.last();
}
if (pumpRecord == null) {
return; return;
} }
...@@ -509,33 +507,22 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -509,33 +507,22 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
String sgvString, units; String sgvString, units;
if (prefs.getBoolean("mmolxl", false)) { if (prefs.getBoolean("mmolxl", false)) {
float fBgValue = (float) pumpRecord.getSgv(); float fBgValue = (float) pumpStatusData.getSgv();
sgvString = df.format(fBgValue / 18.016f); sgvString = df.format(fBgValue / 18.016f);
units = "mmol/L"; units = "mmol/L";
Log.d(TAG, "mmolxl true --> " + sgvString); Log.d(TAG, "mmolxl true --> " + sgvString);
} else { } else {
sgvString = String.valueOf(pumpRecord.getSgv()); sgvString = String.valueOf(pumpStatusData.getSgv());
units = "mg/dL"; units = "mg/dL";
Log.d(TAG, "mmolxl false --> " + sgvString); Log.d(TAG, "mmolxl false --> " + sgvString);
} }
textViewBg.setText(sgvString); textViewBg.setText(sgvString);
textViewUnits.setText(units); textViewUnits.setText(units);
textViewBgTime.setText(DateUtils.getRelativeTimeSpanString(pumpRecord.getEventDate().getTime())); textViewBgTime.setText(DateUtils.getRelativeTimeSpanString(pumpStatusData.getEventDate().getTime()));
textViewTrend.setText(Html.fromHtml(renderTrendHtml(pumpRecord.getCgmTrend()))); textViewTrend.setText(Html.fromHtml(renderTrendHtml(pumpStatusData.getCgmTrend())));
textViewIOB.setText(String.format(Locale.getDefault(), "%.2f", pumpRecord.getActiveInsulin())); textViewIOB.setText(String.format(Locale.getDefault(), "%.2f", pumpStatusData.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();
*/
// TODO - waiting for MPAndroidCharts 3.0.0. This will fix: // TODO - waiting for MPAndroidCharts 3.0.0. This will fix:
// Date support // Date support
...@@ -580,20 +567,13 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -580,20 +567,13 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
PumpInfo pump = getActivePump(); PumpInfo pump = getActivePump();
if (pump != null & pump.isValid()) { if (pump != null && pump.isValid()) {
long pumpMac = pump.getPumpMac();
pumpStatusData = pump.getPumpHistory().last(); pumpStatusData = pump.getPumpHistory().last();
} else {
return;
} }
if (pumpStatusData != null) { long nextPoll = pumpStatusData.getEventDate().getTime() + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS + MedtronicCnlIntentService.POLL_PERIOD_MS;
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;
startCgmService(nextPoll); startCgmService(nextPoll);
Log.d(TAG, "Next Poll at " + new Date(nextPoll).toString()); Log.d(TAG, "Next Poll at " + new Date(nextPoll).toString());
...@@ -617,9 +597,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -617,9 +597,6 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
}); });
} }
// TODO - handle isOffline in NightscoutUploadIntentService?
uploadCgmData();
refreshDisplay(); refreshDisplay();
} }
} }
......
package info.nightscout.android.medtronic.service; package info.nightscout.android.medtronic.service;
import android.app.AlarmManager;
import android.app.IntentService; import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
...@@ -25,6 +27,7 @@ import info.nightscout.android.medtronic.message.UnexpectedMessageException; ...@@ -25,6 +27,7 @@ import info.nightscout.android.medtronic.message.UnexpectedMessageException;
import info.nightscout.android.model.medtronicNg.ContourNextLinkInfo; import info.nightscout.android.model.medtronicNg.ContourNextLinkInfo;
import info.nightscout.android.model.medtronicNg.PumpInfo; import info.nightscout.android.model.medtronicNg.PumpInfo;
import info.nightscout.android.model.medtronicNg.PumpStatusEvent; import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
import info.nightscout.android.upload.nightscout.NightscoutUploadReceiver;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
...@@ -267,10 +270,19 @@ public class MedtronicCnlIntentService extends IntentService { ...@@ -267,10 +270,19 @@ public class MedtronicCnlIntentService extends IntentService {
realm.close(); realm.close();
} }
// TODO - set status if offline or Nightscout not reachable
uploadToNightscout();
MedtronicCnlAlarmReceiver.completeWakefulIntent(intent); 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() { private boolean hasUsbHostFeature() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST); return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST);
} }
......
...@@ -87,7 +87,7 @@ public class PumpStatusEvent extends RealmObject { ...@@ -87,7 +87,7 @@ public class PumpStatusEvent extends RealmObject {
this.reservoirAmount = reservoirAmount; this.reservoirAmount = reservoirAmount;
} }
public boolean isRecentBolusWizard() { public boolean hasRecentBolusWizard() {
return recentBolusWizard; return recentBolusWizard;
} }
......
...@@ -85,6 +85,8 @@ public class NightscoutUploadIntentService extends IntentService { ...@@ -85,6 +85,8 @@ public class NightscoutUploadIntentService extends IntentService {
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "ERROR uploading data!!!!!", e); Log.e(TAG, "ERROR uploading data!!!!!", e);
} }
NightscoutUploadReceiver.completeWakefulIntent(intent);
} }
private void doRESTUpload(SharedPreferences prefs, RealmResults<PumpStatusEvent> records) { private void doRESTUpload(SharedPreferences prefs, RealmResults<PumpStatusEvent> records) {
...@@ -185,7 +187,7 @@ public class NightscoutUploadIntentService extends IntentService { ...@@ -185,7 +187,7 @@ public class NightscoutUploadIntentService extends IntentService {
try { try {
// FIXME - Change this to bulk uploads // FIXME - Change this to bulk uploads
populateV1APIEntry(json, record); populateSgvEntry(json, record);
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Unable to populate entry", e); Log.w(TAG, "Unable to populate entry", e);
continue; continue;
...@@ -264,7 +266,7 @@ public class NightscoutUploadIntentService extends IntentService { ...@@ -264,7 +266,7 @@ public class NightscoutUploadIntentService extends IntentService {
httpclient.execute(post, responseHandler); 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 // TODO replace with Retrofit/EntriesSerializer
json.put("sgv", pumpRecord.getSgv()); json.put("sgv", pumpRecord.getSgv());
json.put("direction", EntriesSerializer.getDirectionString(pumpRecord.getCgmTrend())); json.put("direction", EntriesSerializer.getDirectionString(pumpRecord.getCgmTrend()));
...@@ -272,7 +274,17 @@ public class NightscoutUploadIntentService extends IntentService { ...@@ -272,7 +274,17 @@ public class NightscoutUploadIntentService extends IntentService {
json.put("type", "sgv"); json.put("type", "sgv");
json.put("date", pumpRecord.getEventDate().getTime()); json.put("date", pumpRecord.getEventDate().getTime());
json.put("dateString", pumpRecord.getEventDate()); 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() { 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