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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"weather-data/api"
|
"weather-data/api"
|
||||||
"weather-data/config"
|
"weather-data/config"
|
||||||
|
@ -19,19 +19,21 @@ func main() {
|
||||||
//setup new sensorRegistry -> MongodbSensorRegistry
|
//setup new sensorRegistry -> MongodbSensorRegistry
|
||||||
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)
|
||||||
os.Exit(1)
|
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)
|
||||||
os.Exit(1)
|
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 {
|
||||||
fmt.Println("Could not connect to mqtt:", err.Error())
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer weatherSource.Close()
|
defer weatherSource.Close()
|
||||||
|
@ -44,14 +46,15 @@ func main() {
|
||||||
|
|
||||||
err = weatherAPI.Start()
|
err = weatherAPI.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleNewWeatherData(wd storage.WeatherData) error {
|
func handleNewWeatherData(wd storage.WeatherData) error {
|
||||||
_, couldResolve := sensorRegistry.ResolveSensorById(wd.SensorId)
|
_, err := sensorRegistry.ResolveSensorById(wd.SensorId)
|
||||||
if !config.AllowUnregisteredSensors && !couldResolve {
|
if !config.AllowUnregisteredSensors && err != nil {
|
||||||
return errors.New("sensor have to be registered")
|
return fmt.Errorf("could not resolve sensor")
|
||||||
}
|
}
|
||||||
weatherStorage.Save(wd)
|
weatherStorage.Save(wd)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -16,7 +16,11 @@ func NewInmemorySensorRegistry() *inmemorySensorRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *inmemorySensorRegistry) RegisterSensorByName(name string) (*WeatherSensor, error) {
|
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")
|
return nil, fmt.Errorf("Sensorname already exists")
|
||||||
}
|
}
|
||||||
sensor := new(WeatherSensor)
|
sensor := new(WeatherSensor)
|
||||||
|
@ -26,31 +30,31 @@ func (registry *inmemorySensorRegistry) RegisterSensorByName(name string) (*Weat
|
||||||
return sensor, nil
|
return sensor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *inmemorySensorRegistry) ExistSensorName(name string) bool {
|
func (registry *inmemorySensorRegistry) ExistSensorName(name string) (bool, error) {
|
||||||
for _, s := range registry.weatherSensors {
|
for _, s := range registry.weatherSensors {
|
||||||
if s.Name == name {
|
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 {
|
for _, s := range registry.weatherSensors {
|
||||||
if s.Id == sensorId {
|
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 {
|
for _, s := range registry.weatherSensors {
|
||||||
if s.Id == sensor.Id {
|
if s.Id == sensor.Id {
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *inmemorySensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
func (registry *inmemorySensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
||||||
|
|
|
@ -52,7 +52,11 @@ func NewMongodbSensorRegistry(mongoCfg config.MongoConfig) (*mongodbSensorRegist
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*WeatherSensor, error) {
|
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")
|
return nil, fmt.Errorf("Sensorname already exists")
|
||||||
}
|
}
|
||||||
sensor := new(WeatherSensor)
|
sensor := new(WeatherSensor)
|
||||||
|
@ -60,51 +64,51 @@ func (registry *mongodbSensorRegistry) RegisterSensorByName(name string) (*Weath
|
||||||
sensor.Id = uuid.New()
|
sensor.Id = uuid.New()
|
||||||
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
_, err := registry.sensorCollection.InsertOne(ctx, sensor)
|
_, err = registry.sensorCollection.InsertOne(ctx, sensor)
|
||||||
|
|
||||||
return sensor, err
|
return sensor, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) ExistSensorName(name string) bool {
|
func (registry *mongodbSensorRegistry) ExistSensorName(name string) (bool, error) {
|
||||||
sensors, err := registry.GetSensors()
|
sensors, err := registry.GetSensors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return false
|
return false, err
|
||||||
}
|
}
|
||||||
for _, s := range sensors {
|
for _, s := range sensors {
|
||||||
if s.Name == name {
|
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()
|
sensors, err := registry.GetSensors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return nil, false
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, s := range sensors {
|
for _, s := range sensors {
|
||||||
if s.Id == sensorId {
|
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()
|
sensors, err := registry.GetSensors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return false
|
return false, err
|
||||||
}
|
}
|
||||||
for _, s := range sensors {
|
for _, s := range sensors {
|
||||||
if s.Id == sensor.Id {
|
if s.Id == sensor.Id {
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
func (registry *mongodbSensorRegistry) GetSensors() ([]*WeatherSensor, error) {
|
||||||
|
|
|
@ -19,8 +19,8 @@ type WeatherStorage interface {
|
||||||
|
|
||||||
type SensorRegistry interface {
|
type SensorRegistry interface {
|
||||||
RegisterSensorByName(string) (*WeatherSensor, error)
|
RegisterSensorByName(string) (*WeatherSensor, error)
|
||||||
ExistSensor(*WeatherSensor) bool
|
ExistSensor(*WeatherSensor) (bool, error)
|
||||||
ResolveSensorById(uuid.UUID) (*WeatherSensor, bool)
|
ResolveSensorById(uuid.UUID) (*WeatherSensor, error)
|
||||||
GetSensors() ([]*WeatherSensor, error)
|
GetSensors() ([]*WeatherSensor, error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package weathersource
|
package weathersource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -106,10 +107,14 @@ func (source *mqttWeatherSource) publishDataValues() {
|
||||||
current := *source.lastWeatherDataPoints[0]
|
current := *source.lastWeatherDataPoints[0]
|
||||||
diff := time.Now().Sub(current.TimeStamp)
|
diff := time.Now().Sub(current.TimeStamp)
|
||||||
if diff >= source.config.MinDistToLastValue {
|
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:]
|
source.lastWeatherDataPoints = source.lastWeatherDataPoints[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
time.Sleep(source.config.PublishInterval)
|
time.Sleep(source.config.PublishInterval)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue