fix(#1787): handle unknown hardware details crash (#1789)

This commit is contained in:
James Rich 2025-04-26 19:37:49 -05:00 committed by GitHub
parent 00ac8d504c
commit 1bd6173f9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
90 changed files with 15108 additions and 35821 deletions

View file

@ -17,84 +17,21 @@
package com.geeksville.mesh.model
import androidx.annotation.DrawableRes
import com.geeksville.mesh.MeshProtos.HardwareModel
import com.geeksville.mesh.R
import kotlinx.serialization.Serializable
data class DeviceHardware(
val hwModel: Int,
val hwModelSlug: String,
val architecture: String,
val activelySupported: Boolean,
val supportLevel: Int? = null,
val displayName: String,
val tags: List<String>? = listOf(),
@DrawableRes val image: Int,
val requiresDfu: Boolean? = null,
)
@Serializable
data class DeviceHardwareDto(
val hwModel: Int,
val hwModelSlug: String,
val platformioTarget: String,
val architecture: String,
val activelySupported: Boolean,
val supportLevel: Int? = null,
val displayName: String,
val tags: List<String>? = listOf(),
val images: List<String>? = listOf(),
data class DeviceHardware(
val activelySupported: Boolean = false,
val architecture: String = "",
val displayName: String = "",
val hasInkHud: Boolean? = null,
val hasMui: Boolean? = null,
val hwModel: Int = 0,
val hwModelSlug: String = "",
val images: List<String>? = null,
val partitionScheme: String? = null,
val platformioTarget: String = "",
val requiresDfu: Boolean? = null,
) {
fun toDeviceHardware() = DeviceHardware(
hwModel = hwModel,
hwModelSlug = hwModelSlug,
architecture = architecture,
activelySupported = activelySupported,
supportLevel = supportLevel,
displayName = displayName,
tags = tags,
image = getDrawableFrom(hwModel),
requiresDfu = requiresDfu
)
}
@Suppress("CyclomaticComplexMethod")
@DrawableRes
private fun getDrawableFrom(hwModel: Int): Int = when (hwModel) {
HardwareModel.DIY_V1_VALUE -> R.drawable.hw_diy
HardwareModel.HELTEC_HT62_VALUE -> R.drawable.hw_heltec_ht62_esp32c3_sx1262
HardwareModel.HELTEC_MESH_NODE_T114_VALUE -> R.drawable.hw_heltec_mesh_node_t114
HardwareModel.HELTEC_V3_VALUE -> R.drawable.hw_heltec_v3_case
HardwareModel.HELTEC_VISION_MASTER_E213_VALUE -> R.drawable.hw_heltec_vision_master_e213
HardwareModel.HELTEC_VISION_MASTER_E290_VALUE -> R.drawable.hw_heltec_vision_master_e290
HardwareModel.HELTEC_VISION_MASTER_T190_VALUE -> R.drawable.hw_heltec_vision_master_t190
HardwareModel.HELTEC_WIRELESS_PAPER_VALUE -> R.drawable.hw_heltec_wireless_paper
HardwareModel.HELTEC_WIRELESS_TRACKER_VALUE -> R.drawable.hw_heltec_wireless_tracker
HardwareModel.HELTEC_WIRELESS_TRACKER_V1_0_VALUE -> R.drawable.hw_heltec_wireless_tracker_v1_0
HardwareModel.HELTEC_WSL_V3_VALUE -> R.drawable.hw_heltec_wsl_v3
HardwareModel.NANO_G2_ULTRA_VALUE -> R.drawable.hw_nano_g2_ultra
HardwareModel.RPI_PICO_VALUE -> R.drawable.hw_pico
HardwareModel.NRF52_PROMICRO_DIY_VALUE -> R.drawable.hw_promicro
HardwareModel.RAK11310_VALUE -> R.drawable.hw_rak11310
HardwareModel.RAK2560_VALUE -> R.drawable.hw_rak2560
HardwareModel.RAK4631_VALUE -> R.drawable.hw_rak4631_case
HardwareModel.RPI_PICO2_VALUE -> R.drawable.hw_rpipicow
HardwareModel.SENSECAP_INDICATOR_VALUE -> R.drawable.hw_seeed_sensecap_indicator
HardwareModel.SEEED_XIAO_S3_VALUE -> R.drawable.hw_seeed_xiao_s3
HardwareModel.STATION_G2_VALUE -> R.drawable.hw_station_g2
HardwareModel.T_DECK_VALUE -> R.drawable.hw_t_deck
HardwareModel.T_ECHO_VALUE -> R.drawable.hw_t_echo
HardwareModel.T_WATCH_S3_VALUE -> R.drawable.hw_t_watch_s3
HardwareModel.TBEAM_VALUE -> R.drawable.hw_tbeam
HardwareModel.LILYGO_TBEAM_S3_CORE_VALUE -> R.drawable.hw_tbeam_s3_core
HardwareModel.TLORA_C6_VALUE -> R.drawable.hw_tlora_c6
HardwareModel.TLORA_T3_S3_VALUE -> R.drawable.hw_tlora_t3s3_v1
HardwareModel.TLORA_V2_1_1P6_VALUE -> R.drawable.hw_tlora_v2_1_1_6
HardwareModel.TLORA_V2_1_1P8_VALUE -> R.drawable.hw_tlora_v2_1_1_8
HardwareModel.TRACKER_T1000_E_VALUE -> R.drawable.hw_tracker_t1000_e
HardwareModel.WIO_WM1110_VALUE -> R.drawable.hw_wio_tracker_wm1110
HardwareModel.WISMESH_TAP_VALUE -> R.drawable.hw_rak_wismeshtap
else -> R.drawable.hw_unknown
}
val supportLevel: Int? = null,
val tags: List<String>? = null
)

View file

@ -229,8 +229,11 @@ class MetricsViewModel @Inject constructor(
.onEach { node ->
_state.update { state -> state.copy(node = node) }
node?.user?.hwModel?.let { hwModel ->
_state.update { state ->
state.copy(deviceHardware = getDeviceHardwareFromHardwareModel(hwModel))
val deviceHardware = getDeviceHardwareFromHardwareModel(hwModel)
deviceHardware?.let {
_state.update { state ->
state.copy(deviceHardware = it)
}
}
}
}
@ -348,12 +351,14 @@ class MetricsViewModel @Inject constructor(
try {
val json =
app.assets.open("device_hardware.json").bufferedReader().use { it.readText() }
deviceHardwareList = Json.decodeFromString<List<DeviceHardwareDto>>(json)
.map { it.toDeviceHardware() }
deviceHardwareList = Json.decodeFromString<List<DeviceHardware>>(json)
return deviceHardwareList.find { it.hwModel == hwModel.number }
} catch (ex: IOException) {
errormsg("Can't read device_hardware.json error: ${ex.message}")
} catch (ex: IllegalArgumentException) {
errormsg(ex.message.toString())
}
}
return deviceHardwareList.find { it.hwModel == hwModel.number }
return null
}
}

View file

@ -19,7 +19,6 @@
package com.geeksville.mesh.ui
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@ -81,6 +80,8 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
@ -90,8 +91,10 @@ import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil3.compose.AsyncImage
import com.geeksville.mesh.ConfigProtos.Config.DisplayConfig.DisplayUnits
import com.geeksville.mesh.R
import com.geeksville.mesh.model.DeviceHardware
import com.geeksville.mesh.model.MetricsState
import com.geeksville.mesh.model.MetricsViewModel
import com.geeksville.mesh.model.Node
@ -237,10 +240,10 @@ private fun DeviceDetailsContent(
),
contentAlignment = Alignment.Center
) {
Image(
modifier = Modifier.padding(16.dp),
imageVector = ImageVector.vectorResource(deviceHardware.image),
contentDescription = hwModelName,
DeviceHardwareImage(
deviceHardware = deviceHardware,
modifier = Modifier
.size(100.dp)
)
}
NodeDetailRow(
@ -258,6 +261,27 @@ private fun DeviceDetailsContent(
}
}
@Composable
fun DeviceHardwareImage(
deviceHardware: DeviceHardware,
modifier: Modifier = Modifier,
) {
val hwImg = deviceHardware.images?.lastOrNull()
if (hwImg != null) {
val imageUrl = "file:///android_asset/device_hardware/$hwImg"
AsyncImage(
model = imageUrl,
contentScale = ContentScale.Inside,
contentDescription = deviceHardware.displayName,
placeholder = painterResource(R.drawable.hw_unknown),
error = painterResource(R.drawable.hw_unknown),
fallback = painterResource(R.drawable.hw_unknown),
modifier = modifier
.padding(16.dp)
)
}
}
@Composable
private fun NodeDetailsContent(
node: Node,