commit 7992b1bb522b87716b89656ff470bc00d33fa2fc
parent 07710b1f0dd67c1a780a8cf2874fc094feea2663
Author: Chris Bracken <chris@bracken.jp>
Date: Sat, 13 Nov 2021 13:59:11 -0800
Improve error handling for non-float values
When a sensor sends an AirRohr record with multiple values, only one of
which has a value that can't be converted to a float, store the rest of
them.
Diffstat:
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/airrohr/airrohr.go b/airrohr/airrohr.go
@@ -19,8 +19,8 @@ import (
// max_micro: the maximum time for a loop through main() in μs
// signal: WiFi signal strength in dBm
type Value struct {
- Type string `json:"value_type"`
- Value float64 `json:"value,string"`
+ Type string `json:"value_type"`
+ Value string `json:"value"`
}
// Sensor report from an ESP8266 NodeMCU running AirRohr firmware.
diff --git a/http/http.go b/http/http.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"path/filepath"
+ "strconv"
"time"
"git.bracken.jp/tempestas/airrohr"
@@ -59,7 +60,12 @@ func (s HttpServer) PostAirrohr(w http.ResponseWriter, r *http.Request) {
t := time.Now().UTC()
fmt.Println(t.String() + " Report received from sensor " + rpt.SensorId)
for _, v := range rpt.Values {
- rdg := storage.Reading{rpt.SensorId, rpt.SoftwareVersion, t, v.Type, v.Value}
+ fvalue, err := strconv.ParseFloat(v.Value, 64)
+ if err != nil {
+ fmt.Println("Error: received non-numeric value '" + v.Value + "'")
+ continue
+ }
+ rdg := storage.Reading{rpt.SensorId, rpt.SoftwareVersion, t, v.Type, fvalue}
if err = s.ds.StoreReading(&rdg); err != nil {
fmt.Println("Error: failed to write sensor data: " + err.Error())
}