improved logs & errorhandling
This commit is contained in:
parent
69f8c4e9dc
commit
26204b4ed7
5 changed files with 49 additions and 33 deletions
13
main.go
13
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue