DanaR: ignore history records with invalid date

This commit is contained in:
Milos Kozak 2023-01-21 14:48:56 +01:00
parent 2b7c082979
commit c1fca211bf

View file

@ -7,6 +7,7 @@ import info.nightscout.rx.events.EventDanaRSyncStatus
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
@Suppress("SpellCheckingInspection")
open class MsgHistoryAll( open class MsgHistoryAll(
injector: HasAndroidInjector injector: HasAndroidInjector
) : MessageBase(injector) { ) : MessageBase(injector) {
@ -17,136 +18,143 @@ open class MsgHistoryAll(
} }
override fun handleMessage(bytes: ByteArray) { override fun handleMessage(bytes: ByteArray) {
val recordCode = intFromBuff(bytes, 0, 1).toByte() try {
val date = dateFromBuff(bytes, 1) // 3 bytes val recordCode = intFromBuff(bytes, 0, 1).toByte()
val dailyBasal = intFromBuff(bytes, 4, 2) * 0.01 val date = dateFromBuff(bytes, 1) // 3 bytes
val dailyBolus = intFromBuff(bytes, 6, 2) * 0.01 val dailyBasal = intFromBuff(bytes, 4, 2) * 0.01
//val paramByte5 = intFromBuff(bytes, 4, 1).toByte() val dailyBolus = intFromBuff(bytes, 6, 2) * 0.01
//val paramByte6 = intFromBuff(bytes, 5, 1).toByte() //val paramByte5 = intFromBuff(bytes, 4, 1).toByte()
val paramByte7 = intFromBuff(bytes, 6, 1).toByte() //val paramByte6 = intFromBuff(bytes, 5, 1).toByte()
val paramByte8 = intFromBuff(bytes, 7, 1).toByte() val paramByte7 = intFromBuff(bytes, 6, 1).toByte()
val value = intFromBuff(bytes, 8, 2).toDouble() val paramByte8 = intFromBuff(bytes, 7, 1).toByte()
val danaHistoryRecord = DanaHistoryRecord( val value = intFromBuff(bytes, 8, 2).toDouble()
timestamp = date, val danaHistoryRecord = DanaHistoryRecord(
code = recordCode timestamp = date,
) code = recordCode
var messageType = "" )
when (recordCode) { var messageType = ""
RecordTypes.RECORD_TYPE_BOLUS -> { when (recordCode) {
val datetime = dateTimeFromBuff(bytes, 1) // 5 bytes RecordTypes.RECORD_TYPE_BOLUS -> {
danaHistoryRecord.timestamp = datetime val datetime = dateTimeFromBuff(bytes, 1) // 5 bytes
when (0xF0 and paramByte8.toInt()) { danaHistoryRecord.timestamp = datetime
0xA0 -> { when (0xF0 and paramByte8.toInt()) {
danaHistoryRecord.bolusType = "DS" 0xA0 -> {
messageType += "DS bolus" danaHistoryRecord.bolusType = "DS"
} messageType += "DS bolus"
}
0xC0 -> { 0xC0 -> {
danaHistoryRecord.bolusType = "E" danaHistoryRecord.bolusType = "E"
messageType += "E bolus" messageType += "E bolus"
} }
0x80 -> { 0x80 -> {
danaHistoryRecord.bolusType = "S" danaHistoryRecord.bolusType = "S"
messageType += "S bolus" messageType += "S bolus"
} }
0x90 -> { 0x90 -> {
danaHistoryRecord.bolusType = "DE" danaHistoryRecord.bolusType = "DE"
messageType += "DE bolus" messageType += "DE bolus"
} }
else -> danaHistoryRecord.bolusType = "None" else -> danaHistoryRecord.bolusType = "None"
}
danaHistoryRecord.duration = T.mins((paramByte8.toInt() and 0x0F) * 60 + paramByte7.toLong()).msecs()
danaHistoryRecord.value = value * 0.01
} }
danaHistoryRecord.duration = T.mins((paramByte8.toInt() and 0x0F) * 60 + paramByte7.toLong()).msecs()
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_DAILY -> { RecordTypes.RECORD_TYPE_DAILY -> {
messageType += "dailyinsulin" messageType += "dailyinsulin"
danaHistoryRecord.timestamp = date danaHistoryRecord.timestamp = date
danaHistoryRecord.dailyBasal = dailyBasal danaHistoryRecord.dailyBasal = dailyBasal
danaHistoryRecord.dailyBolus = dailyBolus danaHistoryRecord.dailyBolus = dailyBolus
}
RecordTypes.RECORD_TYPE_PRIME -> {
messageType += "prime"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_ERROR -> {
messageType += "error"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_REFILL -> {
messageType += "refill"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_BASALHOUR -> {
messageType += "basal hour"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_TB -> {
messageType += "tb"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_GLUCOSE -> {
messageType += "glucose"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value
}
RecordTypes.RECORD_TYPE_CARBO -> {
messageType += "carbo"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value
}
RecordTypes.RECORD_TYPE_ALARM -> {
messageType += "alarm"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
var strAlarm = "None"
when (paramByte8.toInt()) {
67 -> strAlarm = "Check"
79 -> strAlarm = "Occlusion"
66 -> strAlarm = "Low Battery"
83 -> strAlarm = "Shutdown"
} }
danaHistoryRecord.alarm = strAlarm
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_SUSPEND -> { RecordTypes.RECORD_TYPE_PRIME -> {
messageType += "suspend" messageType += "prime"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec danaHistoryRecord.timestamp = datetimewihtsec
var strRecordValue = "Off" danaHistoryRecord.value = value * 0.01
if (paramByte8.toInt() == 79) strRecordValue = "On" }
danaHistoryRecord.stringValue = strRecordValue
}
17.toByte() -> failed = true RecordTypes.RECORD_TYPE_ERROR -> {
messageType += "error"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_REFILL -> {
messageType += "refill"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_BASALHOUR -> {
messageType += "basal hour"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_TB -> {
messageType += "tb"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_GLUCOSE -> {
messageType += "glucose"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value
}
RecordTypes.RECORD_TYPE_CARBO -> {
messageType += "carbo"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
danaHistoryRecord.value = value
}
RecordTypes.RECORD_TYPE_ALARM -> {
messageType += "alarm"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
var strAlarm = "None"
when (paramByte8.toInt()) {
67 -> strAlarm = "Check"
79 -> strAlarm = "Occlusion"
66 -> strAlarm = "Low Battery"
83 -> strAlarm = "Shutdown"
}
danaHistoryRecord.alarm = strAlarm
danaHistoryRecord.value = value * 0.01
}
RecordTypes.RECORD_TYPE_SUSPEND -> {
messageType += "suspend"
val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes
danaHistoryRecord.timestamp = datetimewihtsec
var strRecordValue = "Off"
if (paramByte8.toInt() == 79) strRecordValue = "On"
danaHistoryRecord.stringValue = strRecordValue
}
17.toByte() -> failed = true
}
danaHistoryRecordDao.createOrUpdate(danaHistoryRecord)
if (recordCode == RecordTypes.RECORD_TYPE_DAILY)
pumpSync.createOrUpdateTotalDailyDose(date, dailyBolus, dailyBasal, dailyBolus + dailyBasal, date, activePlugin.activePump.model(), danaPump.serialNumber)
rxBus.send(EventDanaRSyncStatus(dateUtil.dateAndTimeString(danaHistoryRecord.timestamp) + " " + messageType))
} catch (e: Exception) {
// DanaR id sometimes producing invalid date in history
// ignore these records
aapsLogger.error(e.stackTraceToString())
return
} }
danaHistoryRecordDao.createOrUpdate(danaHistoryRecord)
if (recordCode == RecordTypes.RECORD_TYPE_DAILY)
pumpSync.createOrUpdateTotalDailyDose(date, dailyBolus, dailyBasal, dailyBolus + dailyBasal, date, activePlugin.activePump.model(), danaPump.serialNumber)
rxBus.send(EventDanaRSyncStatus(dateUtil.dateAndTimeString(danaHistoryRecord.timestamp) + " " + messageType))
} }
} }