2020-05-21 15:40:24 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-06 22:09:26 +00:00
|
|
|
"fmt"
|
2020-05-21 15:40:24 +00:00
|
|
|
"net/url"
|
|
|
|
|
2020-06-10 19:13:05 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
|
|
|
"git.cryptic.systems/volker.raschek/go-logger"
|
2020-05-21 15:40:24 +00:00
|
|
|
)
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
type Repository interface {
|
|
|
|
AddDevices(ctx context.Context, devices ...*types.Device) error
|
|
|
|
AddMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error
|
|
|
|
AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error
|
|
|
|
AddOrUpdateMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error
|
|
|
|
AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error
|
|
|
|
AddSensors(ctx context.Context, sensors ...*types.Sensor) error
|
|
|
|
Close() error
|
|
|
|
GetDeviceByID(ctx context.Context, deviceID string) (*types.Device, error)
|
|
|
|
GetDeviceByName(ctx context.Context, name string) (*types.Device, error)
|
|
|
|
GetDevices(ctx context.Context) ([]*types.Device, error)
|
|
|
|
GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error)
|
|
|
|
GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
|
|
|
GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
|
|
|
GetPressures(ctx context.Context) ([]*types.MeasuredValue, error)
|
|
|
|
GetSensorByID(ctx context.Context, sensorID string) (*types.Sensor, error)
|
|
|
|
GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error)
|
|
|
|
GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error)
|
|
|
|
GetSensorsByNames(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error)
|
|
|
|
GetSensors(ctx context.Context) ([]*types.Sensor, error)
|
|
|
|
GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
|
|
|
GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error)
|
2021-04-09 14:29:09 +00:00
|
|
|
Import(ctx context.Context, src Repository) error
|
2021-03-21 17:47:14 +00:00
|
|
|
Migrate(ctx context.Context) error
|
|
|
|
RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error
|
|
|
|
RemoveDevicesByNames(ctx context.Context, names ...string) error
|
|
|
|
RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error
|
|
|
|
RemoveSensorsByNames(ctx context.Context, names ...string) error
|
|
|
|
UpdateDevices(ctx context.Context, devices ...*types.Device) error
|
|
|
|
UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error
|
2020-05-21 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// New returns a new database backend interface
|
|
|
|
func New(databaseURL *url.URL, flogger logger.Logger) (Repository, error) {
|
|
|
|
switch databaseURL.Scheme {
|
|
|
|
case "postgres":
|
2021-04-09 14:29:09 +00:00
|
|
|
repo, err := NewPostgres(PostgresOpts{
|
2021-03-21 17:47:14 +00:00
|
|
|
DatabaseURL: databaseURL,
|
|
|
|
Logger: flogger,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-21 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// Initialize database scheme if not exists
|
|
|
|
err = repo.Migrate(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-06 22:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
return repo, nil
|
2020-11-06 22:09:26 +00:00
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
case "sqlite3":
|
2021-04-09 14:29:09 +00:00
|
|
|
repo, err := NewSQLite(SQLiteOpts{
|
2021-03-21 17:47:14 +00:00
|
|
|
DatabaseURL: databaseURL,
|
|
|
|
Logger: flogger,
|
|
|
|
})
|
2020-11-06 22:09:26 +00:00
|
|
|
if err != nil {
|
2021-03-21 17:47:14 +00:00
|
|
|
return nil, err
|
2020-11-06 22:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// Initialize database scheme if not exists
|
|
|
|
err = repo.Migrate(context.Background())
|
2020-11-06 22:09:26 +00:00
|
|
|
if err != nil {
|
2021-03-21 17:47:14 +00:00
|
|
|
return nil, err
|
2020-11-06 22:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
return repo, nil
|
2020-11-06 22:09:26 +00:00
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported repository scheme: %v", databaseURL.Scheme)
|
2020-11-06 22:09:26 +00:00
|
|
|
}
|
|
|
|
}
|