Skip to content

Commit

Permalink
LimiTTer: Support for 4th protocol field of sensor age minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 23, 2016
1 parent 9c17653 commit fb01ff7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 42 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,18 @@ private void displayCurrentInfo() {

final int sensor_age = prefs.getInt("nfc_sensor_age", 0);
if ((sensor_age > 0) && (DexCollectionType.hasLibre())) {
sensorAge.setText("Age: " + JoH.qs(((double) sensor_age) / 1440, 1) + "d"+(Home.getPreferencesBooleanDefaultFalse("nfc_age_problem") ? " \u26A0\u26A0\u26A0" : ""));
final String age_problem = (Home.getPreferencesBooleanDefaultFalse("nfc_age_problem") ? " \u26A0\u26A0\u26A0" : "");
if (prefs.getBoolean("nfc_show_age", true)) {
sensorAge.setText("Age: " + JoH.qs(((double) sensor_age) / 1440, 1) + "d" + age_problem);
} else {
try {
final double expires = JoH.tolerantParseDouble(Home.getPreferencesStringWithDefault("nfc_expiry_days", "14.5")) - ((double) sensor_age) / 1440;
sensorAge.setText(((expires >= 0) ? ("Expires: " + JoH.qs(expires, 1) + "d") : "EXPIRED! ") + age_problem);
} catch (Exception e) {
Log.e(TAG, "expiry calculation: " + e);
sensorAge.setText("Expires: " + "???");
}
}
sensorAge.setVisibility(View.VISIBLE);
if (sensor_age < 1440) {
sensorAge.setTextColor(Color.YELLOW);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.google.gson.annotations.Expose;

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -71,15 +72,28 @@ public static synchronized TransmitterData create(byte[] buffer, int len, Long t
for (int i = 0; i < len; ++i) { data_string.append((char) buffer[i]); }
final String[] data = data_string.toString().split("\\s+");

if (data.length > 1) {
if (data.length > 1) {
transmitterData.sensor_battery_level = Integer.parseInt(data[1]);
if (data.length > 2) {
try {
Home.setPreferencesInt("bridge_battery", Integer.parseInt(data[2]));
if (Home.get_master()) GcmActivity.sendBridgeBattery(Home.getPreferencesInt("bridge_battery",-1));
if (Home.get_master())
GcmActivity.sendBridgeBattery(Home.getPreferencesInt("bridge_battery", -1));
} catch (Exception e) {
Log.e(TAG, "Got exception processing classic wixel or limitter battery value: " + e.toString());
}
if (data.length > 3) {
if (DexCollectionType.getDexCollectionType() == DexCollectionType.LimiTTer) {
try {
// reported sensor age in minutes
final Integer sensorAge = Integer.parseInt(data[3]);
if ((sensorAge > 0) && (sensorAge < 200000))
Home.setPreferencesInt("nfc_sensor_age", sensorAge);
} catch (Exception e) {
Log.e(TAG, "Got exception processing field 4 in classic limitter protocol: " + e);
}
}
}
}
}
transmitterData.raw_data = Integer.parseInt(data[0]);
Expand Down
87 changes: 48 additions & 39 deletions app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
Expand Down Expand Up @@ -41,7 +42,6 @@
import com.eveningoutpost.dexdrip.ParakeetHelper;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.Services.ActivityRecognizedService;
import com.eveningoutpost.dexdrip.Services.MissedReadingService;
import com.eveningoutpost.dexdrip.Services.PlusSyncService;
import com.eveningoutpost.dexdrip.UtilityModels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
Expand Down Expand Up @@ -93,6 +93,7 @@ public class Preferences extends PreferenceActivity {

private static ListPreference locale_choice;
private static Preference force_english;
private static Preference nfc_expiry_days;


private void refreshFragments() {
Expand Down Expand Up @@ -569,43 +570,7 @@ public void onCreate(Bundle savedInstanceState) {
profile_carb_ratio_default = findPreference("profile_carb_ratio_default");
refreshProfileRatios();

// profile_carb_ratio_default.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
// @Override
// public boolean onPreferenceChange(Preference preference, Object newValue) {
// if (!isNumeric(newValue.toString())) {
// return false;
// }
// preference.setTitle(format_carb_ratio(preference.getTitle().toString(), newValue.toString()));
// Profile.reloadPreferences(AllPrefsFragment.this.prefs);
// Home.staticRefreshBGCharts();
// return true;
// }
// });

//profile_carb_ratio_default.setTitle(format_carb_ratio(profile_carb_ratio_default.getTitle().toString(), this.prefs.getString("profile_carb_ratio_default", "")));







// profile_insulin_sensitivity_default.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
// @Override
// public boolean onPreferenceChange(Preference preference, Object newValue) {
// if (!isNumeric(newValue.toString())) {
// return false;
// }
// do_format_insulin_sensitivity(preference, AllPrefsFragment.this.prefs, true, newValue.toString());
// Profile.reloadPreferences(AllPrefsFragment.this.prefs);
// Home.staticRefreshBGCharts();
// return true;
// }
// });

//do_format_insulin_sensitivity(profile_insulin_sensitivity_default, this.prefs, false, null);


nfc_expiry_days = findPreference("nfc_expiry_days");
locale_choice = (ListPreference) findPreference("forced_language");
force_english = findPreference("force_english");

Expand Down Expand Up @@ -963,12 +928,15 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
if (!DexCollectionType.hasLibre(collectionType)) {
collectionCategory.removePreference(nfcSettings);
} else {
// has libre
if (!engineering_mode)
try {
nfcScreen.removePreference(findPreference("nfc_test_diagnostic"));
} catch (NullPointerException e) {
//
}
set_nfc_expiry_change_listeners();
update_nfc_expiry_preferences(null);
}

try {
Expand Down Expand Up @@ -1249,7 +1217,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
bindPreferenceSummaryToValue(transmitterId);
transmitterId.getEditText().setFilters(new InputFilter[]{new InputFilter.AllCaps()});


// when changing collection method
collectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down Expand Up @@ -1378,6 +1346,47 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
});
}

// all this boiler plate for a dynamic interface seems excessive and boring, I would love to know a helper library to simplify this
private void set_nfc_expiry_change_listeners() {
nfc_expiry_days.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// have to pre-save it
preference.getEditor().putString("nfc_expiry_days", (String) newValue).apply();
update_nfc_expiry_preferences(null);
return true;
}
});
final Preference nfc_show_age = findPreference("nfc_show_age");
nfc_show_age.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
update_nfc_expiry_preferences((Boolean) newValue);
return true;
}
});
}

private void update_nfc_expiry_preferences(Boolean show_age) {
try {
;
final PreferenceScreen nfcScreen = (PreferenceScreen) findPreference("xdrip_plus_nfc_settings");
final String nfc_expiry_days_string = AllPrefsFragment.this.prefs.getString("nfc_expiry_days", "14.5");

final CheckBoxPreference nfc_show_age = (CheckBoxPreference) findPreference("nfc_show_age");
nfc_show_age.setSummaryOff("Show the sensor expiry time based on " + nfc_expiry_days_string + " days");
if (show_age == null) show_age = nfc_show_age.isChecked();
if (show_age) {
nfcScreen.removePreference(nfc_expiry_days);
} else {
nfc_expiry_days.setOrder(3);
nfcScreen.addPreference(nfc_expiry_days);
}
} catch (NullPointerException e) {
//
}
}

private void bindWidgetUpdater() {
findPreference("widget_range_lines").setOnPreferenceChangeListener(new WidgetListener());
findPreference("extra_status_line").setOnPreferenceChangeListener(new WidgetListener());
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/pref_data_source.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
android:key="use_nfc_scan"
android:summary="Allow sensors to be scanned when app is open. Historic data will be back-filled. Highly experiemental! Beware this can maybe break a sensor. Test phone compatibility with an almost expired sensor first. You have been warned!"
android:title="Use NFC feature" />
<CheckBoxPreference
android:defaultValue="true"
android:key="nfc_show_age"
android:summaryOff="Show the sensor expiry time based on 14.5 days"
android:summaryOn="Show the sensor age on the main screen"
android:title="Sensor Age or Expiry" />
<EditTextPreference
android:defaultValue="14.5"
android:inputType="numberDecimal"
android:key="nfc_expiry_days"
android:title="Change Sensor Total Days" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="use_nfc_scan"
Expand Down

0 comments on commit fb01ff7

Please sign in to comment.