Revert "Merge pull request #1131 from jotomo/901"

This reverts commit 268dec0059, reversing
changes made to ab41541c69.
This commit is contained in:
Johannes Mockenhaupt 2018-06-29 22:43:54 +02:00
parent 2291472b01
commit 33b66e2ce7
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
25 changed files with 401 additions and 495 deletions

View file

@ -12,8 +12,6 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Objects;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
@ -23,8 +21,6 @@ import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.events.EventNsFood;
import info.nightscout.androidaps.events.EventNsTreatment; 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.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
@ -64,60 +60,56 @@ public class DataService extends IntentService {
public DataService() { public DataService() {
super("DataService"); super("DataService");
MainApp.subscribe(this); registerBus();
} }
@Override @Override
protected void onHandleIntent(final Intent intent) { protected void onHandleIntent(final Intent intent) {
if (intent == null)
return;
if (Config.logIncommingData)
log.debug("Got intent: " + intent.getAction());
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras()));
if (ConfigBuilderPlugin.getActiveBgSource() == null) { if (ConfigBuilderPlugin.getPlugin().getActiveBgSource() == null) {
xDripEnabled = true; xDripEnabled = true;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = false; mm640gEnabled = false;
glimpEnabled = false; glimpEnabled = false;
dexcomG5Enabled = false; dexcomG5Enabled = false;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) {
xDripEnabled = true; xDripEnabled = true;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = false; mm640gEnabled = false;
glimpEnabled = false; glimpEnabled = false;
dexcomG5Enabled = false; dexcomG5Enabled = false;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceNSClientPlugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceNSClientPlugin.class)) {
xDripEnabled = false; xDripEnabled = false;
nsClientEnabled = true; nsClientEnabled = true;
mm640gEnabled = false; mm640gEnabled = false;
glimpEnabled = false; glimpEnabled = false;
dexcomG5Enabled = false; dexcomG5Enabled = false;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceMM640gPlugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceMM640gPlugin.class)) {
xDripEnabled = false; xDripEnabled = false;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = true; mm640gEnabled = true;
glimpEnabled = false; glimpEnabled = false;
dexcomG5Enabled = false; dexcomG5Enabled = false;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceGlimpPlugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceGlimpPlugin.class)) {
xDripEnabled = false; xDripEnabled = false;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = false; mm640gEnabled = false;
glimpEnabled = true; glimpEnabled = true;
dexcomG5Enabled = false; dexcomG5Enabled = false;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceDexcomG5Plugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceDexcomG5Plugin.class)) {
xDripEnabled = false; xDripEnabled = false;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = false; mm640gEnabled = false;
glimpEnabled = false; glimpEnabled = false;
dexcomG5Enabled = true; dexcomG5Enabled = true;
poctechEnabled = false; poctechEnabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourcePoctechPlugin.class)) { } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourcePoctechPlugin.class)) {
xDripEnabled = false; xDripEnabled = false;
nsClientEnabled = false; nsClientEnabled = false;
mm640gEnabled = false; mm640gEnabled = false;
@ -134,53 +126,55 @@ public class DataService extends IntentService {
acceptNSData = acceptNSData || bundles.getBoolean("islocal"); acceptNSData = acceptNSData || bundles.getBoolean("islocal");
} }
final String action = intent.getAction();
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) {
if (xDripEnabled) {
processNewBgIntent(SourceXdripPlugin.getPlugin(), intent);
}
} else if (Intents.NS_EMULATOR.equals(action)) {
if (mm640gEnabled) {
processNewBgIntent(SourceMM640gPlugin.getPlugin(), intent);
}
} else if (Intents.GLIMP_BG.equals(action)) {
if (glimpEnabled) {
processNewBgIntent(SourceGlimpPlugin.getPlugin(), intent);
}
} else if (Intents.DEXCOMG5_BG.equals(action)) {
if (dexcomG5Enabled) {
processNewBgIntent(SourceDexcomG5Plugin.getPlugin(), intent);
}
} else if (Intents.POCTECH_BG.equals(action)) {
if (poctechEnabled) {
processNewBgIntent(SourcePoctechPlugin.getPlugin(), intent);
}
} else if (Intents.ACTION_NEW_SGV.equals(action)) {
if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true))
handleNewDataFromNSClient(intent);
// Objectives 0
ObjectivesPlugin.bgIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
} else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_DEVICESTATUS.equals(action)) {
// always handle Profile if NSProfile is enabled without looking at nsUploadOnly
handleNewDataFromNSClient(intent);
} else if (acceptNSData &&
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
Intents.ACTION_REMOVED_TREATMENT.equals(action) ||
Intents.ACTION_NEW_STATUS.equals(action) ||
Intents.ACTION_NEW_DEVICESTATUS.equals(action) ||
Intents.ACTION_NEW_FOOD.equals(action) ||
Intents.ACTION_CHANGED_FOOD.equals(action) ||
Intents.ACTION_REMOVED_FOOD.equals(action) ||
Intents.ACTION_NEW_CAL.equals(action) ||
Intents.ACTION_NEW_MBG.equals(action))
) {
handleNewDataFromNSClient(intent);
} else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) {
handleNewSMS(intent);
}
if (intent != null) {
final String action = intent.getAction();
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) {
if (xDripEnabled) {
handleNewDataFromXDrip(intent);
}
} else if (Intents.NS_EMULATOR.equals(action)) {
if (mm640gEnabled) {
handleNewDataFromMM640g(intent);
}
} else if (Intents.GLIMP_BG.equals(action)) {
if (glimpEnabled) {
handleNewDataFromGlimp(intent);
}
} else if (Intents.DEXCOMG5_BG.equals(action)) {
if (dexcomG5Enabled) {
handleNewDataFromDexcomG5(intent);
}
} else if (Intents.POCTECH_BG.equals(action)) {
if (poctechEnabled) {
handleNewDataFromPoctech(intent);
}
} else if (Intents.ACTION_NEW_SGV.equals(action)) {
if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true))
handleNewDataFromNSClient(intent);
// Objectives 0
ObjectivesPlugin.bgIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
} else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_DEVICESTATUS.equals(action)) {
// always handle Profile if NSProfile is enabled without looking at nsUploadOnly
handleNewDataFromNSClient(intent);
} else if (acceptNSData &&
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
Intents.ACTION_REMOVED_TREATMENT.equals(action) ||
Intents.ACTION_NEW_STATUS.equals(action) ||
Intents.ACTION_NEW_DEVICESTATUS.equals(action) ||
Intents.ACTION_NEW_FOOD.equals(action) ||
Intents.ACTION_CHANGED_FOOD.equals(action) ||
Intents.ACTION_REMOVED_FOOD.equals(action) ||
Intents.ACTION_NEW_CAL.equals(action) ||
Intents.ACTION_NEW_MBG.equals(action))
) {
handleNewDataFromNSClient(intent);
} else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) {
handleNewSMS(intent);
}
}
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onHandleIntent exit " + intent); log.debug("onHandleIntent exit " + intent);
DataReceiver.completeWakefulIntent(intent); DataReceiver.completeWakefulIntent(intent);
@ -201,25 +195,167 @@ public class DataService extends IntentService {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
MainApp.unsubscribe(this); MainApp.bus().unregister(this);
} }
private void processNewBgIntent(BgSourceInterface bgSource, Intent intent) { private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
private void handleNewDataFromXDrip(Intent intent) {
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
if (bundle == null) return; if (bundle == null) return;
bgSource.processNewData(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);
String source = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "no Source specified");
SourceXdripPlugin.getPlugin().setSource(source);
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;
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;
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) {
e.printStackTrace();
}
}
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");
MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
break;
default:
log.debug("Unknown entries type: " + type);
}
}
} catch (JSONException e) {
log.error("Got JSON exception: " + e);
}
}
}
} }
private void handleNewDataFromNSClient(Intent intent) { private void handleNewDataFromNSClient(Intent intent) {
Bundle bundle = intent.getExtras(); Bundle bundles = intent.getExtras();
if (bundle == null) return; if (bundles == null) return;
if (Config.logIncommingData)
log.debug("Got intent: " + intent.getAction());
if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) {
if (bundle.containsKey("nsclientversioncode")) { if (bundles.containsKey("nsclientversioncode")) {
ConfigBuilderPlugin.nightscoutVersionCode = bundle.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203 ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
ConfigBuilderPlugin.nightscoutVersionName = bundle.getString("nightscoutversionname"); ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname");
ConfigBuilderPlugin.nsClientVersionCode = bundle.getInt("nsclientversioncode"); // for ver 1.17 contains 117 ConfigBuilderPlugin.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117
ConfigBuilderPlugin.nsClientVersionName = bundle.getString("nsclientversionname"); ConfigBuilderPlugin.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName); log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName);
try { try {
if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) { if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) {
@ -241,9 +377,9 @@ public class DataService extends IntentService {
Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification)); MainApp.bus().post(new EventNewNotification(notification));
} }
if (bundle.containsKey("status")) { if (bundles.containsKey("status")) {
try { try {
JSONObject statusJson = new JSONObject(bundle.getString("status")); JSONObject statusJson = new JSONObject(bundles.getString("status"));
NSSettingsStatus.getInstance().setData(statusJson); NSSettingsStatus.getInstance().setData(statusJson);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Received status: " + statusJson.toString()); log.debug("Received status: " + statusJson.toString());
@ -260,8 +396,8 @@ public class DataService extends IntentService {
} }
if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) { if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) {
try { try {
if (bundle.containsKey("devicestatus")) { if (bundles.containsKey("devicestatus")) {
JSONObject devicestatusJson = new JSONObject(bundle.getString("devicestatus")); JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus"));
NSDeviceStatus.getInstance().setData(devicestatusJson); NSDeviceStatus.getInstance().setData(devicestatusJson);
if (devicestatusJson.has("pump")) { if (devicestatusJson.has("pump")) {
// Objectives 0 // Objectives 0
@ -269,8 +405,8 @@ public class DataService extends IntentService {
ObjectivesPlugin.saveProgress(); ObjectivesPlugin.saveProgress();
} }
} }
if (bundle.containsKey("devicestatuses")) { if (bundles.containsKey("devicestatuses")) {
String devicestatusesstring = bundle.getString("devicestatuses"); String devicestatusesstring = bundles.getString("devicestatuses");
JSONArray jsonArray = new JSONArray(devicestatusesstring); JSONArray jsonArray = new JSONArray(devicestatusesstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject devicestatusJson = jsonArray.getJSONObject(i); JSONObject devicestatusJson = jsonArray.getJSONObject(i);
@ -289,8 +425,8 @@ public class DataService extends IntentService {
// Handle profile // Handle profile
if (intent.getAction().equals(Intents.ACTION_NEW_PROFILE)) { if (intent.getAction().equals(Intents.ACTION_NEW_PROFILE)) {
try { try {
String activeProfile = bundle.getString("activeprofile"); String activeProfile = bundles.getString("activeprofile");
String profile = bundle.getString("profile"); String profile = bundles.getString("profile");
ProfileStore profileStore = new ProfileStore(new JSONObject(profile)); ProfileStore profileStore = new ProfileStore(new JSONObject(profile));
NSProfilePlugin.getPlugin().storeNewProfile(profileStore); NSProfilePlugin.getPlugin().storeNewProfile(profileStore);
MainApp.bus().post(new EventNSProfileUpdateGUI()); MainApp.bus().post(new EventNSProfileUpdateGUI());
@ -303,12 +439,12 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT) || intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) { if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT) || intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) {
try { try {
if (bundle.containsKey("treatment")) { if (bundles.containsKey("treatment")) {
JSONObject json = new JSONObject(bundle.getString("treatment")); JSONObject json = new JSONObject(bundles.getString("treatment"));
handleTreatmentFromNS(json, intent); handleTreatmentFromNS(json, intent);
} }
if (bundle.containsKey("treatments")) { if (bundles.containsKey("treatments")) {
String trstring = bundle.getString("treatments"); String trstring = bundles.getString("treatments");
JSONArray jsonArray = new JSONArray(trstring); JSONArray jsonArray = new JSONArray(trstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i); JSONObject json = jsonArray.getJSONObject(i);
@ -322,14 +458,14 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_REMOVED_TREATMENT)) { if (intent.getAction().equals(Intents.ACTION_REMOVED_TREATMENT)) {
try { try {
if (bundle.containsKey("treatment")) { if (bundles.containsKey("treatment")) {
String trstring = bundle.getString("treatment"); String trstring = bundles.getString("treatment");
JSONObject json = new JSONObject(trstring); JSONObject json = new JSONObject(trstring);
handleTreatmentFromNS(json); handleTreatmentFromNS(json);
} }
if (bundle.containsKey("treatments")) { if (bundles.containsKey("treatments")) {
String trstring = bundle.getString("treatments"); String trstring = bundles.getString("treatments");
JSONArray jsonArray = new JSONArray(trstring); JSONArray jsonArray = new JSONArray(trstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i); JSONObject json = jsonArray.getJSONObject(i);
@ -342,19 +478,36 @@ public class DataService extends IntentService {
} }
if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) { if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) {
SourceNSClientPlugin.getPlugin().processNewData(bundle); try {
if (bundles.containsKey("sgv")) {
String sgvstring = bundles.getString("sgv");
JSONObject sgvJson = new JSONObject(sgvstring);
storeSgv(sgvJson);
}
if (bundles.containsKey("sgvs")) {
String sgvstring = bundles.getString("sgvs");
JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sgvJson = jsonArray.getJSONObject(i);
storeSgv(sgvJson);
}
}
} catch (Exception e) {
log.error("Unhandled exception", e);
}
} }
if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) { if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) {
try { try {
if (bundle.containsKey("mbg")) { if (bundles.containsKey("mbg")) {
String mbgstring = bundle.getString("mbg"); String mbgstring = bundles.getString("mbg");
JSONObject mbgJson = new JSONObject(mbgstring); JSONObject mbgJson = new JSONObject(mbgstring);
storeMbg(mbgJson); storeMbg(mbgJson);
} }
if (bundle.containsKey("mbgs")) { if (bundles.containsKey("mbgs")) {
String sgvstring = bundle.getString("mbgs"); String sgvstring = bundles.getString("mbgs");
JSONArray jsonArray = new JSONArray(sgvstring); JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mbgJson = jsonArray.getJSONObject(i); JSONObject mbgJson = jsonArray.getJSONObject(i);
@ -369,12 +522,12 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_NEW_FOOD) if (intent.getAction().equals(Intents.ACTION_NEW_FOOD)
|| intent.getAction().equals(Intents.ACTION_CHANGED_FOOD)) { || intent.getAction().equals(Intents.ACTION_CHANGED_FOOD)) {
int mode = Intents.ACTION_NEW_FOOD.equals(intent.getAction()) ? EventNsFood.ADD : EventNsFood.UPDATE; int mode = Intents.ACTION_NEW_FOOD.equals(intent.getAction()) ? EventNsFood.ADD : EventNsFood.UPDATE;
EventNsFood evt = new EventNsFood(mode, bundle); EventNsFood evt = new EventNsFood(mode, bundles);
MainApp.bus().post(evt); MainApp.bus().post(evt);
} }
if (intent.getAction().equals(Intents.ACTION_REMOVED_FOOD)) { if (intent.getAction().equals(Intents.ACTION_REMOVED_FOOD)) {
EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundle); EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles);
MainApp.bus().post(evt); MainApp.bus().post(evt);
} }
} }
@ -392,7 +545,7 @@ public class DataService extends IntentService {
MainApp.getDbHelper().deleteProfileSwitchById(_id); MainApp.getDbHelper().deleteProfileSwitchById(_id);
} }
private void handleTreatmentFromNS(JSONObject json, Intent intent) { private void handleTreatmentFromNS(JSONObject json, Intent intent) throws JSONException {
// new DB model // new DB model
int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.ADD : EventNsTreatment.UPDATE; int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.ADD : EventNsTreatment.UPDATE;
double insulin = JsonHelper.safeGetDouble(json, "insulin"); double insulin = JsonHelper.safeGetDouble(json, "insulin");
@ -447,6 +600,13 @@ public class DataService extends IntentService {
log.debug("Adding/Updating new MBG: " + careportalEvent.log()); log.debug("Adding/Updating new MBG: " + careportalEvent.log());
} }
private void storeSgv(JSONObject sgvJson) {
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
SourceNSClientPlugin.getPlugin().detectSource(JsonHelper.safeGetString(sgvJson, "device"), JsonHelper.safeGetLong(sgvJson, "mills"));
}
private void handleNewSMS(Intent intent) { private void handleNewSMS(Intent intent) {
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
if (bundle == null) return; if (bundle == null) return;

View file

@ -42,6 +42,10 @@ public class ConstraintChecker implements ConstraintsInterface {
return isSMBModeEnabled(new Constraint<>(true)); return isSMBModeEnabled(new Constraint<>(true));
} }
public Constraint<Boolean> isAdvancedFilteringEnabled() {
return isAdvancedFilteringEnabled(new Constraint<>(true));
}
public Constraint<Double> getMaxBasalAllowed(Profile profile) { public Constraint<Double> getMaxBasalAllowed(Profile profile) {
return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile); return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile);
} }
@ -65,7 +69,7 @@ public class ConstraintChecker implements ConstraintsInterface {
@Override @Override
public Constraint<Boolean> isLoopInvocationAllowed(Constraint<Boolean> value) { public Constraint<Boolean> isLoopInvocationAllowed(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) { for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p; ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginType.CONSTRAINTS)) continue; if (!p.isEnabled(PluginType.CONSTRAINTS)) continue;
@ -77,7 +81,7 @@ public class ConstraintChecker implements ConstraintsInterface {
@Override @Override
public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) { public Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) { for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p; ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginType.CONSTRAINTS)) continue; if (!p.isEnabled(PluginType.CONSTRAINTS)) continue;
@ -122,6 +126,17 @@ public class ConstraintChecker implements ConstraintsInterface {
return value; return value;
} }
@Override
public Constraint<Boolean> isAdvancedFilteringEnabled(Constraint<Boolean> value) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constraint = (ConstraintsInterface) p;
if (!p.isEnabled(PluginType.CONSTRAINTS)) continue;
constraint.isAdvancedFilteringEnabled(value);
}
return value;
}
@Override @Override
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); ArrayList<PluginBase> constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class);

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.db; package info.nightscout.androidaps.db;
import android.content.res.Resources;
import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; import com.j256.ormlite.table.DatabaseTable;
@ -36,10 +38,6 @@ public class BgReading implements DataPointWithLabelInterface {
public String direction; public String direction;
@DatabaseField @DatabaseField
public double raw; public double raw;
@DatabaseField
public boolean isFiltered;
@DatabaseField
public String sourcePlugin;
@DatabaseField @DatabaseField
public int source = Source.NONE; public int source = Source.NONE;
@ -122,11 +120,19 @@ public class BgReading implements DataPointWithLabelInterface {
", value=" + value + ", value=" + value +
", direction=" + direction + ", direction=" + direction +
", raw=" + raw + ", raw=" + raw +
", filtered=" + isFiltered +
", sourcePlugin=" + sourcePlugin +
'}'; '}';
} }
public boolean isDataChanging(BgReading other) {
if (date != other.date) {
log.error("Comparing different");
return false;
}
if (value != other.value)
return true;
return false;
}
public boolean isEqual(BgReading other) { public boolean isEqual(BgReading other) {
if (date != other.date) { if (date != other.date) {
log.error("Comparing different"); log.error("Comparing different");

View file

@ -28,6 +28,7 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.OverlappingIntervals;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
@ -117,28 +118,12 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
TableUtils.createTableIfNotExists(connectionSource, TDD.class); TableUtils.createTableIfNotExists(connectionSource, TDD.class);
// soft migration without changing DB version
createColumnIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS,
"isFiltered", "integer");
createColumnIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS,
"sourcePlugin", "text");
} catch (SQLException e) { } catch (SQLException e) {
log.error("Can't create database", e); log.error("Can't create database", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private void createColumnIfNotExists(Dao dao, String table, String name, String type) {
try {
final String statement = "ALTER TABLE `" + table + "` ADD COLUMN `" + name + "` " + type;
dao.executeRaw(statement);
} catch (SQLException e) {
// row already exists
}
}
@Override @Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try { try {

View file

@ -1,14 +1,8 @@
package info.nightscout.androidaps.interfaces; package info.nightscout.androidaps.interfaces;
import android.os.Bundle;
import java.util.List;
import info.nightscout.androidaps.db.BgReading;
/** /**
* Created by mike on 20.06.2016. * Created by mike on 20.06.2016.
*/ */
public interface BgSourceInterface { public interface BgSourceInterface {
List<BgReading> processNewData(Bundle bundle); boolean advancedFilteringSupported();
} }

View file

@ -27,6 +27,10 @@ public interface ConstraintsInterface {
return value; return value;
} }
default Constraint<Boolean> isAdvancedFilteringEnabled(Constraint<Boolean> value) {
return value;
}
default Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { default Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
return absoluteRate; return absoluteRate;
} }

View file

@ -65,11 +65,12 @@ public class ConfigBuilderPlugin extends PluginBase {
return configBuilderPlugin; return configBuilderPlugin;
} }
private static BgSourceInterface activeBgSource; private BgSourceInterface activeBgSource;
private static PumpInterface activePump; private static PumpInterface activePump;
private static ProfileInterface activeProfile; private static ProfileInterface activeProfile;
private static TreatmentsInterface activeTreatments; private static TreatmentsInterface activeTreatments;
private static APSInterface activeAPS; private static APSInterface activeAPS;
private static LoopPlugin activeLoop;
private static InsulinInterface activeInsulin; private static InsulinInterface activeInsulin;
private static SensitivityInterface activeSensitivity; private static SensitivityInterface activeSensitivity;
@ -256,7 +257,7 @@ public class ConfigBuilderPlugin extends PluginBase {
return commandQueue; return commandQueue;
} }
public static BgSourceInterface getActiveBgSource() { public BgSourceInterface getActiveBgSource() {
return activeBgSource; return activeBgSource;
} }
@ -336,6 +337,9 @@ public class ConfigBuilderPlugin extends PluginBase {
} }
this.setFragmentVisiblities(((PluginBase) activePump).getName(), pluginsInCategory, PluginType.PUMP); this.setFragmentVisiblities(((PluginBase) activePump).getName(), pluginsInCategory, PluginType.PUMP);
// PluginType.LOOP
activeLoop = this.determineActivePlugin(PluginType.LOOP);
// PluginType.TREATMENT // PluginType.TREATMENT
activeTreatments = this.determineActivePlugin(PluginType.TREATMENT); activeTreatments = this.determineActivePlugin(PluginType.TREATMENT);
} }

View file

@ -92,6 +92,17 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface {
return value; return value;
} }
@Override
public Constraint<Boolean> isAdvancedFilteringEnabled(Constraint<Boolean> value) {
BgSourceInterface bgSource = MainApp.getConfigBuilder().getActiveBgSource();
if (bgSource != null) {
if (!bgSource.advancedFilteringSupported())
value.set(false, MainApp.gs(R.string.smbalwaysdisabled), this);
}
return value;
}
@Override @Override
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {

View file

@ -20,7 +20,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Date; import java.util.Date;
import java.util.Objects;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
@ -33,6 +32,7 @@ import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.events.Event;
import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
@ -57,9 +57,11 @@ import info.nightscout.utils.SP;
public class LoopPlugin extends PluginBase { public class LoopPlugin extends PluginBase {
private static Logger log = LoggerFactory.getLogger(LoopPlugin.class); private static Logger log = LoggerFactory.getLogger(LoopPlugin.class);
private static final String CHANNEL_ID = "AndroidAPS-Openloop"; public static final String CHANNEL_ID = "AndroidAPS-Openloop";
private long lastBgTriggeredRun = 0;
private static LoopPlugin loopPlugin; long lastBgTriggeredRun = 0;
protected static LoopPlugin loopPlugin;
@NonNull @NonNull
public static LoopPlugin getPlugin() { public static LoopPlugin getPlugin() {
@ -155,20 +157,15 @@ public class LoopPlugin extends PluginBase {
// already looped with that value // already looped with that value
return; return;
} }
PluginBase bgSource = (PluginBase) ConfigBuilderPlugin.getActiveBgSource();
if (bgSource == null) {
// no BG source active
return;
}
if (!Objects.equals(bgReading.sourcePlugin, bgSource.getName())) {
// reading not from active BG source (likely coming in from NS)
return;
}
lastBgTriggeredRun = bgReading.date; lastBgTriggeredRun = bgReading.date;
invoke("AutosenseCalculation for " + bgReading, true); invoke("AutosenseCalculation for " + bgReading, true);
} }
public long suspendedTo() {
return loopSuspendedTill;
}
public void suspendTo(long endTime) { public void suspendTo(long endTime) {
loopSuspendedTill = endTime; loopSuspendedTill = endTime;
isSuperBolus = false; isSuperBolus = false;

View file

@ -14,8 +14,6 @@ import info.nightscout.androidaps.data.GlucoseStatus;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.MealData;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.APSInterface;
@ -106,7 +104,6 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
} }
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
BgReading bgReading = DatabaseHelper.actualBg();
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
PumpInterface pump = ConfigBuilderPlugin.getActivePump(); PumpInterface pump = ConfigBuilderPlugin.getActivePump();
@ -124,7 +121,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
return; return;
} }
if (glucoseStatus == null || bgReading == null) { if (glucoseStatus == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
if (Config.logAPSResult) if (Config.logAPSResult)
log.debug(MainApp.gs(R.string.openapsma_noglucosedata)); log.debug(MainApp.gs(R.string.openapsma_noglucosedata));
@ -192,11 +189,9 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed);
inputConstraints.copyReasons(smbAllowed); inputConstraints.copyReasons(smbAllowed);
Constraint<Boolean> bgFiltered = new Constraint<>(bgReading.isFiltered); Constraint<Boolean> advancedFiltering = new Constraint<>(!tempBasalFallback);
if (!bgReading.isFiltered) { MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering);
bgFiltered.set(false, MainApp.gs(R.string.smbalwaysdisabled), this); inputConstraints.copyReasons(advancedFiltering);
}
inputConstraints.copyReasons(bgFiltered);
Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart);
Profiler.log(log, "SMB data gathering", start); Profiler.log(log, "SMB data gathering", start);
@ -207,7 +202,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
lastAutosensResult.ratio, //autosensDataRatio lastAutosensResult.ratio, //autosensDataRatio
isTempTarget, isTempTarget,
smbAllowed.value(), smbAllowed.value(),
bgReading.isFiltered advancedFiltering.value()
); );
} catch (JSONException e) { } catch (JSONException e) {
log.error(e.getMessage()); log.error(e.getMessage());

View file

@ -1,33 +1,17 @@
package info.nightscout.androidaps.plugins.Source; 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 java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP;
/** /**
* Created by mike on 28.11.2017. * Created by mike on 28.11.2017.
*/ */
public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterface { public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterface {
private static final Logger log = LoggerFactory.getLogger(SourceDexcomG5Plugin.class);
private static SourceDexcomG5Plugin plugin = null; private static SourceDexcomG5Plugin plugin = null;
@ -50,39 +34,7 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
List<BgReading> bgReadings = new ArrayList<>(); return true;
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 bgReading = new BgReading();
bgReading.value = json.getInt("m_value");
bgReading.direction = json.getString("m_trend");
bgReading.date = json.getLong("m_time") * 1000L;
bgReading.raw = 0;
bgReading.isFiltered = true;
bgReading.sourcePlugin = getName();
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
if (isNew) {
bgReadings.add(bgReading);
if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) {
NSUpload.uploadBg(bgReading);
}
if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) {
NSUpload.sendToXdrip(bgReading);
}
}
}
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return bgReadings;
} }
} }

View file

@ -1,15 +1,6 @@
package info.nightscout.androidaps.plugins.Source; package info.nightscout.androidaps.plugins.Source;
import android.os.Bundle;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
@ -38,17 +29,7 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface {
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
BgReading bgReading = new BgReading(); return false;
bgReading.value = bundle.getDouble("mySGV");
bgReading.direction = bundle.getString("myTrend");
bgReading.date = bundle.getLong("myTimestamp");
bgReading.raw = 0;
bgReading.isFiltered = false;
bgReading.sourcePlugin = getName();
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList();
} }
} }

View file

@ -1,20 +1,6 @@
package info.nightscout.androidaps.plugins.Source; 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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
@ -24,8 +10,6 @@ import info.nightscout.androidaps.interfaces.PluginType;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface { public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface {
private static final Logger log = LoggerFactory.getLogger(SourceMM640gPlugin.class);
private static SourceMM640gPlugin plugin = null; private static SourceMM640gPlugin plugin = null;
public static SourceMM640gPlugin getPlugin() { public static SourceMM640gPlugin getPlugin() {
@ -44,44 +28,7 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
List<BgReading> bgReadings = new ArrayList<>(); return false;
if (Objects.equals(bundle.getString("collection"), "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.isFiltered = true;
bgReading.sourcePlugin = getName();
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
if (isNew) {
bgReadings.add(bgReading);
}
break;
default:
log.debug("Unknown entries type: " + type);
}
}
} catch (JSONException e) {
log.error("Got JSON exception: " + e);
}
}
}
return bgReadings;
} }
} }

View file

@ -1,31 +1,16 @@
package info.nightscout.androidaps.plugins.Source; package info.nightscout.androidaps.plugins.Source;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
import info.nightscout.utils.JsonHelper;
/** /**
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class SourceNSClientPlugin extends PluginBase implements BgSourceInterface { public class SourceNSClientPlugin extends PluginBase implements BgSourceInterface {
private static final Logger log = LoggerFactory.getLogger(SourceNSClientPlugin.class);
private static SourceNSClientPlugin plugin = null; private static SourceNSClientPlugin plugin = null;
@ -35,6 +20,9 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac
return plugin; return plugin;
} }
private long lastBGTimeStamp = 0;
private boolean isAdvancedFilteringEnabled = false;
private SourceNSClientPlugin() { private SourceNSClientPlugin() {
super(new PluginDescription() super(new PluginDescription()
.mainType(PluginType.BGSOURCE) .mainType(PluginType.BGSOURCE)
@ -47,39 +35,17 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
List<BgReading> sgvs = new ArrayList<>(); return isAdvancedFilteringEnabled;
try {
if (bundle.containsKey("sgv")) {
String sgvstring = bundle.getString("sgv");
JSONObject sgvJson = new JSONObject(sgvstring);
BgReading bgReading = new BgReading(new NSSgv(sgvJson));
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
if (isNew) {
sgvs.add(bgReading);
}
}
if (bundle.containsKey("sgvs")) {
String sgvstring = bundle.getString("sgvs");
JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sgvJson = jsonArray.getJSONObject(i);
BgReading bgReading = new BgReading(new NSSgv(sgvJson));
String sourceDescription = JsonHelper.safeGetString(sgvJson, "device");
bgReading.isFiltered = sourceDescription != null
&& (sourceDescription.contains("G5 Native") || sourceDescription.contains("AndroidAPS-DexcomG5"));
bgReading.sourcePlugin = getName();
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
if (isNew) {
sgvs.add(bgReading);
}
}
}
} catch (Exception e) {
log.error("Unhandled exception", e);
}
return sgvs;
} }
public void detectSource(String source, long timeStamp) {
if (timeStamp > lastBGTimeStamp) {
if (source.contains("G5 Native") || source.contains("AndroidAPS-DexcomG5"))
isAdvancedFilteringEnabled = true;
else
isAdvancedFilteringEnabled = false;
lastBGTimeStamp = timeStamp;
}
}
} }

View file

@ -1,34 +1,16 @@
package info.nightscout.androidaps.plugins.Source; 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 java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; 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. * Created by mike on 05.08.2016.
*/ */
public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface { public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface {
private static final Logger log = LoggerFactory.getLogger(SourcePoctechPlugin.class);
private static SourcePoctechPlugin plugin = null; private static SourcePoctechPlugin plugin = null;
@ -50,40 +32,8 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
List<BgReading> bgReadings = new ArrayList<>(); return false;
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 bgReading = new BgReading();
bgReading.value = json.getDouble("current");
bgReading.direction = json.getString("direction");
bgReading.date = json.getLong("date");
bgReading.raw = json.getDouble("raw");
bgReading.isFiltered = false;
bgReading.sourcePlugin = getName();
if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L"))
bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL;
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
if (isNew) {
bgReadings.add(bgReading);
if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) {
NSUpload.uploadBg(bgReading);
}
if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) {
NSUpload.sendToXdrip(bgReading);
}
}
}
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return bgReadings;
} }
} }

View file

@ -1,19 +1,6 @@
package info.nightscout.androidaps.plugins.Source; package info.nightscout.androidaps.plugins.Source;
import android.os.Bundle;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; 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.BgSourceInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginDescription;
@ -23,9 +10,10 @@ import info.nightscout.androidaps.interfaces.PluginType;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { public class SourceXdripPlugin extends PluginBase implements BgSourceInterface {
private static final Logger log = LoggerFactory.getLogger(SourceXdripPlugin.class);
private static SourceXdripPlugin plugin = null; private static SourceXdripPlugin plugin = null;
boolean advancedFiltering;
public static SourceXdripPlugin getPlugin() { public static SourceXdripPlugin getPlugin() {
if (plugin == null) if (plugin == null)
@ -43,18 +31,11 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface {
} }
@Override @Override
public List<BgReading> processNewData(Bundle bundle) { public boolean advancedFilteringSupported() {
BgReading bgReading = new BgReading(); return advancedFiltering;
}
bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); public void setSource(String source) {
bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); this.advancedFiltering = source.contains("G5 Native");
bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "");
bgReading.isFiltered = sourceDescription.equals("G5 Native");
bgReading.sourcePlugin = getName();
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName());
return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList();
} }
} }

View file

@ -180,7 +180,7 @@ public class CommandQueue {
if (type == Command.CommandType.SMB_BOLUS) { if (type == Command.CommandType.SMB_BOLUS) {
if (isRunning(Command.CommandType.BOLUS) || bolusInQueue()) { if (isRunning(Command.CommandType.BOLUS) || bolusInQueue()) {
log.debug("Rejecting SMB since a bolus is queued/running"); log.debug("Rejecting SMB since a bolus is queue/running");
return false; return false;
} }
if (detailedBolusInfo.lastKnownBolusTime < TreatmentsPlugin.getPlugin().getLastBolusTime()) { if (detailedBolusInfo.lastKnownBolusTime < TreatmentsPlugin.getPlugin().getLastBolusTime()) {

View file

@ -469,8 +469,6 @@ public class NSUpload {
data.put("sgv", reading.value); data.put("sgv", reading.value);
data.put("direction", reading.direction); data.put("direction", reading.direction);
data.put("type", "sgv"); data.put("type", "sgv");
data.put("isFiltered", reading.isFiltered);
data.put("sourcePlugin", reading.sourcePlugin);
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }

View file

@ -111,6 +111,16 @@ public class ConstraintsCheckerTest {
Assert.assertEquals(Boolean.FALSE, c.value()); Assert.assertEquals(Boolean.FALSE, c.value());
} }
@Test
public void isAdvancedFilteringEnabledTest() throws Exception {
when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin());
Constraint<Boolean> c = constraintChecker.isAdvancedFilteringEnabled();
Assert.assertEquals(true, c.getReasonList().size() == 1); // Safety
Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 1); // Safety
Assert.assertEquals(Boolean.FALSE, c.value());
}
@Test @Test
public void isSMBModeEnabledTest() throws Exception { public void isSMBModeEnabledTest() throws Exception {
objectivesPlugin.objectives.get(7).setStartedOn(null); objectivesPlugin.objectives.get(7).setStartedOn(null);
@ -223,14 +233,13 @@ public class ConstraintsCheckerTest {
// No limit by default // No limit by default
when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d);
when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); when(SP.getString(R.string.key_age, "")).thenReturn("teenage");
OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true);
OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true);
OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false);
OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, false);
// Apply all limits // Apply all limits
Constraint<Double> d = constraintChecker.getMaxIOBAllowed(); Constraint<Double> d = constraintChecker.getMaxIOBAllowed();
Assert.assertEquals(1.5d, d.value()); Assert.assertEquals(1.5d, d.value());
Assert.assertEquals(d.getReasonList().toString(),2, d.getReasonList().size()); Assert.assertEquals(3, d.getReasonList().size());
Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons());
} }
@ -241,13 +250,11 @@ public class ConstraintsCheckerTest {
when(SP.getDouble(R.string.key_openapssmb_max_iob, 3d)).thenReturn(3d); when(SP.getDouble(R.string.key_openapssmb_max_iob, 3d)).thenReturn(3d);
when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); when(SP.getString(R.string.key_age, "")).thenReturn("teenage");
OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true);
OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false);
OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false);
// Apply all limits // Apply all limits
Constraint<Double> d = constraintChecker.getMaxIOBAllowed(); Constraint<Double> d = constraintChecker.getMaxIOBAllowed();
Assert.assertEquals(3d, d.value()); Assert.assertEquals(3d, d.value());
Assert.assertEquals(d.getReasonList().toString(), 2, d.getReasonList().size()); Assert.assertEquals(4, d.getReasonList().size());
Assert.assertEquals("Safety: Limiting IOB to 3.0 U because of max value in preferences", d.getMostLimitedReasons()); Assert.assertEquals("Safety: Limiting IOB to 3.0 U because of max value in preferences", d.getMostLimitedReasons());
} }

View file

@ -89,6 +89,16 @@ public class SafetyPluginTest {
Assert.assertEquals(Boolean.FALSE, c.value()); Assert.assertEquals(Boolean.FALSE, c.value());
} }
@Test
public void bgsourceShouldPreventSMBAlways() throws Exception {
when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin());
Constraint<Boolean> c = new Constraint<>(true);
c = safetyPlugin.isAdvancedFilteringEnabled(c);
Assert.assertEquals("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering", c.getReasons());
Assert.assertEquals(Boolean.FALSE, c.value());
}
@Test @Test
public void basalRateShouldBeLimited() throws Exception { public void basalRateShouldBeLimited() throws Exception {
when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d);

View file

@ -14,4 +14,9 @@ public class SourceDexcomG5PluginTest {
public void getPlugin() { public void getPlugin() {
Assert.assertNotEquals(null, SourceDexcomG5Plugin.getPlugin()); Assert.assertNotEquals(null, SourceDexcomG5Plugin.getPlugin());
} }
@Test
public void advancedFilteringSupported() {
Assert.assertEquals(true, SourceDexcomG5Plugin.getPlugin().advancedFilteringSupported());
}
} }

View file

@ -14,4 +14,9 @@ public class SourceGlimpPluginTest {
public void getPlugin() { public void getPlugin() {
Assert.assertNotEquals(null, SourceGlimpPlugin.getPlugin()); Assert.assertNotEquals(null, SourceGlimpPlugin.getPlugin());
} }
@Test
public void advancedFilteringSupported() {
Assert.assertEquals(false, SourceGlimpPlugin.getPlugin().advancedFilteringSupported());
}
} }

View file

@ -14,4 +14,9 @@ public class SourceMM640gPluginTest {
public void getPlugin() { public void getPlugin() {
Assert.assertNotEquals(null, SourceMM640gPlugin.getPlugin()); Assert.assertNotEquals(null, SourceMM640gPlugin.getPlugin());
} }
@Test
public void advancedFilteringSupported() {
Assert.assertEquals(false, SourceMM640gPlugin.getPlugin().advancedFilteringSupported());
}
} }

View file

@ -12,4 +12,9 @@ public class SourceNSClientPluginTest {
public void getPlugin() { public void getPlugin() {
Assert.assertNotEquals(null, SourceNSClientPlugin.getPlugin()); Assert.assertNotEquals(null, SourceNSClientPlugin.getPlugin());
} }
@Test
public void advancedFilteringSupported() {
Assert.assertEquals(false, SourceNSClientPlugin.getPlugin().advancedFilteringSupported());
}
} }

View file

@ -1,99 +1,22 @@
package info.nightscout.androidaps.plugins.Source; package info.nightscout.androidaps.plugins.Source;
import android.os.Bundle;
import android.support.annotation.NonNull;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.plugins.Source.SourceXdripPlugin;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.DatabaseHelper;
import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, DatabaseHelper.class})
public class SourceXdripPluginTest { public class SourceXdripPluginTest {
private SourceXdripPlugin plugin = SourceXdripPlugin.getPlugin();
@Before
public void prepareTest() {
PowerMockito.mockStatic(MainApp.class);
when(MainApp.gs(0)).thenReturn("");
DatabaseHelper databaseHelper = mock(DatabaseHelper.class);
when(MainApp.getDbHelper()).thenReturn(databaseHelper);
when(databaseHelper.createIfNotExists(any(), any())).thenReturn(true);
}
@Test @Test
public void pluginInitializes() { public void getPlugin() {
Assert.assertNotEquals(null, SourceXdripPlugin.getPlugin()); Assert.assertNotEquals(null, SourceXdripPlugin.getPlugin());
} }
// TODO
@Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved")
@Test @Test
public void bgWithUnknownSourceIsMarkedUnfiltered() { public void advancedFilteringSupported() {
Bundle bundle = createBroadcastBundle(); Assert.assertEquals(false, SourceXdripPlugin.getPlugin().advancedFilteringSupported());
BgReading bgReadings = plugin.processNewData(bundle).get(0);
assertFalse(bgReadings.isFiltered);
}
// TODO
@Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved")
@Test
public void bgWithSourceG5NativeIsMarkedFiltered() {
Bundle bundle = createBroadcastBundle();
bundle.putString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "G5 Native");
BgReading bgReadings = plugin.processNewData(bundle).get(0);
assertTrue(bgReadings.isFiltered);
}
/*
// TODO
@Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved")
@Test
public void bgWithWithGoodNoiseIsMarkedFiltered() {
Bundle bundle = createBroadcastBundle();
bundle.putString(Intents.EXTRA_NOISE, "1.0");
BgReading bgReadings = plugin.processNewData(bundle).get(0);
assertTrue(bgReadings.isFiltered);
}
// TODO
@Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved")
@Test
public void bgWithWithExcessiveNoiseDataIsMarkedFiltered() {
Bundle bundle = createBroadcastBundle();
bundle.putString(Intents.EXTRA_NOISE, "80.0");
BgReading bgReadings = plugin.processNewData(bundle).get(0);
assertTrue(bgReadings.isFiltered);
}
*/
@NonNull
private Bundle createBroadcastBundle() {
Bundle bundle = new Bundle();
bundle.putDouble(Intents.EXTRA_BG_ESTIMATE, 100.0);
bundle.putString(Intents.EXTRA_BG_SLOPE_NAME, "DoubleDown");
bundle.putLong(Intents.EXTRA_TIMESTAMP, 0L);
bundle.putDouble(Intents.EXTRA_RAW, 430.0);
return bundle;
} }
} }