set current timestamp if add weatherdata-request is without timestamp

This commit is contained in:
Joel Schmid 2021-11-26 11:51:22 +01:00
parent ff2bbb543e
commit a230cfd30e
3 changed files with 30 additions and 45 deletions

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"time"
"weather-data/config" "weather-data/config"
"weather-data/storage" "weather-data/storage"
"weather-data/weathersource" "weather-data/weathersource"
@ -144,15 +145,13 @@ func (api *weatherRestApi) addWeatherDataHandler(w http.ResponseWriter, r *http.
http.Error(w, "", http.StatusBadRequest) http.Error(w, "", http.StatusBadRequest)
return return
} }
delete(data, "sensorId")
weatherData, err := storage.FromMap(data) data[storage.SensorId] = id
if err != nil { if _, containsTimeStamp := data[storage.TimeStamp]; !containsTimeStamp {
http.Error(w, "", http.StatusBadRequest) data[storage.TimeStamp] = time.Now()
return
} }
weatherData.SensorId, err = uuid.Parse(id) weatherData, err := storage.FromMap(data)
if err != nil { if err != nil {
http.Error(w, "", http.StatusBadRequest) http.Error(w, "", http.StatusBadRequest)
return return

View file

@ -21,21 +21,18 @@ func main() {
var err error var err error
if sensorRegistry, err = storage.NewMongodbSensorRegistry(config.MongoConfiguration); err != nil { if sensorRegistry, err = storage.NewMongodbSensorRegistry(config.MongoConfiguration); err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1)
} }
defer sensorRegistry.Close() defer sensorRegistry.Close()
//setup a new weatherstorage -> InfluxDB //setup a new weatherstorage -> InfluxDB
if weatherStorage, err = storage.NewInfluxStorage(config.InfluxConfiguration); err != nil { if weatherStorage, err = storage.NewInfluxStorage(config.InfluxConfiguration); err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1)
} }
defer weatherStorage.Close() defer weatherStorage.Close()
//setup new weatherData source -> mqtt //setup new weatherData source -> mqtt
if weatherSource, err = weathersource.NewMqttSource(config.MqttConfiguration); err != nil { if weatherSource, err = weathersource.NewMqttSource(config.MqttConfiguration); err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1)
} }
defer weatherSource.Close() defer weatherSource.Close()
weatherSource.AddNewWeatherDataCallback(handleNewWeatherData) weatherSource.AddNewWeatherDataCallback(handleNewWeatherData)
@ -45,12 +42,11 @@ func main() {
defer weatherAPI.Close() defer weatherAPI.Close()
weatherAPI.AddNewWeatherDataCallback(handleNewWeatherData) weatherAPI.AddNewWeatherDataCallback(handleNewWeatherData)
log.Print("Application is running")
err = weatherAPI.Start() err = weatherAPI.Start()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1)
} }
log.Print("Application is running")
} }
func handleNewWeatherData(wd storage.WeatherData) { func handleNewWeatherData(wd storage.WeatherData) {

View file

@ -1,7 +1,6 @@
package storage package storage
import ( import (
"errors"
"math" "math"
"math/rand" "math/rand"
"time" "time"
@ -79,44 +78,35 @@ func (data *WeatherData) ToMap() map[string]interface{} {
} }
//FromMap converts a map[string]interface{} to WeatherData //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) var data = new(WeatherData)
data.Values = make(map[SensorValueType]float64) data.Values = make(map[SensorValueType]float64)
var err error var err error
copy := make(map[string]interface{}) for key, val := range values {
for key, value := range value { switch value := val.(type) {
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) {
case float64: 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: default:
} }
} }