Skip to content

Commit

Permalink
Add separate FROST http endpoint URL for the pre-fetch of observations (
Browse files Browse the repository at this point in the history
  • Loading branch information
adeveloper-wq authored Jun 28, 2024
1 parent 611cb7f commit 8c0176b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ PREDICTION_MQTT_USERNAME=
PREDICTION_MQTT_PASSWORD=

# The FROST server config.
SENSORTHINGS_URL=https://tld.iot.hamburg.de/v1.1/
SENSORTHINGS_URL_THINGS=https://tld.iot.hamburg.de/v1.1/ # The URL of the SensorThings API. Used to fetch the things.
SENSORTHINGS_URL_OBSERVATIONS=https://tld.iot.hamburg.de/v1.1/ # The URL of the SensorThings API. Used to pre-fetch the observations. Can be the same as the things URL.
SENSORTHINGS_MQTT_URL=tcp://tld.iot.hamburg.de:1883

# The path under which all resources will be stored for the web API.
Expand Down
10 changes: 7 additions & 3 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ func loadOptional(name string, validate func(string) *error) string {
// The path under which the history files are stored, from the environment variable.
var StaticPath string

// The SensorThings API base URL.
var SensorThingsBaseUrl string
// The SensorThings API base URL used for fetching things.
var SensorThingsBaseUrlThings string

// The SensorThings API base URL used for pre-fetching observations.
var SensorThingsBaseUrlObservations string

// The URL to the observation MQTT broker from the environment variable.
var SensorThingsObservationMqttUrl string
Expand Down Expand Up @@ -85,7 +88,8 @@ var emptyValidator = func(value string) *error {

func Init() {
StaticPath = loadRequired("STATIC_PATH", staticPathValidator)
SensorThingsBaseUrl = loadRequired("SENSORTHINGS_URL", sensorThingsBaseUrlValidator)
SensorThingsBaseUrlThings = loadRequired("SENSORTHINGS_URL_THINGS", sensorThingsBaseUrlValidator)
SensorThingsBaseUrlObservations = loadRequired("SENSORTHINGS_URL_OBSERVATIONS", sensorThingsBaseUrlValidator)
SensorThingsObservationMqttUrl = loadRequired("SENSORTHINGS_MQTT_URL", sensorThingsObservationMqttUrlValidator)
PredictionMqttUrl = loadRequired("PREDICTION_MQTT_URL", predictionMqttUrlValidator)
PredictionMqttUsername = loadOptional("PREDICTION_MQTT_USERNAME", emptyValidator)
Expand Down
6 changes: 4 additions & 2 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func TestLoadRequiredPanics(t *testing.T) {

func TestRequiredEnvComplete(t *testing.T) {
t.Setenv("STATIC_PATH", "/usr/share/nginx/html")
t.Setenv("SENSORTHINGS_URL", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_URL_THINGS", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_URL_OBSERVATIONS", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_MQTT_URL", "tcp://tld.iot.hamburg.de:1883")
t.Setenv("PREDICTION_MQTT_URL", "tcp://predictor-mosquitto:1883")
Init()
Expand All @@ -62,7 +63,8 @@ func TestValidatorTriggers(t *testing.T) {
}
}()
t.Setenv("STATIC_PATH", "/usr/share/nginx/html/") // TYPO
t.Setenv("SENSORTHINGS_URL", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_URL_THINGS", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_URL_OBSERVATIONS", "https://tld.iot.hamburg.de/v1.1/")
t.Setenv("SENSORTHINGS_MQTT_URL", "tcp://tld.iot.hamburg.de:1883")
t.Setenv("PREDICTION_MQTT_URL", "tcp://predictor-mosquitto:1883")
Init()
Expand Down
2 changes: 1 addition & 1 deletion observations/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func prefetchMostRecentObservationsPage(page int) (more bool) {
elementsPerPage := 100
pageUrl := env.SensorThingsBaseUrl + "Datastreams?" + url.QueryEscape(
pageUrl := env.SensorThingsBaseUrlObservations + "Datastreams?" + url.QueryEscape(
"$filter="+
"properties/serviceName eq 'HH_STA_traffic_lights' "+
"and (properties/layerName eq 'signal_program') "+
Expand Down
2 changes: 1 addition & 1 deletion observations/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPrefetchMostRecentObservations(t *testing.T) {
}))
defer testServer.Close()

env.SensorThingsBaseUrl = fmt.Sprintf("%s/", testServer.URL)
env.SensorThingsBaseUrlObservations = fmt.Sprintf("%s/", testServer.URL)
PrefetchMostRecentObservations()

signalProgramCycles.Range(func(k, v interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion things/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var BikeDetectorDatastreams = &sync.Map{}

func syncThingsPage(page int) (more bool) {
elementsPerPage := 100
pageUrl := env.SensorThingsBaseUrl + "Things?" + url.QueryEscape(
pageUrl := env.SensorThingsBaseUrlThings + "Things?" + url.QueryEscape(
"$filter="+
"Datastreams/properties/serviceName eq 'HH_STA_traffic_lights' "+
"and (Datastreams/properties/layerName eq 'primary_signal' "+
Expand Down

0 comments on commit 8c0176b

Please sign in to comment.