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

48
pkg/cli/cli.go Normal file
View File

@ -0,0 +1,48 @@
package cli
import (
"fmt"
"io"
"text/tabwriter"
"github.com/volker-raschek/flucky/pkg/config"
)
// PrintSensors displays a list with all configured sensors
func PrintSensors(cnf *config.Config, w io.Writer) error {
// declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\ttick-duration\tenabled\n")
for _, sensor := range cnf.Sensors {
fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.Name, sensor.Location, sensor.Model)
if sensor.WireID != nil {
fmt.Fprintf(tw, "%v\t", *sensor.WireID)
} else {
fmt.Fprintf(tw, "\t")
}
if sensor.I2CBus != nil {
fmt.Fprintf(tw, "%v\t", *sensor.I2CBus)
} else {
fmt.Fprintf(tw, "\t")
}
if sensor.I2CAddress != nil {
fmt.Fprintf(tw, "%#v\t", *sensor.I2CAddress)
} else {
fmt.Fprintf(tw, "\t")
}
fmt.Fprintf(tw, "%v\t", sensor.GPIONumber)
fmt.Fprintf(tw, "%v\t%v\n", sensor.TickDuration, sensor.Enabled)
}
tw.Flush()
return nil
}

View File

@ -1,6 +1,7 @@
package config
import (
"context"
"fmt"
"os"
"path/filepath"
@ -9,6 +10,7 @@ import (
"time"
"github.com/volker-raschek/flucky/pkg/internal/format"
"github.com/volker-raschek/flucky/pkg/storage"
uuid "github.com/satori/go.uuid"
"github.com/volker-raschek/flucky/pkg/types"
@ -154,21 +156,56 @@ func (cnf *Config) GetSensorByID(id string) *types.Sensor {
return nil
}
// RemoveSensor deletes a sensor by its name or its unique UUID
func (cnf *Config) RemoveSensor(name string) error {
// RemoveSensor deletes a sensor by its name or its unique UUID, If definitive
// is set to true, the sensor will not only be removed in the configuration file
// but also in the backend.
func (cnf *Config) RemoveSensor(name string, definitive bool) error {
backend, err := storage.New(cnf.StorageEndpoint, nil)
if err != nil {
return err
}
for i, sensor := range cnf.Sensors {
// remove machted name
if !validUUID.MatchString(name) &&
sensor.Name == name {
cnf.Sensors = append(cnf.Sensors[:i], cnf.Sensors[i+1:]...)
if definitive {
err = backend.RemoveSensorByName(context.Background(), name)
if err != nil {
return err
}
}
return nil
}
// remove machted uuid
if validUUID.MatchString(name) &&
sensor.ID == name {
cnf.Sensors = append(cnf.Sensors[:i], cnf.Sensors[i+1:]...)
if definitive {
err = backend.RemoveSensorByID(context.Background(), name)
if err != nil {
return err
}
}
return nil
}
}
return fmt.Errorf("Can not find sensor %v", name)
}
// RenameSensor renamed a sensor
func (cnf *Config) RenameSensor(oldName string, newName string) error {
for _, cnfSensor := range cnf.Sensors {
if cnfSensor.Name == oldName {
cnfSensor.Name = newName
return nil
}
}
return fmt.Errorf("No sensor %v found", oldName)
}

View File

@ -45,7 +45,7 @@ func Read(configFile string) (*Config, error) {
func Write(cnf *Config, configFile string) error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
configDir := filepath.Dir(configFile)
err := os.MkdirAll(configDir, os.ModeDir)
err := os.MkdirAll(configDir, 0775)
if err != nil {
return fmt.Errorf("Failed to create config directory %v: %v", configDir, err)
}

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")