fix(cmd/temperature/read): use measured values instaed own type
This commit is contained in:
parent
08c2cbbf57
commit
003fe8ab9e
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/go-flucky/flucky/cmd/daemon"
|
"github.com/go-flucky/flucky/cmd/daemon"
|
||||||
"github.com/go-flucky/flucky/cmd/rgbled"
|
"github.com/go-flucky/flucky/cmd/rgbled"
|
||||||
"github.com/go-flucky/flucky/cmd/sensor"
|
"github.com/go-flucky/flucky/cmd/sensor"
|
||||||
|
"github.com/go-flucky/flucky/cmd/temperature"
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
@ -59,6 +60,6 @@ func Execute(version string) {
|
|||||||
//humidity.InitCmd(rootCmd, configFile)
|
//humidity.InitCmd(rootCmd, configFile)
|
||||||
rgbled.InitCmd(rootCmd, configFile)
|
rgbled.InitCmd(rootCmd, configFile)
|
||||||
sensor.InitCmd(rootCmd, configFile)
|
sensor.InitCmd(rootCmd, configFile)
|
||||||
//temperature.InitCmd(rootCmd, configFile)
|
temperature.InitCmd(rootCmd, configFile)
|
||||||
rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,28 @@
|
|||||||
package temperature
|
package temperature
|
||||||
|
|
||||||
import (
|
// var compressTemperatureCmd = &cobra.Command{
|
||||||
"log"
|
// Use: "compress",
|
||||||
|
// Short: "Compress temperature logfiles",
|
||||||
|
// Args: cobra.ExactArgs(1),
|
||||||
|
// Example: `flucky temperature compress /var/log/flucky/temperature.json
|
||||||
|
// flucky temperature compress /var/log/flucky/temperature.xml`,
|
||||||
|
// Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/logfile"
|
// temperatureLogfile := logfile.New(args[0])
|
||||||
"github.com/spf13/cobra"
|
// temperatures, err := temperatureLogfile.ReadTemperatures()
|
||||||
)
|
// if err != nil {
|
||||||
|
// log.Fatalln(err)
|
||||||
|
// }
|
||||||
|
|
||||||
var compressTemperatureCmd = &cobra.Command{
|
// temperatures = logfile.CompressTemperature(temperatures)
|
||||||
Use: "compress",
|
|
||||||
Short: "Compress temperature logfiles",
|
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
Example: `flucky temperature compress /var/log/flucky/temperature.json
|
|
||||||
flucky temperature compress /var/log/flucky/temperature.xml`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
|
|
||||||
temperatureLogfile := logfile.New(args[0])
|
// err = temperatureLogfile.WriteTemperatures(temperatures)
|
||||||
temperatures, err := temperatureLogfile.ReadTemperatures()
|
// if err != nil {
|
||||||
if err != nil {
|
// log.Fatalln(err)
|
||||||
log.Fatalln(err)
|
// }
|
||||||
}
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
temperatures = logfile.CompressTemperature(temperatures)
|
// func init() {
|
||||||
|
// temperatureCmd.AddCommand(compressTemperatureCmd)
|
||||||
err = temperatureLogfile.WriteTemperatures(temperatures)
|
// }
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
temperatureCmd.AddCommand(compressTemperatureCmd)
|
|
||||||
}
|
|
||||||
|
@ -1,39 +1,32 @@
|
|||||||
package temperature
|
package temperature
|
||||||
|
|
||||||
import (
|
// var convertTemperatureCmd = &cobra.Command{
|
||||||
"log"
|
// Use: "convert",
|
||||||
|
// Short: "Convert temperature logfiles into other markup language",
|
||||||
|
// Args: cobra.ExactArgs(2),
|
||||||
|
// Example: "flucky temperature convert /var/log/flucky/temperature.json /var/log/flucky/temperature.xml",
|
||||||
|
// Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/logfile"
|
// temperatureLogfileInput := logfile.New(args[0])
|
||||||
"github.com/spf13/cobra"
|
// temperatures, err := temperatureLogfileInput.ReadTemperatures()
|
||||||
)
|
// if err != nil {
|
||||||
|
// log.Fatalln(err)
|
||||||
|
// }
|
||||||
|
|
||||||
var convertTemperatureCmd = &cobra.Command{
|
// if compression {
|
||||||
Use: "convert",
|
// temperatures = logfile.CompressTemperature(temperatures)
|
||||||
Short: "Convert temperature logfiles into other markup language",
|
// }
|
||||||
Args: cobra.ExactArgs(2),
|
|
||||||
Example: "flucky temperature convert /var/log/flucky/temperature.json /var/log/flucky/temperature.xml",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
|
|
||||||
temperatureLogfileInput := logfile.New(args[0])
|
// temperatureLogfileOutput := logfile.New(args[1])
|
||||||
temperatures, err := temperatureLogfileInput.ReadTemperatures()
|
// err = temperatureLogfileOutput.WriteTemperatures(temperatures)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Fatalln(err)
|
// log.Fatalln(err)
|
||||||
}
|
// }
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
if compression {
|
// func init() {
|
||||||
temperatures = logfile.CompressTemperature(temperatures)
|
// temperatureCmd.AddCommand(convertTemperatureCmd)
|
||||||
}
|
// convertTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
||||||
|
// convertTemperatureCmd.Flags().Float64Var(&round, "round", 0.25, "Round values. The value 0 deactivates the function")
|
||||||
temperatureLogfileOutput := logfile.New(args[1])
|
// }
|
||||||
err = temperatureLogfileOutput.WriteTemperatures(temperatures)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
temperatureCmd.AddCommand(convertTemperatureCmd)
|
|
||||||
convertTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
|
||||||
convertTemperatureCmd.Flags().Float64Var(&round, "round", 0.25, "Round values. The value 0 deactivates the function")
|
|
||||||
}
|
|
||||||
|
@ -23,14 +23,14 @@ var listTemperatureCmd = &cobra.Command{
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile)
|
logfile := logfile.New(cnf)
|
||||||
|
|
||||||
temperatures, err := temperatureLogfile.ReadTemperatures()
|
measuredValues, err := logfile.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.PrintTemperatures(temperatures, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -33,31 +33,25 @@ var readTemperatureCmd = &cobra.Command{
|
|||||||
rgbled.Run(rgbLEDs)
|
rgbled.Run(rgbLEDs)
|
||||||
|
|
||||||
// fetch all temperature sensors or sensors by args
|
// fetch all temperature sensors or sensors by args
|
||||||
temperatureSensors := make([]sensor.TemperatureSensor, 0)
|
sensors := make([]sensor.Sensor, 0)
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
temperatureSensors = cnf.GetTemperatureSensors(config.ENABLED)
|
sensors = cnf.GetTemperatureSensors(config.ENABLED)
|
||||||
} else {
|
} else {
|
||||||
temperatureSensors = cnf.GetTemperatureSensorsByName(args)
|
sensors = cnf.GetTemperatureSensorsByName(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit)
|
measuredValues, err := sensor.Read(sensors)
|
||||||
if err != nil {
|
|
||||||
rgbled.Error(rgbLEDs)
|
|
||||||
log.Fatalf("Can not parse temperature unit: %v", temperatureUnit)
|
|
||||||
}
|
|
||||||
|
|
||||||
temperatures, err := sensor.ReadTemperatures(temperatureSensors, measurementUnit, round)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rgbled.Error(rgbLEDs)
|
rgbled.Error(rgbLEDs)
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// print temperatures on stdout
|
// print temperatures on stdout
|
||||||
cli.PrintTemperatures(temperatures, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
if logs {
|
if logs {
|
||||||
temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile)
|
logfile := logfile.New(cnf)
|
||||||
err := logfile.AppendTemperatures(temperatureLogfile, compression, temperatures)
|
err := logfile.Append(compression, measuredValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rgbled.Error(rgbLEDs)
|
rgbled.Error(rgbLEDs)
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
170
pkg/cli/cli.go
170
pkg/cli/cli.go
@ -4,20 +4,19 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetSensorsByTemperatures returns commulated list of sensors by temperature values
|
// GetSensorsByMeasuredValues returns commulated list of sensors by measured values
|
||||||
func GetSensorsByHumidities(humidities []*types.Humidity, cnf *config.Configuration) []*types.Sensor {
|
func GetSensorsByMeasuredValues(measuredValues []types.MeasuredValue, cnf *config.Configuration) []*types.Sensor {
|
||||||
sensors := []*types.Sensor{}
|
sensors := []*types.Sensor{}
|
||||||
for _, humidity := range humidities {
|
for _, measuredValue := range measuredValues {
|
||||||
duplicated := false
|
duplicated := false
|
||||||
foundSensor := &types.Sensor{}
|
foundSensor := &types.Sensor{}
|
||||||
for _, cnfSensor := range cnf.Sensors {
|
for _, cnfSensor := range cnf.Sensors {
|
||||||
if humidity.SensorID == cnfSensor.SensorID {
|
if measuredValue.GetSensorID() == cnfSensor.SensorID {
|
||||||
foundSensor = cnfSensor
|
foundSensor = cnfSensor
|
||||||
|
|
||||||
// compare if id has already been added to list
|
// compare if id has already been added to list
|
||||||
@ -36,147 +35,12 @@ func GetSensorsByHumidities(humidities []*types.Humidity, cnf *config.Configurat
|
|||||||
sensors = append(sensors, foundSensor)
|
sensors = append(sensors, foundSensor)
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
sensors = append(sensors, &types.Sensor{SensorID: humidity.SensorID})
|
sensors = append(sensors, &types.Sensor{SensorID: measuredValue.GetSensorID()})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sensors
|
return sensors
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSensorsByTemperatures returns commulated list of sensors by temperature values
|
|
||||||
func GetSensorsByTemperatures(temperatures []*types.Temperature, cnf *config.Configuration) []*types.Sensor {
|
|
||||||
sensors := []*types.Sensor{}
|
|
||||||
for _, temperature := range temperatures {
|
|
||||||
duplicated := false
|
|
||||||
foundSensor := &types.Sensor{}
|
|
||||||
for _, cnfSensor := range cnf.Sensors {
|
|
||||||
if temperature.SensorID == cnfSensor.SensorID {
|
|
||||||
foundSensor = cnfSensor
|
|
||||||
|
|
||||||
// compare if id has already been added to list
|
|
||||||
for _, sensor := range sensors {
|
|
||||||
if cnfSensor.SensorID == sensor.SensorID {
|
|
||||||
duplicated = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if duplicated {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if foundSensor != nil {
|
|
||||||
sensors = append(sensors, foundSensor)
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
sensors = append(sensors, &types.Sensor{SensorID: temperature.SensorID})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sensors
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTemperaturesBetweenTimeRange returns a list of temperatures between a given time range
|
|
||||||
func GetTemperaturesBetweenTimeRange(from time.Time, till *time.Time, temperatures []*types.Temperature) ([]*types.Temperature, error) {
|
|
||||||
|
|
||||||
if till != nil && from.After(*till) {
|
|
||||||
fFormat := from.Format("2006-01-02 15:04:05")
|
|
||||||
tFormat := till.Format("2006-01-02 15:04:05")
|
|
||||||
return nil, fmt.Errorf("%v is after %v", fFormat, tFormat)
|
|
||||||
}
|
|
||||||
|
|
||||||
cachedTemperatures := []*types.Temperature{}
|
|
||||||
for _, temperature := range temperatures {
|
|
||||||
|
|
||||||
if temperature.TemperatureFromDate.After(from) && till == nil {
|
|
||||||
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if temperature.TemperatureFromDate.After(from) && temperature.TemperatureTillDate.Before(*till) {
|
|
||||||
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cachedTemperatures, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTemperaturesBySensors returns a list of temperatures given by the sensor id.
|
|
||||||
// If the sensor name, wire-id or id can not found in configured sensors, it would be skiped!
|
|
||||||
func GetTemperaturesBySensors(sensorNamesOrIDs []string, temperatures []*types.Temperature, cnf *config.Configuration) []*types.Temperature {
|
|
||||||
cachedSensors := []*types.Sensor{}
|
|
||||||
cachedTemperatures := []*types.Temperature{}
|
|
||||||
|
|
||||||
// match sensor name, wire-id or sensor id with configured sensors
|
|
||||||
// and append the matched sensor to the list
|
|
||||||
for _, sensor := range sensorNamesOrIDs {
|
|
||||||
for _, cs := range cnf.Sensors {
|
|
||||||
if cs.SensorID == sensor {
|
|
||||||
cachedSensors = append(cachedSensors, cs)
|
|
||||||
}
|
|
||||||
if cs.WireID != nil && *cs.WireID == sensor {
|
|
||||||
cachedSensors = append(cachedSensors, cs)
|
|
||||||
}
|
|
||||||
if cs.SensorName == sensor {
|
|
||||||
cachedSensors = append(cachedSensors, cs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// march all temperatures which matched with the sensor id
|
|
||||||
for _, temperature := range temperatures {
|
|
||||||
for _, cachedSensor := range cachedSensors {
|
|
||||||
if temperature.SensorID == cachedSensor.SensorID {
|
|
||||||
cachedTemperatures = append(cachedTemperatures, temperature)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cachedTemperatures
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrintHumidities displays a list of humidities
|
|
||||||
func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w io.Writer) {
|
|
||||||
|
|
||||||
sensors := GetSensorsByHumidities(humidities, cnf)
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrintRGBLEDs displays a list with all configured RGBLEDs
|
// PrintRGBLEDs displays a list with all configured RGBLEDs
|
||||||
func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
|
func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
|
||||||
|
|
||||||
@ -216,15 +80,15 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintTemperatures displays a list of temperatures
|
// PrintMeasuredValues displays a list of measured values
|
||||||
func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configuration, w io.Writer) {
|
func PrintMeasuredValues(measuredValues []types.MeasuredValue, cnf *config.Configuration, w io.Writer) {
|
||||||
|
|
||||||
sensors := GetSensorsByTemperatures(temperatures, cnf)
|
sensors := GetSensorsByMeasuredValues(measuredValues, cnf)
|
||||||
|
|
||||||
// sort temperature values for every sensor
|
// sort measured values for every sensor
|
||||||
orderedTemperatures := make(map[string][]*types.Temperature)
|
orderedMeasuredValues := make(map[string][]types.MeasuredValue)
|
||||||
for _, temperature := range temperatures {
|
for _, measuredValue := range measuredValues {
|
||||||
orderedTemperatures[temperature.SensorID] = append(orderedTemperatures[temperature.SensorID], temperature)
|
orderedMeasuredValues[measuredValue.GetSensorID()] = append(orderedMeasuredValues[measuredValue.GetSensorID()], measuredValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// declare tabwriter
|
// declare tabwriter
|
||||||
@ -240,17 +104,17 @@ func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configurat
|
|||||||
|
|
||||||
// find sensor with maximum temperature values
|
// find sensor with maximum temperature values
|
||||||
maxLength := 0
|
maxLength := 0
|
||||||
for _, orderedTemperature := range orderedTemperatures {
|
for _, orderedMeasuredValue := range orderedMeasuredValues {
|
||||||
if len(orderedTemperature) > maxLength {
|
if len(orderedMeasuredValue) > maxLength {
|
||||||
maxLength = len(orderedTemperature)
|
maxLength = len(orderedMeasuredValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// body
|
// body
|
||||||
for i := 0; i < maxLength; i++ {
|
for i := 0; i < maxLength; i++ {
|
||||||
for _, sensor := range sensors {
|
for _, sensor := range sensors {
|
||||||
if len(orderedTemperatures[sensor.SensorID]) > i {
|
if len(orderedMeasuredValues[sensor.SensorID]) > i {
|
||||||
fmt.Fprintf(tw, "%3.3f\t", orderedTemperatures[sensor.SensorID][i].TemperatureValue)
|
fmt.Fprintf(tw, "%3.3f\t", orderedMeasuredValues[sensor.SensorID][i].GetValue())
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprint(tw, "\t")
|
fmt.Fprint(tw, "\t")
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package logfile
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -26,12 +25,8 @@ func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredV
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(len(allMeasuredValues))
|
|
||||||
|
|
||||||
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
||||||
|
|
||||||
log.Println(len(allMeasuredValues))
|
|
||||||
|
|
||||||
err = jl.Write(allMeasuredValues)
|
err = jl.Write(allMeasuredValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -11,15 +11,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Read measured values from sensors
|
// Read measured values from sensors
|
||||||
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
|
func Read(sensors []Sensor) ([]types.MeasuredValue, error) {
|
||||||
measuredValueChannel := make(chan types.MeasuredValue, len(sensors))
|
measuredValueChannel := make(chan types.MeasuredValue, 0)
|
||||||
errorChannel := make(chan error, len(sensors))
|
errorChannel := make(chan error, 0)
|
||||||
|
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
wg.Add(len(sensors))
|
wg.Add(len(sensors))
|
||||||
|
|
||||||
for _, sensor := range sensors {
|
for _, sensor := range sensors {
|
||||||
go sensor.ReadContinously(ctx, measuredValueChannel, errorChannel)
|
go sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
31
pkg/types/typeswitch/typeswitch.go
Normal file
31
pkg/types/typeswitch/typeswitch.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package typeswitch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HumidityValues(measuredValues []types.MeasuredValue) []types.MeasuredValue {
|
||||||
|
cachedMeasuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
switch v := measuredValue.(type) {
|
||||||
|
case *types.Humidity:
|
||||||
|
cachedMeasuredValues = append(cachedMeasuredValues, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedMeasuredValues
|
||||||
|
}
|
||||||
|
|
||||||
|
func TemperatureValues(measuredValues []types.MeasuredValue) []types.MeasuredValue {
|
||||||
|
cachedMeasuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
switch v := measuredValue.(type) {
|
||||||
|
case *types.Temperature:
|
||||||
|
cachedMeasuredValues = append(cachedMeasuredValues, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedMeasuredValues
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user