Skip to content

Commit

Permalink
chore: 默认显示温度为所有元器件最高温度
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 8, 2024
1 parent 05b3a4e commit 4eb3bef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ go.work

.idea
build
config.json
config.json
mock_data.json
31 changes: 20 additions & 11 deletions temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ func MockLoadSensorsTemperature(path string) TemperatureLoader {
}

func RenderLogMessage(s *SensorsTemperature) string {
if len(s.Modules) > 0 && len(s.Modules[0].Data) > 0 {
if f, err := s.Modules[0].Data[0].Input.Float64(); err == nil {
return fmt.Sprintf("Temperature: %.2f°C", f)
}
}
return "Temperature: N/A"
return fmt.Sprintf("Temperature: %s", s.HighestTemperature())
}

func RenderTableMessage(s *SensorsTemperature) string {
Expand All @@ -81,16 +76,14 @@ func RenderTableMessage(s *SensorsTemperature) string {
},
}
var sb strings.Builder
for i, module := range s.Modules {
for j, data := range module.Data {
sb.WriteString(fmt.Sprintf("%s: Temperature: <strong>%.2f°C</strong>\n\n", time.Now().Format(time.DateTime), s.HighestTemperature()))
for _, module := range s.Modules {
for _, data := range module.Data {
temp, err := data.Input.Float64()
if err != nil {
continue
}
tempStr := fmt.Sprintf("%.2f°C", temp)
if i == 0 && j == 0 {
sb.WriteString(fmt.Sprintf("%s: Temperature: <strong>%s</strong>\n\n", time.Now().Format(time.DateTime), tempStr))
}
row := []*simpletable.Cell{
{Text: module.Name},
{Text: data.Name},
Expand All @@ -105,6 +98,22 @@ func RenderTableMessage(s *SensorsTemperature) string {
return sb.String()
}

func (s *SensorsTemperature) HighestTemperature() string {
var maxTemp float64
for _, module := range s.Modules {
for _, data := range module.Data {
t, err := data.Input.Float64()
if err != nil {
return "N/A"
}
if t > maxTemp {
maxTemp = t
}
}
}
return fmt.Sprintf("%.2f°C", maxTemp)
}

func (s *SensorsTemperature) IsHigherThanThreshold(threshold float64) bool {
for _, module := range s.Modules {
for _, data := range module.Data {
Expand Down

0 comments on commit 4eb3bef

Please sign in to comment.