diff --git a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java index 6538ae565c03771d99a6f3eaf7b144dbb0c16c81..4a7e1110aad092b11c1c44bacec2596712abe957 100644 --- a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java +++ b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java @@ -75,6 +75,8 @@ import info.nightscout.android.model.medtronicNg.PumpInfo; import info.nightscout.android.model.medtronicNg.PumpStatusEvent; import info.nightscout.android.settings.SettingsActivity; import info.nightscout.android.upload.nightscout.NightscoutUploadIntentService; +import info.nightscout.android.utils.ConfigurationStore; +import info.nightscout.android.utils.DataStore; import io.realm.Realm; import io.realm.RealmChangeListener; import io.realm.RealmResults; @@ -85,25 +87,15 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc private static final String TAG = MainActivity.class.getSimpleName(); public static final float MMOLXLFACTOR = 18.016f; - public static int batLevel = 0; - public static boolean reducePollOnPumpAway = false; - public static long pollInterval = MedtronicCnlIntentService.POLL_PERIOD_MS; - public static long lowBatteryPollInterval = MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS; + private DataStore dataStore = DataStore.getInstance(); + private ConfigurationStore configurationStore = ConfigurationStore.getInstance(); - private static long activePumpMac; private int chartZoom = 3; private boolean hasZoomedChart = false; - private NumberFormat sgvFormatter; - private static boolean mmolxl; - private static boolean mmolxlDecimals; - - public static long timeLastGoodSGV = 0; - public static short pumpBattery = 0; - public static int countUnavailableSGV = 0; - boolean mEnableCgmService = true; - SharedPreferences prefs = null; + private boolean mEnableCgmService = true; + private SharedPreferences prefs = null; private PumpInfo mActivePump; private TextView mTextViewLog; // This will eventually move to a status page. private GraphView mChart; @@ -122,22 +114,23 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc */ public static long getNextPoll(PumpStatusEvent pumpStatusData) { long nextPoll = pumpStatusData.getEventDate().getTime() + pumpStatusData.getPumpTimeOffset(), - now = System.currentTimeMillis(); + now = System.currentTimeMillis(), + pollInterval = ConfigurationStore.getInstance().getPollInterval(); // align to next poll slot if (nextPoll + 2 * 60 * 60 * 1000 < now) { // last event more than 2h old -> could be a calibration nextPoll = System.currentTimeMillis() + 1000; } else { // align to poll interval - nextPoll += (((now - nextPoll) / MainActivity.pollInterval)) * MainActivity.pollInterval + nextPoll += (((now - nextPoll) / pollInterval)) * pollInterval + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS; if (pumpStatusData.getBatteryPercentage() > 25) { // poll every 5 min - nextPoll += MainActivity.pollInterval; + nextPoll += pollInterval; } else { // if pump battery seems to be empty reduce polling to save battery (every 15 min) //TODO add message & document it - nextPoll += MainActivity.lowBatteryPollInterval; + nextPoll += ConfigurationStore.getInstance().getLowBatteryPollInterval(); } } @@ -145,9 +138,10 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc } public static String strFormatSGV(float sgvValue) { - if (mmolxl) { + ConfigurationStore configurationStore = ConfigurationStore.getInstance(); + if (configurationStore.isMmolxl()) { NumberFormat sgvFormatter; - if (mmolxlDecimals) { + if (configurationStore.isMmolxlDecimals()) { sgvFormatter = new DecimalFormat("0.00"); } else { sgvFormatter = new DecimalFormat("0.0"); @@ -164,6 +158,12 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc super.onCreate(savedInstanceState); mRealm = Realm.getDefaultInstance(); + + RealmResults<PumpStatusEvent> data = mRealm.where(PumpStatusEvent.class) + .findAllSorted("eventDate", Sort.DESCENDING); + if (data.size() > 0) + dataStore.setLastPumpStatus(data.first()); + mNightscoutUploadService = new Intent(this, NightscoutUploadIntentService.class); setContentView(R.layout.activity_main); @@ -176,15 +176,16 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc } // setup preferences - MainActivity.pollInterval = Long.parseLong(prefs.getString("pollInterval", Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS))); - MainActivity.lowBatteryPollInterval = Long.parseLong(prefs.getString("lowBatPollInterval", Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS))); - MainActivity.reducePollOnPumpAway = prefs.getBoolean("doublePollOnPumpAway", false); + configurationStore.setPollInterval(Long.parseLong(prefs.getString("pollInterval", Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS)))); + configurationStore.setLowBatteryPollInterval(Long.parseLong(prefs.getString("lowBatPollInterval", Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS)))); + configurationStore.setReducePollOnPumpAway(prefs.getBoolean("doublePollOnPumpAway", false)); + chartZoom = Integer.parseInt(prefs.getString("chartZoom", "3")); - mmolxl = prefs.getBoolean("mmolxl", false); - mmolxlDecimals = prefs.getBoolean("mmolDecimals", false); + configurationStore.setMmolxl(prefs.getBoolean("mmolxl", false)); + configurationStore.setMmolxlDecimals(prefs.getBoolean("mmolDecimals", false)); - if (mmolxl) { - if (mmolxlDecimals) + if (configurationStore.isMmolxl()) { + if (configurationStore.isMmolxlDecimals()) sgvFormatter = new DecimalFormat("0.00"); else sgvFormatter = new DecimalFormat("0.0"); @@ -534,10 +535,10 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc startCgmService(); } } else if (key.equals("mmolxl") || key.equals("mmolDecimals")) { - mmolxl = sharedPreferences.getBoolean("mmolxl", false); - mmolxlDecimals = sharedPreferences.getBoolean("mmolDecimals", false); - if (mmolxl) { - if (mmolxlDecimals) + configurationStore.setMmolxl(sharedPreferences.getBoolean("mmolxl", false)); + configurationStore.setMmolxlDecimals(sharedPreferences.getBoolean("mmolDecimals", false)); + if (configurationStore.isMmolxl()) { + if (configurationStore.isMmolxlDecimals()) sgvFormatter = new DecimalFormat("0.00"); else sgvFormatter = new DecimalFormat("0.0"); @@ -546,13 +547,13 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc } refreshDisplay(); } else if (key.equals("pollInterval")) { - MainActivity.pollInterval = Long.parseLong(sharedPreferences.getString("pollInterval", - Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS))); + configurationStore.setPollInterval(Long.parseLong(sharedPreferences.getString("pollInterval", + Long.toString(MedtronicCnlIntentService.POLL_PERIOD_MS)))); } else if (key.equals("lowBatPollInterval")) { - MainActivity.lowBatteryPollInterval = Long.parseLong(sharedPreferences.getString("lowBatPollInterval", - Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS))); + configurationStore.setLowBatteryPollInterval(Long.parseLong(sharedPreferences.getString("lowBatPollInterval", + Long.toString(MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS)))); } else if (key.equals("doublePollOnPumpAway")) { - MainActivity.reducePollOnPumpAway = sharedPreferences.getBoolean("doublePollOnPumpAway", false); + configurationStore.setReducePollOnPumpAway(sharedPreferences.getBoolean("doublePollOnPumpAway", false)); } else if (key.equals("chartZoom")) { chartZoom = Integer.parseInt(sharedPreferences.getString("chartZoom", "3")); hasZoomedChart = false; @@ -601,11 +602,8 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc } } - public static void setActivePumpMac(long pumpMac) { - activePumpMac = pumpMac; - } - private PumpInfo getActivePump() { + long activePumpMac = dataStore.getActivePumpMac(); if (activePumpMac != 0L && (mActivePump == null || !mActivePump.isValid() || mActivePump.getPumpMac() != activePumpMac)) { if (mActivePump != null) { // remove listener on old pump @@ -615,7 +613,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc PumpInfo pump = mRealm .where(PumpInfo.class) - .equalTo("pumpMac", MainActivity.activePumpMac) + .equalTo("pumpMac", activePumpMac) .findFirst(); if (pump != null && pump.isValid()) { @@ -736,7 +734,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc TextView textViewBg = (TextView) findViewById(R.id.textview_bg); TextView textViewBgTime = (TextView) findViewById(R.id.textview_bg_time); TextView textViewUnits = (TextView) findViewById(R.id.textview_units); - if (mmolxl) { + if (configurationStore.isMmolxl()) { textViewUnits.setText(R.string.text_unit_mmolxl); } else { textViewUnits.setText(R.string.text_unit_mgxdl); @@ -761,7 +759,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc if (pumpStatusData != null) { String sgvString; - if (mmolxl) { + if (configurationStore.isMmolxl()) { float fBgValue = (float) pumpStatusData.getSgv(); sgvString = sgvFormatter.format(fBgValue / MMOLXLFACTOR); Log.d(TAG, sgvString + " mmol/L"); @@ -824,7 +822,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc mChart.getViewport().setMinX(left); mChart.getViewport().setYAxisBoundsManual(true); - if (mmolxl) { + if (configurationStore.isMmolxl()) { mChart.getViewport().setMinY(80 / MMOLXLFACTOR); mChart.getViewport().setMaxY(120 / MMOLXLFACTOR); } else { @@ -843,7 +841,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc // turn your data into Entry objects int sgv = pumpStatus.getSgv(); - if (mmolxl) { + if (configurationStore.isMmolxl()) { entries[pos++] = new DataPoint(pumpStatus.getEventDate(), (float) pumpStatus.getSgv() / MMOLXLFACTOR); } else { entries[pos++] = new DataPoint(pumpStatus.getEventDate(), (float) pumpStatus.getSgv()); @@ -881,6 +879,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc @Override public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) { double sgv = dataPoint.getY(); + boolean mmolxl = configurationStore.isMmolxl(); if (sgv < (mmolxl?4.5:80)) paint.setColor(Color.RED); else if (sgv <= (mmolxl?10:180)) @@ -970,7 +969,7 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc if (arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW) || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED) || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) { - batLevel = arg1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); + dataStore.setUplooaderBatteryLevel(arg1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)); } } } diff --git a/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java b/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java index f97a73934acf42bcb5ffffb6f778a440af158563..1d3862bf29250eff2f7b07123e9cf5684657ecf4 100644 --- a/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java +++ b/app/src/main/java/info/nightscout/android/medtronic/message/PumpStatusResponseMessage.java @@ -14,6 +14,7 @@ import info.nightscout.android.medtronic.exception.ChecksumException; import info.nightscout.android.medtronic.exception.EncryptionException; import info.nightscout.android.medtronic.exception.UnexpectedMessageException; import info.nightscout.android.model.medtronicNg.PumpStatusEvent; +import info.nightscout.android.utils.DataStore; import info.nightscout.android.utils.HexDump; /** @@ -197,6 +198,8 @@ public class PumpStatusResponseMessage extends MedtronicSendMessageResponseMessa // Recent Bolus Wizard BGL pumpRecord.setRecentBolusWizard(recentBolusWizard); - pumpRecord.setBolusWizardBGL(bolusWizardBGL); // In mg/DL + if (recentBolusWizard && activeInsulin > DataStore.getInstance().getLastPumpStatus().getActiveInsulin()) { // there is a BolusWizard usage & the IOB increaseed + pumpRecord.setBolusWizardBGL(bolusWizardBGL); // In mg/DL + } } } diff --git a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java index 08737107d34864e8164ba6e04f25f4b327ed4c5a..b4f2e153000ada1bbf5932ecbf321dcaa6e5bd57 100644 --- a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java +++ b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlAlarmManager.java @@ -11,6 +11,7 @@ import android.util.Log; import java.util.Date; import info.nightscout.android.medtronic.MainActivity; +import info.nightscout.android.utils.ConfigurationStore; /** * Created by lgoedhart on 14/07/2016. @@ -81,7 +82,7 @@ public class MedtronicCnlAlarmManager { // restarting the alarm after MedtronicCnlIntentService.POLL_PERIOD_MS from now public static void restartAlarm() { //setAlarmAfterMillis(MainActivity.pollInterval + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS); - setAlarmAfterMillis(MainActivity.pollInterval); // grace already accounted for when using current intent time to set default restart + setAlarmAfterMillis(ConfigurationStore.getInstance().getPollInterval()); // grace already accounted for when using current intent time to set default restart } // Cancel the alarm. diff --git a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java index 3d9e0eb7cea5e6e3fbdf9b866e6a1ad59b32ad8e..b7881f6c2d24c284ef14f116d98f83d15b61b85f 100644 --- a/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java +++ b/app/src/main/java/info/nightscout/android/medtronic/service/MedtronicCnlIntentService.java @@ -35,6 +35,8 @@ import info.nightscout.android.model.medtronicNg.ContourNextLinkInfo; import info.nightscout.android.model.medtronicNg.PumpInfo; import info.nightscout.android.model.medtronicNg.PumpStatusEvent; import info.nightscout.android.upload.nightscout.NightscoutUploadReceiver; +import info.nightscout.android.utils.ConfigurationStore; +import info.nightscout.android.utils.DataStore; import info.nightscout.android.xdrip_plus.XDripPlusUploadReceiver; import io.realm.Realm; import io.realm.RealmResults; @@ -48,10 +50,13 @@ public class MedtronicCnlIntentService extends IntentService { // Number of additional seconds to wait after the next expected CGM poll, so that we don't interfere with CGM radio comms. public final static long POLL_GRACE_PERIOD_MS = 30000L; private static final String TAG = MedtronicCnlIntentService.class.getSimpleName(); + private UsbHidDriver mHidDevice; private Context mContext; private NotificationManagerCompat nm; private UsbManager mUsbManager; + private DataStore dataStore = DataStore.getInstance(); + private ConfigurationStore configurationStore = ConfigurationStore.getInstance(); public MedtronicCnlIntentService() { super(MedtronicCnlIntentService.class.getName()); @@ -100,10 +105,15 @@ public class MedtronicCnlIntentService extends IntentService { protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent called"); - long timePollStarted = System.currentTimeMillis(); - long timePollExpected = timePollStarted; - if (MainActivity.timeLastGoodSGV != 0) { - timePollExpected = MainActivity.timeLastGoodSGV + POLL_PERIOD_MS + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((timePollStarted - 1000L - (MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS)); + long timePollStarted = System.currentTimeMillis(), + timePollExpected = timePollStarted, + timeLastGoodSGV = dataStore.getLastPumpStatus().getEventDate().getTime(); + + short pumpBatteryLevel = dataStore.getLastPumpStatus().getBatteryPercentage(); + + + if (timeLastGoodSGV != 0) { + timePollExpected = timeLastGoodSGV + POLL_PERIOD_MS + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((timePollStarted - 1000L - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS)); } // avoid polling when too close to sensor-pump comms @@ -114,9 +124,9 @@ public class MedtronicCnlIntentService extends IntentService { return; } - long pollInterval = MainActivity.pollInterval; - if ((MainActivity.pumpBattery > 0) && (MainActivity.pumpBattery <= 25)) { - pollInterval = MainActivity.lowBatteryPollInterval; + long pollInterval = configurationStore.getPollInterval(); + if ((pumpBatteryLevel > 0) && (pumpBatteryLevel <= 25)) { + pollInterval = configurationStore.getLowBatteryPollInterval(); } if (!hasUsbHostFeature()) { @@ -216,9 +226,10 @@ public class MedtronicCnlIntentService extends IntentService { if (radioChannel == 0) { sendStatus("Could not communicate with the 640g. Are you near the pump?"); Log.i(TAG, "Could not communicate with the 640g. Are you near the pump?"); - pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L); // reduce polling interval to half until pump is available + pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L); // reduce polling interval to half until pump is available } else { - setActivePumpMac(pumpMAC); + dataStore.setActivePumpMac(pumpMAC); + activePump.setLastRadioChannel(radioChannel); sendStatus(String.format(Locale.getDefault(), "Connected on channel %d RSSI: %d%%", (int) radioChannel, cnlReader.getPumpSession().getRadioRSSIpercentage())); Log.d(TAG, String.format("Connected to Contour Next Link on channel %d.", (int) radioChannel)); @@ -242,7 +253,6 @@ public class MedtronicCnlIntentService extends IntentService { cnlReader.updatePumpStatus(pumpRecord); if (pumpRecord.getSgv() != 0) { - String offsetSign = ""; if (pumpOffset > 0) { offsetSign = "+"; @@ -250,14 +260,14 @@ public class MedtronicCnlIntentService extends IntentService { sendStatus("SGV: " + MainActivity.strFormatSGV(pumpRecord.getSgv()) + " At: " + df.format(pumpRecord.getEventDate().getTime()) + " Pump: " + offsetSign + (pumpOffset / 1000L) + "sec"); //note: event time is currently stored with offset // Check if pump sent old event when new expected and schedule a re-poll - if (((pumpRecord.getEventDate().getTime() - MainActivity.timeLastGoodSGV) < 5000L) && ((timePollExpected - timePollStarted) < 5000L)) { + if (((pumpRecord.getEventDate().getTime() - dataStore.getLastPumpStatus().getEventDate().getTime()) < 5000L) && ((timePollExpected - timePollStarted) < 5000L)) { pollInterval = 90000L; // polling interval set to 90 seconds sendStatus("Pump sent old SGV event, re-polling..."); } - MainActivity.timeLastGoodSGV = pumpRecord.getEventDate().getTime(); // track last good sgv event time - MainActivity.pumpBattery = pumpRecord.getBatteryPercentage(); // track pump battery - MainActivity.countUnavailableSGV = 0; // reset unavailable sgv count + //MainActivity.timeLastGoodSGV = pumpRecord.getEventDate().getTime(); // track last good sgv event time + //MainActivity.pumpBattery = pumpRecord.getBatteryPercentage(); // track pump battery + dataStore.clearUnavailableSGVCount(); // reset unavailable sgv count // Check that the record doesn't already exist before committing RealmResults<PumpStatusEvent> checkExistingRecords = activePump.getPumpHistory() @@ -269,13 +279,12 @@ public class MedtronicCnlIntentService extends IntentService { // There should be the 1 record we've already added in this transaction. if (checkExistingRecords.size() == 0) { activePump.getPumpHistory().add(pumpRecord); + dataStore.setLastPumpStatus(pumpRecord); } - Log.d(TAG, "history reading size: " + activePump.getPumpHistory().size()); - Log.d(TAG, "history reading date: " + activePump.getPumpHistory().last().getEventDate()); } else { sendStatus("SGV: unavailable from pump"); - MainActivity.countUnavailableSGV ++; // poll clash detection + dataStore.incUnavailableSGVCount(); // poll clash detection } realm.commitTransaction(); @@ -286,11 +295,11 @@ public class MedtronicCnlIntentService extends IntentService { } catch (UnexpectedMessageException e) { Log.e(TAG, "Unexpected Message", e); sendStatus("Communication Error: " + e.getMessage()); - pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L); + pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L); } catch (TimeoutException e) { Log.e(TAG, "Timeout communicating with the Contour Next Link.", e); sendStatus("Timeout communicating with the Contour Next Link."); - pollInterval = MainActivity.pollInterval / (MainActivity.reducePollOnPumpAway?2L:1L); + pollInterval = configurationStore.getPollInterval() / (configurationStore.isReducePollOnPumpAway()?2L:1L); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "Could not determine CNL HMAC", e); sendStatus("Error connecting to Contour Next Link: Hashing error."); @@ -331,8 +340,8 @@ public class MedtronicCnlIntentService extends IntentService { // smart polling and pump-sensor poll clash detection long lastActualPollTime = timePollStarted; - if (MainActivity.timeLastGoodSGV > 0) { - lastActualPollTime = MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (MainActivity.timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS)); + if (timeLastGoodSGV > 0) { + lastActualPollTime = timeLastGoodSGV + POLL_GRACE_PERIOD_MS + (POLL_PERIOD_MS * ((System.currentTimeMillis() - (timeLastGoodSGV + POLL_GRACE_PERIOD_MS)) / POLL_PERIOD_MS)); } long nextActualPollTime = lastActualPollTime + POLL_PERIOD_MS; long nextRequestedPollTime = lastActualPollTime + pollInterval; @@ -341,13 +350,13 @@ public class MedtronicCnlIntentService extends IntentService { } // extended unavailable SGV may be due to clash with the current polling time // while we wait for a good SGV event, polling is auto adjusted by offsetting the next poll based on miss count - if (MainActivity.countUnavailableSGV > 0) { - if (MainActivity.timeLastGoodSGV == 0) { + if (dataStore.getUnavailableSGVCount() > 0) { + if (timeLastGoodSGV == 0) { nextRequestedPollTime += POLL_PERIOD_MS / 5L; // if there is a uploader/sensor poll clash on startup then this will push the next attempt out by 60 seconds } - else if (MainActivity.countUnavailableSGV > 2) { - sendStatus("Warning: No SGV available from pump for " + MainActivity.countUnavailableSGV + " attempts"); - nextRequestedPollTime += ((long) ((MainActivity.countUnavailableSGV - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10) + else if (dataStore.getUnavailableSGVCount() > 2) { + sendStatus("Warning: No SGV available from pump for " +dataStore.getUnavailableSGVCount() + " attempts"); + nextRequestedPollTime += ((long) ((dataStore.getUnavailableSGVCount() - 2) % 5)) * (POLL_PERIOD_MS / 10L); // adjust poll time in 1/10 steps to avoid potential poll clash (max adjustment at 5/10) } } MedtronicCnlAlarmManager.setAlarm(nextRequestedPollTime); @@ -357,10 +366,6 @@ public class MedtronicCnlIntentService extends IntentService { } } - private void setActivePumpMac(long pumpMAC) { - MainActivity.setActivePumpMac(pumpMAC); - } - // reliable wake alarm manager wake up for all android versions public static void wakeUpIntent(Context context, long wakeTime, PendingIntent pendingIntent) { final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); diff --git a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java index 51174e3cfe59108f0b24fc827e0f620f6363e424..74afe684f6bbaf431df34c31dd4d08009968bf25 100644 --- a/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java +++ b/app/src/main/java/info/nightscout/android/upload/nightscout/NightscoutUploadIntentService.java @@ -11,8 +11,8 @@ import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import info.nightscout.android.R; -import info.nightscout.android.medtronic.MainActivity; import info.nightscout.android.model.medtronicNg.PumpStatusEvent; +import info.nightscout.android.utils.DataStore; import io.realm.Realm; import io.realm.RealmResults; @@ -67,9 +67,8 @@ public class NightscoutUploadIntentService extends IntentService { Log.i(TAG, String.format("Starting upload of %s record using a REST API", records.size())); String urlSetting = prefs.getString(mContext.getString(R.string.preference_nightscout_url), ""); String secretSetting = prefs.getString(mContext.getString(R.string.preference_api_secret), "YOURAPISECRET"); - int uploaderBatteryLevel = MainActivity.batLevel; Boolean uploadSuccess = mNightScoutUpload.doRESTUpload(urlSetting, - secretSetting, uploaderBatteryLevel, records); + secretSetting, DataStore.getInstance().getUplooaderBatteryLevel(), records); if (uploadSuccess) { mRealm.beginTransaction(); for (PumpStatusEvent updateRecord : records) { diff --git a/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java b/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java new file mode 100644 index 0000000000000000000000000000000000000000..1a4fca0c7c7a15b5e96607ed2f56ea389702c501 --- /dev/null +++ b/app/src/main/java/info/nightscout/android/utils/ConfigurationStore.java @@ -0,0 +1,67 @@ +package info.nightscout.android.utils; + + +import info.nightscout.android.medtronic.service.MedtronicCnlIntentService; +import info.nightscout.android.model.medtronicNg.PumpStatusEvent; + +/** + * Created by volker on 30.03.2017. + */ + +public class ConfigurationStore { + private static ConfigurationStore instance; + + private boolean reducePollOnPumpAway = false; + private long pollInterval = MedtronicCnlIntentService.POLL_PERIOD_MS; + private long lowBatteryPollInterval = MedtronicCnlIntentService.LOW_BATTERY_POLL_PERIOD_MS; + private boolean mmolxl; + private boolean mmolxlDecimals; + + public static ConfigurationStore getInstance() { + if (ConfigurationStore.instance == null) { + instance = new ConfigurationStore(); + } + + return instance; + } + + public boolean isReducePollOnPumpAway() { + return reducePollOnPumpAway; + } + + public void setReducePollOnPumpAway(boolean reducePollOnPumpAway) { + this.reducePollOnPumpAway = reducePollOnPumpAway; + } + + public long getPollInterval() { + return pollInterval; + } + + public void setPollInterval(long pollInterval) { + this.pollInterval = pollInterval; + } + + public long getLowBatteryPollInterval() { + return lowBatteryPollInterval; + } + + public void setLowBatteryPollInterval(long lowBatteryPollInterval) { + this.lowBatteryPollInterval = lowBatteryPollInterval; + } + + public boolean isMmolxl() { + return mmolxl; + } + + public void setMmolxl(boolean mmolxl) { + this.mmolxl = mmolxl; + } + + public boolean isMmolxlDecimals() { + return mmolxlDecimals; + } + + public void setMmolxlDecimals(boolean mmolxlDecimals) { + this.mmolxlDecimals = mmolxlDecimals; + } +} diff --git a/app/src/main/java/info/nightscout/android/utils/DataStore.java b/app/src/main/java/info/nightscout/android/utils/DataStore.java new file mode 100644 index 0000000000000000000000000000000000000000..34ff4deec0737d25133e442a9f1bc6948ebf0ba6 --- /dev/null +++ b/app/src/main/java/info/nightscout/android/utils/DataStore.java @@ -0,0 +1,74 @@ +package info.nightscout.android.utils; + + +import java.util.Date; + +import info.nightscout.android.model.medtronicNg.PumpStatusEvent; + +/** + * Created by volker on 30.03.2017. + */ + +public class DataStore { + private static DataStore instance; + + private PumpStatusEvent lastPumpStatus; + private int uplooaderBatteryLevel = 0; + private int unavailableSGVCount = 0; + private long activePumpMac = 0; + + private DataStore() {} + + public static DataStore getInstance() { + if (DataStore.instance == null) { + instance = new DataStore(); + + // set some initial dummy values + PumpStatusEvent dummyStatus = new PumpStatusEvent(); + dummyStatus.setEventDate(new Date(0)); + + instance.setLastPumpStatus(dummyStatus); + } + + return instance; + } + + public PumpStatusEvent getLastPumpStatus() { + return lastPumpStatus; + } + + public void setLastPumpStatus(PumpStatusEvent lastPumpStatus) { + this.lastPumpStatus = lastPumpStatus; + } + + public int getUplooaderBatteryLevel() { + return uplooaderBatteryLevel; + } + + public void setUplooaderBatteryLevel(int uplooaderBatteryLevel) { + this.uplooaderBatteryLevel = uplooaderBatteryLevel; + } + + public int getUnavailableSGVCount() { + return unavailableSGVCount; + } + + public int incUnavailableSGVCount() { + return unavailableSGVCount++; + } + + public void clearUnavailableSGVCount() { + this.unavailableSGVCount = 0; + } + public void setUnavailableSGVCount(int unavailableSGVCount) { + this.unavailableSGVCount = unavailableSGVCount; + } + + public long getActivePumpMac() { + return activePumpMac; + } + + public void setActivePumpMac(long activePumpMac) { + this.activePumpMac = activePumpMac; + } +} diff --git a/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java b/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java index eef010f25bcd95a441250f3090c01fd3d367e4e3..d27f95ecf8f02b258b2e06564eb191ad8c8d70e6 100644 --- a/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java +++ b/app/src/main/java/info/nightscout/android/xdrip_plus/XDripPlusUploadIntentService.java @@ -16,9 +16,9 @@ import java.text.SimpleDateFormat; import java.util.List; import java.util.Locale; -import info.nightscout.android.medtronic.MainActivity; import info.nightscout.android.model.medtronicNg.PumpStatusEvent; import info.nightscout.android.upload.nightscout.serializer.EntriesSerializer; +import info.nightscout.android.utils.DataStore; import io.realm.Realm; import io.realm.RealmResults; import io.realm.Sort; @@ -116,7 +116,7 @@ public class XDripPlusUploadIntentService extends IntentService { private void addDeviceStatus(JSONArray devicestatusArray, PumpStatusEvent record) throws Exception { JSONObject json = new JSONObject(); - json.put("uploaderBattery", MainActivity.batLevel); + json.put("uploaderBattery", DataStore.getInstance().getUplooaderBatteryLevel()); json.put("device", record.getDeviceName()); json.put("created_at", ISO8601_DATE_FORMAT.format(record.getPumpDate()));