resolve sensor

This commit is contained in:
Joel Schmid 2021-03-20 16:23:17 +01:00
parent 4b6d8e6c6b
commit 395e4530af
3 changed files with 6 additions and 5 deletions

View file

@ -55,7 +55,8 @@ func main() {
}
func handleNewWeatherData(wd storage.WeatherData) error {
if !config.AllowUnregisteredSensors() && !sensorRegistry.ExistSensorId(wd.SensorId) {
_, couldResolve := sensorRegistry.ResolveSensorById(wd.SensorId)
if !config.AllowUnregisteredSensors() && !couldResolve {
return errors.New("sensor have to be registered")
}
weatherStorage.Save(wd)

View file

@ -35,13 +35,13 @@ func (registry *inmemorySensorRegistry) ExistSensorName(name string) bool {
return false
}
func (registry *inmemorySensorRegistry) ExistSensorId(sensorId uuid.UUID) bool {
func (registry *inmemorySensorRegistry) ResolveSensorById(sensorId uuid.UUID) (*WeatherSensor, bool) {
for _, s := range registry.weatherSensors {
if s.Id == sensorId {
return true
return s, true
}
}
return false
return nil, false
}
func (registry *inmemorySensorRegistry) ExistSensor(sensor *WeatherSensor) bool {

View file

@ -17,7 +17,7 @@ type WeatherStorage interface {
type SensorRegistry interface {
RegisterSensorByName(string) (*WeatherSensor, error)
ExistSensor(*WeatherSensor) bool
ExistSensorId(uuid.UUID) bool
ResolveSensorById(uuid.UUID) (*WeatherSensor, bool)
GetSensors() []*WeatherSensor
Close() error
}