2021-04-09 14:29:09 +00:00
|
|
|
package repository
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/repository/postgres"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/repository/sqlite3"
|
2021-03-21 17:47:14 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
|
|
|
"git.cryptic.systems/volker.raschek/go-logger"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/johejo/golang-migrate-extra/source/iofs"
|
|
|
|
)
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
type SQLiteOpts struct {
|
2021-03-21 17:47:14 +00:00
|
|
|
DatabaseURL *url.URL
|
|
|
|
Logger logger.Logger
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (o *SQLiteOpts) Validate() error {
|
2021-03-21 17:47:14 +00:00
|
|
|
for k, v := range map[string]interface{}{
|
|
|
|
"DatabaseURL": o.DatabaseURL,
|
|
|
|
"Logger": o.Logger,
|
|
|
|
} {
|
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("%v does not be empty or nil", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DatabaseURL.Scheme != "sqlite3" {
|
|
|
|
return fmt.Errorf("Unsupported database scheme %v for database backend sqlite3", o.DatabaseURL.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func NewSQLite(opts SQLiteOpts) (Repository, error) {
|
2021-03-21 17:47:14 +00:00
|
|
|
if err := opts.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directory if not exist
|
|
|
|
if _, err := os.Stat(filepath.Dir(opts.DatabaseURL.Path)); os.IsNotExist(err) {
|
2021-05-16 21:02:46 +00:00
|
|
|
/* #nosec */
|
2021-03-21 17:47:14 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(opts.DatabaseURL.Path), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable foreign keys
|
|
|
|
values := opts.DatabaseURL.Query()
|
|
|
|
values.Set("_foreign_keys", "on")
|
|
|
|
|
|
|
|
customRawURL := fmt.Sprintf("file://%v?%v", opts.DatabaseURL.Path, values.Encode())
|
|
|
|
|
|
|
|
sqlDBO, err := sql.Open(opts.DatabaseURL.Scheme, customRawURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SQLite{
|
|
|
|
databaseURL: opts.DatabaseURL,
|
|
|
|
dbo: sqlDBO,
|
|
|
|
flogger: opts.Logger,
|
|
|
|
mutex: new(sync.Mutex),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQLite implementation
|
|
|
|
type SQLite struct {
|
|
|
|
databaseURL *url.URL
|
|
|
|
dbo *sql.DB
|
|
|
|
flogger logger.Logger
|
|
|
|
mutex *sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// AddDevices into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddDevices(ctx context.Context, devices ...*types.Device) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err = d.insertDevices(tx, devices...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SQLite) insertDevices(tx *sql.Tx, devices ...*types.Device) error {
|
|
|
|
stmt, err := tx.Prepare(sqlite3.InsertDeviceSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
for _, device := range devices {
|
2021-05-16 21:02:46 +00:00
|
|
|
_, err = stmt.Exec(
|
|
|
|
&device.ID,
|
|
|
|
&device.Name,
|
|
|
|
&device.Location,
|
|
|
|
&device.CreationDate,
|
|
|
|
&device.UpdateDate,
|
|
|
|
)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
2021-03-21 21:30:50 +00:00
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
2021-03-21 17:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
return nil
|
2021-03-21 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// AddOrUpdateDevices into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err = d.insertOrUpdateDevices(tx, devices...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SQLite) insertOrUpdateDevices(tx *sql.Tx, devices ...*types.Device) error {
|
|
|
|
stmt, err := tx.Prepare(sqlite3.InsertOrUpdateDeviceSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for _, device := range devices {
|
2021-05-16 21:02:46 +00:00
|
|
|
_, err = stmt.Exec(
|
|
|
|
&device.ID,
|
|
|
|
&device.Name,
|
|
|
|
&device.Location,
|
|
|
|
&device.CreationDate,
|
|
|
|
&device.UpdateDate,
|
|
|
|
)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
return nil
|
2021-03-21 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddMeasuredValues into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
splittedMeasuredValues := make(map[types.MeasuredValueType][]*types.MeasuredValue, 0)
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
if _, ok := splittedMeasuredValues[measuredValue.ValueType]; !ok {
|
|
|
|
splittedMeasuredValues[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
|
|
|
|
}
|
|
|
|
splittedMeasuredValues[measuredValue.ValueType] = append(splittedMeasuredValues[measuredValue.ValueType], measuredValue)
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for measuredValueType, measuredValues := range splittedMeasuredValues {
|
2021-04-05 21:22:04 +00:00
|
|
|
var query string
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
switch measuredValueType {
|
|
|
|
case types.Humidity:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertHumiditySQL
|
2021-03-21 17:47:14 +00:00
|
|
|
case types.Pressure:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertPressureSQL
|
2021-03-21 17:47:14 +00:00
|
|
|
case types.Temperature:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertTemperatureSQL
|
2021-03-21 17:47:14 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err := d.insertMeasuredValues(tx, query, measuredValues...)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) insertMeasuredValues(tx *sql.Tx, query string, measuredValues ...*types.MeasuredValue) error {
|
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-04-09 14:29:09 +00:00
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
_, err := stmt.Exec(
|
|
|
|
&measuredValue.ID,
|
|
|
|
&measuredValue.Value,
|
|
|
|
&measuredValue.Date,
|
|
|
|
&measuredValue.SensorID,
|
|
|
|
&measuredValue.CreationDate,
|
|
|
|
&measuredValue.UpdateDate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// AddOrUpdateMeasuredValues into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddOrUpdateMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
splittedMeasuredValues := make(map[types.MeasuredValueType][]*types.MeasuredValue, 0)
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
if _, ok := splittedMeasuredValues[measuredValue.ValueType]; !ok {
|
|
|
|
splittedMeasuredValues[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
|
|
|
|
}
|
|
|
|
splittedMeasuredValues[measuredValue.ValueType] = append(splittedMeasuredValues[measuredValue.ValueType], measuredValue)
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for measuredValueType, measuredValues := range splittedMeasuredValues {
|
2021-04-09 14:29:09 +00:00
|
|
|
var query string
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
switch measuredValueType {
|
|
|
|
case types.Humidity:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertOrUpdateHumiditySQL
|
2021-03-21 17:47:14 +00:00
|
|
|
case types.Pressure:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertOrUpdatePressureSQL
|
2021-03-21 17:47:14 +00:00
|
|
|
case types.Temperature:
|
2021-04-09 14:29:09 +00:00
|
|
|
query = sqlite3.InsertOrUpdateTemperatureSQL
|
2021-03-21 17:47:14 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err := d.insertOrUpdateMeasuredValues(tx, query, measuredValues...)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) insertOrUpdateMeasuredValues(tx *sql.Tx, query string, measuredValues ...*types.MeasuredValue) error {
|
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-04-09 14:29:09 +00:00
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
_, err := stmt.Exec(
|
|
|
|
&measuredValue.ID,
|
|
|
|
&measuredValue.Value,
|
|
|
|
&measuredValue.Date,
|
|
|
|
&measuredValue.SensorID,
|
|
|
|
&measuredValue.CreationDate,
|
|
|
|
&measuredValue.UpdateDate,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// AddSensors into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddSensors(ctx context.Context, sensors ...*types.Sensor) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.InsertSensorSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
&sensor.ID,
|
|
|
|
&sensor.Name,
|
|
|
|
&sensor.Location,
|
|
|
|
&sensor.WireID,
|
|
|
|
&sensor.I2CBus,
|
|
|
|
&sensor.I2CAddress,
|
|
|
|
&sensor.GPIONumber,
|
|
|
|
&sensor.Model,
|
|
|
|
&sensor.Enabled,
|
|
|
|
&sensor.TickDuration,
|
|
|
|
&sensor.DeviceID,
|
|
|
|
&sensor.CreationDate,
|
|
|
|
&sensor.UpdateDate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// AddOrUpdateSensors into the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err = d.insertOrUpdateSensors(tx, sensors...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SQLite) insertOrUpdateSensors(tx *sql.Tx, sensors ...*types.Sensor) error {
|
|
|
|
stmt, err := tx.Prepare(sqlite3.InsertOrUpdateSensorSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
&sensor.ID,
|
|
|
|
&sensor.Name,
|
|
|
|
&sensor.Location,
|
|
|
|
&sensor.WireID,
|
|
|
|
&sensor.I2CBus,
|
|
|
|
&sensor.I2CAddress,
|
|
|
|
&sensor.GPIONumber,
|
|
|
|
&sensor.Model,
|
|
|
|
&sensor.Enabled,
|
|
|
|
&sensor.TickDuration,
|
|
|
|
&sensor.DeviceID,
|
|
|
|
&sensor.CreationDate,
|
|
|
|
&sensor.UpdateDate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute statement: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
return nil
|
2021-03-21 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// Close closes the database and prevents new queries from starting. Close then
|
|
|
|
// waits for all queries that have started processing on the server to finish.
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) Close() error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
return d.dbo.Close()
|
2021-03-21 21:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// Migrate creates all required tables if not exist
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) Migrate(ctx context.Context) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
sourceDriver, err := iofs.New(sqlite3.DDLAssets, sqlite3.DDLAssetPath)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
m, err := migrate.NewWithSourceInstance("iofs", sourceDriver, d.databaseURL.String())
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.Up()
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, migrate.ErrNoChange):
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevice from database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetDeviceByID(ctx context.Context, id string) (*types.Device, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
devices, err := d.selectDevices(tx, sqlite3.SelectDeviceByIDSQL, id)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devices) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevice from database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetDeviceByName(ctx context.Context, name string) (*types.Device, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
devices, err := d.selectDevices(tx, sqlite3.SelectDeviceByNameSQL, name)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devices) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevices from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetDevices(ctx context.Context) ([]*types.Device, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
devices, err := d.selectDevices(tx, sqlite3.SelectDevicesSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) selectDevices(tx *sql.Tx, query string, args ...interface{}) ([]*types.Device, error) {
|
2021-03-21 17:47:14 +00:00
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
rows, err := stmt.Query(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to query statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = rows.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
devices := make([]*types.Device, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
device := new(types.Device)
|
|
|
|
err = rows.Scan(
|
|
|
|
&device.ID,
|
|
|
|
&device.Name,
|
|
|
|
&device.Location,
|
|
|
|
&device.CreationDate,
|
|
|
|
&device.UpdateDate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to scan row: %v", err)
|
|
|
|
}
|
|
|
|
devices = append(devices, device)
|
|
|
|
}
|
|
|
|
|
2021-05-16 21:02:46 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan rows: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHumidity returns humidity from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectHumidityByIDSQL, id)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if measuredValues == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Humidity
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHumidities returns humidities from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectHumiditiesSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Humidity
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...interface{}) ([]*types.MeasuredValue, error) {
|
2021-03-21 17:47:14 +00:00
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
rows, err := stmt.Query(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = rows.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
measuredValues := make([]*types.MeasuredValue, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
measuredValue := new(types.MeasuredValue)
|
2021-05-16 21:02:46 +00:00
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&measuredValue.ID,
|
|
|
|
&measuredValue.Value,
|
|
|
|
&measuredValue.Date,
|
|
|
|
&measuredValue.SensorID,
|
|
|
|
&measuredValue.CreationDate,
|
|
|
|
&measuredValue.UpdateDate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
measuredValues = append(measuredValues, measuredValue)
|
|
|
|
}
|
|
|
|
|
2021-05-16 21:02:46 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan rows: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
return measuredValues, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPressure returns pressure from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectPressureByIDSQL, id)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if measuredValues == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Pressure
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPressures returns pressure from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetPressures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectPressuresSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Pressure
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensor from database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetSensorByID(ctx context.Context, id string) (*types.Sensor, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorByIDSQL, id)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sensors) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensors[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensors from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorsSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensorsByModels from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
|
|
for i := range deviceIDs {
|
2021-04-09 14:29:09 +00:00
|
|
|
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorsByDeviceIDSQL, deviceIDs[i])
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedSensors = append(cachedSensors, sensors...)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedSensors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensorsByModels from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
|
|
for i := range sensorModels {
|
2021-04-09 14:29:09 +00:00
|
|
|
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorsByModelSQL, sensorModels[i])
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedSensors = append(cachedSensors, sensors...)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedSensors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensorsByModels from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetSensorsByNames(ctx context.Context, sensorNames ...string) ([]*types.Sensor, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
cachedSensors := make([]*types.Sensor, 0)
|
|
|
|
for i := range sensorNames {
|
2021-04-09 14:29:09 +00:00
|
|
|
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorsByNameSQL, sensorNames[i])
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedSensors = append(cachedSensors, sensors...)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedSensors, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) selectSensors(tx *sql.Tx, query string, args ...interface{}) ([]*types.Sensor, error) {
|
2021-03-21 17:47:14 +00:00
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
rows, err := stmt.Query(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to query statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = rows.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
sensors := make([]*types.Sensor, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
sensor := new(types.Sensor)
|
|
|
|
err = rows.Scan(
|
|
|
|
&sensor.ID,
|
|
|
|
&sensor.Name,
|
|
|
|
&sensor.Location,
|
|
|
|
&sensor.WireID,
|
|
|
|
&sensor.I2CBus,
|
|
|
|
&sensor.I2CAddress,
|
|
|
|
&sensor.GPIONumber,
|
|
|
|
&sensor.Model,
|
|
|
|
&sensor.Enabled,
|
|
|
|
&sensor.TickDuration,
|
|
|
|
&sensor.DeviceID,
|
|
|
|
&sensor.CreationDate,
|
|
|
|
&sensor.UpdateDate,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to scan row: %v", err)
|
|
|
|
}
|
|
|
|
sensors = append(sensors, sensor)
|
|
|
|
}
|
|
|
|
|
2021-05-16 21:02:46 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan rows: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
return sensors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperature returns temperatures from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectTemperatureByIDSQL, id)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if measuredValues == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemperatures returns temperatures from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectTemperaturesSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measuredValue := range measuredValues {
|
|
|
|
measuredValue.ValueType = types.Temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
// Import imports devices, sensors and all measured values from a source
|
|
|
|
// repository. Existing entries will be updated.
|
|
|
|
func (d *SQLite) Import(ctx context.Context, src Repository) error {
|
2021-04-09 14:55:35 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-04-09 14:29:09 +00:00
|
|
|
|
|
|
|
devices, err := src.GetDevices(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.insertOrUpdateDevices(tx, devices...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sensors, err := src.GetSensors(ctx)
|
2021-04-09 14:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
err = d.insertOrUpdateSensors(tx, sensors...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range []struct {
|
|
|
|
f func(ctx context.Context) ([]*types.MeasuredValue, error)
|
|
|
|
query string
|
|
|
|
}{
|
|
|
|
{f: src.GetHumidities, query: postgres.InsertOrUpdateHumiditySQL},
|
|
|
|
{f: src.GetPressures, query: postgres.InsertOrUpdatePressureSQL},
|
|
|
|
{f: src.GetTemperatures, query: postgres.InsertOrUpdateTemperatureSQL},
|
|
|
|
} {
|
|
|
|
measuredValues, err := value.f(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.insertOrUpdateMeasuredValues(tx, value.query, measuredValues...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:30:50 +00:00
|
|
|
// RemoveDevices from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.DeleteDeviceByIDSQL)
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
|
|
|
for _, deviceID := range deviceIDs {
|
|
|
|
_, err = stmt.Exec(deviceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDevices from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) RemoveDevicesByNames(ctx context.Context, names ...string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.DeleteDeviceByNameSQL)
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
|
|
|
for _, deviceID := range names {
|
|
|
|
_, err = stmt.Exec(deviceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSensors from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.DeleteSensorByIDSQL)
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
|
|
|
for _, sensorID := range sensorIDs {
|
|
|
|
_, err = stmt.Exec(sensorID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSensors from the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) RemoveSensorsByNames(ctx context.Context, names ...string) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.DeleteSensorByNameSQL)
|
2021-03-21 21:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to prepare statement: %v", err)
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 21:30:50 +00:00
|
|
|
|
|
|
|
for _, sensorID := range names {
|
|
|
|
_, err = stmt.Exec(sensorID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:47:14 +00:00
|
|
|
// UpdateDevices updates a device in the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) UpdateDevices(ctx context.Context, devices ...*types.Device) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.UpdateDeviceSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for _, device := range devices {
|
|
|
|
_, err := stmt.Exec(
|
|
|
|
&device.Name,
|
|
|
|
&device.Location,
|
|
|
|
&device.CreationDate,
|
|
|
|
&device.UpdateDate,
|
|
|
|
&device.ID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSensors updates a sensor in the database
|
2021-04-09 14:29:09 +00:00
|
|
|
func (d *SQLite) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
tx, err := d.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
2021-04-09 14:29:09 +00:00
|
|
|
stmt, err := tx.Prepare(sqlite3.UpdateSensorSQL)
|
2021-03-21 17:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 21:02:46 +00:00
|
|
|
defer func() { _ = stmt.Close() }()
|
2021-03-21 17:47:14 +00:00
|
|
|
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
_, err := stmt.Exec(
|
|
|
|
&sensor.Name,
|
|
|
|
&sensor.Location,
|
|
|
|
&sensor.WireID,
|
|
|
|
&sensor.I2CBus,
|
|
|
|
&sensor.I2CAddress,
|
|
|
|
&sensor.GPIONumber,
|
|
|
|
&sensor.Model,
|
|
|
|
&sensor.Enabled,
|
|
|
|
&sensor.TickDuration,
|
|
|
|
&sensor.DeviceID,
|
|
|
|
&sensor.CreationDate,
|
|
|
|
&sensor.UpdateDate,
|
|
|
|
&sensor.ID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|