Skip to content

Commit

Permalink
fix: handle issues with temperature readings
Browse files Browse the repository at this point in the history
* check if read values are empty
* catch errors while reading data
* catch errors while doing any kind of accessing the data
  • Loading branch information
atennert committed Apr 9, 2022
1 parent 3ae6f84 commit c009794
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
22.2
* fix potential errors when reading temperatures

22.1
* add autostart file

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import kotlin.collections.sortedBy
class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer<GtkCssProvider>)
: RadarGraphWidget(widgetConfiguration, cssProvider, 5000, 125) {

private val tempRegex = Regex("^\\d+$")

private val attentionTemp = 60
private val warningTemp = 80

Expand All @@ -30,8 +32,8 @@ class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvid
.map { it.value }

val maxTemp = temperatures.maxOrNull() ?: return
var angle = 0.0
val points = ArrayList<Pair<Double, Double>>(temperatures.size)
var angle = 0.0
for (temp in temperatures) {
points.add(polarToCartesian(this, temp * scale, angle))
angle += 360 / temperatures.size
Expand All @@ -54,19 +56,36 @@ class TemperatureGraphWidget(widgetConfiguration: WidgetConfiguration, cssProvid
private fun getTemperatures(): Map<String, Int> {
val basePath = "/sys/class/thermal"
val temperatures = HashMap<String, Int>()
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
Expand Down

0 comments on commit c009794

Please sign in to comment.