Refactor DataService to delegate BG processing to BgSources.

This commit is contained in:
Johannes Mockenhaupt 2018-06-22 18:28:05 +02:00
parent 7a331e0c94
commit 922f9ff83d
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
10 changed files with 202 additions and 153 deletions

View file

@ -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)) {

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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);
}
}
}

View file

@ -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());
}
}

View file

@ -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);
}
}
}
}
}

View file

@ -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");
}
}

View file

@ -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);
}
}
}

View file

@ -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");
}
}