Markus Pesch
10069568f9
Changes: - Renamed storage endpoint into dsn (data source name). - Add additional dsn fallback property. This dsn will be used in futes to store informations, if the main dsn backend does not work correctly. For example, if no connection can be established over the network to a database.
190 lines
5.8 KiB
Go
190 lines
5.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/repository/db"
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
|
)
|
|
|
|
// Repository represent a repository where all devices, sensors and measured
|
|
// values are stored.
|
|
type Repository struct {
|
|
database db.Database
|
|
}
|
|
|
|
// AddDevices to the repository
|
|
func (repo *Repository) AddDevices(devices ...*types.Device) error {
|
|
return repo.database.InsertDevices(context.Background(), devices...)
|
|
}
|
|
|
|
// AddMeasuredValues to the repository
|
|
func (repo *Repository) AddMeasuredValues(measuredValues ...*types.MeasuredValue) error {
|
|
return repo.database.InsertMeasuredValues(context.Background(), measuredValues...)
|
|
}
|
|
|
|
// AddSensors to the repository
|
|
func (repo *Repository) AddSensors(sensors ...*types.Sensor) error {
|
|
return repo.database.InsertSensors(context.Background(), sensors...)
|
|
}
|
|
|
|
// DisableSensorsByNames disable all sensors which match bei their name
|
|
func (repo *Repository) DisableSensorsByNames(sensorNames ...string) error {
|
|
sensors, err := repo.GetSensors()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
matchedSensors := make([]*types.Sensor, 0)
|
|
for _, sensor := range sensors {
|
|
for _, sensorName := range sensorNames {
|
|
if strings.Compare(sensor.Name, sensorName) == 0 {
|
|
sensor.Enabled = false
|
|
matchedSensors = append(matchedSensors, sensor)
|
|
}
|
|
}
|
|
}
|
|
|
|
return repo.UpdateSensors(matchedSensors...)
|
|
}
|
|
|
|
// EnableSensorsByNames enable all sensors which match bei their name
|
|
func (repo *Repository) EnableSensorsByNames(sensorNames ...string) error {
|
|
sensors, err := repo.GetSensors()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
matchedSensors := make([]*types.Sensor, 0)
|
|
for _, sensor := range sensors {
|
|
for _, sensorName := range sensorNames {
|
|
if strings.Compare(sensor.Name, sensorName) == 0 {
|
|
sensor.Enabled = true
|
|
matchedSensors = append(matchedSensors, sensor)
|
|
}
|
|
}
|
|
}
|
|
|
|
return repo.UpdateSensors(matchedSensors...)
|
|
}
|
|
|
|
// GetDevice returns a device by his id. If no device has been found, the
|
|
// function returns nil.
|
|
func (repo *Repository) GetDevice(deviceID string) (*types.Device, error) {
|
|
return repo.database.SelectDevice(context.Background(), deviceID)
|
|
}
|
|
|
|
// GetDevices returns all devices. If no devices has been found, the function
|
|
// returns nil.
|
|
func (repo *Repository) GetDevices() ([]*types.Device, error) {
|
|
return repo.database.SelectDevices(context.Background())
|
|
}
|
|
|
|
// GetSensor returns a sensor by his id. If no sensor has been found, the
|
|
// function returns nil.
|
|
func (repo *Repository) GetSensor(sensorID string) (*types.Sensor, error) {
|
|
return repo.database.SelectSensor(context.Background(), sensorID)
|
|
}
|
|
|
|
// GetSensors returns all sensors. If no sensors has been found, the function
|
|
// returns nil.
|
|
func (repo *Repository) GetSensors() ([]*types.Sensor, error) {
|
|
return repo.database.SelectSensors(context.Background())
|
|
}
|
|
|
|
// GetSensorsByDeviceID returns all sensors by a device id. If no sensor has
|
|
// been found by the id, the function returns nil.
|
|
func (repo *Repository) GetSensorsByDeviceID(deviceID string) ([]*types.Sensor, error) {
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
sensors, err := repo.GetSensors()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, sensor := range sensors {
|
|
if strings.Compare(sensor.DeviceID, deviceID) == 0 {
|
|
cachedSensors = append(cachedSensors, sensor)
|
|
}
|
|
}
|
|
|
|
return cachedSensors, nil
|
|
}
|
|
|
|
// RemoveDevices removes devices by their ids from the repository. Additional
|
|
// all sensors and measured values, which are in relation with the device
|
|
// respectively the sensors will also be deleted.
|
|
func (repo *Repository) RemoveDevices(deviceIDs ...string) error {
|
|
return repo.database.DeleteDevices(context.Background(), deviceIDs...)
|
|
}
|
|
|
|
// RenameSensors all sensors which match by their current name to the new name
|
|
func (repo *Repository) RenameSensors(oldName string, newName string) error {
|
|
sensors, err := repo.GetSensors()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
matchedSensors := make([]*types.Sensor, 0)
|
|
for _, sensor := range sensors {
|
|
if strings.Compare(sensor.Name, oldName) == 0 {
|
|
sensor.Name = newName
|
|
matchedSensors = append(matchedSensors, sensor)
|
|
}
|
|
}
|
|
|
|
return repo.UpdateSensors(matchedSensors...)
|
|
}
|
|
|
|
// RemoveSensors removes sensors by their ids from the repository. Additional
|
|
// all measured values, which are in relation with the sensor will also be
|
|
// deleted.
|
|
func (repo *Repository) RemoveSensors(sensorIDs ...string) error {
|
|
return repo.database.DeleteSensors(context.Background(), sensorIDs...)
|
|
}
|
|
|
|
// RemoveSensorsByNames removes all sensors which match bei their name
|
|
func (repo *Repository) RemoveSensorsByNames(sensorNames ...string) error {
|
|
sensors, err := repo.GetSensors()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
matchedSensorIDs := make([]string, 0)
|
|
for _, sensor := range sensors {
|
|
for _, sensorName := range sensorNames {
|
|
if strings.Compare(sensor.Name, sensorName) == 0 {
|
|
matchedSensorIDs = append(matchedSensorIDs, sensor.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
return repo.RemoveSensors(matchedSensorIDs...)
|
|
}
|
|
|
|
// UpdateDevices update devices which are stored into the repository by their
|
|
// id. The id of the device can not be updated.
|
|
func (repo *Repository) UpdateDevices(devices ...*types.Device) error {
|
|
return repo.database.UpdateDevices(context.Background(), devices...)
|
|
}
|
|
|
|
// UpdateSensors update sensors which are stored into the repository by their
|
|
// id. The id of the sensor can not be updated.
|
|
func (repo *Repository) UpdateSensors(sensors ...*types.Sensor) error {
|
|
return repo.database.UpdateSensors(context.Background(), sensors...)
|
|
}
|
|
|
|
// New returns a new repository based on the data source name (dsn)
|
|
func New(dsnURL *url.URL, flogger logger.Logger) (*Repository, error) {
|
|
database, err := db.New(dsnURL, flogger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Repository{
|
|
database: database,
|
|
}, nil
|
|
}
|