fix: cli temperature read
changes: - fix: read temperature values without daemon Add subcommand to read temperature values without starting the daemon - fix: implement measured value types Replace measured value types with constants - fix: add sensor pipelines Add functions which returns a channel with measured values - fix: filter measured values from a channel Add functions to filter measured values by sensor id or measured value types.
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/temperature"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"github.com/Masterminds/semver"
|
||||
|
||||
@ -38,6 +39,7 @@ func Execute(version *semver.Version) error {
|
||||
subCommands := []func(cmd *cobra.Command) error{
|
||||
daemon.InitCmd,
|
||||
sensor.InitCmd,
|
||||
temperature.InitCmd,
|
||||
}
|
||||
|
||||
for _, subCommand := range subCommands {
|
||||
|
133
cli/temperature/temperature.go
Normal file
133
cli/temperature/temperature.go
Normal file
@ -0,0 +1,133 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/cli"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/repository"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/sensor"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func InitCmd(cmd *cobra.Command) error {
|
||||
temperatureCmd := &cobra.Command{
|
||||
Use: "temperature",
|
||||
Short: "Read and list temperature values",
|
||||
}
|
||||
|
||||
readTemperatureCmd := &cobra.Command{
|
||||
Use: "read",
|
||||
Short: "Read temperature values from sensors",
|
||||
RunE: readTemperature,
|
||||
}
|
||||
readTemperatureCmd.Flags().Bool("persist", true, "Persist measured values to the repository")
|
||||
|
||||
temperatureCmd.AddCommand(readTemperatureCmd)
|
||||
cmd.AddCommand(temperatureCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readTemperature(cmd *cobra.Command, args []string) error {
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
persist, err := cmd.Flags().GetBool("persist")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Flag persist not defined: %v", err)
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logLevelString, err := cmd.Flags().GetString("loglevel")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logLevel, err := logger.ParseLogLevel(logLevelString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
flogger := logger.NewLogger(logLevel)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensorTypes, err := repo.GetSensors()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensorTypes, err = types.FilterSensorByMeasuredValueTypes(sensorTypes, types.Temperature)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensors := make([]sensor.Sensor, 0)
|
||||
for i := range sensorTypes {
|
||||
s, err := sensor.New(sensorTypes[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensors = append(sensors, s)
|
||||
}
|
||||
|
||||
measuredValueChannel, errorChannel := sensor.ReadPipeline(context.TODO(), sensors...)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case err, open := <-errorChannel:
|
||||
if !open {
|
||||
return
|
||||
}
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0)
|
||||
LOOP:
|
||||
for {
|
||||
select {
|
||||
case measuredValue, open := <-measuredValueChannel:
|
||||
if !open {
|
||||
break LOOP
|
||||
}
|
||||
measuredValues = append(measuredValues, measuredValue)
|
||||
}
|
||||
}
|
||||
|
||||
err = cli.PrintMeasuredValues(measuredValues, os.Stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if persist {
|
||||
err = repo.AddMeasuredValues(measuredValues...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user