NS: Sync BolusCalculatorResult
This commit is contained in:
parent
b16318da2c
commit
60c14cda21
|
@ -214,6 +214,34 @@ class NSClientAddUpdateWorker(
|
||||||
}
|
}
|
||||||
} ?: aapsLogger.error("Error parsing EffectiveProfileSwitch json $json")
|
} ?: aapsLogger.error("Error parsing EffectiveProfileSwitch json $json")
|
||||||
}
|
}
|
||||||
|
eventType == TherapyEvent.Type.BOLUS_WIZARD.text ->
|
||||||
|
if (config.NSCLIENT) {
|
||||||
|
bolusCalculatorResultFromJson(json)?.let { bolusCalculatorResult ->
|
||||||
|
repository.runTransactionForResult(SyncNsBolusCalculatorResultTransaction(bolusCalculatorResult))
|
||||||
|
.doOnError {
|
||||||
|
aapsLogger.error(LTag.DATABASE, "Error while saving BolusCalculatorResult", it)
|
||||||
|
ret = Result.failure(workDataOf("Error" to it.toString()))
|
||||||
|
}
|
||||||
|
.blockingGet()
|
||||||
|
.also { result ->
|
||||||
|
result.inserted.forEach {
|
||||||
|
uel.log(Action.BOLUS_CALCULATOR_RESULT, Sources.NSClient,
|
||||||
|
ValueWithUnit.Timestamp(it.timestamp),
|
||||||
|
)
|
||||||
|
aapsLogger.debug(LTag.DATABASE, "Inserted BolusCalculatorResult $it")
|
||||||
|
}
|
||||||
|
result.invalidated.forEach {
|
||||||
|
uel.log(Action.BOLUS_CALCULATOR_RESULT_REMOVED, Sources.NSClient,
|
||||||
|
ValueWithUnit.Timestamp(it.timestamp),
|
||||||
|
)
|
||||||
|
aapsLogger.debug(LTag.DATABASE, "Invalidated BolusCalculatorResult $it")
|
||||||
|
}
|
||||||
|
result.updatedNsId.forEach {
|
||||||
|
aapsLogger.debug(LTag.DATABASE, "Updated nsId BolusCalculatorResult $it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} ?: aapsLogger.error("Error parsing BolusCalculatorResult json $json")
|
||||||
|
}
|
||||||
eventType == TherapyEvent.Type.CANNULA_CHANGE.text ||
|
eventType == TherapyEvent.Type.CANNULA_CHANGE.text ||
|
||||||
eventType == TherapyEvent.Type.INSULIN_CHANGE.text ||
|
eventType == TherapyEvent.Type.INSULIN_CHANGE.text ||
|
||||||
eventType == TherapyEvent.Type.SENSOR_CHANGE.text ||
|
eventType == TherapyEvent.Type.SENSOR_CHANGE.text ||
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package info.nightscout.androidaps.extensions
|
package info.nightscout.androidaps.extensions
|
||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonSyntaxException
|
||||||
import info.nightscout.androidaps.Constants
|
import info.nightscout.androidaps.Constants
|
||||||
import info.nightscout.androidaps.database.entities.BolusCalculatorResult
|
import info.nightscout.androidaps.database.entities.BolusCalculatorResult
|
||||||
import info.nightscout.androidaps.database.entities.TherapyEvent
|
import info.nightscout.androidaps.database.entities.TherapyEvent
|
||||||
import info.nightscout.androidaps.utils.DateUtil
|
import info.nightscout.androidaps.utils.DateUtil
|
||||||
|
import info.nightscout.androidaps.utils.JsonHelper
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
fun BolusCalculatorResult.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
|
fun BolusCalculatorResult.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
|
||||||
|
@ -18,3 +20,23 @@ fun BolusCalculatorResult.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject
|
||||||
.put("units", Constants.MGDL)
|
.put("units", Constants.MGDL)
|
||||||
.put("notes", note)
|
.put("notes", note)
|
||||||
.also { if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId) }
|
.also { if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId) }
|
||||||
|
|
||||||
|
fun bolusCalculatorResultFromJson(jsonObject: JSONObject): BolusCalculatorResult? {
|
||||||
|
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
|
||||||
|
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
|
||||||
|
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
|
||||||
|
val bcrString = JsonHelper.safeGetStringAllowNull(jsonObject, "bolusCalculatorResult", null) ?: return null
|
||||||
|
|
||||||
|
if (timestamp == 0L) return null
|
||||||
|
|
||||||
|
return try {
|
||||||
|
Gson().fromJson(bcrString, BolusCalculatorResult::class.java)
|
||||||
|
.also {
|
||||||
|
it.id = 0
|
||||||
|
it.isValid = isValid
|
||||||
|
it.interfaceIDs.nightscoutId = id
|
||||||
|
}
|
||||||
|
} catch (e: JsonSyntaxException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ class Translator @Inject internal constructor(
|
||||||
|
|
||||||
fun translate(action: Action): String = when (action) {
|
fun translate(action: Action): String = when (action) {
|
||||||
Action.BOLUS -> resourceHelper.gs(R.string.uel_bolus)
|
Action.BOLUS -> resourceHelper.gs(R.string.uel_bolus)
|
||||||
|
Action.BOLUS_CALCULATOR_RESULT -> resourceHelper.gs(R.string.uel_bolus_calculator)
|
||||||
|
Action.BOLUS_CALCULATOR_RESULT_REMOVED -> resourceHelper.gs(R.string.uel_bolus_calculator)
|
||||||
Action.SMB -> resourceHelper.gs(R.string.smb_shortname)
|
Action.SMB -> resourceHelper.gs(R.string.smb_shortname)
|
||||||
Action.BOLUS_ADVISOR -> resourceHelper.gs(R.string.uel_bolus_advisor)
|
Action.BOLUS_ADVISOR -> resourceHelper.gs(R.string.uel_bolus_advisor)
|
||||||
Action.EXTENDED_BOLUS -> resourceHelper.gs(R.string.uel_extended_bolus)
|
Action.EXTENDED_BOLUS -> resourceHelper.gs(R.string.uel_extended_bolus)
|
||||||
|
|
|
@ -397,6 +397,7 @@
|
||||||
|
|
||||||
<!-- User Entry -->
|
<!-- User Entry -->
|
||||||
<string name="uel_bolus">BOLUS</string>
|
<string name="uel_bolus">BOLUS</string>
|
||||||
|
<string name="uel_bolus_calculator">BOLUS CALCULATOR</string>
|
||||||
<string name="uel_bolus_advisor">BOLUS ADVISOR</string>
|
<string name="uel_bolus_advisor">BOLUS ADVISOR</string>
|
||||||
<string name="uel_extended_bolus">EXTENDED BOLUS</string>
|
<string name="uel_extended_bolus">EXTENDED BOLUS</string>
|
||||||
<string name="uel_superbolus_tbr">SUPERBOLUS TBR</string>
|
<string name="uel_superbolus_tbr">SUPERBOLUS TBR</string>
|
||||||
|
|
|
@ -2,7 +2,6 @@ package info.nightscout.androidaps.database.daos
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import info.nightscout.androidaps.database.TABLE_APS_RESULTS
|
|
||||||
import info.nightscout.androidaps.database.TABLE_BOLUS_CALCULATOR_RESULTS
|
import info.nightscout.androidaps.database.TABLE_BOLUS_CALCULATOR_RESULTS
|
||||||
import info.nightscout.androidaps.database.entities.BolusCalculatorResult
|
import info.nightscout.androidaps.database.entities.BolusCalculatorResult
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
|
@ -22,6 +21,12 @@ internal interface BolusCalculatorResultDao : TraceableDao<BolusCalculatorResult
|
||||||
@Query("SELECT id FROM $TABLE_BOLUS_CALCULATOR_RESULTS ORDER BY id DESC limit 1")
|
@Query("SELECT id FROM $TABLE_BOLUS_CALCULATOR_RESULTS ORDER BY id DESC limit 1")
|
||||||
fun getLastId(): Maybe<Long>
|
fun getLastId(): Maybe<Long>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE timestamp = :timestamp AND referenceId IS NULL")
|
||||||
|
fun findByTimestamp(timestamp: Long): BolusCalculatorResult?
|
||||||
|
|
||||||
|
@Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE nightscoutId = :nsId AND referenceId IS NULL")
|
||||||
|
fun findByNSId(nsId: String): BolusCalculatorResult?
|
||||||
|
|
||||||
@Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC")
|
@Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC")
|
||||||
fun getBolusCalculatorResultsFromTime(timestamp: Long): Single<List<BolusCalculatorResult>>
|
fun getBolusCalculatorResultsFromTime(timestamp: Long): Single<List<BolusCalculatorResult>>
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ data class UserEntry(
|
||||||
) : DBEntry, DBEntryWithTime {
|
) : DBEntry, DBEntryWithTime {
|
||||||
enum class Action (val colorGroup: ColorGroup) {
|
enum class Action (val colorGroup: ColorGroup) {
|
||||||
BOLUS (ColorGroup.InsulinTreatment),
|
BOLUS (ColorGroup.InsulinTreatment),
|
||||||
|
BOLUS_CALCULATOR_RESULT (ColorGroup.InsulinTreatment),
|
||||||
|
BOLUS_CALCULATOR_RESULT_REMOVED (ColorGroup.Aaps),
|
||||||
SMB (ColorGroup.InsulinTreatment),
|
SMB (ColorGroup.InsulinTreatment),
|
||||||
BOLUS_ADVISOR (ColorGroup.InsulinTreatment),
|
BOLUS_ADVISOR (ColorGroup.InsulinTreatment),
|
||||||
EXTENDED_BOLUS (ColorGroup.InsulinTreatment),
|
EXTENDED_BOLUS (ColorGroup.InsulinTreatment),
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package info.nightscout.androidaps.database.transactions
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.database.entities.BolusCalculatorResult
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync the BolusCalculatorResult from NS
|
||||||
|
*/
|
||||||
|
class SyncNsBolusCalculatorResultTransaction(private val bolusCalculatorResult: BolusCalculatorResult) :
|
||||||
|
Transaction<SyncNsBolusCalculatorResultTransaction.TransactionResult>() {
|
||||||
|
|
||||||
|
override fun run(): TransactionResult {
|
||||||
|
val result = TransactionResult()
|
||||||
|
|
||||||
|
val current: BolusCalculatorResult? =
|
||||||
|
bolusCalculatorResult.interfaceIDs.nightscoutId?.let {
|
||||||
|
database.bolusCalculatorResultDao.findByNSId(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current != null) {
|
||||||
|
// nsId exists, allow only invalidation
|
||||||
|
if (current.isValid && !bolusCalculatorResult.isValid) {
|
||||||
|
current.isValid = false
|
||||||
|
database.bolusCalculatorResultDao.updateExistingEntry(current)
|
||||||
|
result.invalidated.add(current)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// not known nsId
|
||||||
|
val existing = database.bolusCalculatorResultDao.findByTimestamp(bolusCalculatorResult.timestamp)
|
||||||
|
if (existing != null && existing.interfaceIDs.nightscoutId == null) {
|
||||||
|
// the same record, update nsId only
|
||||||
|
existing.interfaceIDs.nightscoutId = bolusCalculatorResult.interfaceIDs.nightscoutId
|
||||||
|
existing.isValid = bolusCalculatorResult.isValid
|
||||||
|
database.bolusCalculatorResultDao.updateExistingEntry(existing)
|
||||||
|
result.updatedNsId.add(existing)
|
||||||
|
} else {
|
||||||
|
database.bolusCalculatorResultDao.insertNewEntry(bolusCalculatorResult)
|
||||||
|
result.inserted.add(bolusCalculatorResult)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class TransactionResult {
|
||||||
|
|
||||||
|
val updatedNsId = mutableListOf<BolusCalculatorResult>()
|
||||||
|
val inserted = mutableListOf<BolusCalculatorResult>()
|
||||||
|
val invalidated = mutableListOf<BolusCalculatorResult>()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue