2018-11-20 21:55:06 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2018-11-28 17:07:20 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-11-20 21:55:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-28 17:07:20 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2018-11-21 19:48:10 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
"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"
|
2018-11-20 21:55:06 +00:00
|
|
|
)
|
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// Add ...
|
2018-11-29 15:06:39 +00:00
|
|
|
func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled *bool) error {
|
2018-11-28 17:07:20 +00:00
|
|
|
|
|
|
|
// read cnf file
|
|
|
|
cnf, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 15:06:39 +00:00
|
|
|
s := &stypes.Sensor{
|
|
|
|
SensorID: uuid.NewV4().String(),
|
|
|
|
SensorName: sensorName,
|
|
|
|
SensorLocation: sensorLocation,
|
|
|
|
GPIONumber: gpioNumber,
|
|
|
|
SensorType: sensorType,
|
|
|
|
SensorEnabled: enabled,
|
|
|
|
DeviceID: cnf.DeviceID,
|
|
|
|
CreationDate: time.Now(),
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-11-29 15:06:39 +00:00
|
|
|
s.WireID = wireID
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-11-29 15:06:39 +00:00
|
|
|
if *sensor.SensorName == sensorName ||
|
|
|
|
sensor.SensorID == sensorName {
|
|
|
|
*sensor.SensorEnabled = true
|
2018-11-28 17:07:20 +00:00
|
|
|
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 {
|
2018-11-29 15:06:39 +00:00
|
|
|
if *sensor.SensorName == sensorName ||
|
|
|
|
sensor.SensorID == sensorName {
|
|
|
|
*sensor.SensorEnabled = false
|
2018-11-28 17:07:20 +00:00
|
|
|
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 {
|
2018-11-29 15:06:39 +00:00
|
|
|
fmt.Fprintf(tw, "%v\n", sensor.SensorID)
|
2018-11-28 17:07:20 +00:00
|
|
|
} else {
|
2018-11-29 15:06:39 +00:00
|
|
|
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)
|
2018-11-28 17:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-11-29 15:06:39 +00:00
|
|
|
if *sensor.SensorName == sensorName ||
|
|
|
|
sensor.SensorID == sensorName {
|
2018-11-28 17:07:20 +00:00
|
|
|
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 {
|
2018-11-20 21:55:06 +00:00
|
|
|
sensorPath := filepath.Join(wirePath, sensorID)
|
|
|
|
if _, err := os.Stat(sensorPath); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-11-21 19:48:10 +00:00
|
|
|
|
2018-11-28 17:07:20 +00:00
|
|
|
// isWireIDRedundant returns a boolean if the sensor id redundant
|
2018-11-29 15:06:39 +00:00
|
|
|
func isWireIDRedundant(wireID string, sensors []*stypes.Sensor) bool {
|
2018-11-28 17:07:20 +00:00
|
|
|
for _, sensor := range sensors {
|
2018-11-29 15:06:39 +00:00
|
|
|
if *sensor.WireID == wireID {
|
2018-11-28 17:07:20 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSensorNameRedundant returns a boolean if the sensor name redundant
|
2018-11-29 15:06:39 +00:00
|
|
|
func isSensorNameRedundant(sensorName string, sensors []*stypes.Sensor) bool {
|
2018-11-21 19:48:10 +00:00
|
|
|
for _, sensor := range sensors {
|
2018-11-29 15:06:39 +00:00
|
|
|
if *sensor.SensorName == sensorName {
|
2018-11-21 19:48:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|