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
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<Any>()
private val publisher = PublishSubject.create<Event>()
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 <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 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
}

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.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<T>(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<T>(private val session: Session, val name: Strin
override fun onFailure(call: Call<T>, t: Throwable) {
val msg = "$name Failed: $t"
if (L.isEnabled(L.TIDEPOOL)) log.debug(msg)
RxBus.send(msg)
RxBus.send(EventTidepoolStatus(msg))
onFail()
}

View file

@ -40,6 +40,8 @@ object TidepoolUploader {
DISCONNECTED, CONNECTING, CONNECTED, FAILED
}
val PUMPTYPE = "Tandem"
var connectionStatus: ConnectionStatus = TidepoolUploader.ConnectionStatus.DISCONNECTED
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.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

View file

@ -1,3 +1,5 @@
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? {
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 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