Skip to content

Commit

Permalink
Fix http polling and http listener timestamp from string to number si…
Browse files Browse the repository at this point in the history
…nce it changed on gateway firmare v1.14.1
  • Loading branch information
Scrin committed Sep 16, 2023
1 parent ca27483 commit 1f588f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
17 changes: 6 additions & 11 deletions data_sources/gateway_polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -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"`
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions data_sources/http_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package data_sources
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"

"github.com/Scrin/RuuviBridge/config"
Expand All @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit 1f588f6

Please sign in to comment.