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

View file

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