diff --git a/cmd/cmd.go b/cmd/cmd.go index 8823c2c..c41ad16 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -36,13 +36,13 @@ var rootCmd = &cobra.Command{ return fmt.Errorf("Can not locate the hostname: %v", err) } - // Time must be truncted for postgres - // Postgres currently does not support nanoseconds which is automatically - // include into the go time object + // Time must be truncted for postgres. Postgres currently does not support + // nanoseconds which is automatically include into the go time object t := time.Now() l, _ := time.LoadLocation("Europe/Berlin") t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l) + // Default configuration cnf := config.Configuration{ Device: &types.Device{ DeviceID: uuid.NewV4().String(), @@ -71,7 +71,6 @@ func Execute(version *semver.Version) { compression.InitCmd(rootCmd, &configFile, version) convert.InitCmd(rootCmd, &configFile, version) daemon.InitCmd(rootCmd, &configFile, version) - // db.InitCmd(rootCmd, &configFile, version) humidity.InitCmd(rootCmd, &configFile, version) pressure.InitCmd(rootCmd, &configFile, version) rgbled.InitCmd(rootCmd, &configFile, version) diff --git a/cmd/db/db.go b/cmd/db/db.go deleted file mode 100644 index 0fb6f6b..0000000 --- a/cmd/db/db.go +++ /dev/null @@ -1,56 +0,0 @@ -package db - -import ( - "github.com/Masterminds/semver" - "github.com/spf13/cobra" -) - -var ( - configFile *string - - version *semver.Version -) - -var dbCmd = &cobra.Command{ - Use: "db", - Short: "Operates with the configured database", - Run: func(cmd *cobra.Command, args []string) { - - // // read configuration - // cnf, err := config.Read(*configFile) - // if err != nil { - // log.Fatalln(err) - // } - - // postgresDB, err := database.New(cnf.DatabaseSettings) - // if err != nil { - // log.Fatalf("%v", err) - // } - - // ctx := context.Background() - - // devices := []*types.Device{ - // &types.Device{ - // DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c", - // DeviceName: "raspberr-pi", - // }, - // &types.Device{ - // DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c", - // DeviceName: "raspberr-pi", - // }, - // } - - // if err := postgresDB.InsertDevices(ctx, devices); err != nil { - // log.Fatalln(err) - // } - }, -} - -// Execute a -func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) { - configFile = cnfFile - version = sversion - - cmd.AddCommand(dbCmd) - -} diff --git a/cmd/temperature/read.go b/cmd/temperature/read.go index 89127f8..d122033 100644 --- a/cmd/temperature/read.go +++ b/cmd/temperature/read.go @@ -15,8 +15,6 @@ import ( "github.com/spf13/cobra" ) -var logs bool - var readTemperatureCmd = &cobra.Command{ Use: "read", Short: "Reading temperature values from different or specified sensors by arguments", @@ -38,7 +36,7 @@ var readTemperatureCmd = &cobra.Command{ } if len(sensors) == 0 { - return + log.Fatalln("No sensors found, specified or configured") } ctx := context.Background() @@ -47,12 +45,20 @@ var readTemperatureCmd = &cobra.Command{ log.Fatalln(err) } + storage.Round(measuredValues, round) + measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues) // print temperatures on stdout cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) + // Save the new measured values, if desired if logs { + + if compression { + measuredValues = storage.Compression(measuredValues) + } + storageEndpoint, err := cnf.GetStorageEndpointURL() if err != nil { log.Fatalln(err) diff --git a/cmd/temperature/temperature.go b/cmd/temperature/temperature.go index 47d0f0c..d4396bf 100644 --- a/cmd/temperature/temperature.go +++ b/cmd/temperature/temperature.go @@ -10,6 +10,7 @@ import ( var ( compression bool configFile *string + logs bool round float64 version *semver.Version @@ -21,7 +22,7 @@ var temperatureCmd = &cobra.Command{ Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"), } -// Execute a +// InitCmd initialize the subcommand func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) { configFile = cnfFile version = sversion diff --git a/pkg/storage/logfile/logfile.go b/pkg/storage/logfile/logfile.go index e3824c7..940e55e 100644 --- a/pkg/storage/logfile/logfile.go +++ b/pkg/storage/logfile/logfile.go @@ -7,24 +7,17 @@ import ( "github.com/go-flucky/flucky/pkg/types" ) -// var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") - // Append adds an array of several measured values to a logfile func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error { - allMeasuredValues, err := logfile.Read() if err != nil { return err } - allMeasuredValues = append(allMeasuredValues, measuredValues...) - err = logfile.Write(allMeasuredValues) if err != nil { return err - } - return nil } @@ -32,9 +25,7 @@ func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error { // file extension of the logfile is taken into account to format the logfile // into the correct format. func New(logfile string) Logfile { - ext := filepath.Ext(logfile) - switch ext { case ".csv": return &csvLogfile{ @@ -53,7 +44,6 @@ func New(logfile string) Logfile { logfile: logfile, } } - } func writeCreationDate(measuredValues []*types.MeasuredValue) error { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 4cf1e6a..e83da3c 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -31,6 +31,13 @@ func Compression(measuredValues []*types.MeasuredValue) []*types.MeasuredValue { now := format.FormatedTime() for _, measuredValue := range measuredValues { + + // If the sensor id does not exist in the map, a new map is initialized, + // which can assume measured value types as the key. Behind this key there + // is a pointer which refers to a measured value in the memory. This new map + // is added to the map "lastMeasuredValuesBySensors" under the sensor ID. + // This makes it possible to store one measured value per measured value + // type per sensor. if _, ok := lastMeasuredValuesBySensors[measuredValue.SensorID]; !ok { lastMeasuredValuesBySensors[measuredValue.SensorID] = make(map[types.MeasuredValueType]*types.MeasuredValue, 0) } @@ -93,21 +100,27 @@ func Round(measuredValues []*types.MeasuredValue, round float64) { } } -// Write measured values to the given storage endpoint url. The scheme must be -// matched to a provider, if the scheme is not implemented, the function -// returns an error +// Write measured values to the given storage endpoint url. If the storage +// provider defined to a file, the data will be overwritten. If a database +// provider is used, the data is simply added without deleting the existing +// data. The scheme must be matched to a storage provider, if the scheme is not +// implemented, the function returns an error func Write(ctx context.Context, measuredValues []*types.MeasuredValue, storageEndpoint *url.URL) error { switch storageEndpoint.Scheme { case "file": measuredValueLogfile := logfile.New(storageEndpoint.Path) - return measuredValueLogfile.Write(measuredValues) + storedMeasuredValues, err := measuredValueLogfile.Read() + if err != nil { + return err + } + storedMeasuredValues = append(storedMeasuredValues, measuredValues...) + return measuredValueLogfile.Write(storedMeasuredValues) case "postgres": database, err := db.New(storageEndpoint) if err != nil { return err } defer database.Close() - if err := database.InsertMeasuredValues(ctx, measuredValues); err != nil { return err }