diff --git a/cmd/cmd.go b/cmd/cmd.go index 514cae7..6542ae8 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -8,6 +8,7 @@ import ( "github.com/go-flucky/flucky/cmd/daemon" "github.com/go-flucky/flucky/cmd/rgbled" "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/config" @@ -59,6 +60,6 @@ func Execute(version string) { //humidity.InitCmd(rootCmd, configFile) rgbled.InitCmd(rootCmd, configFile) sensor.InitCmd(rootCmd, configFile) - //temperature.InitCmd(rootCmd, configFile) + temperature.InitCmd(rootCmd, configFile) rootCmd.Execute() } diff --git a/cmd/temperature/compression.go b/cmd/temperature/compression.go index a82afcb..17e3207 100644 --- a/cmd/temperature/compression.go +++ b/cmd/temperature/compression.go @@ -1,35 +1,28 @@ package temperature -import ( - "log" +// var compressTemperatureCmd = &cobra.Command{ +// 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" - "github.com/spf13/cobra" -) +// temperatureLogfile := logfile.New(args[0]) +// temperatures, err := temperatureLogfile.ReadTemperatures() +// if err != nil { +// log.Fatalln(err) +// } -var compressTemperatureCmd = &cobra.Command{ - 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) { +// temperatures = logfile.CompressTemperature(temperatures) - temperatureLogfile := logfile.New(args[0]) - temperatures, err := temperatureLogfile.ReadTemperatures() - if err != nil { - log.Fatalln(err) - } +// err = temperatureLogfile.WriteTemperatures(temperatures) +// if err != nil { +// log.Fatalln(err) +// } +// }, +// } - temperatures = logfile.CompressTemperature(temperatures) - - err = temperatureLogfile.WriteTemperatures(temperatures) - if err != nil { - log.Fatalln(err) - } - }, -} - -func init() { - temperatureCmd.AddCommand(compressTemperatureCmd) -} +// func init() { +// temperatureCmd.AddCommand(compressTemperatureCmd) +// } diff --git a/cmd/temperature/convert.go b/cmd/temperature/convert.go index 91c2eac..12ece2a 100644 --- a/cmd/temperature/convert.go +++ b/cmd/temperature/convert.go @@ -1,39 +1,32 @@ package temperature -import ( - "log" +// var convertTemperatureCmd = &cobra.Command{ +// 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" - "github.com/spf13/cobra" -) +// temperatureLogfileInput := logfile.New(args[0]) +// temperatures, err := temperatureLogfileInput.ReadTemperatures() +// if err != nil { +// log.Fatalln(err) +// } -var convertTemperatureCmd = &cobra.Command{ - 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) { +// if compression { +// temperatures = logfile.CompressTemperature(temperatures) +// } - temperatureLogfileInput := logfile.New(args[0]) - temperatures, err := temperatureLogfileInput.ReadTemperatures() - if err != nil { - log.Fatalln(err) - } +// temperatureLogfileOutput := logfile.New(args[1]) +// err = temperatureLogfileOutput.WriteTemperatures(temperatures) +// if err != nil { +// log.Fatalln(err) +// } +// }, +// } - if compression { - temperatures = logfile.CompressTemperature(temperatures) - } - - 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") -} +// 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") +// } diff --git a/cmd/temperature/list.go b/cmd/temperature/list.go index 8ef7031..350c12d 100644 --- a/cmd/temperature/list.go +++ b/cmd/temperature/list.go @@ -23,14 +23,14 @@ var listTemperatureCmd = &cobra.Command{ log.Fatalln(err) } - temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile) + logfile := logfile.New(cnf) - temperatures, err := temperatureLogfile.ReadTemperatures() + measuredValues, err := logfile.Read() if err != nil { log.Fatalln(err) } - cli.PrintTemperatures(temperatures, cnf, os.Stdout) + cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) }, } diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index c22eb4b..9fb943d 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -33,31 +33,25 @@ var readTemperatureCmd = &cobra.Command{ rgbled.Run(rgbLEDs) // fetch all temperature sensors or sensors by args - temperatureSensors := make([]sensor.TemperatureSensor, 0) + sensors := make([]sensor.Sensor, 0) if len(args) == 0 { - temperatureSensors = cnf.GetTemperatureSensors(config.ENABLED) + sensors = cnf.GetTemperatureSensors(config.ENABLED) } else { - temperatureSensors = cnf.GetTemperatureSensorsByName(args) + sensors = cnf.GetTemperatureSensorsByName(args) } - measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit) - if err != nil { - rgbled.Error(rgbLEDs) - log.Fatalf("Can not parse temperature unit: %v", temperatureUnit) - } - - temperatures, err := sensor.ReadTemperatures(temperatureSensors, measurementUnit, round) + measuredValues, err := sensor.Read(sensors) if err != nil { rgbled.Error(rgbLEDs) log.Fatalln(err) } // print temperatures on stdout - cli.PrintTemperatures(temperatures, cnf, os.Stdout) + cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) if logs { - temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile) - err := logfile.AppendTemperatures(temperatureLogfile, compression, temperatures) + logfile := logfile.New(cnf) + err := logfile.Append(compression, measuredValues) if err != nil { rgbled.Error(rgbLEDs) log.Fatalln(err) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 9b13313..e6170d6 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -4,20 +4,19 @@ import ( "fmt" "io" "text/tabwriter" - "time" "github.com/go-flucky/flucky/pkg/config" "github.com/go-flucky/flucky/pkg/types" ) -// GetSensorsByTemperatures returns commulated list of sensors by temperature values -func GetSensorsByHumidities(humidities []*types.Humidity, cnf *config.Configuration) []*types.Sensor { +// GetSensorsByMeasuredValues returns commulated list of sensors by measured values +func GetSensorsByMeasuredValues(measuredValues []types.MeasuredValue, cnf *config.Configuration) []*types.Sensor { sensors := []*types.Sensor{} - for _, humidity := range humidities { + for _, measuredValue := range measuredValues { duplicated := false foundSensor := &types.Sensor{} for _, cnfSensor := range cnf.Sensors { - if humidity.SensorID == cnfSensor.SensorID { + if measuredValue.GetSensorID() == cnfSensor.SensorID { foundSensor = cnfSensor // 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) continue } else { - sensors = append(sensors, &types.Sensor{SensorID: humidity.SensorID}) + sensors = append(sensors, &types.Sensor{SensorID: measuredValue.GetSensorID()}) } } 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 func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) { @@ -216,15 +80,15 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error { return nil } -// PrintTemperatures displays a list of temperatures -func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configuration, w io.Writer) { +// PrintMeasuredValues displays a list of measured values +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 - orderedTemperatures := make(map[string][]*types.Temperature) - for _, temperature := range temperatures { - orderedTemperatures[temperature.SensorID] = append(orderedTemperatures[temperature.SensorID], temperature) + // sort measured values for every sensor + orderedMeasuredValues := make(map[string][]types.MeasuredValue) + for _, measuredValue := range measuredValues { + orderedMeasuredValues[measuredValue.GetSensorID()] = append(orderedMeasuredValues[measuredValue.GetSensorID()], measuredValue) } // declare tabwriter @@ -240,17 +104,17 @@ func PrintTemperatures(temperatures []*types.Temperature, cnf *config.Configurat // find sensor with maximum temperature values maxLength := 0 - for _, orderedTemperature := range orderedTemperatures { - if len(orderedTemperature) > maxLength { - maxLength = len(orderedTemperature) + for _, orderedMeasuredValue := range orderedMeasuredValues { + if len(orderedMeasuredValue) > maxLength { + maxLength = len(orderedMeasuredValue) } } // body for i := 0; i < maxLength; i++ { for _, sensor := range sensors { - if len(orderedTemperatures[sensor.SensorID]) > i { - fmt.Fprintf(tw, "%3.3f\t", orderedTemperatures[sensor.SensorID][i].TemperatureValue) + if len(orderedMeasuredValues[sensor.SensorID]) > i { + fmt.Fprintf(tw, "%3.3f\t", orderedMeasuredValues[sensor.SensorID][i].GetValue()) } else { fmt.Fprint(tw, "\t") } diff --git a/pkg/logfile/json.go b/pkg/logfile/json.go index 0b24947..d6116bc 100644 --- a/pkg/logfile/json.go +++ b/pkg/logfile/json.go @@ -3,7 +3,6 @@ package logfile import ( "encoding/json" "fmt" - "log" "os" "sync" @@ -26,12 +25,8 @@ func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredV return err } - log.Println(len(allMeasuredValues)) - allMeasuredValues = append(allMeasuredValues, measuredValues...) - log.Println(len(allMeasuredValues)) - err = jl.Write(allMeasuredValues) if err != nil { return err diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index 0ffe666..554ccbd 100644 --- a/pkg/sensor/sensor.go +++ b/pkg/sensor/sensor.go @@ -11,15 +11,15 @@ import ( ) // Read measured values from sensors -func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) { - measuredValueChannel := make(chan types.MeasuredValue, len(sensors)) - errorChannel := make(chan error, len(sensors)) +func Read(sensors []Sensor) ([]types.MeasuredValue, error) { + measuredValueChannel := make(chan types.MeasuredValue, 0) + errorChannel := make(chan error, 0) wg := new(sync.WaitGroup) wg.Add(len(sensors)) for _, sensor := range sensors { - go sensor.ReadContinously(ctx, measuredValueChannel, errorChannel) + go sensor.ReadChannel(measuredValueChannel, errorChannel, wg) } wg.Wait() diff --git a/pkg/types/typeswitch/typeswitch.go b/pkg/types/typeswitch/typeswitch.go new file mode 100644 index 0000000..be9cfbc --- /dev/null +++ b/pkg/types/typeswitch/typeswitch.go @@ -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 +}