From 26204b4ed7a57e489e2f93df900b7c337691593c Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Fri, 2 Apr 2021 20:18:49 +0200 Subject: [PATCH] improved logs & errorhandling --- main.go | 13 ++++++++----- storage/inmemory-storage.go | 24 ++++++++++++++---------- storage/mongodb-storage.go | 32 ++++++++++++++++++-------------- storage/weather-data.go | 4 ++-- weathersource/mqtt-source.go | 9 +++++++-- 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index c2323d0..ca799a0 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( - "errors" "fmt" + "log" "os" "weather-data/api" "weather-data/config" @@ -19,19 +19,21 @@ func main() { //setup new sensorRegistry -> MongodbSensorRegistry 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 { - fmt.Println("Could not connect to mqtt:", err.Error()) + log.Fatal(err) os.Exit(1) } defer weatherSource.Close() @@ -44,14 +46,15 @@ func main() { err = weatherAPI.Start() if err != nil { + log.Fatal(err) os.Exit(1) } } func handleNewWeatherData(wd storage.WeatherData) error { - _, couldResolve := sensorRegistry.ResolveSensorById(wd.SensorId) - if !config.AllowUnregisteredSensors && !couldResolve { - return errors.New("sensor have to be registered") + _, err := sensorRegistry.ResolveSensorById(wd.SensorId) + if !config.AllowUnregisteredSensors && err != nil { + return fmt.Errorf("could not resolve sensor") } weatherStorage.Save(wd) return nil diff --git a/storage/inmemory-storage.go b/storage/inmemory-storage.go index 27c7b46..da528cd 100644 --- a/storage/inmemory-storage.go +++ b/storage/inmemory-storage.go @@ -16,7 +16,11 @@ func NewInmemorySensorRegistry() *inmemorySensorRegistry { } func (registry *inmemorySensorRegistry) RegisterSensorByName(name string) (*WeatherSensor, error) { - if registry.ExistSensorName(name) { + exist, err := registry.ExistSensorName(name) + if err != nil { + return nil, err + } + if exist { return nil, fmt.Errorf("Sensorname already exists") } sensor := new(WeatherSensor) @@ -26,31 +30,31 @@ func (registry *inmemorySensorRegistry) RegisterSensorByName(name string) (*Weat return sensor, nil } -func (registry *inmemorySensorRegistry) ExistSensorName(name string) bool { +func (registry *inmemorySensorRegistry) ExistSensorName(name string) (bool, error) { for _, s := range registry.weatherSensors { if s.Name == name { - return true + return true, nil } } - return false + return false, nil } -func (registry *inmemorySensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, bool) { +func (registry *inmemorySensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, error) { for _, s := range registry.weatherSensors { if s.Id == sensorId { - return s, true + return s, nil } } - return nil, false + return nil, fmt.Errorf("sensor does not exist") } -func (registry *inmemorySensorRegistry) ExistSensor(sensor *WeatherSensor) bool { +func (registry *inmemorySensorRegistry) ExistSensor(sensor *WeatherSensor) (bool, error) { for _, s := range registry.weatherSensors { if s.Id == sensor.Id { - return true + return true, nil } } - return false + return false, nil } func (registry *inmemorySensorRegistry) GetSensors() ([]*WeatherSensor, error) { diff --git a/storage/mongodb-storage.go b/storage/mongodb-storage.go index 4857235..12eb481 100644 --- a/storage/mongodb-storage.go +++ b/storage/mongodb-storage.go @@ -52,7 +52,11 @@ func NewMongodbSensorRegistry(mongoCfg config.MongoConfig) (*mongodbSensorRegist } func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*WeatherSensor, error) { - if registry.ExistSensorName(name) { + exist, err := registry.ExistSensorName(name) + if err != nil { + return nil, err + } + if exist { return nil, fmt.Errorf("Sensorname already exists") } sensor := new(WeatherSensor) @@ -60,51 +64,51 @@ func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*Weath sensor.Id = uuid.New() ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) - _, err := registry.sensorCollection.InsertOne(ctx, sensor) + _, err = registry.sensorCollection.InsertOne(ctx, sensor) return sensor, err } -func (registry *mongodbSensorRegistry) ExistSensorName(name string) bool { +func (registry *mongodbSensorRegistry) ExistSensorName(name string) (bool, error) { sensors, err := registry.GetSensors() if err != nil { log.Fatal(err) - return false + return false, err } for _, s := range sensors { if s.Name == name { - return true + return true, nil } } - return false + return false, nil } -func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, bool) { +func (registry *mongodbSensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, error) { sensors, err := registry.GetSensors() if err != nil { log.Fatal(err) - return nil, false + return nil, err } for _, s := range sensors { if s.Id == sensorId { - return s, true + return s, nil } } - return nil, false + return nil, fmt.Errorf("sensor does not exist") } -func (registry *mongodbSensorRegistry) ExistSensor(sensor *WeatherSensor) bool { +func (registry *mongodbSensorRegistry) ExistSensor(sensor *WeatherSensor) (bool, error) { sensors, err := registry.GetSensors() if err != nil { log.Fatal(err) - return false + return false, err } for _, s := range sensors { if s.Id == sensor.Id { - return true + return true, nil } } - return false + return false, nil } func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) { diff --git a/storage/weather-data.go b/storage/weather-data.go index a3f2276..21ec527 100644 --- a/storage/weather-data.go +++ b/storage/weather-data.go @@ -19,8 +19,8 @@ type WeatherStorage interface { type SensorRegistry interface { RegisterSensorByName(string) (*WeatherSensor, error) - ExistSensor(*WeatherSensor) bool - ResolveSensorById(uuid.UUID) (*WeatherSensor, bool) + ExistSensor(*WeatherSensor) (bool, error) + ResolveSensorById(uuid.UUID) (*WeatherSensor, error) GetSensors() ([]*WeatherSensor, error) Close() error } diff --git a/weathersource/mqtt-source.go b/weathersource/mqtt-source.go index 714b092..3c3c5a1 100644 --- a/weathersource/mqtt-source.go +++ b/weathersource/mqtt-source.go @@ -1,6 +1,7 @@ package weathersource import ( + "log" "regexp" "strconv" "strings" @@ -106,10 +107,14 @@ func (source *mqttWeatherSource) publishDataValues() { current := *source.lastWeatherDataPoints[0] diff := time.Now().Sub(current.TimeStamp) if diff >= source.config.MinDistToLastValue { - source.newWeatherData(current) + if err := source.newWeatherData(current); err != nil { + log.Fatal(err) + //if error than put the dataPoint to the end of the slice and try again later + dataPoint := source.lastWeatherDataPoints[0] + source.lastWeatherDataPoints = append(source.lastWeatherDataPoints, dataPoint) + } source.lastWeatherDataPoints = source.lastWeatherDataPoints[1:] } - } time.Sleep(source.config.PublishInterval) }