From 8a6a3d2283d8e2eca6441ff0f13e5613f50571e5 Mon Sep 17 00:00:00 2001 From: Geoffrey Hansen Date: Thu, 3 Sep 2020 18:01:42 +0200 Subject: [PATCH] OH uploader enqueue Treatments and don_t load historical values --- .../general/openhumans/OHUploadWorker.kt | 1 - .../openhumans/OpenHumansLoginActivity.kt | 1 - .../general/openhumans/OpenHumansUploader.kt | 19 ++++++- .../plugins/treatments/TreatmentService.java | 57 +++++++++++++++++-- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt index 672bdb65cf..d9ed370aa9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt @@ -9,7 +9,6 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.Single import javax.inject.Inject -//TODO OH: make injectable class OHUploadWorker(context: Context, workerParameters: WorkerParameters) : RxWorker(context, workerParameters) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt index 3603ef0328..9220457bbb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt @@ -43,7 +43,6 @@ class OpenHumansLoginActivity : NoSplashAppCompatActivity() { } } - //TODO OH: make injectable class ExchangeAuthTokenDialog : DaggerDialogFragment() { @Inject diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt index 97d2a8e54a..465c6e54dc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt @@ -162,6 +162,23 @@ class OpenHumansUploader @Inject constructor( put("nsId", bgReading._id) } + @JvmOverloads + fun enqueueTreatment(treatment: Treatment, deleted: Boolean = false) = insertQueueItem("Treatments") { + put("date", treatment.date) + put("isValid", treatment.isValid) + put("source", treatment.source) + put("nsId", treatment._id) + put("boluscalc", treatment.boluscalc) + put("carbs", treatment.carbs) + put("dia", treatment.dia) + put("insulin", treatment.insulin) + put("insulinInterfaceID", treatment.insulinInterfaceID) + put("isSMB", treatment.isSMB) + put("mealBolus", treatment.mealBolus) + put("bolusCalcJson", treatment.getBoluscalc()) + put("isDeletion", deleted) + } + @JvmOverloads fun enqueueCareportalEvent(careportalEvent: CareportalEvent, deleted: Boolean = false) = insertQueueItem("CareportalEvents") { put("date", careportalEvent.date) @@ -306,7 +323,7 @@ class OpenHumansUploader @Inject constructor( .flatMap { openHumansAPI.getProjectMemberId(it.accessToken) } .doOnSuccess { projectMemberId = it - copyExistingDataToQueue() + // TODO: halted for now. Might create too much upload data. copyExistingDataToQueue() rxBus.send(OpenHumansFragment.UpdateViewEvent) } .doOnError { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 34ab6bde72..4bc5cf9ec7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -42,6 +42,7 @@ import info.nightscout.androidaps.interfaces.DatabaseHelperInterface; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; +import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; @@ -63,6 +64,7 @@ public class TreatmentService extends OrmLiteBaseService { @Inject RxBusWrapper rxBus; @Inject MedtronicPumpPlugin medtronicPumpPlugin; @Inject DatabaseHelperInterface databaseHelper; + @Inject OpenHumansUploader openHumansUploader; private CompositeDisposable disposable = new CompositeDisposable(); @@ -107,9 +109,9 @@ public class TreatmentService extends OrmLiteBaseService { } } - public Dao getDao() { + public TreatmentDaoWrapper getDao() { try { - return DaoManager.createDao(this.getConnectionSource(), Treatment.class); + return new TreatmentDaoWrapper(DaoManager.createDao(this.getConnectionSource(), Treatment.class)); } catch (SQLException e) { aapsLogger.error("Cannot create Dao for Treatment.class"); } @@ -117,6 +119,49 @@ public class TreatmentService extends OrmLiteBaseService { return null; } + class TreatmentDaoWrapper { + private final Dao wrapped; + + TreatmentDaoWrapper(Dao wrapped) { + this.wrapped = wrapped; + } + + public void executeRaw(String statement, String... arguments) throws SQLException { + wrapped.executeRaw(statement, arguments); + } + + public List queryForAll() throws SQLException { + return wrapped.queryForAll(); + } + + public void delete(Treatment data) throws SQLException { + wrapped.delete(data); + openHumansUploader.enqueueTreatment(data, true); + } + + public void create(Treatment data) throws SQLException { + wrapped.create(data); + openHumansUploader.enqueueTreatment(data); + } + + public Treatment queryForId(long id) throws SQLException { + return wrapped.queryForId(id); + } + + public void update(Treatment data) throws SQLException { + wrapped.update(data); + openHumansUploader.enqueueTreatment(data); + } + + public QueryBuilder queryBuilder() { + return wrapped.queryBuilder(); + } + + public List query(PreparedQuery data) throws SQLException { + return wrapped.query(data); + } + } + @Override public void onCreate() { super.onCreate(); @@ -308,7 +353,7 @@ public class TreatmentService extends OrmLiteBaseService { //preserve carbs if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { treatment.carbs = existingTreatment.carbs; - // preserve insulin + // preserve insulin } else if (existingTreatment.isValid && existingTreatment.insulin > 0 && treatment.insulin == 0) { treatment.insulin = existingTreatment.insulin; } @@ -702,7 +747,7 @@ public class TreatmentService extends OrmLiteBaseService { @Nullable public Treatment findByNSId(String _id) { try { - Dao daoTreatments = getDao(); + TreatmentDaoWrapper daoTreatments = getDao(); QueryBuilder queryBuilder = daoTreatments.queryBuilder(); Where where = queryBuilder.where(); where.eq("_id", _id); @@ -724,7 +769,7 @@ public class TreatmentService extends OrmLiteBaseService { public List getTreatmentDataFromTime(long mills, boolean ascending) { try { - Dao daoTreatments = getDao(); + TreatmentDaoWrapper daoTreatments = getDao(); List treatments; QueryBuilder queryBuilder = daoTreatments.queryBuilder(); queryBuilder.orderBy("date", ascending); @@ -741,7 +786,7 @@ public class TreatmentService extends OrmLiteBaseService { public List getTreatmentDataFromTime(long from, long to, boolean ascending) { try { - Dao daoTreatments = getDao(); + TreatmentDaoWrapper daoTreatments = getDao(); List treatments; QueryBuilder queryBuilder = daoTreatments.queryBuilder(); queryBuilder.orderBy("date", ascending);