From 14ce354b326f807b72d7d3a55755fa5496ba27bd Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 28 Feb 2019 19:48:25 +0100 Subject: [PATCH] fix: sensor typ to static sensor model --- cmd/cmd.go | 2 +- cmd/sensor/add.go | 8 +++- cmd/temperature/read.go | 13 +++++-- pkg/cli/cli.go | 6 ++- pkg/config/fluckyconfig.go | 80 +++++++++++++++++++------------------- pkg/sensor/sensor.go | 7 ---- pkg/types/device.go | 12 ++++++ pkg/types/humidity.go | 12 ++++++ pkg/types/sensor.go | 44 +++++++++++++++------ pkg/types/temperature.go | 12 ++++++ pkg/types/types.go | 32 --------------- 11 files changed, 129 insertions(+), 99 deletions(-) create mode 100644 pkg/types/device.go create mode 100644 pkg/types/humidity.go create mode 100644 pkg/types/temperature.go delete mode 100644 pkg/types/types.go diff --git a/cmd/cmd.go b/cmd/cmd.go index b71749a..aae02ab 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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(), }, } diff --git a/cmd/sensor/add.go b/cmd/sensor/add.go index 7b8ce3d..bb812d4 100644 --- a/cmd/sensor/add.go +++ b/cmd/sensor/add.go @@ -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], diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 86cd608..6f80f59 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -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) }, } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index fbdf0ca..0974400 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -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 diff --git a/pkg/config/fluckyconfig.go b/pkg/config/fluckyconfig.go index 10fc6d8..f5b2b59 100644 --- a/pkg/config/fluckyconfig.go +++ b/pkg/config/fluckyconfig.go @@ -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 diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index bd45350..cae837f 100644 --- a/pkg/sensor/sensor.go +++ b/pkg/sensor/sensor.go @@ -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 { diff --git a/pkg/types/device.go b/pkg/types/device.go new file mode 100644 index 0000000..d7b791e --- /dev/null +++ b/pkg/types/device.go @@ -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"` +} diff --git a/pkg/types/humidity.go b/pkg/types/humidity.go new file mode 100644 index 0000000..ff7ca25 --- /dev/null +++ b/pkg/types/humidity.go @@ -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"` +} diff --git a/pkg/types/sensor.go b/pkg/types/sensor.go index 5944eaa..2861560 100644 --- a/pkg/types/sensor.go +++ b/pkg/types/sensor.go @@ -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) + } +} diff --git a/pkg/types/temperature.go b/pkg/types/temperature.go new file mode 100644 index 0000000..8bd797c --- /dev/null +++ b/pkg/types/temperature.go @@ -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"` +} diff --git a/pkg/types/types.go b/pkg/types/types.go deleted file mode 100644 index 7e16f5e..0000000 --- a/pkg/types/types.go +++ /dev/null @@ -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"` -}