Skip to content

Commit

Permalink
set up option to broadcast to local pebble
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenBlackWasAlreadyTaken committed Jan 31, 2015
1 parent 05c4125 commit c6d577d
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<orderEntry type="library" exported="" name="ActiveAndroid" level="project" />
<orderEntry type="library" exported="" name="usb-serial-for-android-v010" level="project" />
<orderEntry type="library" exported="" name="retrofit-1.7.1" level="project" />
<orderEntry type="library" exported="" name="pebblekit-2.6.0" level="project" />
<orderEntry type="library" exported="" name="hellocharts-library-1.1" level="project" />
<orderEntry type="library" exported="" name="gson-2.3" level="project" />
</component>
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ dependencies {
compile 'com.google.code.gson:gson:2.3'
compile 'org.mongodb:mongo-java-driver:2.10.1'
compile 'com.squareup.retrofit:retrofit:1.7.1'
compile 'com.getpebble:pebblekit:2.6.0@aar'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission." />


<application
android:name="com.activeandroid.app.Application"
android:allowBackup="true"
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.eveningoutpost.dexdrip.Services.WixelReader;
import com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder;
import com.eveningoutpost.dexdrip.UtilityModels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.UtilityModels.Intents;

import java.text.DecimalFormat;
import java.util.Date;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class Home extends Activity implements NavigationDrawerFragment.Navigatio

public BgGraphBuilder bgGraphBuilder;
BroadcastReceiver _broadcastReceiver;
BroadcastReceiver newDataReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -90,7 +92,14 @@ public void onReceive(Context ctx, Intent intent) {
}
}
};
newDataReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
updateCurrentBgInfo();
}
};
registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
registerReceiver(newDataReceiver, new IntentFilter(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA));
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), menu_name, this);
holdViewport.set(0, 0, 0, 0);
Expand Down Expand Up @@ -163,8 +172,12 @@ public void setViewport() {
@Override
public void onPause() {
super.onPause();
if (_broadcastReceiver != null)
if (_broadcastReceiver != null) {
unregisterReceiver(_broadcastReceiver);
}
if(newDataReceiver != null) {
unregisterReceiver(newDataReceiver);
}
}

public void updateCurrentBgInfo() {
Expand All @@ -177,7 +190,7 @@ public void updateCurrentBgInfo() {
if (BgReading.latest(2).size() > 1) {
List<Calibration> calibrations = Calibration.latest(2);
if (calibrations.size() > 1) {
if (calibrations.get(0).possible_bad != null && calibrations.get(0).possible_bad == true && calibrations.get(1).possible_bad != null && calibrations.get(1).possible_bad == false) {
if (calibrations.get(0).possible_bad != null && calibrations.get(0).possible_bad == true && calibrations.get(1).possible_bad != null && calibrations.get(1).possible_bad != true) {
notificationText.setText("Possible bad calibration slope, please have a glass of water, wash hands, then recalibrate in a few!");
}
displayCurrentInfo();
Expand Down Expand Up @@ -234,7 +247,7 @@ public void displayCurrentInfo() {
if(!predictive){
estimate=lastBgreading.calculated_value;
String stringEstimate = bgGraphBuilder.unitized_string(estimate);
currentBgValueText.setText( stringEstimate + " " + BgReading.slopeArrow(lastBgreading.staticSlope()));
currentBgValueText.setText( stringEstimate + " " + BgReading.slopeArrow((lastBgreading.staticSlope() * 60000)));
} else {
estimate = BgReading.activePrediction();
String stringEstimate = bgGraphBuilder.unitized_string(estimate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,13 @@ public String unitized_string(double value) {
public double mmolConvert(double mgdl) {
return mgdl * Constants.MGDL_TO_MMOLL;
}

public String unit() {
if(doMgdl){
return "mg/dl";
} else {
return "mmol";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public static void addToQueue(BgReading bgReading, String operation_type, Contex

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

Intent updateIntent = new Intent(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA);
context.sendBroadcast(updateIntent);

if (prefs.getBoolean("cloud_storage_mongodb_enable", false) || prefs.getBoolean("cloud_storage_api_enable", false)) {
Log.w("SENSOR QUEUE:", String.valueOf(bgSendQueue.mongo_success));
if (operation_type.compareTo("create") == 0) {
Expand All @@ -92,6 +95,11 @@ public static void addToQueue(BgReading bgReading, String operation_type, Contex
intent.putExtras(bundle);
context.sendBroadcast(intent, Intents.RECEIVER_PERMISSION);
}

if(prefs.getBoolean("broadcast_to_pebble", false)) {
PebbleSync pebbleSync = new PebbleSync();
pebbleSync.sendData(context, bgReading);
}
}

public void markMongoSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface Intents {
String EXTRA_SENSOR_BATTERY = "com.eveningoutpost.dexdrip.Extras.SensorBattery";
String EXTRA_TIMESTAMP = "com.eveningoutpost.dexdrip.Extras.Time";

String ACTION_NEW_BG_ESTIMATE_NO_DATA = "com.eveningoutpost.dexdrip.BgEstimateNoData";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.eveningoutpost.dexdrip.UtilityModels;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;

import com.eveningoutpost.dexdrip.Models.BgReading;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;

import java.util.Date;
import java.util.UUID;

/**
* Created by THE NIGHTSCOUT PROJECT CONTRIBUTORS (and adapted to fit the needs of this project)
*/
public class PebbleSync {
// CGM_ICON_KEY = 0x0, // TUPLE_CSTRING, MAX 2 BYTES (10)
// CGM_BG_KEY = 0x1, // TUPLE_CSTRING, MAX 4 BYTES (253 OR 22.2)
// CGM_TCGM_KEY = 0x2, // TUPLE_INT, 4 BYTES (CGM TIME)
// CGM_TAPP_KEY = 0x3, // TUPLE_INT, 4 BYTES (APP / PHONE TIME)
// CGM_DLTA_KEY = 0x4, // TUPLE_CSTRING, MAX 5 BYTES (BG DELTA, -100 or -10.0)
// CGM_UBAT_KEY = 0x5, // TUPLE_CSTRING, MAX 3 BYTES (UPLOADER BATTERY, 100)
// CGM_NAME_KEY = 0x6 // TUPLE_CSTRING, MAX 9 BYTES (DexDrip)
public static final UUID PEBBLEAPP_UUID = UUID.fromString("2c3f5ab3-7506-44e7-b8d0-2c63de32e1ec");
public static final int ICON_KEY = 0;
public static final int BG_KEY = 1;
public static final int RECORD_TIME_KEY = 2;
public static final int PHONE_TIME_KEY = 3;
public static final int BG_DELTA_KEY = 4;
public static final int UPLOADER_BATTERY_KEY = 5;
public static final int NAME_KEY = 6;

private Context mContext;
private BgGraphBuilder bgGraphBuilder;
private BgReading mBgReading;

public PebbleDictionary buildDictionary() {
PebbleDictionary dictionary = new PebbleDictionary();
dictionary.addString(ICON_KEY, slopeOrdinal());
dictionary.addString(BG_KEY, bgReading());
dictionary.addUint32(RECORD_TIME_KEY, (int) mBgReading.timestamp);
dictionary.addUint32(PHONE_TIME_KEY, (int) new Date().getTime());
dictionary.addString(BG_DELTA_KEY, bgDelta());
dictionary.addString(UPLOADER_BATTERY_KEY, phoneBattery());
dictionary.addString(NAME_KEY, "DexDrip");
return dictionary;
}

public void sendData(Context context, BgReading bgReading){
mContext = context;
bgGraphBuilder = new BgGraphBuilder(mContext);
mBgReading = BgReading.last();
sendDownload(buildDictionary());
}

public String bgReading() {
return bgGraphBuilder.unitized_string(mBgReading.calculated_value);
}

public String bgDelta() {
String deltaString = bgGraphBuilder.unitized_string((mBgReading.calculated_value_slope * (5 * 60 * 1000)));
if(mBgReading.calculated_value_slope > 0) {
return ("+"+deltaString);
} else {
return deltaString;
}
}

public String phoneBattery() {
return String.valueOf(getBatteryLevel());
}

public String bgUnit() {
return bgGraphBuilder.unit();
}

public void sendDownload(PebbleDictionary dictionary) {
if (PebbleKit.isWatchConnected(mContext)) {
if (dictionary != null && mContext != null) {
Log.d("PEBBLE PUSHER", "Sending data to pebble");
PebbleKit.sendDataToPebble(mContext, PEBBLEAPP_UUID, dictionary);
}
}
}

public int getBatteryLevel() {
Intent batteryIntent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if(level == -1 || scale == -1) {
return 50;
}
return (int)(((float)level / (float)scale) * 100.0f);
}

public String slopeOrdinal(){
double slope_by_minute = mBgReading.calculated_value_slope * 60000;
String arrow = "0";
if (slope_by_minute <= (-3.5)) {
arrow = "7";
} else if (slope_by_minute <= (-2)) {
arrow = "6";
} else if (slope_by_minute <= (-1)) {
arrow = "5";
} else if (slope_by_minute <= (1)) {
arrow = "4";
} else if (slope_by_minute <= (2)) {
arrow = "3";
} else if (slope_by_minute <= (3.5)) {
arrow = "2";
} else {
arrow = "1";
}
return arrow;
}
}

6 changes: 6 additions & 0 deletions app/src/main/res/xml/pref_data_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
android:summary="@string/pref_summary_broadcast_enabled"
android:defaultValue="false" >
</SwitchPreference>
<SwitchPreference
android:title="Send to Pebble"
android:key="broadcast_to_pebble"
android:summary="If you have a pebble watch you can send bg data straight to it"
android:defaultValue="false" >
</SwitchPreference>
</PreferenceCategory>

</PreferenceScreen>

0 comments on commit c6d577d

Please sign in to comment.