refactor: config and remote pkg
This commit is contained in:
@ -2,33 +2,12 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
)
|
||||
|
||||
var validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
|
||||
|
||||
type Config interface {
|
||||
AddSensor(sensor *types.Sensor) error
|
||||
AddRemote(remote *Remote) error
|
||||
DisableRemote(nameOrUUID string) error
|
||||
DisableSensor(nameOrUUID string) error
|
||||
EnableRemote(nameOrUUID string) error
|
||||
EnableSensor(nameOrUUID string) error
|
||||
GetDevice() *Device
|
||||
GetRemotes() []*Remote
|
||||
GetSensors() []*types.Sensor
|
||||
JSONDecoder(r io.Reader) error
|
||||
JSONWriter(w io.Writer) error
|
||||
RemoveSensor(nameOrUUID string) error
|
||||
RemoveRemote(nameOrUUID string) error
|
||||
SetDevice(device *Device)
|
||||
ToJSON() (string, error)
|
||||
}
|
||||
|
||||
// Read the configuration file
|
||||
func Read(configFile string) (*FluckyConfig, error) {
|
||||
|
||||
@ -50,7 +29,7 @@ func Read(configFile string) (*FluckyConfig, error) {
|
||||
}
|
||||
|
||||
// Write the configuration into a file, specified by the configuration filepath
|
||||
func Write(cfg Config, configFile string) error {
|
||||
func Write(cfg *FluckyConfig, configFile string) error {
|
||||
|
||||
f, err := os.Create(configFile)
|
||||
if err != nil {
|
||||
|
@ -1,7 +0,0 @@
|
||||
package config
|
||||
|
||||
type Device struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
DeviceName string `json:"device_name"`
|
||||
DeviceLocation string `json:"device_location"`
|
||||
}
|
@ -14,7 +14,7 @@ import (
|
||||
|
||||
// FluckyConfig dasd
|
||||
type FluckyConfig struct {
|
||||
Device *Device `json:"device"`
|
||||
Device *types.Device `json:"device"`
|
||||
Sensors []*types.Sensor `json:"sensors"`
|
||||
Remotes []*Remote `json:"remotes"`
|
||||
}
|
||||
@ -34,7 +34,12 @@ func (fc *FluckyConfig) AddSensor(sensor *types.Sensor) error {
|
||||
}
|
||||
|
||||
if s.SensorID == sensor.SensorID {
|
||||
return fmt.Errorf("Remote %v with UUID %v already exists", s.SensorName, s.SensorID)
|
||||
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
|
||||
}
|
||||
|
||||
if sensor.WireID != nil &&
|
||||
s.WireID == sensor.WireID {
|
||||
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", s.WireID, s.SensorName)
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +96,7 @@ func (fc *FluckyConfig) DisableRemote(nameOrUUID string) error {
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", nameOrUUID)
|
||||
return fmt.Errorf("Can not found remote name %v", nameOrUUID)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -187,21 +192,6 @@ func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDevice returns device informations
|
||||
func (fc *FluckyConfig) GetDevice() *Device {
|
||||
return fc.Device
|
||||
}
|
||||
|
||||
// GetRemotes returns an array if remote struct poniter
|
||||
func (fc *FluckyConfig) GetRemotes() []*Remote {
|
||||
return fc.Remotes
|
||||
}
|
||||
|
||||
// GetSensors returns an array if remote struct poniter
|
||||
func (fc *FluckyConfig) GetSensors() []*types.Sensor {
|
||||
return fc.Sensors
|
||||
}
|
||||
|
||||
// JSONDecoder decode a JSON string from a reader into a struct
|
||||
func (fc *FluckyConfig) JSONDecoder(r io.Reader) error {
|
||||
jsonDecoder := json.NewDecoder(r)
|
||||
@ -222,6 +212,7 @@ func (fc *FluckyConfig) JSONWriter(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintRemotes displays a list with all configured remote addresses
|
||||
func (fc *FluckyConfig) PrintRemotes(w io.Writer) error {
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
@ -237,6 +228,23 @@ func (fc *FluckyConfig) PrintRemotes(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintSensors displays a list with all configured sensors
|
||||
func (fc *FluckyConfig) PrintSensors(w io.Writer) error {
|
||||
|
||||
// declar tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
fmt.Fprint(tw, "id\tname\tlocation\ttype\twire-id\tgpio\tenabled\n")
|
||||
|
||||
for _, sensor := range fc.Sensors {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorID, *sensor.SensorName, *sensor.SensorLocation, *sensor.SensorType, *sensor.WireID, *sensor.GPIONumber, *sensor.SensorEnabled)
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSensor deletes a sensor by its name or its unique UUID
|
||||
func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error {
|
||||
found := false
|
||||
@ -295,11 +303,6 @@ func (fc *FluckyConfig) RemoveRemote(nameOrUUID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDevice informations, like device name or device location
|
||||
func (fc *FluckyConfig) SetDevice(device *Device) {
|
||||
fc.Device = device
|
||||
}
|
||||
|
||||
// ToJSON returns the struct as JSON string
|
||||
func (fc *FluckyConfig) ToJSON() (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
@ -1,5 +1,7 @@
|
||||
package config
|
||||
|
||||
import "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
|
||||
// Remote ...
|
||||
type Remote struct {
|
||||
RemoteID string `json:"remote_id"`
|
||||
@ -8,3 +10,19 @@ type Remote struct {
|
||||
Registered bool `json:"remote_registered"`
|
||||
Enabled bool `json:"remote_enabled"`
|
||||
}
|
||||
|
||||
func (r *Remote) AddSensor(s *types.Sensor) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Remote) RegisterDevice(d *types.Device) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Remote) RemoveSensor(s *types.Sensor) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Remote) UnregisterDevice(d *types.Device) error {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user