diff --git a/CHANGELOG b/CHANGELOG index 0ee8add..b52be9a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +22.2 +* fix potential errors when reading temperatures + 22.1 * add autostart file diff --git a/src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/widgets/TemperatureGraphWidget.kt b/src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/widgets/TemperatureGraphWidget.kt index e3d54c2..b6a6302 100644 --- a/src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/widgets/TemperatureGraphWidget.kt +++ b/src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/widgets/TemperatureGraphWidget.kt @@ -21,6 +21,8 @@ import kotlin.collections.sortedBy class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer) : RadarGraphWidget(widgetConfiguration, cssProvider, 5000, 125) { + private val tempRegex = Regex("^\\d+$") + private val attentionTemp = 60 private val warningTemp = 80 @@ -30,8 +32,8 @@ class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvid .map { it.value } val maxTemp = temperatures.maxOrNull() ?: return - var angle = 0.0 val points = ArrayList>(temperatures.size) + var angle = 0.0 for (temp in temperatures) { points.add(polarToCartesian(this, temp * scale, angle)) angle += 360 / temperatures.size @@ -54,19 +56,36 @@ class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvid private fun getTemperatures(): Map { val basePath = "/sys/class/thermal" val temperatures = HashMap() + var unknownIndex = 0 + + try { + opendir(basePath)?.let { dir -> + while (true) { + val subDir = readdir(dir)?.pointed?.d_name?.toKString() ?: break + + if (!subDir.startsWith("thermal_zone")) { + continue + } - opendir(basePath)?.let { dir -> - while (true) { - val subDir = readdir(dir)?.pointed?.d_name?.toKString() ?: break + try { + val type = readFile("$basePath/$subDir/type") + ?.let { it.ifEmpty { "unknown${unknownIndex++}" } } + ?: continue - if (subDir.startsWith("thermal_zone")) { - val type = readFile("$basePath/$subDir/type") ?: continue - val temp = readFile("$basePath/$subDir/temp")?.toInt() ?: continue + val temp = readFile("$basePath/$subDir/temp") + ?.let { if (it.matches(tempRegex)) it else null } + ?.toInt() + ?: continue - temperatures[type] = temp / 1000 + temperatures[type] = temp / 1000 + } catch (e: Throwable) { + println("Unable to read values from $subDir: ${e.message}") + } } + closedir(dir) } - closedir(dir) + } catch (e: Throwable) { + println("Unable to read temperatures: ${e.message}") } return temperatures