Skip to content

Commit

Permalink
Merge pull request #181 from nightscout/dev-bletransport-update
Browse files Browse the repository at this point in the history
Dev bletransport update and other fixes
  • Loading branch information
jasoncalabrese committed May 22, 2015
2 parents c29cff4 + ee8f21f commit fca47c7
Show file tree
Hide file tree
Showing 27 changed files with 942 additions and 294 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
minSdkVersion 18
targetSdkVersion 18
versionCode 22
versionName "0.2.0-alpha8"
versionName "0.2.0-alpha10"
buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
buildConfigField "String", "BUILD_TIME", "\"${buildTime}\""
buildConfigField "String", "VERSION_CODENAME", "\"${codeName}\""
Expand Down
133 changes: 98 additions & 35 deletions app/src/main/java/com/nightscout/android/CollectorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
import com.nightscout.android.preferences.AndroidPreferences;
import com.nightscout.core.BusProvider;
import com.nightscout.core.dexcom.CRCFailError;
import com.nightscout.core.dexcom.records.CalRecord;
import com.nightscout.core.dexcom.records.EGVRecord;
import com.nightscout.core.dexcom.records.SensorRecord;
import com.nightscout.core.drivers.AbstractDevice;
import com.nightscout.core.drivers.AbstractUploaderDevice;
import com.nightscout.core.drivers.DeviceConnectionStatus;
import com.nightscout.core.drivers.DeviceState;
import com.nightscout.core.drivers.DeviceTransport;
import com.nightscout.core.drivers.DexcomG4;
import com.nightscout.core.drivers.G4ConnectionState;
import com.nightscout.core.drivers.SupportedDevices;
import com.nightscout.core.events.EventReporter;
import com.nightscout.core.events.EventSeverity;
import com.nightscout.core.events.EventType;
import com.nightscout.core.model.G4Download;
import com.nightscout.core.utils.IsigReading;
import com.squareup.otto.Bus;

import org.joda.time.DateTime;
Expand All @@ -56,7 +59,6 @@ public class CollectorService extends Service {
public static final String NUM_PAGES = "NUM_PAGES";
public static final String STD_SYNC = "STD_SYNC";
public static final String GAP_SYNC = "GAP_SYNC";
public static final String NON_SYNC = "NON_SYNC";
public static final String SYNC_TYPE = "SYNC_TYPE";

private EventReporter reporter;
Expand Down Expand Up @@ -106,7 +108,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
e.printStackTrace();
}
}
setDriver();
// setDriver();
} else {
Log.d(TAG, "Meh... something uninteresting changed");
}
Expand Down Expand Up @@ -144,20 +146,20 @@ private void setDriver() {
DexcomG4.PROTOCOL);
}
}
try {
if (driver == null) {
Log.d("Last hope", "Driver is NULL?!");
return;
}
if ((deviceType == SupportedDevices.DEXCOM_G4 && driver.isConnected())
|| deviceType == SupportedDevices.DEXCOM_G4_SHARE2) {
driver.open();
}
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
//TODO record this in the event log later
// status = DownloadStatus.IO_ERROR;
}
// try {
// if (driver == null) {
// Log.d(TAG, "Driver is NULL?!");
// return;
// }
// if ((deviceType == SupportedDevices.DEXCOM_G4 && driver.isConnected())
// || deviceType == SupportedDevices.DEXCOM_G4_SHARE2) {
// driver.open();
// }
// } catch (IOException e) {
// Log.e(TAG, "IOException: " + e.getMessage());
// //TODO record this in the event log later
//// status = DownloadStatus.IO_ERROR;
// }
}

@Override
Expand All @@ -180,28 +182,60 @@ public void onDestroy() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting service");
if (device == null) {
Log.d(TAG, "Device is null!");
// if (device == null) {
// Log.d(TAG, "Device is null!");
// ACRA.getErrorReporter().handleException(null);
return START_STICKY;
}
// return START_STICKY;
// }
if (intent == null) {
Log.d(TAG, "Intent is null!");
// ACRA.getErrorReporter().handleException(null);
return START_STICKY;
}
if (device.isConnected() || intent.getBooleanExtra("requested", false)) {
// if (device.isConnected()) {
int numOfPages = intent.getIntExtra(NUM_PAGES, 2);
int syncType = intent.getStringExtra(SYNC_TYPE).equals(STD_SYNC) ? 0 : 1;
new AsyncDownload().execute(numOfPages, syncType);
}
// }
return super.onStartCommand(intent, flags, startId);
}

private class AsyncDownload extends AsyncTask<Integer, Integer, G4Download> {

@Override
protected G4Download doInBackground(Integer... params) {
if (driver == null) {
Log.w(TAG, "Driver is null");
return null;
}

long nextUploadTime = Minutes.minutes(2).toStandardDuration().getMillis();

SupportedDevices deviceType = preferences.getDeviceType();

if (deviceType == SupportedDevices.DEXCOM_G4) {
try {
driver.open();
Log.i(TAG, "DEXCOM_G4 was opened for download");
} catch (IOException e) {
Log.e(TAG, "Unable to open DEXCOM_G4, will keep trying", e);
setNextPoll(nextUploadTime);
return null;
}
} else if (!device.isConnected()) {
Log.e(TAG, "Device is not connected");
try {
driver.open();
Log.i(TAG, "Device was opened for download");
} catch (IOException e) {
Log.e(TAG, "Unable to open device, will keep trying", e);
setNextPoll(nextUploadTime);
return null;
}
} else {
Log.e(TAG, "Device is connected");
}

int numOfPages = params[0];
String syncType = params[1] == 0 ? STD_SYNC : GAP_SYNC;
((DexcomG4) device).setNumOfPages(numOfPages);
Expand All @@ -212,6 +246,35 @@ protected G4Download doInBackground(Integer... params) {

try {
download = (G4Download) device.download();
if (download == null || download.download_timestamp == null || download.receiver_system_time_sec == null) {
Log.e(TAG, "Bad download, will try again");
setNextPoll(nextUploadTime);
return null;
}
SensorRecord lastSensorRecord = null;
CalRecord lastCalRecord = null;
EGVRecord lastEgvRecord = null;
long downloadEpoch = DateTime.parse(download.download_timestamp).getMillis();
long rcvrTime = download.receiver_system_time_sec;
if (download.sensor.size() > 0) {
lastSensorRecord = new SensorRecord(download.sensor.get(download.sensor.size() - 1), rcvrTime, downloadEpoch);
} else {
Log.e(TAG, "No sensor records to calculate EGV using isig");
}
if (download.cal.size() > 0) {
lastCalRecord = new CalRecord(download.cal.get(download.cal.size() - 1), rcvrTime, downloadEpoch);
} else {
Log.e(TAG, "No cal records to calculate EGV using isig");
}
if (download.sgv.size() > 0) {
lastEgvRecord = new EGVRecord(download.sgv.get(download.sgv.size() - 1), rcvrTime, downloadEpoch);
} else {
Log.e(TAG, "No egv records to calculate EGV using isig");
}
if (lastSensorRecord != null && lastCalRecord != null && lastEgvRecord != null) {
IsigReading isigReading = new IsigReading(lastSensorRecord, lastCalRecord, lastEgvRecord);
Log.e(TAG, "Calculated EGV using isig values: " + isigReading.asMgdl());
}

if (download != null) {
bus.post(download);
Expand Down Expand Up @@ -260,17 +323,16 @@ protected G4Download doInBackground(Integer... params) {
}
wl.release();

if (syncType.equals(GAP_SYNC)) {
return download;
}
long nextUploadTime = Minutes.minutes(2).toStandardDuration().getMillis();
if (download != null && download.sgv.size() > 0) {
long rcvrTime = download.receiver_system_time_sec;
long refTime = DateTime.parse(download.download_timestamp).getMillis();
EGVRecord lastEgvRecord = new EGVRecord(download.sgv.get(download.sgv.size() - 1), download.receiver_system_time_sec, refTime);
nextUploadTime = Duration.standardSeconds(Minutes.minutes(5).toStandardSeconds().getSeconds() - ((rcvrTime - lastEgvRecord.getRawSystemTimeSeconds()) % Minutes.minutes(5).toStandardSeconds().getSeconds())).getMillis();
if (!syncType.equals(GAP_SYNC)) {
if (download != null && download.sgv.size() > 0) {
long rcvrTime = download.receiver_system_time_sec;
long refTime = DateTime.parse(download.download_timestamp).getMillis();
EGVRecord lastEgvRecord = new EGVRecord(download.sgv.get(download.sgv.size() - 1), download.receiver_system_time_sec, refTime);
nextUploadTime = Duration.standardSeconds(Minutes.minutes(5).toStandardSeconds().getSeconds() - ((rcvrTime - lastEgvRecord.getRawSystemTimeSeconds()) % Minutes.minutes(5).toStandardSeconds().getSeconds())).getMillis();
}
setNextPoll(nextUploadTime);
}
setNextPoll(nextUploadTime);

return download;
}
}
Expand All @@ -282,14 +344,15 @@ public IBinder onBind(Intent intent) {

public DeviceConnectionStatus getDeviceConnectionStatus() {
if (device == null) {
return new DeviceConnectionStatus(preferences.getDeviceType(), DeviceState.DISCONNECTED);
return new DeviceConnectionStatus(preferences.getDeviceType(), G4ConnectionState.CLOSED);
}
Log.d(TAG, "From service: " + device.getDeviceConnectionStatus().deviceType.name());
return device.getDeviceConnectionStatus();
}

public long getNextPoll() {
return nextPoll - System.currentTimeMillis();
// return nextPoll - System.currentTimeMillis();
return 1000 * 60 * 5 - (nextPoll - System.currentTimeMillis());
}


Expand Down
46 changes: 31 additions & 15 deletions app/src/main/java/com/nightscout/android/ProcessorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public void onCreate() {
}
bus.register(this);
uploader = new Uploader(getApplicationContext(), preferences);
setupMqtt();
new Thread(new Runnable() {
@Override
public void run() {
setupMqtt();
}
}).start();
tracker = ((Nightscout) getApplicationContext()).getTracker();
reportUploadMethods();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Expand All @@ -98,12 +103,17 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}
} else if (key.equals(getApplicationContext().getString(R.string.mqtt_enable))) {
// Assume that MQTT already has the information needed and set it up.
setupMqtt();
if (mqttManager != null && mqttManager.isConnected()) {
mqttManager.setShouldReconnect(false);
mqttManager.disconnect();
mqttManager.setShouldReconnect(true);
}
new Thread(new Runnable() {
@Override
public void run() {
setupMqtt();
if (mqttManager != null && mqttManager.isConnected()) {
mqttManager.setShouldReconnect(false);
mqttManager.disconnect();
mqttManager.setShouldReconnect(true);
}
}
}).start();
} else {
Log.d(TAG, "Meh... something uninteresting changed");
}
Expand All @@ -112,13 +122,19 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
// Assume that MQTT is already enabled and the MQTT endpoint and credentials are just changing
prefKeys = new String[]{getApplicationContext().getString(R.string.mqtt_endpoint), getApplicationContext().getString(R.string.mqtt_pass), getApplicationContext().getString(R.string.mqtt_user)};
if (preferences.isMqttEnabled() && Arrays.asList(prefKeys).contains(key)) {
setupMqtt();
Log.d(TAG, "MQTT change detected. Restarting MQTT");
if (mqttManager != null && mqttManager.isConnected()) {
mqttManager.setShouldReconnect(false);
mqttManager.disconnect();
mqttManager.setShouldReconnect(true);
}

new Thread(new Runnable() {
@Override
public void run() {
setupMqtt();
Log.d(TAG, "MQTT change detected. Restarting MQTT");
if (mqttManager != null && mqttManager.isConnected()) {
mqttManager.setShouldReconnect(false);
mqttManager.disconnect();
mqttManager.setShouldReconnect(true);
}
}
}).start();
}
if (key.equals(getString(R.string.preferred_units)) && preferences.isCampingModeEnabled()) {
pebble.config(preferences.getPwdName(), preferences.getPreferredUnits(), getApplicationContext());
Expand Down Expand Up @@ -191,7 +207,7 @@ public MqttEventMgr setupMqttConnection(String user, String pass, String endpoin
}
try {
MqttConnectOptions mqttOptions = new MqttConnectOptions();
mqttOptions.setCleanSession(false);
mqttOptions.setCleanSession(true);
mqttOptions.setKeepAliveInterval(150000);
mqttOptions.setUserName(user);
mqttOptions.setPassword(pass.toCharArray());
Expand Down
Loading

0 comments on commit fca47c7

Please sign in to comment.