remove error of new weather data callback

This commit is contained in:
Joel Schmid 2021-08-22 13:05:03 +02:00
parent 36462b4748
commit 5f58565a53
5 changed files with 11 additions and 23 deletions

View file

@ -184,11 +184,7 @@ func (api *weatherRestApi) addWeatherDataHandler(w http.ResponseWriter, r *http.
return return
} }
err = api.addNewWeatherData(*weatherData) api.addNewWeatherData(*weatherData)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
w.Header().Add("content-type", "application/json") w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -336,6 +332,6 @@ func (api *weatherRestApi) AddNewWeatherDataCallback(callback weathersource.NewW
api.weatherSource.AddNewWeatherDataCallback(callback) api.weatherSource.AddNewWeatherDataCallback(callback)
} }
func (api *weatherRestApi) addNewWeatherData(weatherData storage.WeatherData) error { func (api *weatherRestApi) addNewWeatherData(weatherData storage.WeatherData) {
return api.weatherSource.NewWeatherData(weatherData) api.weatherSource.NewWeatherData(weatherData)
} }

10
main.go
View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"weather-data/api" "weather-data/api"
@ -54,12 +53,9 @@ func main() {
} }
} }
func handleNewWeatherData(wd storage.WeatherData) error { func handleNewWeatherData(wd storage.WeatherData) {
_, err := sensorRegistry.ResolveSensorById(wd.SensorId) _, err := sensorRegistry.ResolveSensorById(wd.SensorId)
if !config.AllowUnregisteredSensors && err != nil { if config.AllowUnregisteredSensors || err == nil {
log.Print("discarded invalid weatherdata")
return fmt.Errorf("could not resolve sensor")
}
weatherStorage.Save(wd) weatherStorage.Save(wd)
return nil }
} }

View file

@ -135,6 +135,6 @@ func (source *mqttWeatherSource) AddNewWeatherDataCallback(callback NewWeatherDa
source.weatherSource.AddNewWeatherDataCallback(callback) source.weatherSource.AddNewWeatherDataCallback(callback)
} }
func (source *mqttWeatherSource) newWeatherData(datapoint storage.WeatherData) error { func (source *mqttWeatherSource) newWeatherData(datapoint storage.WeatherData) {
return source.weatherSource.NewWeatherData(datapoint) source.weatherSource.NewWeatherData(datapoint)
} }

View file

@ -13,12 +13,8 @@ func (source *WeatherSourceBase) AddNewWeatherDataCallback(callback NewWeatherDa
} }
//NewWeatherData executes all newWeatherDataCallbackFuncs for this datapoint //NewWeatherData executes all newWeatherDataCallbackFuncs for this datapoint
func (source *WeatherSourceBase) NewWeatherData(datapoint storage.WeatherData) error { func (source *WeatherSourceBase) NewWeatherData(datapoint storage.WeatherData) {
for _, callback := range source.newWeatherDataCallbackFuncs { for _, callback := range source.newWeatherDataCallbackFuncs {
err := callback(datapoint) callback(datapoint)
if err != nil {
return err
} }
}
return nil
} }

View file

@ -3,7 +3,7 @@ package weathersource
import "weather-data/storage" import "weather-data/storage"
//NewWeatherDataCallbackFunc Function-Signature for new weather data callback function //NewWeatherDataCallbackFunc Function-Signature for new weather data callback function
type NewWeatherDataCallbackFunc func(storage.WeatherData) error type NewWeatherDataCallbackFunc func(storage.WeatherData)
//WeatherSource is the interface for different weather-source implementations //WeatherSource is the interface for different weather-source implementations
type WeatherSource interface { type WeatherSource interface {