Glunovo: catch SecurityException

This commit is contained in:
Milos Kozak 2022-08-01 16:40:52 +02:00
parent 1b3bb6ecdd
commit e920b6bc2d

View file

@ -89,80 +89,84 @@ class GlunovoPlugin @Inject constructor(
private fun handleNewData() { private fun handleNewData() {
if (!isEnabled()) return if (!isEnabled()) return
context.contentResolver.query(contentUri, null, null, null, null)?.let { cr -> try {
val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>() context.contentResolver.query(contentUri, null, null, null, null)?.let { cr ->
val calibrations = mutableListOf<CgmSourceTransaction.Calibration>() val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>()
cr.moveToFirst() val calibrations = mutableListOf<CgmSourceTransaction.Calibration>()
cr.moveToFirst()
while (!cr.isAfterLast) { while (!cr.isAfterLast) {
val timestamp = cr.getLong(0) val timestamp = cr.getLong(0)
val value = cr.getDouble(1) //value in mmol/l... val value = cr.getDouble(1) //value in mmol/l...
val curr = cr.getDouble(2) val curr = cr.getDouble(2)
// bypass already processed // bypass already processed
if (timestamp < sp.getLong(R.string.key_last_processed_glunovo_timestamp, 0L)) { if (timestamp < sp.getLong(R.string.key_last_processed_glunovo_timestamp, 0L)) {
cr.moveToNext() cr.moveToNext()
continue continue
} }
if (timestamp > dateUtil.now() || timestamp == 0L) { if (timestamp > dateUtil.now() || timestamp == 0L) {
aapsLogger.error(LTag.BGSOURCE, "Error in received data date/time $timestamp") aapsLogger.error(LTag.BGSOURCE, "Error in received data date/time $timestamp")
cr.moveToNext() cr.moveToNext()
continue continue
} }
if (value < 2 || value > 25) { if (value < 2 || value > 25) {
aapsLogger.error(LTag.BGSOURCE, "Error in received data value (value out of bounds) $value") aapsLogger.error(LTag.BGSOURCE, "Error in received data value (value out of bounds) $value")
cr.moveToNext() cr.moveToNext()
continue continue
} }
if (curr != 0.0) if (curr != 0.0)
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = timestamp,
value = value * Constants.MMOLL_TO_MGDL,
raw = 0.0,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE
)
else
calibrations.add(
CgmSourceTransaction.Calibration(
timestamp = timestamp, timestamp = timestamp,
value = value, value = value * Constants.MMOLL_TO_MGDL,
glucoseUnit = TherapyEvent.GlucoseUnit.MMOL raw = 0.0,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE
) )
) else
sp.putLong(R.string.key_last_processed_glunovo_timestamp, timestamp) calibrations.add(
cr.moveToNext() CgmSourceTransaction.Calibration(
} timestamp = timestamp,
cr.close() value = value,
glucoseUnit = TherapyEvent.GlucoseUnit.MMOL
)
)
sp.putLong(R.string.key_last_processed_glunovo_timestamp, timestamp)
cr.moveToNext()
}
cr.close()
if (glucoseValues.isNotEmpty() || calibrations.isNotEmpty()) if (glucoseValues.isNotEmpty() || calibrations.isNotEmpty())
repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, null)) repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, null))
.doOnError { .doOnError {
aapsLogger.error(LTag.DATABASE, "Error while saving values from Glunovo App", it) aapsLogger.error(LTag.DATABASE, "Error while saving values from Glunovo App", it)
}
.blockingGet()
.also { savedValues ->
savedValues.inserted.forEach {
xDripBroadcast.send(it)
aapsLogger.debug(LTag.DATABASE, "Inserted bg $it")
} }
savedValues.calibrationsInserted.forEach { calibration -> .blockingGet()
calibration.glucose?.let { glucosevalue -> .also { savedValues ->
uel.log( savedValues.inserted.forEach {
UserEntry.Action.CALIBRATION, xDripBroadcast.send(it)
UserEntry.Sources.Dexcom, aapsLogger.debug(LTag.DATABASE, "Inserted bg $it")
ValueWithUnit.Timestamp(calibration.timestamp), }
ValueWithUnit.TherapyEventType(calibration.type), savedValues.calibrationsInserted.forEach { calibration ->
ValueWithUnit.fromGlucoseUnit(glucosevalue, calibration.glucoseUnit.toString) calibration.glucose?.let { glucosevalue ->
) uel.log(
UserEntry.Action.CALIBRATION,
UserEntry.Sources.Dexcom,
ValueWithUnit.Timestamp(calibration.timestamp),
ValueWithUnit.TherapyEventType(calibration.type),
ValueWithUnit.fromGlucoseUnit(glucosevalue, calibration.glucoseUnit.toString)
)
}
aapsLogger.debug(LTag.DATABASE, "Inserted calibration $calibration")
} }
aapsLogger.debug(LTag.DATABASE, "Inserted calibration $calibration")
} }
} }
} catch (e: SecurityException) {
aapsLogger.error(e.localizedMessage)
} }
} }