refactor: config and remote pkg

This commit is contained in:
2019-02-22 13:08:58 +01:00
parent c437127531
commit d6f41b8105
29 changed files with 247 additions and 1292 deletions

16
pkg/sensor/dht11.go Normal file
View File

@ -0,0 +1,16 @@
package sensor
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
)
type DHT11Sensor struct {
*types.Sensor
}
func (s *DHT11Sensor) Read() (interface{}, error) {
log.Println("DHT11 Read Method not yet implemented")
return nil, nil
}

16
pkg/sensor/dht22.go Normal file
View File

@ -0,0 +1,16 @@
package sensor
import (
"log"
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
)
type DHT22Sensor struct {
*types.Sensor
}
func (s *DHT22Sensor) Read() (interface{}, error) {
log.Println("DHT22 Read Method not yet implemented")
return nil, nil
}

32
pkg/sensor/ds18b20.go Normal file
View File

@ -0,0 +1,32 @@
package sensor
import (
"fmt"
"time"
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
uuid "github.com/satori/go.uuid"
"github.com/yryz/ds18b20"
)
type DS18B20 struct {
*types.Sensor
}
func (s *DS18B20) Read() (interface{}, error) {
t, err := ds18b20.Temperature(*s.WireID)
if err != nil {
return nil, fmt.Errorf("Can not read from Sensor %v (UUID: %v, Wire-ID: %v): %v", s.SensorName, s.SensorID, s.WireID, err)
}
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: t,
TemperatureDate: time.Now(),
SensorID: s.SensorID,
}
return temperature, nil
}

View File

@ -1,216 +1,7 @@
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
type Sensor interface {
Read() (interface{}, error)
}
// 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
}
// func ReadTemperatures