From e978676c16eb327296dd2460a5ba3a8b22b63ae9 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Tue, 17 Oct 2023 07:18:51 -0700 Subject: [PATCH] safer attribute casting. --- .../backend/pkg/models/measurements/smart.go | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/webapp/backend/pkg/models/measurements/smart.go b/webapp/backend/pkg/models/measurements/smart.go index 40c13970..41b00ed6 100644 --- a/webapp/backend/pkg/models/measurements/smart.go +++ b/webapp/backend/pkg/models/measurements/smart.go @@ -64,11 +64,27 @@ func NewSmartFromInfluxDB(attrs map[string]interface{}) (*Smart, error) { for key, val := range attrs { switch key { case "temp": - sm.Temp = val.(int64) + temp, tempOk := val.(int64) + if tempOk { + sm.Temp = temp + } else { + log.Printf("unable to parse temp information: %v", val) + } + case "power_on_hours": - sm.PowerOnHours = val.(int64) + powerOn, powerOnOk := val.(int64) + if powerOnOk { + sm.PowerOnHours = powerOn + } else { + log.Printf("unable to parse power_on_hours information: %v", val) + } case "power_cycle_count": - sm.PowerCycleCount = val.(int64) + powerCycle, powerCycleOk := val.(int64) + if powerCycleOk { + sm.PowerCycleCount = powerCycle + } else { + log.Printf("unable to parse power_cycle_count information: %v", val) + } default: // this key is unknown. if !strings.HasPrefix(key, "attr.") {