Skip to content

Commit

Permalink
Merge pull request #144 from combustion-inc/feature/droid-560-overhea…
Browse files Browse the repository at this point in the history
…t-flags-not-working-for-android

DROID-560 - Fix overheating sensor flags forwareded from a node
  • Loading branch information
angrygorilla authored Jan 11, 2025
2 parents b9016b4 + 5e9a22d commit 986fbf1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ internal data class ProbeStatus(
const val MIN_RAW_SIZE = 30
const val RAW_SIZE_INCLUDING_FOOD_SAFE =
MIN_RAW_SIZE + FoodSafeData.SIZE_BYTES + FoodSafeStatus.SIZE_BYTES
const val RAW_SIZE_INCLUDING_OVERHEAT =
MIN_RAW_SIZE + FoodSafeData.SIZE_BYTES + FoodSafeStatus.SIZE_BYTES + OverheatingSensors.SIZE_BYTES

private const val MIN_SEQ_INDEX = 0
private const val MAX_SEQ_INDEX = 4
Expand All @@ -91,7 +89,7 @@ internal data class ProbeStatus(
private val OVERHEAT_BYTE_RANGE =
OVERHEAT_BYTE_START_INDEX until OVERHEAT_BYTE_START_INDEX + OverheatingSensors.SIZE_BYTES

fun fromRawData(data: UByteArray): ProbeStatus? {
fun fromRawData(data: UByteArray, overheatRange: IntRange = OVERHEAT_BYTE_RANGE): ProbeStatus? {
if (data.size < MIN_RAW_SIZE) return null

val minSequenceNumber = data.getLittleEndianUInt32At(MIN_SEQ_INDEX)
Expand All @@ -107,7 +105,7 @@ internal data class ProbeStatus(
val batteryStatus = ProbeBatteryStatus.fromUByte(deviceStatus)
val virtualSensors = ProbeVirtualSensors.fromDeviceStatus(deviceStatus)

val dataIncludesFoodSafe = data.size > MIN_RAW_SIZE
val dataIncludesFoodSafe = data.size > FOOD_SAFE_DATA_RANGE.last
val foodSafeData = if (dataIncludesFoodSafe) {
FoodSafeData.fromRawData(data.sliceArray(FOOD_SAFE_DATA_RANGE))
} else {
Expand All @@ -120,9 +118,19 @@ internal data class ProbeStatus(
}

// Decode Over heating flags
val dataIncludesOverheat = data.size >= RAW_SIZE_INCLUDING_OVERHEAT
val dataIncludesOverheat = data.size > overheatRange.last
val overheatingSensors = if (dataIncludesOverheat) {
OverheatingSensors.fromRawByte(data.sliceArray(OVERHEAT_BYTE_RANGE)[0])

// Sanity check for the overheating flags. If none of the temperatures are above the
// previous thresholds, then ignore the flag values there are no overheating sensors.
// There is a bug in the repeater firmware versions <= 2.2.0 that can cause these
// flags to be incorrectly set. This should help reduce invalid overheating errors
// to the user.
if (!OverheatingSensors.fromTemperatures(temperatures).isAnySensorOverheating()) {
OverheatingSensors.fromRawByte(0u)
} else {
OverheatingSensors.fromRawByte(data.sliceArray(overheatRange)[0])
}
} else {
OverheatingSensors.fromTemperatures(temperatures)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package inc.combustion.framework.ble.uart.meatnet
import inc.combustion.framework.ble.ProbeStatus
import inc.combustion.framework.ble.getLittleEndianUInt32At
import inc.combustion.framework.service.HopCount
import inc.combustion.framework.service.OverheatingSensors

internal class NodeProbeStatusRequest(
requestId : UInt,
Expand Down Expand Up @@ -67,10 +68,15 @@ internal class NodeProbeStatusRequest(
ProbeStatus.RAW_SIZE_INCLUDING_FOOD_SAFE
}

val probeStatusRange = PROBE_STATUS_INDEX until probeStatusIndexEnd
val probeStatusRange = PROBE_STATUS_INDEX until data.size

// The overheating flags are stored after the hop count for the node messages
// so the range is updated to take that into account
val overheatRangeStart = ProbeStatus.RAW_SIZE_INCLUDING_FOOD_SAFE + 1
val overheatRange = overheatRangeStart until overheatRangeStart + OverheatingSensors.SIZE_BYTES

val probeStatus: ProbeStatus =
ProbeStatus.fromRawData(data.slice(probeStatusRange).toUByteArray()) ?: return null
ProbeStatus.fromRawData(data.slice(probeStatusRange).toUByteArray(), overheatRange) ?: return null

val hopCount: HopCount = HopCount.fromUByte(data[probeStatusIndexEnd])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ package inc.combustion.framework.service

data class OverheatingSensors(val values: List<Int>) {

public fun isAnySensorOverheating(): Boolean {
return values.isNotEmpty()
}

companion object {
internal const val SIZE_BYTES = 1

Expand Down

0 comments on commit 986fbf1

Please sign in to comment.