Skip to content

Commit

Permalink
safer attribute casting.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Oct 17, 2023
1 parent c3a0fb7 commit e978676
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions webapp/backend/pkg/models/measurements/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.") {
Expand Down

0 comments on commit e978676

Please sign in to comment.