Markus Pesch
f8e829d3d2
changes: - add sync command to synchronize device sensor information with remote servers - fix fetch functions to get temperatures
217 lines
4.3 KiB
Go
217 lines
4.3 KiB
Go
package sensor
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"text/tabwriter"
|
|
"time"
|
|
|
|
"github.com/satori/go.uuid"
|
|
|
|
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
|
)
|
|
|
|
// Add ...
|
|
func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled *bool) error {
|
|
|
|
// read cnf file
|
|
cnf, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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
|
|
if wireID != nil {
|
|
|
|
// check if sensor exists
|
|
if !exists(wirePath, *wireID) {
|
|
return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, *wireID))
|
|
}
|
|
|
|
// check if sensor is redundant
|
|
if isWireIDRedundant(*wireID, cnf.Sensors) {
|
|
return fmt.Errorf("Sensor %v already exists", *wireID)
|
|
}
|
|
|
|
s.WireID = wireID
|
|
}
|
|
|
|
// check if sensor is redundant
|
|
if isSensorNameRedundant(*sensorName, cnf.Sensors) {
|
|
return fmt.Errorf("Sensor %v already exists", *sensorName)
|
|
}
|
|
|
|
// append sensor to list
|
|
cnf.Sensors = append(cnf.Sensors, s)
|
|
|
|
// write cnf file
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Enable a remote link
|
|
func Enable(sensorName string, configDir string) error {
|
|
|
|
cnf, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// search after duplicate remote_names
|
|
var found bool
|
|
for _, sensor := range cnf.Sensors {
|
|
if *sensor.SensorName == sensorName ||
|
|
sensor.SensorID == sensorName {
|
|
*sensor.SensorEnabled = true
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return fmt.Errorf("Can not find sensor %v", sensorName)
|
|
}
|
|
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Disable a remote link
|
|
func Disable(sensorName string, configDir string) error {
|
|
|
|
cnf, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// search after duplicate remote_names
|
|
var found bool
|
|
for _, sensor := range cnf.Sensors {
|
|
if *sensor.SensorName == sensorName ||
|
|
sensor.SensorID == sensorName {
|
|
*sensor.SensorEnabled = false
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return fmt.Errorf("Can not find sensor %v", sensorName)
|
|
}
|
|
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Print a list with the given writer w over all temperature sensors
|
|
func Print(w io.Writer, configDir string, quiet bool) error {
|
|
|
|
// read cnf file
|
|
cnf, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// declar tabwriter
|
|
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
|
|
|
// headline
|
|
if !quiet {
|
|
fmt.Fprint(tw, "id\tname\tlocation\ttype\twire-id\tgpio\tenabled\n")
|
|
}
|
|
|
|
for _, sensor := range cnf.Sensors {
|
|
if quiet {
|
|
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.SensorID, *sensor.SensorName, *sensor.SensorLocation, *sensor.SensorType, *sensor.WireID, *sensor.GPIONumber, *sensor.SensorEnabled)
|
|
}
|
|
}
|
|
|
|
tw.Flush()
|
|
|
|
return nil
|
|
}
|
|
|
|
// Remove a sensor
|
|
func Remove(sensorName, configDir string) error {
|
|
|
|
cnf, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var j int
|
|
for _, sensor := range cnf.Sensors {
|
|
if *sensor.SensorName == sensorName ||
|
|
sensor.SensorID == sensorName {
|
|
cnf.Sensors = append(cnf.Sensors[:j], cnf.Sensors[j+1:]...)
|
|
|
|
if j > 0 {
|
|
j = j - 1
|
|
}
|
|
continue
|
|
}
|
|
j++
|
|
}
|
|
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func exists(wirePath, sensorID string) bool {
|
|
sensorPath := filepath.Join(wirePath, sensorID)
|
|
if _, err := os.Stat(sensorPath); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// isWireIDRedundant returns a boolean if the sensor id redundant
|
|
func isWireIDRedundant(wireID string, sensors []*stypes.Sensor) bool {
|
|
for _, sensor := range sensors {
|
|
if *sensor.WireID == wireID {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// isSensorNameRedundant returns a boolean if the sensor name redundant
|
|
func isSensorNameRedundant(sensorName string, sensors []*stypes.Sensor) bool {
|
|
for _, sensor := range sensors {
|
|
if *sensor.SensorName == sensorName {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|