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"
"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

View file

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

View file

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