fix Tidepool crash

This commit is contained in:
Milos Kozak 2019-07-29 23:20:44 +02:00
parent 304cee21ee
commit 2fbc9be152
2 changed files with 36 additions and 28 deletions

View file

@ -5,7 +5,6 @@ import android.text.Spanned
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.db.BgReading
import info.nightscout.androidaps.events.EventNetworkChange
import info.nightscout.androidaps.events.EventNewBG
import info.nightscout.androidaps.events.EventPreferenceChange
@ -56,7 +55,9 @@ object TidepoolPlugin : PluginBase(PluginDescription()
disposable += RxBus
.toObservable(EventTidepoolDoUpload::class.java)
.observeOn(Schedulers.io())
.subscribe({ doUpload() }, {})
.subscribe({ doUpload() }, {
log.error(it.message)
})
disposable += RxBus
.toObservable(EventTidepoolResetData::class.java)
.observeOn(Schedulers.io())
@ -68,7 +69,9 @@ object TidepoolPlugin : PluginBase(PluginDescription()
SP.putLong(R.string.key_tidepool_last_end, 0)
TidepoolUploader.doLogin()
}
}, {})
}, {
log.error(it.message)
})
disposable += RxBus
.toObservable(EventTidepoolStatus::class.java)
.observeOn(Schedulers.io())
@ -78,7 +81,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
.observeOn(Schedulers.io())
.filter { it.bgReading != null } // better would be optional in API level >24
.map { it.bgReading }
.subscribe { bgReading ->
.subscribe({ bgReading ->
if (bgReading!!.date < TidepoolUploader.getLastEnd())
TidepoolUploader.setLastEnd(bgReading.date)
if (isEnabled(PluginType.GENERAL)
@ -86,7 +89,9 @@ object TidepoolPlugin : PluginBase(PluginDescription()
&& (!SP.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || NetworkChangeReceiver.isWifiConnected())
&& RateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt()))
doUpload()
}
}, {
log.error(it.message)
})
disposable += RxBus
.toObservable(EventPreferenceChange::class.java)
.observeOn(Schedulers.io())
@ -96,11 +101,15 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|| event.isChanged(R.string.key_tidepool_password)
)
TidepoolUploader.resetInstance()
}, {})
}, {
log.error(it.message)
})
disposable += RxBus
.toObservable(EventNetworkChange::class.java)
.observeOn(Schedulers.io())
.subscribe({}, {}) // TODO start upload on wifi connect
.subscribe({}, {
log.error(it.message)
}) // TODO start upload on wifi connect
}
@ -109,11 +118,12 @@ object TidepoolPlugin : PluginBase(PluginDescription()
super.onStop()
}
private fun doUpload() {
if (TidepoolUploader.connectionStatus == TidepoolUploader.ConnectionStatus.DISCONNECTED)
TidepoolUploader.doLogin(true)
else
TidepoolUploader.doUpload()
private fun doUpload() =
when (TidepoolUploader.connectionStatus) {
TidepoolUploader.ConnectionStatus.FAILED -> {}
TidepoolUploader.ConnectionStatus.CONNECTING -> {}
TidepoolUploader.ConnectionStatus.DISCONNECTED -> TidepoolUploader.doLogin(true)
TidepoolUploader.ConnectionStatus.CONNECTED -> TidepoolUploader.doUpload()
}
@Synchronized

View file

@ -98,7 +98,7 @@ object TidepoolUploader {
startSession(session!!, doUpload)
}, {
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
loginFailed()
releaseWakeLock()
}))
return
} else {
@ -126,10 +126,6 @@ object TidepoolUploader {
}
private fun loginFailed() {
releaseWakeLock()
}
private fun startSession(session: Session, doUpload: Boolean = false) {
extendWakeLock(30000)
if (session.authReply?.userid != null) {
@ -204,7 +200,8 @@ object TidepoolUploader {
val body = RequestBody.create(MediaType.parse("application/json"), chunk)
RxBus.send(EventTidepoolStatus(("Uploading")))
val call = session.service!!.doUpload(session.token!!, session.datasetReply!!.getUploadId()!!, body)
if (session.service != null && session.token != null && session.datasetReply != null) {
val call = session.service.doUpload(session.token!!, session.datasetReply!!.getUploadId()!!, body)
call.enqueue(TidepoolCallback<UploadReplyMessage>(session, "Data Upload", {
setLastEnd(session.end)
RxBus.send(EventTidepoolStatus(("Upload completed OK")))
@ -218,6 +215,7 @@ object TidepoolUploader {
}
}
}
}
private fun unploadNext() {
if (getLastEnd() < DateUtil.now() - T.mins(1).msecs()) {