chore: rename project

This commit is contained in:
Stefan Zollinger
2024-04-10 10:39:07 +02:00
parent c94ca3f40f
commit e53a269b4f
16 changed files with 19 additions and 19 deletions

View File

@@ -0,0 +1,117 @@
package com.logitech.vc.kirbytest
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 {
private const val INVALID_CO2 = 16383
private const val INVALID_VOC = 2047
private const val INVALID_HUMIDITY = 1023
private const val INVALID_TEMPERATURE = 2047
private const val INVALID_PRESSURE = 65535
private const val INVALID_OCCUPANCY = 3
private const val INVALID_PM25 = 1023
private const val INVALID_PM10 = 1023
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
measurement.co2 = if (co2 == INVALID_CO2) null else co2
val voc = parseUnsignedInt(inputBytes, 2, 4) ushr 3 and INVALID_VOC
measurement.voc = if (co2 == INVALID_VOC) null else voc
val humidity =
parseUnsignedInt(inputBytes, 3, 5) ushr 1 and INVALID_HUMIDITY
measurement.humidity = if (humidity == INVALID_HUMIDITY) null else humidity / 10
val temperature =
parseUnsignedInt(inputBytes, 4, 7) ushr 6 and INVALID_TEMPERATURE
measurement.temperature =
if (temperature == INVALID_TEMPERATURE) null else ((temperature / 10.0 - 40) * 10.0).roundToInt() / 10.0
val pressure =
parseUnsignedInt(inputBytes, 6, 9) ushr 6 and INVALID_PRESSURE
measurement.pressure =
if (pressure == INVALID_PRESSURE) null else (30000 + 19000.0 * pressure / 13107).roundToInt()
val occupancy =
parseUnsignedInt(inputBytes, 8, 9) ushr 4 and INVALID_OCCUPANCY
measurement.occupancy = if (occupancy == INVALID_OCCUPANCY) null else occupancy
if (msgType == 0) {
val pm25 =
parseUnsignedInt(inputBytes, 8, 10) ushr 2 and INVALID_PM25
measurement.pm25 = if (pm25 == INVALID_PM25) null else pm25
val pm10 = parseUnsignedInt(inputBytes, 9, 11) and INVALID_PM10
measurement.pm10 = if (pm10 == INVALID_PM10) null else pm10
}
return measurement
}
private fun parseUnsignedInt(bytes: ByteArray, startIncl: Int, endExcl: Int): Int {
val section = bytes.copyOfRange(startIncl, min(bytes.size, endExcl))
var unsignedInt = 0
for (i in section.indices) {
unsignedInt = unsignedInt shl 8
unsignedInt = unsignedInt or (section[i].toInt() and 0xFF)
}
return unsignedInt
}
private fun hexStringToByteArray(encoded: String): ByteArray {
return try {
Hex.decodeHex(encoded)
} catch (e: DecoderException) {
throw RuntimeException(e)
}
}
data class Measurement (
var msgType: Number? = null,
var co2: Number? = null,
var voc: Number? = null,
var humidity: Number? = null,
var temperature: Number? = null,
var pressure: Number? = null,
var occupancy: Number? = null,
var pm25: Number? = null,
var pm10: Number? = null
) {
override fun toString(): String {
return "M{" +
"type=" + msgType +
", co2=" + co2 +
", voc=" + voc +
", hum=" + humidity +
", temp=" + temperature +
", press=" + pressure +
", pm25=" + pm25 +
", pm10=" + pm10 +
", occ=" + occupancy +
'}'
}
fun toCsv() : String {
return "${msgType},${co2},${voc},${humidity},${temperature},${pressure}${pm25},${pm10},${occupancy}"
}
}
}