fix: support for unknown payloads

This commit is contained in:
Stefan Zollinger
2024-01-29 11:02:22 +01:00
parent d824b79127
commit 57002a0c15
2 changed files with 18 additions and 3 deletions

View File

@@ -1,7 +1,9 @@
package com.example.sensortestingapp
import android.util.Log
import org.apache.commons.codec.DecoderException
import org.apache.commons.codec.binary.Hex
import kotlin.math.min
import kotlin.math.roundToInt
object DecoderIaq {
@@ -13,10 +15,19 @@ object DecoderIaq {
private const val INVALID_OCCUPANCY = 3
private const val INVALID_PM25 = 1023
private const val INVALID_PM10 = 1023
fun parseMeasurement(input: String): Measurement {
private val supportedMessageTypes = listOf<Number>(0, 1)
fun parseMeasurement(input: String): Measurement? {
val measurement = Measurement()
val inputBytes = hexStringToByteArray(input)
val msgType = inputBytes[0].toInt() and 0xFF ushr 4
if(!supportedMessageTypes.contains(msgType)) {
Log.i("Decoder", "Invalid message type: $msgType")
return null;
}
measurement.msgType = msgType
val co2 = parseUnsignedInt(inputBytes, 0, 3) ushr 6 and INVALID_CO2
@@ -55,7 +66,7 @@ object DecoderIaq {
}
private fun parseUnsignedInt(bytes: ByteArray, startIncl: Int, endExcl: Int): Int {
val section = bytes.copyOfRange(startIncl, endExcl)
val section = bytes.copyOfRange(startIncl, min(bytes.size, endExcl))
var unsignedInt = 0
for (i in section.indices) {
unsignedInt = unsignedInt shl 8

View File

@@ -362,7 +362,11 @@ private fun payloadToMeasurements(payload: Payload): List<Measurement> {
}
override fun getFormattedValue(): String {
return DecoderIaq.parseMeasurement(payload.payload).toString();
val measurement = DecoderIaq.parseMeasurement(payload.payload)
if(measurement == null) {
return payload.payload
}
return measurement.toString()
}
override fun getIcon(): Int? {