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{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: &hostname,
DeviceName: hostname,
CreationDate: time.Now(),
},
}

View File

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

View File

@ -23,15 +23,20 @@ var readTemperatureCmd = &cobra.Command{
log.Fatalln(err)
}
// FIXME:
// add sensor entry to list
temperatures, err := sensor.ReadTemperatures(fc.GetTemperatureSensors())
// fetch all temperature sensors
temperatureSensors, err := fc.GetTemperatureSensors()
if err != nil {
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)
// headlines
for _, sensor := range sensors {
for i, sensor := range sensors {
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
maxLength := 0

View File

@ -207,41 +207,48 @@ func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error {
func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor {
hs := []sensor.HumiditySensor{}
// for _, s := range fc.Sensors {
// switch s.SensorType {
// case "DHT11":
// hs = append(hs, sensor.DHT11Sensor{
// Sensor: s,
// })
// case "DHT22":
// hs = append(hs, sensor.DHT22Sensor{
// Sensor: s,
// })
// }
// }
return hs
}
func (fc *FluckyConfig) GetTemperatureSensors() []sensor.TemperatureSensor {
ts := []sensor.TemperatureSensor{}
for _, s := range fc.Sensors {
switch s.SensorType {
case "DHT11":
ts = append(ts, &sensor.DHT11Sensor{
switch s.SensorModel {
case types.DHT11:
hs = append(hs, &sensor.DHT11Sensor{
Sensor: s,
})
case "DHT22":
ts = append(ts, &sensor.DHT22Sensor{
Sensor: s,
})
case "DS18B20":
ts = append(ts, &sensor.DS18B20{
case types.DHT22:
hs = append(hs, &sensor.DHT22Sensor{
Sensor: s,
})
}
}
return ts
return hs
}
func (fc *FluckyConfig) GetTemperatureSensors() ([]sensor.TemperatureSensor, error) {
ts := []sensor.TemperatureSensor{}
for _, s := range fc.Sensors {
// skip disabled sensors
if !s.SensorEnabled {
continue
}
switch s.SensorModel {
case types.DHT11:
ts = append(ts, &sensor.DHT11Sensor{
Sensor: s,
})
case types.DHT22:
ts = append(ts, &sensor.DHT22Sensor{
Sensor: s,
})
case types.DS18B20:
ts = append(ts, &sensor.DS18B20{
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, nil
}
// 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")
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()
@ -299,30 +306,21 @@ func (fc *FluckyConfig) PrintSensors(w io.Writer) error {
// RemoveSensor deletes a sensor by its name or its unique UUID
func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error {
found := false
for i, sensor := range fc.Sensors {
// remove machted name
if !validUUID.MatchString(nameOrUUID) &&
sensor.SensorName == nameOrUUID {
fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...)
found = true
return nil
}
// remove machted uuid
if validUUID.MatchString(nameOrUUID) &&
sensor.SensorID == nameOrUUID {
fc.Sensors = append(fc.Sensors[:i], fc.Sensors[i+1:]...)
found = true
return nil
}
}
if !found {
return fmt.Errorf("Can not find sensor %v", nameOrUUID)
}
return nil
return fmt.Errorf("Can not find sensor %v", nameOrUUID)
}
// RemoveRemote deletes a remote address by its name or its unique UUID

View File

@ -50,13 +50,10 @@ func ReadHumidities(humiditySensors []HumiditySensor) ([]*types.Humidity, error)
}
func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperature, error) {
wg := new(sync.WaitGroup)
wg.Add(len(temperatureSensors))
errChannel := make(chan error, len(temperatureSensors))
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
for _, temperatureSensor := range temperatureSensors {
go func(ts TemperatureSensor) {
defer wg.Done()
@ -67,17 +64,13 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor) ([]*types.Temperat
temperatureChannel <- temperature
}(temperatureSensor)
}
wg.Wait()
errorList := errutils.CollectErrors(errChannel)
if err := errutils.FormatErrors(errorList); err != nil {
return nil, err
}
temperatureList := collectTemperatures(temperatureChannel)
return temperatureList, nil
}
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,21 +1,22 @@
package types
import (
"fmt"
"time"
)
// Sensor ...
type Sensor struct {
SensorID string `json:"sensor_id"`
SensorName string `json:"sensor_name"`
SensorLocation string `json:"sensor_location"`
WireID *string `json:"wire_id"`
GPIONumber *string `json:"gpio_number"`
SensorType string `json:"sensor_type"`
SensorEnabled bool `json:"sensor_enabled"`
SensorLastContact time.Time `json:"sensor_last_contact"`
DeviceID string `json:"device_id"`
CreationDate time.Time `json:"creation_date"`
SensorID string `json:"sensor_id"`
SensorName string `json:"sensor_name"`
SensorLocation string `json:"sensor_location"`
WireID *string `json:"wire_id"`
GPIONumber *string `json:"gpio_number"`
SensorModel SensorModel `json:"sensor_model"`
SensorEnabled bool `json:"sensor_enabled"`
SensorLastContact time.Time `json:"sensor_last_contact"`
DeviceID string `json:"device_id"`
CreationDate time.Time `json:"creation_date"`
}
func (s *Sensor) Name() string {
@ -24,6 +25,27 @@ func (s *Sensor) Name() string {
} else if *s.WireID != "" {
return *s.WireID
}
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"`
}