Skip to content

Commit

Permalink
-Added a special notification for monitor errors (MQTT disconnect, mo…
Browse files Browse the repository at this point in the history
…ngo disconnect). I'm thinking this will need to be moved into the Android Notification and make Android notification monitor a required monitor.

-General code cleanup
-Stabilization of the MQTT Manager (specific conditions caused it to loose the ping timer)
-Added some event correlation in the analyzer to remove duplicate/redundant/known/inferred messages
-Fixed a bug that caused the incorrect last download for a monitor to be retrieved
-Added more useful messages in the notification for each device
-Removed Special value as a download status - didn't make sense. Moved the detection of special value to the G4 analyzer
-Added ACRA code to report IO errors to Dexcom G4's
-Removed unnecessary "isNew" attribute from EGV records
  • Loading branch information
ktind committed Sep 8, 2014
1 parent 8629045 commit 2124dc3
Show file tree
Hide file tree
Showing 31 changed files with 575 additions and 392 deletions.
4 changes: 2 additions & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.ktind.cgm.bgscout"
minSdkVersion 16
targetSdkVersion 20
versionCode 6
versionName "1.0.6"
versionCode 8
versionName "0.0.8"
}
buildTypes {
release {
Expand Down
59 changes: 11 additions & 48 deletions mobile/src/main/java/com/ktind/cgm/bgscout/AbstractDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import android.database.Cursor;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;

import com.google.android.gms.analytics.GoogleAnalytics;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -73,10 +74,8 @@ public AbstractDevice(String name, int deviceID, Context context, String driver)
this.driver=driver;
this.setDeviceID(deviceID);
this.setContext(context);
// this.setHandler(mH);
this.deviceIDStr = "device_" + String.valueOf(getDeviceID());
sharedPref=PreferenceManager.getDefaultSharedPreferences(context);
// monitors=new ArrayList<AbstractMonitor>();
state=State.STOPPED;
String contactDataUri=sharedPref.getString(deviceIDStr+"_contact_data_uri",Uri.EMPTY.toString());

Expand Down Expand Up @@ -144,16 +143,6 @@ public void start(){
context.registerReceiver(uiQuery,intentFilter);
}


// public void mainloop(){
// while(started){
//
// }
// }
// public void setHandler(Handler mH){
// this.mHandler=mH;
// }

public float getUploaderBattery(){
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
Expand Down Expand Up @@ -212,14 +201,8 @@ public void fireMonitors(DownloadObject dl) {
// FIXME why is monitors set to null here sometimes?
if (monitors==null)
return;
for (AbstractMonitor monitor:monitors){
// try {
// monitor.process(getLastDownloadObject());
monitor.process(dl);
// } catch (DeviceException e) {
// Log.w(TAG,e.getMessage());
// }
}
for (AbstractMonitor monitor:monitors)
monitor.process(dl);
stats.stopMonitorTimer();
}

Expand Down Expand Up @@ -308,20 +291,12 @@ public Trend getLastTrend() throws NoDataException {

protected void onDownload(DownloadObject dl){
sendToUI();
// try {
if (Looper.getMainLooper().getThread()==Thread.currentThread())
Log.d(TAG,"ON THE MAIN Thread!!!");
else
Log.d(TAG, "Not on the MAIN Thread ("+Thread.currentThread().getName()+"/"+Thread.currentThread().getState()+")");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(deviceIDStr +"_last_"+driver, dl.getLastReadingDate().getTime());
editor.apply();
// } catch (NoDataException e) {
// Log.i(TAG,"No data on download");
//// e.printStackTrace();
// }
// SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
// SharedPreferences.Editor editor = sharedPref.edit();
// editor.putLong(deviceIDStr +"_last_"+driver, dl.getLastReadingDate().getTime());
// editor.apply();
fireMonitors(dl);
GoogleAnalytics.getInstance(context.getApplicationContext()).dispatchLocalHits();
}

public void sendToUI(){
Expand All @@ -334,17 +309,13 @@ public void sendToUI(){
} catch (NoDataException e) {
downloadObject=new DownloadObject();
Log.e(TAG,"Sending empty DownloadObject",e);
// e.printStackTrace();
} finally {
if (downloadObject!=null) {
downloadObject.setDeviceID(deviceIDStr);
downloadObject.setDeviceName(getName());
// downloadObject.buildMessage();
uiIntent.putExtra("download", downloadObject);
}
}
// Log.d(TAG,"Sending broadcast to UI: "+uiIntent.getExtras().getString("download",""));

context.sendBroadcast(uiIntent);
}

Expand All @@ -354,8 +325,8 @@ public void setRemote(boolean remote) {

@Override
public void stop() {
if (state==State.STOPPED) {
Log.w(TAG,getName()+"/"+getDeviceType()+" has already been stopped");
if (state==State.STOPPED || state==State.STOPPING) {
Log.w(TAG,getName()+"/"+getDeviceType()+" has already been stopped or is being stopped");
return;
}
state=State.STOPPING;
Expand All @@ -365,13 +336,6 @@ public void stop() {
started=false;
}

// public enum State{
// STARTING,
// STARTED,
// STOPPING,
// STOPPED
// }

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -390,7 +354,6 @@ private String getPhone(Uri dataUri){
String id=dataUri.getLastPathSegment();
Log.d(TAG,"id="+id);
Log.d(TAG,"URI="+dataUri);
// TODO Limit fields returned to specific field that we want? Phone?
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data._ID + " = ?", new String[]{id}, null);
int numIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.d(TAG, "cursor.getCount(): " + cursor.getCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

package com.ktind.cgm.bgscout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
* Created by klee24 on 8/28/14.
*/
Expand All @@ -51,4 +47,6 @@ public AnalyzedDownload analyze() {
return this.downloadObject;
}

abstract protected void correlateMessages();

}
21 changes: 3 additions & 18 deletions mobile/src/main/java/com/ktind/cgm/bgscout/AbstractMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ abstract public class AbstractMonitor implements MonitorInterface {
protected String name;
protected boolean allowVirtual=false;
protected String monitorType="generic";
// protected int highThreshold=180;
// protected int lowThreshold=60;
protected int deviceID;
protected String deviceIDStr;
protected Context context;
protected SharedPreferences sharedPref;
protected long lastSuccessDate;
protected State state;
// protected EGVLimits egvLimits;

public AbstractMonitor(String n,int devID,Context context, String monitorName){
this.setName(n);
Expand All @@ -61,7 +58,7 @@ public AbstractMonitor(String n,int devID,Context context, String monitorName){
this.monitorType=monitorName;
sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
// Default to the last 2.5 hours as the "last successful download"
this.lastSuccessDate=sharedPref.getLong(deviceIDStr+monitorType,new Date().getTime()-900000L);
this.lastSuccessDate=sharedPref.getLong("last_"+deviceIDStr+"_"+monitorType,new Date().getTime()-900000L);
Log.d(TAG,"Setting lastSuccessDate to: "+new Date(lastSuccessDate));
}

Expand Down Expand Up @@ -108,7 +105,7 @@ final public void process(DownloadObject d) {
long startTime=System.currentTimeMillis();
Log.d(TAG,"Monitor "+name+" has fired for "+monitorType);
if (isAllowVirtual() || ! d.isRemoteDevice()){
lastSuccessDate=sharedPref.getLong(deviceIDStr+monitorType,new Date().getTime()-900000L);
lastSuccessDate=sharedPref.getLong("last_"+deviceIDStr+"_"+monitorType,new Date().getTime()-900000L);
Log.d(TAG, "Trimming data for monitor "+name+"/"+monitorType);
final DownloadObject dl=new DownloadObject(d);
dl.setEgvRecords(trimReadingsAfter(getlastSuccessDate(), d.getEgvArrayListRecords()));
Expand Down Expand Up @@ -139,7 +136,7 @@ public long getlastSuccessDate(){
// It is up to each implementation to save the last successful date.
public void savelastSuccessDate(long date){
SharedPreferences.Editor editor=sharedPref.edit();
editor.putLong(deviceIDStr+monitorType,date);
editor.putLong("last_"+deviceIDStr+"_"+monitorType,date);
editor.apply();
}

Expand Down Expand Up @@ -173,16 +170,4 @@ public ArrayList<EGVRecord> trimReadingsAfter(Long afterDateLong, ArrayList<EGVR
Log.d(TAG,"Size after trim: "+recs.size()+" vs original "+egvRecords.size());
return recs;
}



// public void setHighThreshold(int highThreshold) {
// Log.v(TAG,"Setting high threshold to "+highThreshold);
// this.highThreshold = highThreshold;
// }
//
// public void setLowThreshold(int lowThreshold) {
// Log.v(TAG,"Setting low threshold to "+lowThreshold);
// this.lowThreshold = lowThreshold;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public void onReceive(Context context, Intent intent) {
Intent pIntent = new Intent(Constants.DEVICE_POLL);
pIntent.putExtra("device",deviceIDStr);
alarmIntent = PendingIntent.getBroadcast(AbstractPollDevice.this.context, deviceID, pIntent, 0);
// FIXME - Needs to use setExact on Kitkat devices otherwise the alarm gets batched
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
alarmMgr.setExact(AlarmManager.RTC_WAKEUP,getNextReadingTime().getTime(),alarmIntent);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public AbstractPushDevice(String n, int deviceID, Context appContext, String dri
super(n, deviceID, appContext, driver);
}

// abstract void onDataReady(DownloadObject ddo);

@Override
public void onDownload(DownloadObject dl){
stats.addDownload();
Expand Down
38 changes: 35 additions & 3 deletions mobile/src/main/java/com/ktind/cgm/bgscout/AlertMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
public class AlertMessage {
public AlertLevels alertLevel;
public String message;
public Conditions condition;

public AlertMessage(AlertLevels aL,String msg){
alertLevel=aL;
message=msg;
public AlertMessage(AlertLevels aL,String msg,Conditions condition){
this.alertLevel=aL;
this.message=msg;
this.condition=condition;
}

public AlertLevels getAlertLevel() {
Expand All @@ -53,4 +55,34 @@ public String getMessage() {
public void setMessage(String message) {
this.message = message;
}

public Conditions getCondition() {
return condition;
}

public void setCondition(Conditions condition) {
this.condition = condition;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

AlertMessage that = (AlertMessage) o;

if (alertLevel != that.alertLevel) return false;
if (condition != that.condition) return false;
if (!message.equals(that.message)) return false;

return true;
}

@Override
public int hashCode() {
int result = alertLevel.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + condition.hashCode();
return result;
}
}
Loading

0 comments on commit 2124dc3

Please sign in to comment.