fix: sync and fetch temperatures
changes: - add sync command to synchronize device sensor information with remote servers - fix fetch functions to get temperatures
This commit is contained in:
@ -12,11 +12,10 @@ import (
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
)
|
||||
|
||||
// Add ...
|
||||
func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled bool) error {
|
||||
func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled *bool) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
@ -24,17 +23,15 @@ func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wir
|
||||
return err
|
||||
}
|
||||
|
||||
s := &types.Sensor{
|
||||
Enabled: enabled,
|
||||
RemoteSensor: &stypes.Sensor{
|
||||
SensorID: uuid.NewV4().String(),
|
||||
SensorName: sensorName,
|
||||
SensorLocation: sensorLocation,
|
||||
GPIONumber: gpioNumber,
|
||||
SensorType: sensorType,
|
||||
DeviceID: cnf.DeviceID,
|
||||
CreationDate: time.Now(),
|
||||
},
|
||||
s := &stypes.Sensor{
|
||||
SensorID: uuid.NewV4().String(),
|
||||
SensorName: sensorName,
|
||||
SensorLocation: sensorLocation,
|
||||
GPIONumber: gpioNumber,
|
||||
SensorType: sensorType,
|
||||
SensorEnabled: enabled,
|
||||
DeviceID: cnf.DeviceID,
|
||||
CreationDate: time.Now(),
|
||||
}
|
||||
|
||||
// If the new sensor is a wire sensor
|
||||
@ -50,7 +47,7 @@ func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wir
|
||||
return fmt.Errorf("Sensor %v already exists", *wireID)
|
||||
}
|
||||
|
||||
s.RemoteSensor.WireID = wireID
|
||||
s.WireID = wireID
|
||||
}
|
||||
|
||||
// check if sensor is redundant
|
||||
@ -80,9 +77,9 @@ func Enable(sensorName string, configDir string) error {
|
||||
// search after duplicate remote_names
|
||||
var found bool
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
sensor.Enabled = true
|
||||
if *sensor.SensorName == sensorName ||
|
||||
sensor.SensorID == sensorName {
|
||||
*sensor.SensorEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -110,9 +107,9 @@ func Disable(sensorName string, configDir string) error {
|
||||
// search after duplicate remote_names
|
||||
var found bool
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
sensor.Enabled = false
|
||||
if *sensor.SensorName == sensorName ||
|
||||
sensor.SensorID == sensorName {
|
||||
*sensor.SensorEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -148,9 +145,9 @@ func Print(w io.Writer, configDir string, quiet bool) error {
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if quiet {
|
||||
fmt.Fprintf(tw, "%v\n", sensor.RemoteSensor.SensorID)
|
||||
fmt.Fprintf(tw, "%v\n", sensor.SensorID)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", sensor.RemoteSensor.SensorID, *sensor.RemoteSensor.SensorName, *sensor.RemoteSensor.SensorLocation, *sensor.RemoteSensor.SensorType, *sensor.RemoteSensor.WireID, *sensor.RemoteSensor.GPIONumber, sensor.Enabled)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -169,8 +166,8 @@ func Remove(sensorName, configDir string) error {
|
||||
|
||||
var j int
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
if *sensor.SensorName == sensorName ||
|
||||
sensor.SensorID == sensorName {
|
||||
cnf.Sensors = append(cnf.Sensors[:j], cnf.Sensors[j+1:]...)
|
||||
|
||||
if j > 0 {
|
||||
@ -197,9 +194,9 @@ func exists(wirePath, sensorID string) bool {
|
||||
}
|
||||
|
||||
// isWireIDRedundant returns a boolean if the sensor id redundant
|
||||
func isWireIDRedundant(wireID string, sensors []*types.Sensor) bool {
|
||||
func isWireIDRedundant(wireID string, sensors []*stypes.Sensor) bool {
|
||||
for _, sensor := range sensors {
|
||||
if *sensor.RemoteSensor.WireID == wireID {
|
||||
if *sensor.WireID == wireID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -208,9 +205,9 @@ func isWireIDRedundant(wireID string, sensors []*types.Sensor) bool {
|
||||
}
|
||||
|
||||
// isSensorNameRedundant returns a boolean if the sensor name redundant
|
||||
func isSensorNameRedundant(sensorName string, sensors []*types.Sensor) bool {
|
||||
func isSensorNameRedundant(sensorName string, sensors []*stypes.Sensor) bool {
|
||||
for _, sensor := range sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName {
|
||||
if *sensor.SensorName == sensorName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user