feat: select optional sensors to read humidity or temperature

This commit is contained in:
Markus Pesch 2019-02-28 23:23:21 +01:00
parent 91d20b6e95
commit 82e82952ed
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 95 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package temperature
import (
"fmt"
"log"
"os"
@ -13,8 +14,9 @@ import (
var follow, push bool
var readTemperatureCmd = &cobra.Command{
Use: "read",
Short: "read temperature from sensor",
Use: "read",
Short: "read temperature from sensor",
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
@ -24,7 +26,7 @@ var readTemperatureCmd = &cobra.Command{
}
// fetch all temperature sensors
temperatureSensors, err := fc.GetTemperatureSensors()
temperatureSensors, err := fc.GetTemperatureSensors(args)
if err != nil {
log.Fatalln(err)
}

View File

@ -9,6 +9,63 @@ import (
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
func PrintHumidities(humidities []*types.Humidity, cnf *config.FluckyConfig, w io.Writer) {
// determine all humidity sensors based on the humidiy values
sensors := []*types.Sensor{}
for _, humidity := range humidities {
// Search for the sensor that has acquired the measured value
for _, sensor := range cnf.Sensors {
if sensor.SensorID == humidity.SensorID {
sensors = append(sensors, sensor)
break
}
}
// If it was not found, pass only the sensor with the UUID on
sensor := &types.Sensor{
SensorID: humidity.SensorID,
}
sensors = append(sensors, sensor)
}
// sort temperature values for every sensor
orderedHumidities := make(map[string][]*types.Humidity)
for _, humidity := range humidities {
orderedHumidities[humidity.SensorID] = append(orderedHumidities[humidity.SensorID], humidity)
}
// declare tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
// headlines
for i, sensor := range sensors {
fmt.Fprintf(tw, "%v\t", sensor.Name())
if i == len(sensors)-1 {
fmt.Fprintf(tw, "\n")
}
}
// find sensor with maximum temperature values
maxLength := 0
for _, orderedHumidity := range orderedHumidities {
if len(orderedHumidity) > maxLength {
maxLength = len(orderedHumidity)
}
}
// body
for i := 0; i < maxLength; i++ {
for _, sensor := range sensors {
if len(orderedHumidities[sensor.SensorID]) > i {
fmt.Fprintf(tw, "%3.3f\t", orderedHumidities[sensor.SensorID][i].HumidityValue)
} else {
fmt.Fprint(tw, "\t")
}
fmt.Fprint(tw, "\n")
}
}
tw.Flush()
}
func PrintTemperatures(temperatures []*types.Temperature, cnf *config.FluckyConfig, w io.Writer) {
sensors := []*types.Sensor{}

View File

@ -11,7 +11,7 @@ import (
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
)
// FluckyConfig dasd
@ -205,9 +205,26 @@ func (fc *FluckyConfig) EnableSensor(nameOrUUID string) error {
return nil
}
func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor {
func (fc *FluckyConfig) GetHumiditySensors(namesOrUUIDs []string) []sensor.HumiditySensor {
hs := []sensor.HumiditySensor{}
for _, s := range fc.Sensors {
// select only named sensors
if len(namesOrUUIDs) > 0 {
found := false
for _, nameOrUUID := range namesOrUUIDs {
if nameOrUUID == s.SensorID || nameOrUUID == s.SensorName {
found = true
break
}
}
if !found {
continue
}
}
// skip disabled sensors
if !s.SensorEnabled {
continue
}
switch s.SensorModel {
case types.DHT11:
hs = append(hs, &sensor.DHT11Sensor{
@ -222,15 +239,26 @@ func (fc *FluckyConfig) GetHumiditySensors() []sensor.HumiditySensor {
return hs
}
func (fc *FluckyConfig) GetTemperatureSensors() ([]sensor.TemperatureSensor, error) {
func (fc *FluckyConfig) GetTemperatureSensors(namesOrUUIDs []string) ([]sensor.TemperatureSensor, error) {
ts := []sensor.TemperatureSensor{}
for _, s := range fc.Sensors {
// select only named sensors
if len(namesOrUUIDs) > 0 {
found := false
for _, nameOrUUID := range namesOrUUIDs {
if nameOrUUID == s.SensorID || nameOrUUID == s.SensorName {
found = true
break
}
}
if !found {
continue
}
}
// skip disabled sensors
if !s.SensorEnabled {
continue
}
switch s.SensorModel {
case types.DHT11:
ts = append(ts, &sensor.DHT11Sensor{