From 89b1dda10b2d0e5081dd2e730656f0d0dda83acb Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 3 Jan 2020 00:26:59 +0100 Subject: [PATCH] Tidepool cleanup --- .../general/tidepool/comm/TidepoolCallback.kt | 8 +++--- .../general/tidepool/comm/TidepoolUploader.kt | 25 ++++++++++------- .../general/tidepool/comm/UploadChunk.kt | 28 +++++++++++++++++-- .../general/tidepool/elements/BasalElement.kt | 18 ++---------- .../tidepool/elements/ProfileElement.kt | 18 ++---------- .../tidepool/messages/AuthRequestMessage.kt | 10 +++---- .../messages/OpenDatasetRequestMessage.kt | 5 ++-- .../androidaps/utils/sharedPreferences/SP.kt | 1 + .../sharedPreferences/SPImplementation.kt | 3 ++ 9 files changed, 61 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt index d2fc11e2c6..7998553eaa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolCallback.kt @@ -1,14 +1,14 @@ 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.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus import org.slf4j.LoggerFactory import retrofit2.Call import retrofit2.Callback import retrofit2.Response -internal class TidepoolCallback(private val session: Session, val name: String, val onSuccess: () -> Unit, val onFail: () -> Unit) : Callback { +internal class TidepoolCallback(private val rxBus: RxBusWrapper, private val session: Session, val name: String, val onSuccess: () -> Unit, val onFail: () -> Unit) : Callback { private val log = LoggerFactory.getLogger(L.TIDEPOOL) override fun onResponse(call: Call, response: Response) { @@ -20,7 +20,7 @@ internal class TidepoolCallback(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.INSTANCE.send(EventTidepoolStatus(msg)) + rxBus.send(EventTidepoolStatus(msg)) onFail() } } @@ -28,7 +28,7 @@ internal class TidepoolCallback(private val session: Session, val name: Strin override fun onFailure(call: Call, t: Throwable) { val msg = "$name Failed: $t" if (L.isEnabled(L.TIDEPOOL)) log.debug(msg) - RxBus.INSTANCE.send(EventTidepoolStatus(msg)) + rxBus.send(EventTidepoolStatus(msg)) onFail() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt index 8091b0d421..45b957de1b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt @@ -9,6 +9,7 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMessage import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthRequestMessage @@ -16,6 +17,7 @@ import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReply import info.nightscout.androidaps.plugins.general.tidepool.messages.OpenDatasetRequestMessage import info.nightscout.androidaps.plugins.general.tidepool.messages.UploadReplyMessage import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.OKDialog import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -36,7 +38,8 @@ class TidepoolUploader @Inject constructor( private val mainApp: MainApp, private val resourceHelper: ResourceHelper, private val sp: SP, - private val uploadChunk: UploadChunk + private val uploadChunk: UploadChunk, + private val configBuilderPlugin: ConfigBuilderPlugin ) { private var wl: PowerManager.WakeLock? = null @@ -80,7 +83,7 @@ class TidepoolUploader @Inject constructor( private fun createSession(): Session { val service = getRetrofitInstance()?.create(TidepoolApiService::class.java) - return Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER, service) + return Session(AuthRequestMessage.getAuthRequestHeader(sp), SESSION_TOKEN_HEADER, service) } // TODO: call on preference change @@ -105,7 +108,7 @@ class TidepoolUploader @Inject constructor( rxBus.send(EventTidepoolStatus(("Connecting"))) val call = session?.service?.getLogin(authHeader) - call?.enqueue(TidepoolCallback(session!!, "Login", { + call?.enqueue(TidepoolCallback(rxBus, session!!, "Login", { startSession(session!!, doUpload) }, { connectionStatus = ConnectionStatus.FAILED @@ -126,7 +129,7 @@ class TidepoolUploader @Inject constructor( session.authHeader?.let { val call = session.service?.getLogin(it) - call?.enqueue(TidepoolCallback(session, "Login", { + call?.enqueue(TidepoolCallback(rxBus, session, "Login", { OKDialog.show(rootContext, resourceHelper.gs(R.string.tidepool), "Successfully logged into Tidepool.") }, { OKDialog.show(rootContext, resourceHelper.gs(R.string.tidepool), "Failed to log into Tidepool.\nCheck that your user name and password are correct.") @@ -144,11 +147,13 @@ class TidepoolUploader @Inject constructor( val datasetCall = session.service!!.getOpenDataSets(session.token!!, session.authReply!!.userid!!, BuildConfig.APPLICATION_ID, 1) - datasetCall.enqueue(TidepoolCallback>(session, "Get Open Datasets", { + datasetCall.enqueue(TidepoolCallback>(rxBus, session, "Get Open Datasets", { if (session.datasetReply == null) { rxBus.send(EventTidepoolStatus(("Creating new dataset"))) - val call = session.service.openDataSet(session.token!!, session.authReply!!.userid!!, OpenDatasetRequestMessage().getBody()) - call.enqueue(TidepoolCallback(session, "Open New Dataset", { + val call = session.service.openDataSet(session.token!!, session.authReply!!.userid!!, + OpenDatasetRequestMessage((configBuilderPlugin.activePump?.serialNumber() + ?: InstanceId.instanceId())).getBody()) + call.enqueue(TidepoolCallback(rxBus, session, "Open New Dataset", { connectionStatus = ConnectionStatus.CONNECTED rxBus.send(EventTidepoolStatus(("New dataset OK"))) if (doUpload) doUpload() @@ -212,7 +217,7 @@ class TidepoolUploader @Inject constructor( rxBus.send(EventTidepoolStatus(("Uploading"))) if (session.service != null && session.token != null && session.datasetReply != null) { val call = session.service.doUpload(session.token!!, session.datasetReply!!.getUploadId()!!, body) - call.enqueue(TidepoolCallback(session, "Data Upload", { + call.enqueue(TidepoolCallback(rxBus, session, "Data Upload", { uploadChunk.setLastEnd(session.end) rxBus.send(EventTidepoolStatus(("Upload completed OK"))) releaseWakeLock() @@ -239,7 +244,7 @@ class TidepoolUploader @Inject constructor( if (session?.datasetReply?.id != null) { extendWakeLock(60000) val call = session!!.service?.deleteDataSet(session!!.token!!, session!!.datasetReply!!.id!!) - call?.enqueue(TidepoolCallback(session!!, "Delete Dataset", { + call?.enqueue(TidepoolCallback(rxBus, session!!, "Delete Dataset", { connectionStatus = ConnectionStatus.DISCONNECTED rxBus.send(EventTidepoolStatus(("Dataset removed OK"))) releaseWakeLock() @@ -264,7 +269,7 @@ class TidepoolUploader @Inject constructor( requireNotNull(userId) extendWakeLock(60000) val call = session.service?.deleteAllData(token, userId) - call?.enqueue(TidepoolCallback(session, "Delete all data", { + call?.enqueue(TidepoolCallback(rxBus, session, "Delete all data", { connectionStatus = ConnectionStatus.DISCONNECTED rxBus.send(EventTidepoolStatus(("All data removed OK"))) releaseWakeLock() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/UploadChunk.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/UploadChunk.kt index fd24befd3c..b2cdc05699 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/UploadChunk.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/UploadChunk.kt @@ -2,15 +2,21 @@ package info.nightscout.androidaps.plugins.general.tidepool.comm import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.Intervals +import info.nightscout.androidaps.db.ProfileSwitch +import info.nightscout.androidaps.db.TemporaryBasal import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction import info.nightscout.androidaps.plugins.general.tidepool.elements.* import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolStatus import info.nightscout.androidaps.plugins.general.tidepool.utils.GsonInstance import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.sharedPreferences.SP import org.slf4j.LoggerFactory @@ -24,7 +30,9 @@ class UploadChunk @Inject constructor( private val sp: SP, private val rxBus: RxBusWrapper, private val aapsLogger: AAPSLogger, - private val treatmentsPlugin: TreatmentsPlugin + private val profileFunction: ProfileFunction, + private val treatmentsPlugin: TreatmentsPlugin, + private val configBuilderPlugin: ConfigBuilderPlugin ) { private val MAX_UPLOAD_SIZE = T.days(7).msecs() // don't change this @@ -134,20 +142,34 @@ class UploadChunk @Inject constructor( return selection } + private fun fromTemporaryBasals(tbrList: Intervals, start: Long, end: Long): List { + val results = LinkedList() + for (tbr in tbrList.list) { + if (tbr.date >= start && tbr.date <= end && tbr.durationInMinutes != 0) + results.add(BasalElement(tbr, profileFunction)) + } + return results + } private fun getBasals(start: Long, end: Long): List { val tbrs = treatmentsPlugin.temporaryBasalsFromHistory tbrs.merge() - val selection = BasalElement.fromTemporaryBasals(tbrs, start, end) // TODO do not upload running TBR + val selection = fromTemporaryBasals(tbrs, start, end) // TODO do not upload running TBR if (selection.isNotEmpty()) rxBus.send(EventTidepoolStatus("${selection.size} TBRs selected for upload")) return selection } + fun newInstanceOrNull(ps: ProfileSwitch): ProfileElement? = try { + ProfileElement(ps, configBuilderPlugin.activePump?.serialNumber() ?: InstanceId.instanceId()) + } catch (e: Throwable) { + null + } + private fun getProfiles(start: Long, end: Long): List { val pss = MainApp.getDbHelper().getProfileSwitchEventsFromTime(start, end, true) val selection = LinkedList() for (ps in pss) { - ProfileElement.newInstanceOrNull(ps)?.let { selection.add(it) } + newInstanceOrNull(ps)?.let { selection.add(it) } } if (selection.size > 0) rxBus.send(EventTidepoolStatus("${selection.size} ProfileSwitches selected for upload")) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/BasalElement.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/BasalElement.kt index 11478afa66..4297e24420 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/BasalElement.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/BasalElement.kt @@ -1,12 +1,11 @@ package info.nightscout.androidaps.plugins.general.tidepool.elements import com.google.gson.annotations.Expose -import info.nightscout.androidaps.data.Intervals import info.nightscout.androidaps.db.TemporaryBasal -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction import java.util.* -class BasalElement(tbr: TemporaryBasal) +class BasalElement(tbr: TemporaryBasal, private val profileFunction: ProfileFunction) : BaseElement(tbr.date, UUID.nameUUIDFromBytes(("AAPS-basal" + tbr.date).toByteArray()).toString()) { internal var timestamp: Long = 0 // not exposed @@ -27,18 +26,7 @@ class BasalElement(tbr: TemporaryBasal) init { type = "basal" timestamp = tbr.date - rate = tbr.tempBasalConvertedToAbsolute(tbr.date, ProfileFunctions.getInstance().getProfile(tbr.date)) + rate = tbr.tempBasalConvertedToAbsolute(tbr.date, profileFunction.getProfile(tbr.date)) duration = tbr.end() - tbr.start() } - - companion object { - internal fun fromTemporaryBasals(tbrList: Intervals, start: Long, end: Long): List { - val results = LinkedList() - for (tbr in tbrList.list) { - if (tbr.date >= start && tbr.date <= end && tbr.durationInMinutes != 0) - results.add(BasalElement(tbr)) - } - return results - } - } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt index 8c15511d60..cae2dd7b6a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/elements/ProfileElement.kt @@ -9,7 +9,7 @@ import info.nightscout.androidaps.utils.InstanceId import java.util.* import kotlin.collections.ArrayList -class ProfileElement private constructor(ps: ProfileSwitch) +class ProfileElement(ps: ProfileSwitch, serialNumber: String) : BaseElement(ps.date, UUID.nameUUIDFromBytes(("AAPS-profile" + ps.date).toByteArray()).toString()) { @Expose @@ -25,11 +25,9 @@ class ProfileElement private constructor(ps: ProfileSwitch) @Expose internal var insulinSensitivities: IsfProfile = IsfProfile() @Expose - internal var deviceId: String = TidepoolUploader.PUMP_TYPE + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() - ?: InstanceId.instanceId()) + internal var deviceId: String = TidepoolUploader.PUMP_TYPE + ":" + serialNumber @Expose - internal var deviceSerialNumber: String = ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() - ?: InstanceId.instanceId() + internal var deviceSerialNumber: String = serialNumber @Expose internal var clockDriftOffset: Long = 0 @Expose @@ -96,16 +94,6 @@ class ProfileElement private constructor(ps: ProfileSwitch) @field:Expose internal var amount: Double ) - - companion object { - @JvmStatic - fun newInstanceOrNull(ps: ProfileSwitch): ProfileElement? = try { - ProfileElement(ps) - } catch (e: Throwable) { - null - } - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/AuthRequestMessage.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/AuthRequestMessage.kt index 33c230ea75..7b5d8a679d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/AuthRequestMessage.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/AuthRequestMessage.kt @@ -1,17 +1,17 @@ package info.nightscout.androidaps.plugins.general.tidepool.messages import info.nightscout.androidaps.R -import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.StringUtils +import info.nightscout.androidaps.utils.sharedPreferences.SP import okhttp3.Credentials class AuthRequestMessage : BaseMessage() { companion object { - fun getAuthRequestHeader(): String? { - val username = SP.getString(R.string.key_tidepool_username, null) - val password = SP.getString(R.string.key_tidepool_password, null) + fun getAuthRequestHeader(sp: SP): String? { + val username = sp.getStringOrNull(R.string.key_tidepool_username, null) + val password = sp.getStringOrNull(R.string.key_tidepool_password, null) - return if (StringUtils.emptyString(username) || StringUtils.emptyString(password)) null else Credentials.basic(username.trim { it <= ' ' }, password) + return if (StringUtils.emptyString(username) || StringUtils.emptyString(password)) null else Credentials.basic(username!!.trim { it <= ' ' }, password!!) } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt index 3f8bd4efb2..c0b0d3a92b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/messages/OpenDatasetRequestMessage.kt @@ -9,11 +9,10 @@ import info.nightscout.androidaps.utils.InstanceId import info.nightscout.androidaps.utils.T import java.util.* -class OpenDatasetRequestMessage : BaseMessage() { +class OpenDatasetRequestMessage (val serialNumber : String): BaseMessage() { @Expose - var deviceId: String = TidepoolUploader.PUMP_TYPE + ":" + (ConfigBuilderPlugin.getPlugin().activePump?.serialNumber() - ?: InstanceId.instanceId()) + var deviceId: String = TidepoolUploader.PUMP_TYPE + ":" + serialNumber @Expose var time = DateUtil.toISOAsUTC(DateUtil.now()) @Expose diff --git a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt index ce1c12f8a4..f407820197 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt @@ -15,6 +15,7 @@ interface SP { fun remove(@StringRes resourceID: Int) fun remove(key: String) fun getString(@StringRes resourceID: Int, defaultValue: String): String + fun getStringOrNull(@StringRes resourceID: Int, defaultValue: String?): String? fun getString(key: String, defaultValue: String): String fun getBoolean(@StringRes resourceID: Int, defaultValue: Boolean): Boolean fun getBoolean(key: String, defaultValue: Boolean): Boolean diff --git a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt index c56439b1ff..3eec951a3f 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt @@ -31,6 +31,9 @@ class SPImplementation @Inject constructor( override fun getString(resourceID: Int, defaultValue: String): String = sharedPreferences.getString(resourceHelper.gs(resourceID), defaultValue) ?: defaultValue + override fun getStringOrNull(resourceID: Int, defaultValue: String?): String? = + sharedPreferences.getString(resourceHelper.gs(resourceID), defaultValue) ?: defaultValue + override fun getString(key: String, defaultValue: String): String = sharedPreferences.getString(key, defaultValue) ?: defaultValue