Skip to content

Commit

Permalink
Mark unavailable values as NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Oct 31, 2023
1 parent 37484a7 commit 9748426
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 15 additions & 1 deletion host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"math"
"os"
"runtime"
"time"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9748426

Please sign in to comment.