fix: remove obsolete attributes from config.json
changes: - removed unused attributes from config.json
This commit is contained in:
parent
c279d288b4
commit
7cbd80c726
14
cli/root.go
14
cli/root.go
@ -11,7 +11,6 @@ import (
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"github.com/Masterminds/semver"
|
||||
|
||||
uuid "github.com/satori/go.uuid"
|
||||
@ -62,11 +61,6 @@ func preRunError(cmd *cobra.Command, args []string) error {
|
||||
// check if config file exists
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to determine the hostname: %v", err)
|
||||
}
|
||||
|
||||
// Time must be truncted for postgres. Postgres currently does not support
|
||||
// nanoseconds which is automatically include into the go time object
|
||||
postgresTimeStamp := time.Now()
|
||||
@ -85,12 +79,8 @@ func preRunError(cmd *cobra.Command, args []string) error {
|
||||
// Default configuration
|
||||
dsn := fmt.Sprintf("sqlite3://%v/sqlite.db?cache=shared&mode=memory&foreign_keys=on", defaultCacheDir)
|
||||
cnf := config.Config{
|
||||
Device: &types.Device{
|
||||
ID: uuid.NewV4().String(),
|
||||
Name: hostname,
|
||||
CreationDate: postgresTimeStamp,
|
||||
},
|
||||
DSN: dsn,
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
DSN: dsn,
|
||||
}
|
||||
|
||||
err = config.Write(&cnf, configFile)
|
||||
|
@ -164,7 +164,7 @@ func addSensor(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
sensor.DeviceID = cnf.Device.ID
|
||||
sensor.DeviceID = cnf.DeviceID
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
@ -292,7 +292,7 @@ func listSensors(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
sensors, err := repo.GetSensorsByDeviceID(cnf.Device.ID)
|
||||
sensors, err := repo.GetSensorsByDeviceID(cnf.DeviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,191 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"time"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/internal/format"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
|
||||
)
|
||||
|
||||
// Config represent the configuration
|
||||
type Config struct {
|
||||
Device *types.Device `json:"device"`
|
||||
Sensors []*types.Sensor `json:"sensors"`
|
||||
DSN string `json:"dsn"`
|
||||
DSNFallback string `json:"dsn_fallback"`
|
||||
}
|
||||
|
||||
// AddSensor add a new sensor
|
||||
func (cnf *Config) AddSensor(sensor *types.Sensor) error {
|
||||
|
||||
// Verify that a device is configured
|
||||
if cnf.Device == nil {
|
||||
return fmt.Errorf("No device configured")
|
||||
}
|
||||
|
||||
// Define a new UUID if the current UUID is invalid
|
||||
if !validUUID.MatchString(sensor.ID) {
|
||||
sensor.ID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// Verify that the sensor has a valid name
|
||||
if len(sensor.Name) <= 0 {
|
||||
return fmt.Errorf("No sensor name defined")
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, cnfSensor := range cnf.Sensors {
|
||||
if cnfSensor.Name == sensor.Name ||
|
||||
cnfSensor.ID == sensor.ID {
|
||||
return fmt.Errorf("Sensor with same name or id already exist")
|
||||
}
|
||||
|
||||
if cnfSensor.WireID != nil &&
|
||||
sensor.WireID != nil &&
|
||||
*cnfSensor.WireID == *sensor.WireID {
|
||||
return fmt.Errorf("Sensor with same wire id already exist")
|
||||
}
|
||||
}
|
||||
|
||||
// check if sensor has a valid tick time
|
||||
if _, err := time.ParseDuration(sensor.TickDuration); err != nil {
|
||||
return fmt.Errorf("Failed to parse tick duration: %v", err)
|
||||
}
|
||||
|
||||
// check if sensor has a valid device id
|
||||
if sensor.DeviceID != cnf.Device.ID {
|
||||
sensor.DeviceID = cnf.Device.ID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
sensor.CreationDate = format.FormatedTime()
|
||||
|
||||
// check if wire socket is available
|
||||
if sensor.WireID != nil {
|
||||
socketPath := filepath.Join("/sys/bus/w1/devices", *sensor.WireID, "/w1_slave")
|
||||
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Wire socket not found: %v", socketPath)
|
||||
}
|
||||
}
|
||||
|
||||
cnf.Sensors = append(cnf.Sensors, sensor)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableSensor disables a sensor by its name or its unique UUID
|
||||
func (cnf *Config) DisableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.Name == name {
|
||||
sensor.Enabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
sensor.Enabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableSensor enables a sensor by its name or its unique UUID
|
||||
func (cnf *Config) EnableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.Name == name {
|
||||
sensor.Enabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
sensor.Enabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSensorByID returns a sensor matched by his id. If no sensor has this id,
|
||||
// the function returns nil
|
||||
func (cnf *Config) GetSensorByID(id string) *types.Sensor {
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if sensor.ID == id {
|
||||
return sensor
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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) error {
|
||||
|
||||
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:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
cnf.Sensors = append(cnf.Sensors[:i], cnf.Sensors[i+1:]...)
|
||||
|
||||
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)
|
||||
DeviceID string `json:"device_id"`
|
||||
DSN string `json:"dsn"`
|
||||
}
|
||||
|
@ -1,31 +0,0 @@
|
||||
package config_test
|
||||
|
||||
// func TestAddRemoveSensor(t *testing.T) {
|
||||
// require := require.New(t)
|
||||
|
||||
// // Test: No device configured
|
||||
// cnf := new(config.Config)
|
||||
// err := cnf.AddSensor(&types.Sensor{ID: "1aa32c9a-b505-456d-868b-0403344f4cdf"})
|
||||
// require.Error(err)
|
||||
|
||||
// // wireID := "sdfsdff"
|
||||
// // i2cBus := 1
|
||||
// // i2cAddress := 78
|
||||
|
||||
// cnf.Device = &types.Device{ID: "d6176a06-2b0b-41af-a85c-913e8f61c35d"}
|
||||
// testCases := map[*types.Sensor]error{
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d"}: fmt.Errorf("No sensor name defined"),
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01"}: fmt.Errorf("Failed to parse tick duration: time: invalid duration "),
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: nil,
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: fmt.Errorf("Sensor with same name or id already exist"),
|
||||
// // {ID: "f90cfc18-f141-4cfd-a8d2-fb40082de5cc", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: fmt.Errorf("Sensor with same name or id already exist"),
|
||||
// // {ID: "860a9922-62cb-4c9b-b5af-5fa783cebe9d", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test02", TickDuration: "5s", WireID: &wireID}: fmt.Errorf("Wire socket not found: /sys/bus/w1/devices/sdfsdff/w1_slave"),
|
||||
// // {ID: "9be8989c-b2a1-4401-a82f-d6989ec226fe", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test02", TickDuration: "5s"}: nil,
|
||||
// }
|
||||
|
||||
// for sensor, expectedErr := range testCases {
|
||||
// err := cnf.AddSensor(sensor)
|
||||
// require.Equal(expectedErr, err)
|
||||
// }
|
||||
|
||||
// }
|
@ -30,17 +30,26 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
|
||||
}
|
||||
|
||||
// Add
|
||||
repoDevice, err := repo.GetDevice(cnf.Device.ID)
|
||||
repoDevice, err := repo.GetDevice(cnf.DeviceID)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case repoDevice == nil:
|
||||
err = repo.AddDevices(cnf.Device)
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repoDevice, err = repo.GetDevice(cnf.Device.ID)
|
||||
err = repo.AddDevices(&types.Device{
|
||||
ID: cnf.DeviceID,
|
||||
Name: hostname,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repoDevice, err = repo.GetDevice(cnf.DeviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user