From 974842696a19df0c611da41a61d976ffab702691 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Tue, 31 Oct 2023 15:43:04 +0100 Subject: [PATCH] Mark unavailable values as NaN --- host/host.go | 16 +++++++++++++++- host/host_linux.go | 7 ++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/host/host.go b/host/host.go index a312eda9b..319e52197 100644 --- a/host/host.go +++ b/host/host.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "math" "os" "runtime" "time" @@ -60,7 +61,20 @@ func (u UserStat) String() string { } func (t TemperatureStat) String() string { - s, _ := json.Marshal(t) + x := t + if math.IsNaN(x.Low) { + x.Low = 0 + } + if math.IsNaN(x.High) { + x.High = 0 + } + if math.IsNaN(x.Critical) { + x.Critical = 0 + } + if math.IsNaN(x.Alarm) { + x.Alarm = 0 + } + s, _ := json.Marshal(x) return string(s) } diff --git a/host/host_linux.go b/host/host_linux.go index ef736db22..262c3d579 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -9,6 +9,7 @@ import ( "encoding/binary" "fmt" "io" + "math" "os" "path/filepath" "regexp" @@ -512,15 +513,15 @@ func optionalValueReadFromFile(filename string) float64 { // Check if file exists if _, err := os.Stat(filename); os.IsNotExist(err) { - return 0 + return math.NaN() } if raw, err = os.ReadFile(filename); err != nil { - return 0 + return math.NaN() } if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil { - return 0 + return math.NaN() } return value