From a230cfd30eafa79897858da1cb8258762ba4cbda Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Fri, 26 Nov 2021 11:51:22 +0100 Subject: [PATCH] set current timestamp if add weatherdata-request is without timestamp --- api/rest-api.go | 11 ++++---- main.go | 6 +---- storage/weather-data.go | 58 +++++++++++++++++------------------------ 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/api/rest-api.go b/api/rest-api.go index e02f83c..dfa777b 100644 --- a/api/rest-api.go +++ b/api/rest-api.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "regexp" + "time" "weather-data/config" "weather-data/storage" "weather-data/weathersource" @@ -144,15 +145,13 @@ func (api *weatherRestApi) addWeatherDataHandler(w http.ResponseWriter, r *http. http.Error(w, "", http.StatusBadRequest) return } - delete(data, "sensorId") - weatherData, err := storage.FromMap(data) - if err != nil { - http.Error(w, "", http.StatusBadRequest) - return + data[storage.SensorId] = id + if _, containsTimeStamp := data[storage.TimeStamp]; !containsTimeStamp { + data[storage.TimeStamp] = time.Now() } - weatherData.SensorId, err = uuid.Parse(id) + weatherData, err := storage.FromMap(data) if err != nil { http.Error(w, "", http.StatusBadRequest) return diff --git a/main.go b/main.go index 07920a7..bc100cf 100644 --- a/main.go +++ b/main.go @@ -21,21 +21,18 @@ func main() { var err error if sensorRegistry, err = storage.NewMongodbSensorRegistry(config.MongoConfiguration); err != nil { log.Fatal(err) - os.Exit(1) } defer sensorRegistry.Close() //setup a new weatherstorage -> InfluxDB if weatherStorage, err = storage.NewInfluxStorage(config.InfluxConfiguration); err != nil { log.Fatal(err) - os.Exit(1) } defer weatherStorage.Close() //setup new weatherData source -> mqtt if weatherSource, err = weathersource.NewMqttSource(config.MqttConfiguration); err != nil { log.Fatal(err) - os.Exit(1) } defer weatherSource.Close() weatherSource.AddNewWeatherDataCallback(handleNewWeatherData) @@ -45,12 +42,11 @@ func main() { defer weatherAPI.Close() weatherAPI.AddNewWeatherDataCallback(handleNewWeatherData) - log.Print("Application is running") err = weatherAPI.Start() if err != nil { log.Fatal(err) - os.Exit(1) } + log.Print("Application is running") } func handleNewWeatherData(wd storage.WeatherData) { diff --git a/storage/weather-data.go b/storage/weather-data.go index d7dc952..9c51502 100644 --- a/storage/weather-data.go +++ b/storage/weather-data.go @@ -1,7 +1,6 @@ package storage import ( - "errors" "math" "math/rand" "time" @@ -79,44 +78,35 @@ func (data *WeatherData) ToMap() map[string]interface{} { } //FromMap converts a map[string]interface{} to WeatherData -func FromMap(value map[string]interface{}) (*WeatherData, error) { +func FromMap(values map[string]interface{}) (*WeatherData, error) { var data = new(WeatherData) data.Values = make(map[SensorValueType]float64) var err error - copy := make(map[string]interface{}) - for key, value := range value { - copy[key] = value - } - - _, exists := copy[SensorId] - idString, ok := copy[SensorId].(string) - if exists && !ok { - return nil, errors.New("sensorId must be of type string") - } - - if exists { - data.SensorId, err = uuid.Parse(idString) - if err != nil { - return nil, err - } - delete(copy, SensorId) - } - - timeStampString, ok := copy[TimeStamp].(string) - if !ok { - return nil, errors.New("timeStamp must be of type string") - } - data.TimeStamp, err = time.Parse(time.RFC3339, timeStampString) - if err != nil { - return nil, err - } - delete(copy, TimeStamp) - - for key, val := range copy { - switch v := val.(type) { + for key, val := range values { + switch value := val.(type) { case float64: - data.Values[SensorValueType(key)] = float64(v) + data.Values[SensorValueType(key)] = float64(value) + case string: + if key == SensorId { + data.SensorId, err = uuid.Parse(value) + if err != nil { + return nil, err + } + } else if key == TimeStamp { + data.TimeStamp, err = time.Parse(time.RFC3339, value) + if err != nil { + return nil, err + } + } + case uuid.UUID: + if key == SensorId { + data.SensorId = value + } + case time.Time: + if key == TimeStamp { + data.TimeStamp = value + } default: } }