From 145da948996ceb392c9e9f9cfe92bbd90a022e7c Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Fri, 19 Mar 2021 23:39:27 +0100 Subject: [PATCH] add weather sensor registration --- api/rest-api.go | 22 ++++++++++++++++++++++ api/weather-api.go | 13 ++++++++++++- go.mod | 1 + go.sum | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/api/rest-api.go b/api/rest-api.go index cc33a94..1ec6a48 100644 --- a/api/rest-api.go +++ b/api/rest-api.go @@ -7,6 +7,7 @@ import ( "weather-data/storage" "weather-data/weathersource" + "github.com/google/uuid" "github.com/gorilla/mux" ) @@ -41,6 +42,7 @@ func (api *weatherRestApi) handleRequests() *mux.Router { router.HandleFunc("/randomlist", api.randomWeatherListHandler) router.HandleFunc("/addData", api.addDataHandler) router.HandleFunc("/getData", api.getData) + router.HandleFunc("/registerWeatherSensor/{name}", api.registerWeatherSensor) return router } @@ -74,6 +76,7 @@ func (api *weatherRestApi) randomWeatherListHandler(w http.ResponseWriter, r *ht func (api *weatherRestApi) addDataHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "only POST-Method allowed", http.StatusMethodNotAllowed) + return } var data storage.WeatherData @@ -89,6 +92,25 @@ func (api *weatherRestApi) homePageHandler(w http.ResponseWriter, r *http.Reques fmt.Fprintf(w, "Welcome to the Weather API!") } +func (api *weatherRestApi) registerWeatherSensor(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "only POST-Method allowed", http.StatusMethodNotAllowed) + return + } + + w.Header().Add("content-type", "application/json") + + vars := mux.Vars(r) + key := vars["name"] + + registration := SensorRegistration{ + Name: key, + Id: uuid.New(), + } + + json.NewEncoder(w).Encode(registration) +} + //AddNewWeatherDataCallback adds a new callbackMethod for incoming weather data func (api *weatherRestApi) AddNewWeatherDataCallback(callback weathersource.NewWeatherDataCallbackFunc) { api.weatherSource.AddNewWeatherDataCallback(callback) diff --git a/api/weather-api.go b/api/weather-api.go index a472e4a..37c1600 100644 --- a/api/weather-api.go +++ b/api/weather-api.go @@ -1,6 +1,10 @@ package api -import "weather-data/weathersource" +import ( + "weather-data/weathersource" + + "github.com/google/uuid" +) //WeatherAPI is the common interface for different apis type WeatherAPI interface { @@ -8,3 +12,10 @@ type WeatherAPI interface { Close() weathersource.WeatherSource } + +//SensorRegistration is the data for a new Sensorregistration +type SensorRegistration struct { + Name string + Id uuid.UUID + Location string +} diff --git a/go.mod b/go.mod index 2285679..17f72f4 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/eclipse/paho.mqtt.golang v1.3.2 + github.com/google/uuid v1.2.0 // indirect github.com/gorilla/mux v1.8.0 github.com/influxdata/influxdb-client-go/v2 v2.2.2 ) diff --git a/go.sum b/go.sum index d036c07..797ebcd 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y= +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=