From 75c13cef8ffdc5ef835b668215e61e9f0617f0ac Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 14 Jun 2019 10:52:41 +0200 Subject: [PATCH] bus observeOn, Tandem as manufacture --- .../info/nightscout/androidaps/plugins/bus/RxBus.kt | 11 ++++++++--- .../plugins/general/tidepool/TidepoolPlugin.kt | 7 +++++++ .../plugins/general/tidepool/comm/TidepoolCallback.kt | 5 +++-- .../plugins/general/tidepool/comm/TidepoolUploader.kt | 2 ++ .../general/tidepool/elements/ProfileElement.kt | 3 ++- .../general/tidepool/events/EventTidepoolDoUpload.kt | 4 +++- .../general/tidepool/messages/DatasetReplyMessage.kt | 2 +- .../tidepool/messages/OpenDatasetRequestMessage.kt | 8 ++++---- 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/bus/RxBus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/bus/RxBus.kt index 724790df07..1774df1471 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/bus/RxBus.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/bus/RxBus.kt @@ -1,18 +1,23 @@ package info.nightscout.androidaps.plugins.bus +import info.nightscout.androidaps.events.Event import io.reactivex.Observable +import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject // Use object so we have a singleton instance object RxBus { - private val publisher = PublishSubject.create() + private val publisher = PublishSubject.create() - fun send(event: Any) { + fun send(event: Event) { publisher.onNext(event) } // Listen should return an Observable and not the publisher // Using ofType we filter only events that match that class type - fun toObservable(eventType: Class): Observable = publisher.ofType(eventType) + fun toObservable(eventType: Class): Observable = + publisher + .subscribeOn(Schedulers.io()) + .ofType(eventType) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index 022a0bc75b..1db9ab1ce7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -26,6 +26,7 @@ import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.ToastUtils import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers import org.slf4j.LoggerFactory import java.util.* @@ -53,9 +54,11 @@ object TidepoolPlugin : PluginBase(PluginDescription() super.onStart() disposable += RxBus .toObservable(EventTidepoolDoUpload::class.java) + .observeOn(Schedulers.io()) .subscribe({ doUpload() }, {}) disposable += RxBus .toObservable(EventTidepoolResetData::class.java) + .observeOn(Schedulers.io()) .subscribe({ if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) { log.debug("Not connected for delete Dataset") @@ -67,9 +70,11 @@ object TidepoolPlugin : PluginBase(PluginDescription() }, {}) disposable += RxBus .toObservable(EventTidepoolStatus::class.java) + .observeOn(Schedulers.io()) .subscribe({ event -> addToLog(event) }, {}) disposable += RxBus .toObservable(EventNewBG::class.java) + .observeOn(Schedulers.io()) .subscribe({ event -> if (event.bgReading!!.date < TidepoolUploader.getLastEnd()) TidepoolUploader.setLastEnd(event.bgReading.date) @@ -81,6 +86,7 @@ object TidepoolPlugin : PluginBase(PluginDescription() }, {}) disposable += RxBus .toObservable(EventPreferenceChange::class.java) + .observeOn(Schedulers.io()) .subscribe({ event -> if (event.isChanged(R.string.key_tidepool_dev_servers) || event.isChanged(R.string.key_tidepool_username) @@ -90,6 +96,7 @@ object TidepoolPlugin : PluginBase(PluginDescription() }, {}) disposable += RxBus .toObservable(EventNetworkChange::class.java) + .observeOn(Schedulers.io()) .subscribe({}, {}) // TODO start upload on wifi connect } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt index f6ae47dbc7..58ba913e91 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.tidepool.comm import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus import org.slf4j.LoggerFactory import retrofit2.Call import retrofit2.Callback @@ -19,7 +20,7 @@ internal class TidepoolCallback(private val session: Session, val name: Strin } else { val msg = name + " was not successful: " + response.code() + " " + response.message() if (L.isEnabled(L.TIDEPOOL)) log.debug(msg) - RxBus.send(msg) + RxBus.send(EventTidepoolStatus(msg)) onFail() } } @@ -27,7 +28,7 @@ internal class TidepoolCallback(private val session: Session, val name: Strin override fun onFailure(call: Call, t: Throwable) { val msg = "$name Failed: $t" if (L.isEnabled(L.TIDEPOOL)) log.debug(msg) - RxBus.send(msg) + RxBus.send(EventTidepoolStatus(msg)) onFail() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt index afec069623..7271eee199 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt @@ -40,6 +40,8 @@ object TidepoolUploader { DISCONNECTED, CONNECTING, CONNECTED, FAILED } + val PUMPTYPE = "Tandem" + var connectionStatus: ConnectionStatus = TidepoolUploader.ConnectionStatus.DISCONNECTED fun getRetrofitInstance(): Retrofit? { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt index 71ff8bd618..64cab1ab9b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt @@ -4,6 +4,7 @@ import com.google.gson.annotations.Expose import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.db.ProfileSwitch import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader import info.nightscout.androidaps.utils.InstanceId import java.util.* import kotlin.collections.ArrayList @@ -24,7 +25,7 @@ class ProfileElement(ps: ProfileSwitch) @Expose internal var insulinSensitivities: IsfProfile = IsfProfile() @Expose - internal var deviceId: String = (ConfigBuilderPlugin.getPlugin().activePump?.model() ?: "Medtronic") + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId()) + internal var deviceId: String = TidepoolUploader.PUMPTYPE + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId()) @Expose internal var deviceSerialNumber: String = ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId() @Expose diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolDoUpload.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolDoUpload.kt index cb5a9966b8..d20868f348 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolDoUpload.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolDoUpload.kt @@ -1,3 +1,5 @@ package info.nightscout.androidaps.plugins.general.tidepool.events -class EventTidepoolDoUpload \ No newline at end of file +import info.nightscout.androidaps.events.Event + +class EventTidepoolDoUpload : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/DatasetReplyMessage.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/DatasetReplyMessage.kt index 7809ddbce0..f19070624b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/DatasetReplyMessage.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/DatasetReplyMessage.kt @@ -37,6 +37,6 @@ class DatasetReplyMessage { } fun getUploadId(): String? { - return if (data != null && data!!.uploadId != null) data!!.uploadId else uploadId + return data?.uploadId ?: uploadId } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt index 4300b2d46b..19ca54410a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.tidepool.messages import com.google.gson.annotations.Expose import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.T @@ -11,8 +12,7 @@ import java.util.* class OpenDatasetRequestMessage : BaseMessage() { @Expose - var deviceId: String = (ConfigBuilderPlugin.getPlugin().activePump?.model() - ?: "Medtronic") + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() + var deviceId: String = TidepoolUploader.PUMPTYPE + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId()) @Expose var time = DateUtil.toISOAsUTC(DateUtil.now()) @@ -28,9 +28,9 @@ class OpenDatasetRequestMessage : BaseMessage() { @Expose var dataSetType = "continuous" @Expose - var deviceManufacturers = arrayOf("Medtronic") + var deviceManufacturers = arrayOf(TidepoolUploader.PUMPTYPE) @Expose - var deviceModel = (ConfigBuilderPlugin.getPlugin().activePump?.model() ?: "Medtronic") + var deviceModel = TidepoolUploader.PUMPTYPE @Expose var deviceTags = arrayOf("bgm", "cgm", "insulin-pump") @Expose