add weather sensor registration

This commit is contained in:
Joel Schmid 2021-03-19 23:39:27 +01:00
parent 69369c210b
commit 145da94899
4 changed files with 37 additions and 1 deletions

View file

@ -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)

View file

@ -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
}

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=