bus observeOn, Tandem as manufacture

This commit is contained in:
Milos Kozak 2019-06-14 10:52:41 +02:00
parent 9d90c36a94
commit 75c13cef8f
8 changed files with 30 additions and 12 deletions

View file

@ -1,18 +1,23 @@
package info.nightscout.androidaps.plugins.bus package info.nightscout.androidaps.plugins.bus
import info.nightscout.androidaps.events.Event
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.PublishSubject
// Use object so we have a singleton instance // Use object so we have a singleton instance
object RxBus { object RxBus {
private val publisher = PublishSubject.create<Any>() private val publisher = PublishSubject.create<Event>()
fun send(event: Any) { fun send(event: Event) {
publisher.onNext(event) publisher.onNext(event)
} }
// Listen should return an Observable and not the publisher // Listen should return an Observable and not the publisher
// Using ofType we filter only events that match that class type // Using ofType we filter only events that match that class type
fun <T> toObservable(eventType: Class<T>): Observable<T> = publisher.ofType(eventType) fun <T> toObservable(eventType: Class<T>): Observable<T> =
publisher
.subscribeOn(Schedulers.io())
.ofType(eventType)
} }

View file

@ -26,6 +26,7 @@ import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.ToastUtils
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.util.* import java.util.*
@ -53,9 +54,11 @@ object TidepoolPlugin : PluginBase(PluginDescription()
super.onStart() super.onStart()
disposable += RxBus disposable += RxBus
.toObservable(EventTidepoolDoUpload::class.java) .toObservable(EventTidepoolDoUpload::class.java)
.observeOn(Schedulers.io())
.subscribe({ doUpload() }, {}) .subscribe({ doUpload() }, {})
disposable += RxBus disposable += RxBus
.toObservable(EventTidepoolResetData::class.java) .toObservable(EventTidepoolResetData::class.java)
.observeOn(Schedulers.io())
.subscribe({ .subscribe({
if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) { if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) {
log.debug("Not connected for delete Dataset") log.debug("Not connected for delete Dataset")
@ -67,9 +70,11 @@ object TidepoolPlugin : PluginBase(PluginDescription()
}, {}) }, {})
disposable += RxBus disposable += RxBus
.toObservable(EventTidepoolStatus::class.java) .toObservable(EventTidepoolStatus::class.java)
.observeOn(Schedulers.io())
.subscribe({ event -> addToLog(event) }, {}) .subscribe({ event -> addToLog(event) }, {})
disposable += RxBus disposable += RxBus
.toObservable(EventNewBG::class.java) .toObservable(EventNewBG::class.java)
.observeOn(Schedulers.io())
.subscribe({ event -> .subscribe({ event ->
if (event.bgReading!!.date < TidepoolUploader.getLastEnd()) if (event.bgReading!!.date < TidepoolUploader.getLastEnd())
TidepoolUploader.setLastEnd(event.bgReading.date) TidepoolUploader.setLastEnd(event.bgReading.date)
@ -81,6 +86,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
}, {}) }, {})
disposable += RxBus disposable += RxBus
.toObservable(EventPreferenceChange::class.java) .toObservable(EventPreferenceChange::class.java)
.observeOn(Schedulers.io())
.subscribe({ event -> .subscribe({ event ->
if (event.isChanged(R.string.key_tidepool_dev_servers) if (event.isChanged(R.string.key_tidepool_dev_servers)
|| event.isChanged(R.string.key_tidepool_username) || event.isChanged(R.string.key_tidepool_username)
@ -90,6 +96,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
}, {}) }, {})
disposable += RxBus disposable += RxBus
.toObservable(EventNetworkChange::class.java) .toObservable(EventNetworkChange::class.java)
.observeOn(Schedulers.io())
.subscribe({}, {}) // TODO start upload on wifi connect .subscribe({}, {}) // TODO start upload on wifi connect
} }

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.tidepool.comm
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
@ -19,7 +20,7 @@ internal class TidepoolCallback<T>(private val session: Session, val name: Strin
} else { } else {
val msg = name + " was not successful: " + response.code() + " " + response.message() val msg = name + " was not successful: " + response.code() + " " + response.message()
if (L.isEnabled(L.TIDEPOOL)) log.debug(msg) if (L.isEnabled(L.TIDEPOOL)) log.debug(msg)
RxBus.send(msg) RxBus.send(EventTidepoolStatus(msg))
onFail() onFail()
} }
} }
@ -27,7 +28,7 @@ internal class TidepoolCallback<T>(private val session: Session, val name: Strin
override fun onFailure(call: Call<T>, t: Throwable) { override fun onFailure(call: Call<T>, t: Throwable) {
val msg = "$name Failed: $t" val msg = "$name Failed: $t"
if (L.isEnabled(L.TIDEPOOL)) log.debug(msg) if (L.isEnabled(L.TIDEPOOL)) log.debug(msg)
RxBus.send(msg) RxBus.send(EventTidepoolStatus(msg))
onFail() onFail()
} }

View file

@ -40,6 +40,8 @@ object TidepoolUploader {
DISCONNECTED, CONNECTING, CONNECTED, FAILED DISCONNECTED, CONNECTING, CONNECTED, FAILED
} }
val PUMPTYPE = "Tandem"
var connectionStatus: ConnectionStatus = TidepoolUploader.ConnectionStatus.DISCONNECTED var connectionStatus: ConnectionStatus = TidepoolUploader.ConnectionStatus.DISCONNECTED
fun getRetrofitInstance(): Retrofit? { fun getRetrofitInstance(): Retrofit? {

View file

@ -4,6 +4,7 @@ import com.google.gson.annotations.Expose
import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.ProfileSwitch import info.nightscout.androidaps.db.ProfileSwitch
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader
import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.InstanceId
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -24,7 +25,7 @@ class ProfileElement(ps: ProfileSwitch)
@Expose @Expose
internal var insulinSensitivities: IsfProfile = IsfProfile() internal var insulinSensitivities: IsfProfile = IsfProfile()
@Expose @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 @Expose
internal var deviceSerialNumber: String = ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId() internal var deviceSerialNumber: String = ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() ?: InstanceId.instanceId()
@Expose @Expose

View file

@ -1,3 +1,5 @@
package info.nightscout.androidaps.plugins.general.tidepool.events package info.nightscout.androidaps.plugins.general.tidepool.events
class EventTidepoolDoUpload import info.nightscout.androidaps.events.Event
class EventTidepoolDoUpload : Event()

View file

@ -37,6 +37,6 @@ class DatasetReplyMessage {
} }
fun getUploadId(): String? { fun getUploadId(): String? {
return if (data != null && data!!.uploadId != null) data!!.uploadId else uploadId return data?.uploadId ?: uploadId
} }
} }

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.tidepool.messages
import com.google.gson.annotations.Expose import com.google.gson.annotations.Expose
import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.BuildConfig
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin 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.DateUtil
import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.InstanceId
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
@ -11,8 +12,7 @@ import java.util.*
class OpenDatasetRequestMessage : BaseMessage() { class OpenDatasetRequestMessage : BaseMessage() {
@Expose @Expose
var deviceId: String = (ConfigBuilderPlugin.getPlugin().activePump?.model() var deviceId: String = TidepoolUploader.PUMPTYPE + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber()
?: "Medtronic") + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber()
?: InstanceId.instanceId()) ?: InstanceId.instanceId())
@Expose @Expose
var time = DateUtil.toISOAsUTC(DateUtil.now()) var time = DateUtil.toISOAsUTC(DateUtil.now())
@ -28,9 +28,9 @@ class OpenDatasetRequestMessage : BaseMessage() {
@Expose @Expose
var dataSetType = "continuous" var dataSetType = "continuous"
@Expose @Expose
var deviceManufacturers = arrayOf("Medtronic") var deviceManufacturers = arrayOf(TidepoolUploader.PUMPTYPE)
@Expose @Expose
var deviceModel = (ConfigBuilderPlugin.getPlugin().activePump?.model() ?: "Medtronic") var deviceModel = TidepoolUploader.PUMPTYPE
@Expose @Expose
var deviceTags = arrayOf("bgm", "cgm", "insulin-pump") var deviceTags = arrayOf("bgm", "cgm", "insulin-pump")
@Expose @Expose