fix: sensor typ to static sensor model

This commit is contained in:
Markus Pesch 2019-02-28 19:48:25 +01:00
parent 1a3a31c5f2
commit 14ce354b32
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
11 changed files with 129 additions and 99 deletions

View File

@ -32,7 +32,7 @@ var rootCmd = &cobra.Command{
fc := config.FluckyConfig{ fc := config.FluckyConfig{
Device: &types.Device{ Device: &types.Device{
DeviceID: uuid.NewV4().String(), DeviceID: uuid.NewV4().String(),
DeviceName: &hostname, DeviceName: hostname,
CreationDate: time.Now(), CreationDate: time.Now(),
}, },
} }

View File

@ -25,10 +25,16 @@ var addSensorCmd = &cobra.Command{
log.Fatalln(err) log.Fatalln(err)
} }
// determine sensor model
sensorModel, err := types.SelectSensorModel(args[1])
if err != nil {
log.Fatalln(err)
}
// create new sensor struct // create new sensor struct
sensor := &types.Sensor{ sensor := &types.Sensor{
SensorName: args[0], SensorName: args[0],
SensorType: args[1], SensorModel: sensorModel,
SensorLocation: location, SensorLocation: location,
SensorEnabled: enabled, SensorEnabled: enabled,
GPIONumber: &args[2], GPIONumber: &args[2],

View File

@ -23,15 +23,20 @@ var readTemperatureCmd = &cobra.Command{
log.Fatalln(err) log.Fatalln(err)
} }
// FIXME: // fetch all temperature sensors
// add sensor entry to list temperatureSensors, err := fc.GetTemperatureSensors()
temperatures, err := sensor.ReadTemperatures(fc.GetTemperatureSensors())
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
cli.PrintTemperatures(temperatures, fc, os.Stdout) // read temperature from sensors
temperatures, err := sensor.ReadTemperatures(temperatureSensors)
if err != nil {
log.Fatalln(err)
}
// print temperatures on stdout
cli.PrintTemperatures(temperatures, fc, os.Stdout)
}, },
} }

View File

@ -45,10 +45,12 @@ func PrintTemperatures(temperatures []*types.Temperature, cnf *config.FluckyConf
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
// headlines // headlines
for _, sensor := range sensors { for i, sensor := range sensors {
fmt.Fprintf(tw, "%v\t", sensor.Name()) fmt.Fprintf(tw, "%v\t", sensor.Name())
} if i == len(sensors)-1 {
fmt.Fprintf(tw, "\n") fmt.Fprintf(tw, "\n")
}
}
// find sensor with maximum temperature values // find sensor with maximum temperature values
maxLength := 0 maxLength := 0

View File

@ -207,41 +207,48 @@ func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error {
func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor { func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor {
hs := []sensor.HumiditySensor{} hs := []sensor.HumiditySensor{}
// for _, s := range fc.Sensors { for _, s := range fc.Sensors {
// switch s.SensorType { switch s.SensorModel {
// case "DHT11": case types.DHT11:
// hs = append(hs, sensor.DHT11Sensor{ hs = append(hs, &sensor.DHT11Sensor{
// Sensor: s, Sensor: s,
// }) })
// case "DHT22": case types.DHT22:
// hs = append(hs, sensor.DHT22Sensor{ hs = append(hs, &sensor.DHT22Sensor{
// Sensor: s, Sensor: s,
// }) })
// } }
// } }
return hs return hs
} }
func (fc *FluckyConfig) GetTemperatureSensors() []sensor.TemperatureSensor { func (fc *FluckyConfig) GetTemperatureSensors() ([]sensor.TemperatureSensor, error) {
ts := []sensor.TemperatureSensor{} ts := []sensor.TemperatureSensor{}
for _, s := range fc.Sensors { for _, s := range fc.Sensors {
switch s.SensorType { // skip disabled sensors
case "DHT11": if !s.SensorEnabled {
continue
}
switch s.SensorModel {
case types.DHT11:
ts = append(ts, &sensor.DHT11Sensor{ ts = append(ts, &sensor.DHT11Sensor{
Sensor: s, Sensor: s,
}) })
case "DHT22": case types.DHT22:
ts = append(ts, &sensor.DHT22Sensor{ ts = append(ts, &sensor.DHT22Sensor{
Sensor: s, Sensor: s,
}) })
case "DS18B20": case types.DS18B20:
ts = append(ts, &sensor.DS18B20{ ts = append(ts, &sensor.DS18B20{
Sensor: s, Sensor: s,
}) })
default:
return nil, fmt.Errorf("Sensor Model %v is not a valid sensor model. Please remove the sensor named %v", s.SensorModel, s.Name())
} }
} }
return ts return ts, nil
} }
// JSONDecoder decode a JSON string from a reader into a struct // JSONDecoder decode a JSON string from a reader into a struct
@ -289,7 +296,7 @@ func (fc *FluckyConfig) PrintSensors(w io.Writer) error {
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n") fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n")
for _, sensor := range fc.Sensors { for _, sensor := range fc.Sensors {
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorType, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled) fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled)
} }
tw.Flush() tw.Flush()
@ -299,31 +306,22 @@ func (fc *FluckyConfig) PrintSensors(w io.Writer) error {
// RemoveSensor deletes a sensor by its name or its unique UUID // RemoveSensor deletes a sensor by its name or its unique UUID
func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error { func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error {
found := false
for i, sensor := range fc.Sensors { for i, sensor := range fc.Sensors {
// remove machted name // remove machted name
if !validUUID.MatchString(nameOrUUID) && if !validUUID.MatchString(nameOrUUID) &&
sensor.SensorName == nameOrUUID { sensor.SensorName == nameOrUUID {
fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...) fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...)
found = true return nil
} }
// remove machted uuid // remove machted uuid
if validUUID.MatchString(nameOrUUID) && if validUUID.MatchString(nameOrUUID) &&
sensor.SensorID == nameOrUUID { sensor.SensorID == nameOrUUID {
fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...) fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...)
found = true
}
}
if !found {
return fmt.Errorf("Can not find sensor %v", nameOrUUID)
}
return nil return nil
} }
}
return fmt.Errorf("Can not find sensor %v", nameOrUUID)
}
// RemoveRemote deletes a remote address by its name or its unique UUID // RemoveRemote deletes a remote address by its name or its unique UUID
func (fc *FluckyConfig) RemoveRemote(nameOrUUID string) error { func (fc *FluckyConfig) RemoveRemote(nameOrUUID string) error {

View File

@ -50,13 +50,10 @@ func ReadHumidities(humiditySensors []HumiditySensor) ([]*types.Humidity, error)
} }
func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) { func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(len(temperatureSensors)) wg.Add(len(temperatureSensors))
errChannel := make(chan error, len(temperatureSensors)) errChannel := make(chan error, len(temperatureSensors))
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors)) temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors { for _, temperatureSensor := range temperatureSensors {
go func(ts TemperatureSensor) { go func(ts TemperatureSensor) {
defer wg.Done() defer wg.Done()
@ -67,17 +64,13 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperat
temperatureChannel <- temperature temperatureChannel <- temperature
}(temperatureSensor) }(temperatureSensor)
} }
wg.Wait() wg.Wait()
errorList := errutils.CollectErrors(errChannel) errorList := errutils.CollectErrors(errChannel)
if err := errutils.FormatErrors(errorList); err != nil { if err := errutils.FormatErrors(errorList); err != nil {
return nil, err return nil, err
} }
temperatureList := collectTemperatures(temperatureChannel) temperatureList := collectTemperatures(temperatureChannel)
return temperatureList, nil return temperatureList, nil
} }
func collectHumidities(humChan <-chan *types.Humidity) []*types.Humidity { func collectHumidities(humChan <-chan *types.Humidity) []*types.Humidity {

12
pkg/types/device.go Normal file
View File

@ -0,0 +1,12 @@
package types
import "time"
// Device ...
type Device struct {
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceLocation *string `json:"device_location"`
DeviceLastContact time.Time `json:"device_last_contact"`
CreationDate time.Time `json:"creation_date"`
}

12
pkg/types/humidity.go Normal file
View File

@ -0,0 +1,12 @@
package types
import "time"
// Humidity ...
type Humidity struct {
HumidityID string `json:"humidity_id"`
HumidityValue float64 `json:"humidity_value,string"`
HumidityDate time.Time `json:"humidity_date"`
SensorID string `json:"sensor_id"`
CreationDate time.Time `json:"creation_date"`
}

View File

@ -1,6 +1,7 @@
package types package types
import ( import (
"fmt"
"time" "time"
) )
@ -11,7 +12,7 @@ type Sensor struct {
SensorLocation string `json:"sensor_location"` SensorLocation string `json:"sensor_location"`
WireID *string `json:"wire_id"` WireID *string `json:"wire_id"`
GPIONumber *string `json:"gpio_number"` GPIONumber *string `json:"gpio_number"`
SensorType string `json:"sensor_type"` SensorModel SensorModel `json:"sensor_model"`
SensorEnabled bool `json:"sensor_enabled"` SensorEnabled bool `json:"sensor_enabled"`
SensorLastContact time.Time `json:"sensor_last_contact"` SensorLastContact time.Time `json:"sensor_last_contact"`
DeviceID string `json:"device_id"` DeviceID string `json:"device_id"`
@ -24,6 +25,27 @@ func (s *Sensor) Name() string {
} else if *s.WireID != "" { } else if *s.WireID != "" {
return *s.WireID return *s.WireID
} }
return s.SensorID return s.SensorID
} }
type SensorModel string
const (
DHT11 SensorModel = "DHT11"
DHT22 = "DHT22"
DS18B20 = "DS18B20"
)
// SelectSensorModel converts a string into a constant
func SelectSensorModel(model string) (SensorModel, error) {
switch model {
case "DHT11":
return DHT11, nil
case "DHT22":
return DHT22, nil
case "DS18B20":
return DS18B20, nil
default:
return "", fmt.Errorf("Sensor Model %v currently not supported", model)
}
}

12
pkg/types/temperature.go Normal file
View File

@ -0,0 +1,12 @@
package types
import "time"
// Temperature ...
type Temperature struct {
TemperatureID string `json:"temperature_id"`
TemperatureValue float64 `json:"temperature_value,string"`
TemperatureDate time.Time `json:"temperature_date"`
SensorID string `json:"sensor_id"`
CreationDate time.Time `json:"creation_date"`
}

View File

@ -1,32 +0,0 @@
package types
import (
"time"
)
// Device ...
type Device struct {
DeviceID string `json:"device_id"`
DeviceName *string `json:"device_name"`
DeviceLocation *string `json:"device_location"`
DeviceLastContact time.Time `json:"device_last_contact"`
CreationDate time.Time `json:"creation_date"`
}
// Humidity ...
type Humidity struct {
HumidityID string `json:"humidity_id"`
HumidityValue float64 `json:"humidity_value,string"`
HumidityDate time.Time `json:"humidity_date"`
SensorID string `json:"sensor_id"`
CreationDate time.Time `json:"creation_date"`
}
// Temperature ...
type Temperature struct {
TemperatureID string `json:"temperature_id"`
TemperatureValue float64 `json:"temperature_value,string"`
TemperatureDate time.Time `json:"temperature_date"`
SensorID string `json:"sensor_id"`
CreationDate time.Time `json:"creation_date"`
}