weatherSourceBase as composition to mqtt-source and rest-api
This commit is contained in:
parent
f623f76c1a
commit
261cfb96c4
7 changed files with 33 additions and 53 deletions
|
@ -32,10 +32,10 @@ type UserClaims struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type weatherRestApi struct {
|
type weatherRestApi struct {
|
||||||
|
weathersource.WeatherSourceBase
|
||||||
connection string
|
connection string
|
||||||
config config.RestConfig
|
config config.RestConfig
|
||||||
weaterStorage storage.WeatherStorage
|
weaterStorage storage.WeatherStorage
|
||||||
weatherSource weathersource.WeatherSourceBase
|
|
||||||
sensorRegistry storage.SensorRegistry
|
sensorRegistry storage.SensorRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ func (api *weatherRestApi) addWeatherDataHandler(w http.ResponseWriter, r *http.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
api.addNewWeatherData(*weatherData)
|
api.NewWeatherData(weatherData)
|
||||||
|
|
||||||
w.Header().Add("content-type", "application/json")
|
w.Header().Add("content-type", "application/json")
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
@ -336,12 +336,3 @@ func (api *weatherRestApi) parseToken(header http.Header) (*UserClaims, error) {
|
||||||
)
|
)
|
||||||
return claims, err
|
return claims, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddNewWeatherDataCallback adds a new callbackMethod for incoming weather data
|
|
||||||
func (api *weatherRestApi) AddNewWeatherDataCallback(callback weathersource.NewWeatherDataCallbackFunc) {
|
|
||||||
api.weatherSource.AddNewWeatherDataCallback(callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *weatherRestApi) addNewWeatherData(weatherData storage.WeatherData) {
|
|
||||||
api.weatherSource.NewWeatherData(weatherData)
|
|
||||||
}
|
|
||||||
|
|
13
main.go
13
main.go
|
@ -35,23 +35,24 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer weatherSource.Close()
|
defer weatherSource.Close()
|
||||||
weatherSource.AddNewWeatherDataCallback(handleNewWeatherData)
|
weatherSource.OnNewWeatherData(handleNewWeatherData)
|
||||||
|
|
||||||
//setup a API -> REST
|
//setup a API -> REST
|
||||||
weatherAPI = api.NewRestAPI(":10000", weatherStorage, sensorRegistry, config.RestConfiguration)
|
weatherAPI = api.NewRestAPI(":10000", weatherStorage, sensorRegistry, config.RestConfiguration)
|
||||||
defer weatherAPI.Close()
|
defer weatherAPI.Close()
|
||||||
weatherAPI.AddNewWeatherDataCallback(handleNewWeatherData)
|
weatherAPI.OnNewWeatherData(handleNewWeatherData)
|
||||||
|
|
||||||
|
log.Print("Application is running")
|
||||||
err = weatherAPI.Start()
|
err = weatherAPI.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Print("Application is running")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleNewWeatherData(wd storage.WeatherData) {
|
func handleNewWeatherData(wd *storage.WeatherData) {
|
||||||
_, err := sensorRegistry.GetSensor(wd.SensorId)
|
if config.AllowUnregisteredSensors {
|
||||||
if config.AllowUnregisteredSensors || err == nil {
|
weatherStorage.Save(wd)
|
||||||
|
} else if exist, err := sensorRegistry.ExistSensor(wd.SensorId); err == nil && exist {
|
||||||
weatherStorage.Save(wd)
|
weatherStorage.Save(wd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func NewInfluxStorage(cfg config.InfluxConfig) (*influxStorage, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save WeatherData to InfluxDB
|
//Save WeatherData to InfluxDB
|
||||||
func (storage *influxStorage) Save(data WeatherData) error {
|
func (storage *influxStorage) Save(data *WeatherData) error {
|
||||||
tags := map[string]string{
|
tags := map[string]string{
|
||||||
"sensorId": data.SensorId.String()}
|
"sensorId": data.SensorId.String()}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package storage
|
||||||
|
|
||||||
//WeatherStorage interface for different storage-implementations of weather data
|
//WeatherStorage interface for different storage-implementations of weather data
|
||||||
type WeatherStorage interface {
|
type WeatherStorage interface {
|
||||||
Save(WeatherData) error
|
Save(*WeatherData) error
|
||||||
GetData(*WeatherQuery) ([]*WeatherData, error)
|
GetData(*WeatherQuery) ([]*WeatherData, error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,9 @@ var regexTopic *regexp.Regexp = regexp.MustCompile(mqttTopicRegexPattern)
|
||||||
var channelBufferSize = 10
|
var channelBufferSize = 10
|
||||||
|
|
||||||
type mqttWeatherSource struct {
|
type mqttWeatherSource struct {
|
||||||
|
WeatherSourceBase
|
||||||
config config.MqttConfig
|
config config.MqttConfig
|
||||||
mqttClient mqtt.Client
|
mqttClient mqtt.Client
|
||||||
weatherSource WeatherSourceBase
|
|
||||||
activeSensorMeasurements map[uuid.UUID](chan map[storage.SensorValueType]float64)
|
activeSensorMeasurements map[uuid.UUID](chan map[storage.SensorValueType]float64)
|
||||||
sensorMutex sync.RWMutex
|
sensorMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -127,14 +127,5 @@ func (source *mqttWeatherSource) publishSensorMeasurement(sensorId uuid.UUID, ch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
source.newWeatherData(*weatherData)
|
source.NewWeatherData(weatherData)
|
||||||
}
|
|
||||||
|
|
||||||
//AddNewWeatherDataCallback adds a new callbackMethod for incoming weather data
|
|
||||||
func (source *mqttWeatherSource) AddNewWeatherDataCallback(callback NewWeatherDataCallbackFunc) {
|
|
||||||
source.weatherSource.AddNewWeatherDataCallback(callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (source *mqttWeatherSource) newWeatherData(datapoint storage.WeatherData) {
|
|
||||||
source.weatherSource.NewWeatherData(datapoint)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
package weathersource
|
|
||||||
|
|
||||||
import "weather-data/storage"
|
|
||||||
|
|
||||||
//WeatherSourceBase is the lowlevel-implementation of the WeatherSource interface, intended to used by highlevel-implementations
|
|
||||||
type WeatherSourceBase struct {
|
|
||||||
newWeatherDataCallbackFuncs []NewWeatherDataCallbackFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddNewWeatherDataCallback adds a new callbackMethod for incoming weather data
|
|
||||||
func (source *WeatherSourceBase) AddNewWeatherDataCallback(callback NewWeatherDataCallbackFunc) {
|
|
||||||
source.newWeatherDataCallbackFuncs = append(source.newWeatherDataCallbackFuncs, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewWeatherData executes all newWeatherDataCallbackFuncs for this datapoint
|
|
||||||
func (source *WeatherSourceBase) NewWeatherData(datapoint storage.WeatherData) {
|
|
||||||
for _, callback := range source.newWeatherDataCallbackFuncs {
|
|
||||||
callback(datapoint)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,11 +2,28 @@ package weathersource
|
||||||
|
|
||||||
import "weather-data/storage"
|
import "weather-data/storage"
|
||||||
|
|
||||||
//NewWeatherDataCallbackFunc Function-Signature for new weather data callback function
|
//NewWeatherDataFunc Function-Signature for new weather data
|
||||||
type NewWeatherDataCallbackFunc func(storage.WeatherData)
|
type NewWeatherDataFunc func(*storage.WeatherData)
|
||||||
|
|
||||||
//WeatherSource is the interface for different weather-source implementations
|
//WeatherSource is the interface for different weather-source implementations
|
||||||
type WeatherSource interface {
|
type WeatherSource interface {
|
||||||
AddNewWeatherDataCallback(NewWeatherDataCallbackFunc)
|
OnNewWeatherData(callback NewWeatherDataFunc)
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//WeatherSourceBase is the lowlevel-implementation of the WeatherSource interface, intended to used by highlevel-implementations
|
||||||
|
type WeatherSourceBase struct {
|
||||||
|
onNewWeatherDataFunctions []NewWeatherDataFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
//OnNewWeatherData add a function executed on NewWeatherData called
|
||||||
|
func (source *WeatherSourceBase) OnNewWeatherData(callback NewWeatherDataFunc) {
|
||||||
|
source.onNewWeatherDataFunctions = append(source.onNewWeatherDataFunctions, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewWeatherData executes all NewWeatherDataFunc for the weatherData
|
||||||
|
func (source *WeatherSourceBase) NewWeatherData(weatherData *storage.WeatherData) {
|
||||||
|
for _, function := range source.onNewWeatherDataFunctions {
|
||||||
|
function(weatherData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue