From 922f9ff83d080e604e540e776d9d3b02e72c59c5 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:28:05 +0200 Subject: [PATCH] Refactor DataService to delegate BG processing to BgSources. --- .../androidaps/Services/DataService.java | 157 ++---------------- .../interfaces/BgSourceInterface.java | 6 +- .../interfaces/PluginDescription.java | 6 - .../androidaps/plugins/Loop/LoopPlugin.java | 2 +- .../plugins/Source/SourceDexcomG5Plugin.java | 44 +++++ .../plugins/Source/SourceGlimpPlugin.java | 17 ++ .../plugins/Source/SourceMM640gPlugin.java | 49 ++++++ .../plugins/Source/SourceNSClientPlugin.java | 7 + .../plugins/Source/SourcePoctechPlugin.java | 46 +++++ .../plugins/Source/SourceXdripPlugin.java | 21 +++ 10 files changed, 202 insertions(+), 153 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index e7dd87ab44..2b6d1c1604 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -23,6 +23,8 @@ import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.events.EventNsTreatment; +import info.nightscout.androidaps.interfaces.BgSourceInterface; +import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; @@ -133,23 +135,23 @@ public class DataService extends IntentService { final String action = intent.getAction(); if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { if (xDripEnabled) { - handleNewDataFromXDrip(intent); + processNewBgIntent(SourceXdripPlugin.getPlugin(), intent); } } else if (Intents.NS_EMULATOR.equals(action)) { if (mm640gEnabled) { - handleNewDataFromMM640g(intent); + processNewBgIntent(SourceMM640gPlugin.getPlugin(), intent); } } else if (Intents.GLIMP_BG.equals(action)) { if (glimpEnabled) { - handleNewDataFromGlimp(intent); + processNewBgIntent(SourceGlimpPlugin.getPlugin(), intent); } } else if (Intents.DEXCOMG5_BG.equals(action)) { if (dexcomG5Enabled) { - handleNewDataFromDexcomG5(intent); + processNewBgIntent(SourceDexcomG5Plugin.getPlugin(), intent); } } else if (Intents.POCTECH_BG.equals(action)) { if (poctechEnabled) { - handleNewDataFromPoctech(intent); + processNewBgIntent(SourcePoctechPlugin.getPlugin(), intent); } } else if (Intents.ACTION_NEW_SGV.equals(action)) { if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true)) @@ -200,153 +202,18 @@ public class DataService extends IntentService { MainApp.unsubscribe(this); } - private void handleNewDataFromXDrip(Intent intent) { + private void processNewBgIntent(BgSourceInterface bgSource, Intent intent) { + if (Config.logIncommingData) + log.debug("Got intent: " + intent.getAction()); Bundle bundle = intent.getExtras(); if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); - bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); - bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); - bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); - - MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); - } - - private void handleNewDataFromGlimp(Intent intent) { - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - bgReading.value = bundle.getDouble("mySGV"); - bgReading.direction = bundle.getString("myTrend"); - bgReading.date = bundle.getLong("myTimestamp"); - bgReading.raw = 0; - bgReading.filtered = false; - bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - - MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP"); - } - - private void handleNewDataFromDexcomG5(Intent intent) { - // onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle - - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - String data = bundle.getString("data"); - log.debug("Received Dexcom Data", data); - - try { - JSONArray jsonArray = new JSONArray(data); - log.debug("Received Dexcom Data size:" + jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject json = jsonArray.getJSONObject(i); - bgReading.value = json.getInt("m_value"); - bgReading.direction = json.getString("m_trend"); - bgReading.date = json.getLong("m_time") * 1000L; - bgReading.raw = 0; - bgReading.filtered = true; - bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().pluginDescription.getUserfriendlyName(); - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); - } - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - } - - private void handleNewDataFromPoctech(Intent intent) { - - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - String data = bundle.getString("data"); - log.debug("Received Poctech Data", data); - - try { - JSONArray jsonArray = new JSONArray(data); - log.debug("Received Poctech Data size:" + jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject json = jsonArray.getJSONObject(i); - bgReading.value = json.getDouble("current"); - bgReading.direction = json.getString("direction"); - bgReading.date = json.getLong("date"); - bgReading.raw = json.getDouble("raw"); - if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) - bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "Poctech"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); - } - } - - } catch (JSONException e) { - e.printStackTrace(); - } - } - - private void handleNewDataFromMM640g(Intent intent) { - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - final String collection = bundle.getString("collection"); - if (collection == null) return; - - if (collection.equals("entries")) { - final String data = bundle.getString("data"); - - if ((data != null) && (data.length() > 0)) { - try { - final JSONArray json_array = new JSONArray(data); - for (int i = 0; i < json_array.length(); i++) { - final JSONObject json_object = json_array.getJSONObject(i); - final String type = json_object.getString("type"); - switch (type) { - case "sgv": - BgReading bgReading = new BgReading(); - - bgReading.value = json_object.getDouble("sgv"); - bgReading.direction = json_object.getString("direction"); - bgReading.date = json_object.getLong("date"); - bgReading.raw = json_object.getDouble("sgv"); - bgReading.filtered = true; - bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - - MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); - break; - default: - log.debug("Unknown entries type: " + type); - } - } - } catch (JSONException e) { - log.error("Got JSON exception: " + e); - } - } - } + bgSource.processNewData(bundle); } private void handleNewDataFromNSClient(Intent intent) { Bundle bundles = intent.getExtras(); if (bundles == null) return; - if (Config.logIncommingData) - log.debug("Got intent: " + intent.getAction()); + if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java index 429bfff8fe..c6cb7c1b8e 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java @@ -1,6 +1,10 @@ package info.nightscout.androidaps.interfaces; +import android.os.Bundle; + /** * Created by mike on 20.06.2016. */ -public interface BgSourceInterface {} +public interface BgSourceInterface { + void processNewData(Bundle bundle); +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java index 4b073443bb..1634fc672d 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.interfaces; -import info.nightscout.androidaps.MainApp; - public class PluginDescription { PluginType mainType = PluginType.GENERAL; String fragmentClass = null; @@ -89,8 +87,4 @@ public class PluginDescription { public PluginType getType() { return mainType; } - - public String getUserfriendlyName() { - return MainApp.gs(pluginName); - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index 84c0d8b54d..e4a5be19b2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -160,7 +160,7 @@ public class LoopPlugin extends PluginBase { // no BG source active return; } - if (!Objects.equals(bgReading.sourcePlugin, bgSource.pluginDescription.getUserfriendlyName())) { + if (!Objects.equals(bgReading.sourcePlugin, bgSource.getName())) { // reading not from active BG source (likely coming in from NS) return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index 64fa05fca8..9327b31754 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -1,17 +1,30 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; /** * Created by mike on 28.11.2017. */ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceDexcomG5Plugin.class); private static SourceDexcomG5Plugin plugin = null; @@ -33,4 +46,35 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + String data = bundle.getString("data"); + // onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle + log.debug("Received Dexcom Data", data); + + try { + JSONArray jsonArray = new JSONArray(data); + log.debug("Received Dexcom Data size:" + jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject json = jsonArray.getJSONObject(i); + bgReading.value = json.getInt("m_value"); + bgReading.direction = json.getString("m_trend"); + bgReading.date = json.getLong("m_time") * 1000L; + bgReading.raw = 0; + bgReading.filtered = true; + bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().getName(); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); + if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } + } + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index bc3bb0dcd3..a94caa0155 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -1,6 +1,10 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -28,4 +32,17 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + bgReading.value = bundle.getDouble("mySGV"); + bgReading.direction = bundle.getString("myTrend"); + bgReading.date = bundle.getLong("myTimestamp"); + bgReading.raw = 0; + bgReading.filtered = false; + bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().getName(); + + MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index e2a73c46fc..8b7b08e9fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -1,6 +1,16 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -10,6 +20,8 @@ import info.nightscout.androidaps.interfaces.PluginType; * Created by mike on 05.08.2016. */ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceMM640gPlugin.class); + private static SourceMM640gPlugin plugin = null; public static SourceMM640gPlugin getPlugin() { @@ -27,4 +39,41 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface ); } + @Override + public void processNewData(Bundle bundle) { + final String collection = bundle.getString("collection"); + if (collection == null) return; + + if (collection.equals("entries")) { + final String data = bundle.getString("data"); + + if ((data != null) && (data.length() > 0)) { + try { + final JSONArray json_array = new JSONArray(data); + for (int i = 0; i < json_array.length(); i++) { + final JSONObject json_object = json_array.getJSONObject(i); + final String type = json_object.getString("type"); + switch (type) { + case "sgv": + BgReading bgReading = new BgReading(); + + bgReading.value = json_object.getDouble("sgv"); + bgReading.direction = json_object.getString("direction"); + bgReading.date = json_object.getLong("date"); + bgReading.raw = json_object.getDouble("sgv"); + bgReading.filtered = true; + bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().getName(); + + MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); + break; + default: + log.debug("Unknown entries type: " + type); + } + } + } catch (JSONException e) { + log.error("Got JSON exception: " + e); + } + } + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index d5f30b1a37..1bd2c3ae7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.BgSourceInterface; @@ -31,4 +33,9 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac ); } + @Override + public void processNewData(Bundle bundle) { + // TODO + throw new IllegalStateException("Nope"); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index 9f15bd9b29..fc900f38c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -1,16 +1,31 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.utils.JsonHelper; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. */ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourcePoctechPlugin.class); private static SourcePoctechPlugin plugin = null; @@ -31,4 +46,35 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + String data = bundle.getString("data"); + log.debug("Received Poctech Data", data); + + try { + JSONArray jsonArray = new JSONArray(data); + log.debug("Received Poctech Data size:" + jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject json = jsonArray.getJSONObject(i); + bgReading.value = json.getDouble("current"); + bgReading.direction = json.getString("direction"); + bgReading.date = json.getLong("date"); + bgReading.raw = json.getDouble("raw"); + if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) + bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "Poctech"); + if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } + } + + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index a69ff84c2d..786edb2b49 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -1,6 +1,13 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import java.util.Objects; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -27,4 +34,18 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { .description(R.string.description_source_xdrip) ); } + + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); + bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); + bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); + bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); + bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().getName(); + bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); + + MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); + } }