From 1f588f61517da37d077e84dc7242061cc60baa10 Mon Sep 17 00:00:00 2001 From: Scrin Date: Sat, 16 Sep 2023 11:26:10 +0300 Subject: [PATCH] Fix http polling and http listener timestamp from string to number since it changed on gateway firmare v1.14.1 --- data_sources/gateway_polling.go | 17 ++++++----------- data_sources/http_listener.go | 7 +++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/data_sources/gateway_polling.go b/data_sources/gateway_polling.go index 96179be..5dcba32 100644 --- a/data_sources/gateway_polling.go +++ b/data_sources/gateway_polling.go @@ -4,7 +4,6 @@ import ( "encoding/json" "io" "net/http" - "strconv" "strings" "time" @@ -15,22 +14,19 @@ import ( type gatewayHistoryTag struct { Rssi int64 `json:"rssi"` - Timestamp string `json:"timestamp"` + Timestamp int64 `json:"timestamp"` Data string `json:"data"` } // seems to be emitted only if the authentication fails type gatewayInfo struct { - Success bool `json:"success"` GatewayName string `json:"gateway_name"` } type gatewayHistory struct { Data struct { - Coordinates string `json:"coordinates"` - Timestamp string `json:"timestamp"` - GwMac string `json:"gw_mac"` - Tags map[string]gatewayHistoryTag `json:"tags"` + GwMac string `json:"gw_mac"` + Tags map[string]gatewayHistoryTag `json:"tags"` } `json:"data"` } @@ -86,14 +82,13 @@ func poll(url string, bearer_token string, measurements chan<- parser.Measuremen return } - // initialize Success so we can detect if the field was populated - gatewayInfo := gatewayInfo{Success: true, GatewayName: ""} + var gatewayInfo gatewayInfo err = json.Unmarshal(body, &gatewayInfo) if err != nil { log.WithError(err).Error("Failed to deserialize gateway data") return } - if !gatewayInfo.Success { + if len(gatewayInfo.GatewayName) > 0 { log.Error("Failed to authenticate") return } @@ -107,7 +102,7 @@ func poll(url string, bearer_token string, measurements chan<- parser.Measuremen for mac, data := range gatewayHistory.Data.Tags { mac = strings.ToUpper(mac) - timestamp, _ := strconv.ParseInt(data.Timestamp, 10, 64) + timestamp := data.Timestamp if seenTags[mac] == timestamp { continue } diff --git a/data_sources/http_listener.go b/data_sources/http_listener.go index 12170ea..ca19189 100644 --- a/data_sources/http_listener.go +++ b/data_sources/http_listener.go @@ -3,9 +3,8 @@ package data_sources import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" - "strconv" "strings" "github.com/Scrin/RuuviBridge/config" @@ -23,7 +22,7 @@ func StartHTTPListener(conf config.HTTPListener, measurements chan<- parser.Meas seenTags := make(map[string]int64) handlerFunc := func(w http.ResponseWriter, req *http.Request) { - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { log.WithFields(log.Fields{ "path": req.URL.Path, @@ -46,7 +45,7 @@ func StartHTTPListener(conf config.HTTPListener, measurements chan<- parser.Meas for mac, data := range gatewayHistory.Data.Tags { mac = strings.ToUpper(mac) - timestamp, _ := strconv.ParseInt(data.Timestamp, 10, 64) + timestamp := data.Timestamp if seenTags[mac] == timestamp { continue }