more kotlin, less java
This commit is contained in:
parent
7d6ed94491
commit
edb4844b7a
|
@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.tidepool;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
@ -25,7 +26,7 @@ public class TidepoolJavaFragment extends SubscriberFragment {
|
||||||
private ScrollView scrollView;
|
private ScrollView scrollView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.tidepool_fragment, container, false);
|
View view = inflater.inflate(R.layout.tidepool_fragment, container, false);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package info.nightscout.androidaps.plugins.general.tidepool
|
package info.nightscout.androidaps.plugins.general.tidepool
|
||||||
|
|
||||||
import android.text.Html
|
import android.text.Html
|
||||||
|
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
|
||||||
|
@ -42,7 +43,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
|
|
||||||
private val listLog = ArrayList<EventTidepoolStatus>()
|
private val listLog = ArrayList<EventTidepoolStatus>()
|
||||||
@Suppress("DEPRECATION") // API level 24 to replace call
|
@Suppress("DEPRECATION") // API level 24 to replace call
|
||||||
var textLog = Html.fromHtml("")
|
var textLog: Spanned = Html.fromHtml("")
|
||||||
|
|
||||||
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
|
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
|
||||||
add(disposable)
|
add(disposable)
|
||||||
|
@ -52,12 +53,12 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
super.onStart()
|
super.onStart()
|
||||||
disposable += RxBus
|
disposable += RxBus
|
||||||
.toObservable(EventTidepoolDoUpload::class.java)
|
.toObservable(EventTidepoolDoUpload::class.java)
|
||||||
.subscribe({ event -> doUpload() }, {})
|
.subscribe({ doUpload() }, {})
|
||||||
disposable += RxBus
|
disposable += RxBus
|
||||||
.toObservable(EventTidepoolResetData::class.java)
|
.toObservable(EventTidepoolResetData::class.java)
|
||||||
.subscribe({
|
.subscribe({
|
||||||
if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) {
|
if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) {
|
||||||
log.debug("Not connected for deleteDataset")
|
log.debug("Not connected for delete Dataset")
|
||||||
} else {
|
} else {
|
||||||
TidepoolUploader.deleteDataSet()
|
TidepoolUploader.deleteDataSet()
|
||||||
SP.putLong(R.string.key_tidepool_last_end, 0)
|
SP.putLong(R.string.key_tidepool_last_end, 0)
|
||||||
|
@ -70,12 +71,12 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
disposable += RxBus
|
disposable += RxBus
|
||||||
.toObservable(EventNewBG::class.java)
|
.toObservable(EventNewBG::class.java)
|
||||||
.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)
|
||||||
if (isEnabled(PluginType.GENERAL)
|
if (isEnabled(PluginType.GENERAL)
|
||||||
&& (!SP.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging())
|
&& (!SP.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging())
|
||||||
&& (!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()
|
||||||
}, {})
|
}, {})
|
||||||
disposable += RxBus
|
disposable += RxBus
|
||||||
|
@ -98,7 +99,7 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
super.onStop()
|
super.onStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doUpload() {
|
private fun doUpload() {
|
||||||
if (TidepoolUploader.connectionStatus == TidepoolUploader.ConnectionStatus.DISCONNECTED)
|
if (TidepoolUploader.connectionStatus == TidepoolUploader.ConnectionStatus.DISCONNECTED)
|
||||||
TidepoolUploader.doLogin(true)
|
TidepoolUploader.doLogin(true)
|
||||||
else
|
else
|
||||||
|
|
|
@ -19,7 +19,7 @@ class InfoInterceptor(tag: String) : Interceptor {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val request = chain.request()
|
||||||
if (request != null && request.body() != null) {
|
if (request?.body() != null) {
|
||||||
if (L.isEnabled(L.TIDEPOOL)) {
|
if (L.isEnabled(L.TIDEPOOL)) {
|
||||||
log.debug("Interceptor Body size: " + request.body()!!.contentLength())
|
log.debug("Interceptor Body size: " + request.body()!!.contentLength())
|
||||||
val requestBuffer = Buffer()
|
val requestBuffer = Buffer()
|
||||||
|
|
|
@ -4,9 +4,7 @@ import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMes
|
||||||
import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage
|
import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage
|
||||||
import okhttp3.Headers
|
import okhttp3.Headers
|
||||||
|
|
||||||
class Session (authHeader: String?, session_token_header: String) {
|
class Session (var authHeader: String?, private var sessionTokenHeader: String) {
|
||||||
var SESSION_TOKEN_HEADER: String
|
|
||||||
var authHeader: String?
|
|
||||||
|
|
||||||
val service = TidepoolUploader.getRetrofitInstance()?.create(TidepoolApiService::class.java)
|
val service = TidepoolUploader.getRetrofitInstance()?.create(TidepoolApiService::class.java)
|
||||||
|
|
||||||
|
@ -19,14 +17,9 @@ class Session (authHeader: String?, session_token_header: String) {
|
||||||
internal var iterations: Int = 0
|
internal var iterations: Int = 0
|
||||||
|
|
||||||
|
|
||||||
init {
|
|
||||||
this.authHeader = authHeader
|
|
||||||
this.SESSION_TOKEN_HEADER = session_token_header
|
|
||||||
}
|
|
||||||
|
|
||||||
fun populateHeaders(headers: Headers) {
|
fun populateHeaders(headers: Headers) {
|
||||||
if (this.token == null) {
|
if (this.token == null) {
|
||||||
this.token = headers.get(SESSION_TOKEN_HEADER)
|
this.token = headers.get(sessionTokenHeader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,23 +29,11 @@ class Session (authHeader: String?, session_token_header: String) {
|
||||||
authReply = obj
|
authReply = obj
|
||||||
} else if (obj is List<*>) {
|
} else if (obj is List<*>) {
|
||||||
val list = obj as List<*>?
|
val list = obj as List<*>?
|
||||||
if (list!!.size > 0 && list[0] is DatasetReplyMessage) {
|
if (list!!.isNotEmpty() && list[0] is DatasetReplyMessage) {
|
||||||
datasetReply = list[0] as DatasetReplyMessage
|
datasetReply = list[0] as DatasetReplyMessage
|
||||||
}
|
}
|
||||||
} else if (obj is DatasetReplyMessage) {
|
} else if (obj is DatasetReplyMessage) {
|
||||||
datasetReply = obj
|
datasetReply = obj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun exceededIterations(): Boolean {
|
|
||||||
return iterations > 50
|
|
||||||
}
|
|
||||||
|
|
||||||
fun authHeader(): String? {
|
|
||||||
return authHeader;
|
|
||||||
}
|
|
||||||
|
|
||||||
fun service(): TidepoolApiService? {
|
|
||||||
return service;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -19,30 +19,30 @@ interface TidepoolApiService {
|
||||||
)
|
)
|
||||||
|
|
||||||
@POST("/auth/login")
|
@POST("/auth/login")
|
||||||
abstract fun getLogin(@Header("Authorization") secret: String): Call<AuthReplyMessage>
|
fun getLogin(@Header("Authorization") secret: String): Call<AuthReplyMessage>
|
||||||
|
|
||||||
@DELETE("/v1/users/{userId}/data")
|
@DELETE("/v1/users/{userId}/data")
|
||||||
abstract fun deleteAllData(@Header(SESSION_TOKEN_HEADER) token: String, @Path("userId") id: String): Call<DatasetReplyMessage>
|
fun deleteAllData(@Header(SESSION_TOKEN_HEADER) token: String, @Path("userId") id: String): Call<DatasetReplyMessage>
|
||||||
|
|
||||||
@DELETE("/v1/datasets/{dataSetId}")
|
@DELETE("/v1/datasets/{dataSetId}")
|
||||||
abstract fun deleteDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("dataSetId") id: String): Call<DatasetReplyMessage>
|
fun deleteDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("dataSetId") id: String): Call<DatasetReplyMessage>
|
||||||
|
|
||||||
@GET("/v1/users/{userId}/data_sets")
|
@GET("/v1/users/{userId}/data_sets")
|
||||||
abstract fun getOpenDataSets(@Header(SESSION_TOKEN_HEADER) token: String,
|
fun getOpenDataSets(@Header(SESSION_TOKEN_HEADER) token: String,
|
||||||
@Path("userId") id: String,
|
@Path("userId") id: String,
|
||||||
@Query("client.name") clientName: String,
|
@Query("client.name") clientName: String,
|
||||||
@Query("size") size: Int): Call<List<DatasetReplyMessage>>
|
@Query("size") size: Int): Call<List<DatasetReplyMessage>>
|
||||||
|
|
||||||
@GET("/v1/datasets/{dataSetId}")
|
@GET("/v1/datasets/{dataSetId}")
|
||||||
abstract fun getDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("dataSetId") id: String): Call<DatasetReplyMessage>
|
fun getDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("dataSetId") id: String): Call<DatasetReplyMessage>
|
||||||
|
|
||||||
@POST("/v1/users/{userId}/data_sets")
|
@POST("/v1/users/{userId}/data_sets")
|
||||||
abstract fun openDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("userId") id: String, @Body body: RequestBody): Call<DatasetReplyMessage>
|
fun openDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("userId") id: String, @Body body: RequestBody): Call<DatasetReplyMessage>
|
||||||
|
|
||||||
@POST("/v1/datasets/{sessionId}/data")
|
@POST("/v1/datasets/{sessionId}/data")
|
||||||
abstract fun doUpload(@Header(SESSION_TOKEN_HEADER) token: String, @Path("sessionId") id: String, @Body body: RequestBody): Call<UploadReplyMessage>
|
fun doUpload(@Header(SESSION_TOKEN_HEADER) token: String, @Path("sessionId") id: String, @Body body: RequestBody): Call<UploadReplyMessage>
|
||||||
|
|
||||||
@PUT("/v1/datasets/{sessionId}")
|
@PUT("/v1/datasets/{sessionId}")
|
||||||
abstract fun closeDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("sessionId") id: String, @Body body: RequestBody): Call<DatasetReplyMessage>
|
fun closeDataSet(@Header(SESSION_TOKEN_HEADER) token: String, @Path("sessionId") id: String, @Body body: RequestBody): Call<DatasetReplyMessage>
|
||||||
|
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ import retrofit2.Call
|
||||||
import retrofit2.Callback
|
import retrofit2.Callback
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
|
|
||||||
internal class TidepoolCallback<T>(val session: Session, val name: String, val onSucc: () -> Unit, val onFail: () -> Unit) : Callback<T> {
|
internal class TidepoolCallback<T>(private val session: Session, val name: String, val onSuccess: () -> Unit, val onFail: () -> Unit) : Callback<T> {
|
||||||
private val log = LoggerFactory.getLogger(L.TIDEPOOL)
|
private val log = LoggerFactory.getLogger(L.TIDEPOOL)
|
||||||
|
|
||||||
override fun onResponse(call: Call<T>, response: Response<T>) {
|
override fun onResponse(call: Call<T>, response: Response<T>) {
|
||||||
|
@ -15,7 +15,7 @@ internal class TidepoolCallback<T>(val session: Session, val name: String, val o
|
||||||
if (L.isEnabled(L.TIDEPOOL)) log.debug("$name success")
|
if (L.isEnabled(L.TIDEPOOL)) log.debug("$name success")
|
||||||
session.populateBody(response.body())
|
session.populateBody(response.body())
|
||||||
session.populateHeaders(response.headers())
|
session.populateHeaders(response.headers())
|
||||||
onSucc()
|
onSuccess()
|
||||||
} 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)
|
||||||
|
|
|
@ -33,7 +33,7 @@ object TidepoolUploader {
|
||||||
|
|
||||||
private var retrofit: Retrofit? = null
|
private var retrofit: Retrofit? = null
|
||||||
|
|
||||||
var session: Session? = null
|
private var session: Session? = null
|
||||||
|
|
||||||
enum class ConnectionStatus {
|
enum class ConnectionStatus {
|
||||||
DISCONNECTED, CONNECTING, CONNECTED, FAILED
|
DISCONNECTED, CONNECTING, CONNECTED, FAILED
|
||||||
|
@ -87,13 +87,13 @@ object TidepoolUploader {
|
||||||
call?.enqueue(TidepoolCallback<AuthReplyMessage>(session!!, "Login", {
|
call?.enqueue(TidepoolCallback<AuthReplyMessage>(session!!, "Login", {
|
||||||
startSession(session!!, doUpload)
|
startSession(session!!, doUpload)
|
||||||
}, {
|
}, {
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
|
||||||
loginFailed()
|
loginFailed()
|
||||||
}))
|
}))
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if (L.isEnabled(L.TIDEPOOL)) log.debug("Cannot do login as user credentials have not been set correctly")
|
if (L.isEnabled(L.TIDEPOOL)) log.debug("Cannot do login as user credentials have not been set correctly")
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
|
||||||
RxBus.send(EventTidepoolStatus(("Invalid credentials")))
|
RxBus.send(EventTidepoolStatus(("Invalid credentials")))
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
return
|
return
|
||||||
|
@ -106,13 +106,13 @@ object TidepoolUploader {
|
||||||
val call = session.service!!.getLogin(session.authHeader!!)
|
val call = session.service!!.getLogin(session.authHeader!!)
|
||||||
|
|
||||||
call.enqueue(TidepoolCallback<AuthReplyMessage>(session, "Login", {
|
call.enqueue(TidepoolCallback<AuthReplyMessage>(session, "Login", {
|
||||||
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Successfully logged into Tidepool.", null);
|
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Successfully logged into Tidepool.", null)
|
||||||
}, {
|
}, {
|
||||||
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Failed to log into Tidepool.\nCheck that your user name and password are correct.", null);
|
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Failed to log into Tidepool.\nCheck that your user name and password are correct.", null)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Cannot do login as user credentials have not been set correctly", null);
|
OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Cannot do login as user credentials have not been set correctly", null)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ object TidepoolUploader {
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
// See if we already have an open data set to write to
|
// See if we already have an open data set to write to
|
||||||
|
@ -134,13 +134,13 @@ object TidepoolUploader {
|
||||||
RxBus.send(EventTidepoolStatus(("Creating new dataset")))
|
RxBus.send(EventTidepoolStatus(("Creating new dataset")))
|
||||||
val call = session.service.openDataSet(session.token!!, session.authReply!!.userid!!, OpenDatasetRequestMessage().getBody())
|
val call = session.service.openDataSet(session.token!!, session.authReply!!.userid!!, OpenDatasetRequestMessage().getBody())
|
||||||
call.enqueue(TidepoolCallback<DatasetReplyMessage>(session, "Open New Dataset", {
|
call.enqueue(TidepoolCallback<DatasetReplyMessage>(session, "Open New Dataset", {
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTED
|
||||||
RxBus.send(EventTidepoolStatus(("New dataset OK")))
|
RxBus.send(EventTidepoolStatus(("New dataset OK")))
|
||||||
if (doUpload) doUpload()
|
if (doUpload) doUpload()
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}, {
|
}, {
|
||||||
RxBus.send(EventTidepoolStatus(("New dataset FAILED")))
|
RxBus.send(EventTidepoolStatus(("New dataset FAILED")))
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
|
@ -148,20 +148,20 @@ object TidepoolUploader {
|
||||||
log.debug("Existing Dataset: " + session.datasetReply!!.getUploadId())
|
log.debug("Existing Dataset: " + session.datasetReply!!.getUploadId())
|
||||||
// TODO: Wouldn't need to do this if we could block on the above `call.enqueue`.
|
// TODO: Wouldn't need to do this if we could block on the above `call.enqueue`.
|
||||||
// ie, do the openDataSet conditionally, and then do `doUpload` either way.
|
// ie, do the openDataSet conditionally, and then do `doUpload` either way.
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTED
|
||||||
RxBus.send(EventTidepoolStatus(("Appending to existing dataset")))
|
RxBus.send(EventTidepoolStatus(("Appending to existing dataset")))
|
||||||
if (doUpload) doUpload()
|
if (doUpload) doUpload()
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
|
||||||
RxBus.send(EventTidepoolStatus(("Open dataset FAILED")))
|
RxBus.send(EventTidepoolStatus(("Open dataset FAILED")))
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
log.error("Got login response but cannot determine userid - cannot proceed")
|
log.error("Got login response but cannot determine userId - cannot proceed")
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED;
|
connectionStatus = TidepoolUploader.ConnectionStatus.FAILED
|
||||||
RxBus.send(EventTidepoolStatus(("Error userid")))
|
RxBus.send(EventTidepoolStatus(("Error userId")))
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,26 +175,30 @@ object TidepoolUploader {
|
||||||
extendWakeLock(60000)
|
extendWakeLock(60000)
|
||||||
session!!.iterations++
|
session!!.iterations++
|
||||||
val chunk = UploadChunk.getNext(session)
|
val chunk = UploadChunk.getNext(session)
|
||||||
if (chunk == null) {
|
when {
|
||||||
log.error("Upload chunk is null, cannot proceed")
|
chunk == null -> {
|
||||||
releaseWakeLock()
|
log.error("Upload chunk is null, cannot proceed")
|
||||||
} else if (chunk.length == 2) {
|
releaseWakeLock()
|
||||||
if (L.isEnabled(L.TIDEPOOL)) log.debug("Empty dataset - marking as succeeded")
|
}
|
||||||
RxBus.send(EventTidepoolStatus(("No data to upload")))
|
chunk.length == 2 -> {
|
||||||
releaseWakeLock()
|
if (L.isEnabled(L.TIDEPOOL)) log.debug("Empty dataset - marking as succeeded")
|
||||||
} else {
|
RxBus.send(EventTidepoolStatus(("No data to upload")))
|
||||||
val body = RequestBody.create(MediaType.parse("application/json"), chunk)
|
releaseWakeLock()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
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)
|
val call = session!!.service!!.doUpload(session!!.token!!, session!!.datasetReply!!.getUploadId()!!, body)
|
||||||
call.enqueue(TidepoolCallback<UploadReplyMessage>(session!!, "Data Upload", {
|
call.enqueue(TidepoolCallback<UploadReplyMessage>(session!!, "Data Upload", {
|
||||||
setLastEnd(session!!.end)
|
setLastEnd(session!!.end)
|
||||||
RxBus.send(EventTidepoolStatus(("Upload completed OK")))
|
RxBus.send(EventTidepoolStatus(("Upload completed OK")))
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}, {
|
}, {
|
||||||
RxBus.send(EventTidepoolStatus(("Upload FAILED")))
|
RxBus.send(EventTidepoolStatus(("Upload FAILED")))
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}))
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -213,7 +217,7 @@ object TidepoolUploader {
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
log.error("Got login response but cannot determine dataseId - cannot proceed")
|
log.error("Got login response but cannot determine datasetId - cannot proceed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,7 +253,7 @@ object TidepoolUploader {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun releaseWakeLock() {
|
private fun releaseWakeLock() {
|
||||||
if (wl == null) return
|
if (wl == null) return
|
||||||
if (wl!!.isHeld()) {
|
if (wl!!.isHeld) {
|
||||||
try {
|
try {
|
||||||
wl!!.release()
|
wl!!.release()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
|
@ -70,7 +70,7 @@ object UploadChunk {
|
||||||
else -1
|
else -1
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun getTreatments(start: Long, end: Long): List<BaseElement> {
|
private fun getTreatments(start: Long, end: Long): List<BaseElement> {
|
||||||
val result = LinkedList<BaseElement>()
|
val result = LinkedList<BaseElement>()
|
||||||
val treatments = TreatmentsPlugin.getPlugin().service.getTreatmentDataFromTime(start, end, true)
|
val treatments = TreatmentsPlugin.getPlugin().service.getTreatmentDataFromTime(start, end, true)
|
||||||
for (treatment in treatments) {
|
for (treatment in treatments) {
|
||||||
|
@ -83,7 +83,7 @@ object UploadChunk {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun getBloodTests(start: Long, end: Long): List<BloodGlucoseElement> {
|
private fun getBloodTests(start: Long, end: Long): List<BloodGlucoseElement> {
|
||||||
val readings = MainApp.getDbHelper().getCareportalEvents(start, end, true)
|
val readings = MainApp.getDbHelper().getCareportalEvents(start, end, true)
|
||||||
val selection = BloodGlucoseElement.fromCareportalEvents(readings)
|
val selection = BloodGlucoseElement.fromCareportalEvents(readings)
|
||||||
if (selection.isNotEmpty())
|
if (selection.isNotEmpty())
|
||||||
|
@ -100,7 +100,7 @@ object UploadChunk {
|
||||||
return selection
|
return selection
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun getBasals(start: Long, end: Long): List<BasalElement> {
|
private fun getBasals(start: Long, end: Long): List<BasalElement> {
|
||||||
val tbrs = MainApp.getDbHelper().getTemporaryBasalsDataFromTime(start, end, true)
|
val tbrs = MainApp.getDbHelper().getTemporaryBasalsDataFromTime(start, end, true)
|
||||||
val selection = BasalElement.fromTemporaryBasals(tbrs)
|
val selection = BasalElement.fromTemporaryBasals(tbrs)
|
||||||
if (selection.isNotEmpty())
|
if (selection.isNotEmpty())
|
||||||
|
@ -108,7 +108,7 @@ object UploadChunk {
|
||||||
return selection
|
return selection
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun getProfiles(start: Long, end: Long): List<ProfileElement> {
|
private fun getProfiles(start: Long, end: Long): List<ProfileElement> {
|
||||||
val pss = MainApp.getDbHelper().getProfileSwitchEventsFromTime(start, end, true)
|
val pss = MainApp.getDbHelper().getProfileSwitchEventsFromTime(start, end, true)
|
||||||
val selection = LinkedList<ProfileElement>()
|
val selection = LinkedList<ProfileElement>()
|
||||||
for (ps in pss) {
|
for (ps in pss) {
|
||||||
|
|
|
@ -24,10 +24,10 @@ class BasalElement(tbr: TemporaryBasal)
|
||||||
internal var conversionOffset: Long = 0
|
internal var conversionOffset: Long = 0
|
||||||
|
|
||||||
init {
|
init {
|
||||||
type = "basal";
|
type = "basal"
|
||||||
timestamp = tbr.date
|
timestamp = tbr.date
|
||||||
rate = tbr.tempBasalConvertedToAbsolute(tbr.date, ProfileFunctions.getInstance().getProfile(tbr.date))
|
rate = tbr.tempBasalConvertedToAbsolute(tbr.date, ProfileFunctions.getInstance().getProfile(tbr.date))
|
||||||
duration = duration
|
duration = tbr.durationInMsec()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -21,7 +21,7 @@ class BloodGlucoseElement(careportalEvent: CareportalEvent)
|
||||||
init {
|
init {
|
||||||
type = "cbg"
|
type = "cbg"
|
||||||
subType = "manual" // TODO
|
subType = "manual" // TODO
|
||||||
var json = if (careportalEvent.json != null) JSONObject(careportalEvent.json) else JSONObject()
|
val json = if (careportalEvent.json != null) JSONObject(careportalEvent.json) else JSONObject()
|
||||||
value = Profile.toMgdl(JsonHelper.safeGetDouble(json, "glucose"), JsonHelper.safeGetString(json, "units", Constants.MGDL)).toInt()
|
value = Profile.toMgdl(JsonHelper.safeGetDouble(json, "glucose"), JsonHelper.safeGetString(json, "units", Constants.MGDL)).toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ class BolusElement(treatment: Treatment)
|
||||||
var expectedNormal: Double = 0.0
|
var expectedNormal: Double = 0.0
|
||||||
|
|
||||||
init {
|
init {
|
||||||
type = "bolus";
|
type = "bolus"
|
||||||
normal = treatment.insulin
|
normal = treatment.insulin
|
||||||
expectedNormal = treatment.insulin
|
expectedNormal = treatment.insulin
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ProfileElement(ps: ProfileSwitch)
|
||||||
internal var insulinSensitivities: IsfProfile = IsfProfile()
|
internal var insulinSensitivities: IsfProfile = IsfProfile()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
type = "pumpSettings";
|
type = "pumpSettings"
|
||||||
val profile: Profile = ps.getProfileObject()!!
|
val profile: Profile = ps.getProfileObject()!!
|
||||||
for (br in profile.basalValues)
|
for (br in profile.basalValues)
|
||||||
basalSchedules.Normal.add(BasalRate(br.timeAsSeconds * 1000, br.value))
|
basalSchedules.Normal.add(BasalRate(br.timeAsSeconds * 1000, br.value))
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package info.nightscout.androidaps.plugins.general.tidepool.elements
|
package info.nightscout.androidaps.plugins.general.tidepool.elements
|
||||||
|
|
||||||
import com.google.gson.annotations.Expose
|
import com.google.gson.annotations.Expose
|
||||||
import info.nightscout.androidaps.R2.string.result
|
|
||||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
|
|
||||||
import info.nightscout.androidaps.plugins.treatments.Treatment
|
import info.nightscout.androidaps.plugins.treatments.Treatment
|
||||||
import org.json.JSONObject
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class WizardElement(treatment: Treatment)
|
class WizardElement(treatment: Treatment)
|
||||||
|
@ -22,17 +19,17 @@ class WizardElement(treatment: Treatment)
|
||||||
init {
|
init {
|
||||||
type = "wizard"
|
type = "wizard"
|
||||||
carbInput = treatment.carbs
|
carbInput = treatment.carbs
|
||||||
insulinCarbRatio = treatment.ic;
|
insulinCarbRatio = treatment.ic
|
||||||
if (treatment.insulin > 0) {
|
if (treatment.insulin > 0) {
|
||||||
bolus = BolusElement(treatment)
|
bolus = BolusElement(treatment)
|
||||||
} else {
|
} else {
|
||||||
var fake = Treatment()
|
val fake = Treatment()
|
||||||
fake.insulin = 0.0001
|
fake.insulin = 0.0001
|
||||||
fake.date = treatment.date;
|
fake.date = treatment.date
|
||||||
bolus = BolusElement(fake) // fake insulin record
|
bolus = BolusElement(fake) // fake insulin record
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO fill the rest
|
/* TODO fill the rest
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
package info.nightscout.androidaps.plugins.general.tidepool.events
|
package info.nightscout.androidaps.plugins.general.tidepool.events
|
||||||
|
|
||||||
class EventTidepoolDoUpload {
|
class EventTidepoolDoUpload
|
||||||
}
|
|
|
@ -2,5 +2,4 @@ package info.nightscout.androidaps.plugins.general.tidepool.events
|
||||||
|
|
||||||
import info.nightscout.androidaps.events.Event
|
import info.nightscout.androidaps.events.Event
|
||||||
|
|
||||||
class EventTidepoolResetData :Event() {
|
class EventTidepoolResetData :Event()
|
||||||
}
|
|
|
@ -17,7 +17,7 @@ class EventTidepoolStatus(val status: String) : Event() {
|
||||||
log.debug("New status: $status")
|
log.debug("New status: $status")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.getLocale())
|
private var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.getLocale())
|
||||||
|
|
||||||
fun toPreparedHtml(): StringBuilder {
|
fun toPreparedHtml(): StringBuilder {
|
||||||
val stringBuilder = StringBuilder()
|
val stringBuilder = StringBuilder()
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.general.tidepool.events;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.events.Event;
|
|
||||||
|
|
||||||
public class EventTidepoolUpdateGUI extends Event {
|
|
||||||
}
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package info.nightscout.androidaps.plugins.general.tidepool.events
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.events.Event
|
||||||
|
|
||||||
|
class EventTidepoolUpdateGUI : Event()
|
|
@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.general.tidepool.messages
|
||||||
|
|
||||||
import com.google.gson.annotations.Expose
|
import com.google.gson.annotations.Expose
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
import info.nightscout.androidaps.plugins.general.tidepool.utils.GsonInstance
|
|
||||||
|
|
||||||
class AuthReplyMessage {
|
class AuthReplyMessage {
|
||||||
|
|
||||||
|
@ -21,9 +20,4 @@ class AuthReplyMessage {
|
||||||
@Expose
|
@Expose
|
||||||
@SerializedName("username")
|
@SerializedName("username")
|
||||||
internal var username: String? = null
|
internal var username: String? = null
|
||||||
|
|
||||||
fun toS(): String {
|
|
||||||
return GsonInstance.defaultGsonInstance().toJson(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import okhttp3.MediaType
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
|
|
||||||
open class BaseMessage {
|
open class BaseMessage {
|
||||||
fun toS(): String {
|
private fun toS(): String {
|
||||||
return GsonInstance.defaultGsonInstance().toJson(this) ?: "null"
|
return GsonInstance.defaultGsonInstance().toJson(this) ?: "null"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,15 +14,15 @@ object RateLimit {
|
||||||
|
|
||||||
// return true if below rate limit
|
// return true if below rate limit
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun ratelimit(name: String, seconds: Int): Boolean {
|
fun rateLimit(name: String, seconds: Int): Boolean {
|
||||||
// check if over limit
|
// check if over limit
|
||||||
if (rateLimits.containsKey(name) && DateUtil.now() - rateLimits.get(name)!! < T.secs(seconds.toLong()).msecs()) {
|
if (rateLimits.containsKey(name) && DateUtil.now() - rateLimits[name]!! < T.secs(seconds.toLong()).msecs()) {
|
||||||
if (L.isEnabled(L.TIDEPOOL))
|
if (L.isEnabled(L.TIDEPOOL))
|
||||||
log.debug("$name rate limited: $seconds seconds")
|
log.debug("$name rate limited: $seconds seconds")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// not over limit
|
// not over limit
|
||||||
rateLimits.put(name, DateUtil.now())
|
rateLimits[name] = DateUtil.now()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue