fix: add, rename and remove sensor

changes:
- Implement function to add, rename and remove sensors
This commit is contained in:
2020-05-17 13:00:51 +02:00
parent fb8d4dd5eb
commit fb916c94ae
12 changed files with 757 additions and 85 deletions

View File

@ -0,0 +1,2 @@
DELETE FROM sensors
WHERE sensor_id = $1;

View File

@ -0,0 +1,2 @@
DELETE FROM sensors
WHERE sensor_name = $1;

View File

@ -17,6 +17,8 @@ type Storage interface {
InsertDevice(ctx context.Context, device *types.Device) error
InsertMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error
InsertSensor(ctx context.Context, sensor *types.Sensor) error
RemoveSensorByID(ctx context.Context, sensorID string) error
RemoveSensorByName(ctx context.Context, sensorName string) error
SelectDevice(ctx context.Context, id string) (*types.Device, error)
SelectSensor(ctx context.Context, id string) (*types.Sensor, error)
}
@ -154,6 +156,64 @@ func (postgres *Postgres) InsertSensor(ctx context.Context, sensor *types.Sensor
return tx.Commit()
}
// RemoveSensorByID from the database
func (postgres *Postgres) RemoveSensorByID(ctx context.Context, sensorID string) error {
asset := filepath.Join(postgresAssetPath, "removeSensorByID.sql")
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("Failed to load asset %v: %v", asset, err)
}
query := string(queryBytes)
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
_, err = stmt.Exec(sensorID)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
// RemoveSensorByName from the database
func (postgres *Postgres) RemoveSensorByName(ctx context.Context, sensorID string) error {
asset := filepath.Join(postgresAssetPath, "removeSensorByName.sql")
queryBytes, err := Asset(asset)
if err != nil {
return fmt.Errorf("Failed to load asset %v: %v", asset, err)
}
query := string(queryBytes)
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
_, err = stmt.Exec(sensorID)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
// SelectDevice from database
func (postgres *Postgres) SelectDevice(ctx context.Context, id string) (*types.Device, error) {
asset := filepath.Join(postgresAssetPath, "selectDeviceByID.sql")