Skip to content

Commit

Permalink
gateway_polling: Implemented support for an optional bearer token
Browse files Browse the repository at this point in the history
Simply specify gateway_polling.bearer_token and off you go. Empty
(and missing) value disables it.
  • Loading branch information
eras authored and Scrin committed Sep 4, 2022
1 parent 3dff9e8 commit 01695ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ gateway_polling:
# Flag to enable or disable gateway polling
enabled: false
gateway_url: http://ip.or.hostname.of.the.gateway
# If you have enabled authentication on the gateway, specify the API key (bearer token) from the gateway Access Settings configuration page
bearer_token: ""
interval: 10s

# Recommended option: Have the gateway send the measurements to a MQTT server and let RuuviBridge subscribe to updates in real time
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

type GatewayPolling struct {
Enabled *bool `yaml:"enabled,omitempty"`
GatewayUrl string `yaml:"gateway_url"`
Interval time.Duration `yaml:"interval"`
Enabled *bool `yaml:"enabled,omitempty"`
GatewayUrl string `yaml:"gateway_url"`
BearerToken string `yaml:"bearer_token"`
Interval time.Duration `yaml:"interval"`
}

type MQTTListener struct {
Expand Down
42 changes: 36 additions & 6 deletions data_sources/gateway_polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type gatewayHistoryTag struct {
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"`
Expand All @@ -39,25 +45,36 @@ func StartGatewayPolling(conf config.GatewayPolling, measurements chan<- parser.
})
log.Info("Starting gateway polling")
stop := make(chan bool)
go gatewayPoller(conf.GatewayUrl, interval, measurements, stop, log)
go gatewayPoller(conf.GatewayUrl, conf.BearerToken, interval, measurements, stop, log)
return stop
}

func gatewayPoller(url string, interval time.Duration, measurements chan<- parser.Measurement, stop <-chan bool, log *log.Entry) {
func gatewayPoller(url string, bearer_token string, interval time.Duration, measurements chan<- parser.Measurement, stop <-chan bool, log *log.Entry) {
seenTags := make(map[string]int64)
poll(url, measurements, seenTags, log)
poll(url, bearer_token, measurements, seenTags, log)
for {
select {
case <-stop:
return
case <-time.After(interval):
poll(url, measurements, seenTags, log)
poll(url, bearer_token, measurements, seenTags, log)
}
}
}

func poll(url string, measurements chan<- parser.Measurement, seenTags map[string]int64, log *log.Entry) {
resp, err := http.Get(url + "/history")
func poll(url string, bearer_token string, measurements chan<- parser.Measurement, seenTags map[string]int64, log *log.Entry) {
req, err := http.NewRequest("GET", url+"/history", nil)
if err != nil {
log.WithError(err).Error("Failed to construct GET request")
return
}

if bearer_token != "" {
req.Header.Add("Authorization", "Bearer "+bearer_token)
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.WithError(err).Error("Failed to get history from gateway")
return
Expand All @@ -68,6 +85,19 @@ func poll(url string, measurements chan<- parser.Measurement, seenTags map[strin
log.WithError(err).Error("Failed to read data from gateway")
return
}

// initialize Success so we can detect if the field was populated
gatewayInfo := gatewayInfo{Success: true, GatewayName: ""}
err = json.Unmarshal(body, &gatewayInfo)
if err != nil {
log.WithError(err).Error("Failed to deserialize gateway data")
return
}
if !gatewayInfo.Success {
log.Error("Failed to authenticate")
return
}

var gatewayHistory gatewayHistory
err = json.Unmarshal(body, &gatewayHistory)
if err != nil {
Expand Down

0 comments on commit 01695ba

Please sign in to comment.