2018-11-07 19:07:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-02-22 12:08:58 +00:00
|
|
|
"fmt"
|
2019-08-20 19:37:45 +00:00
|
|
|
"math"
|
2019-02-22 12:08:58 +00:00
|
|
|
"os"
|
2019-02-24 21:46:36 +00:00
|
|
|
"time"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2020-01-08 20:54:15 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/completion"
|
2019-06-28 11:18:55 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/compression"
|
2019-06-28 11:12:49 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/convert"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/daemon"
|
2019-06-27 16:49:04 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/humidity"
|
2019-08-19 10:24:08 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/pressure"
|
2019-06-16 11:00:50 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/rgbled"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/sensor"
|
2019-06-25 20:56:09 +00:00
|
|
|
"github.com/go-flucky/flucky/cmd/temperature"
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/config"
|
2019-02-22 12:08:58 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-11-07 19:07:15 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
var configFile string
|
2018-11-19 21:36:21 +00:00
|
|
|
|
2018-11-07 19:07:15 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "flucky",
|
2019-07-02 20:54:58 +00:00
|
|
|
Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
|
2019-02-22 12:08:58 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
// check if config file exists
|
2019-06-16 11:00:50 +00:00
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
2019-02-22 12:08:58 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not locate the hostname: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-07 17:57:41 +00:00
|
|
|
// Time must be truncted for postgres. Postgres currently does not support
|
|
|
|
// nanoseconds which is automatically include into the go time object
|
2019-08-20 19:37:45 +00:00
|
|
|
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)
|
|
|
|
|
2019-12-07 17:57:41 +00:00
|
|
|
// Default configuration
|
2019-06-13 19:25:32 +00:00
|
|
|
cnf := config.Configuration{
|
2019-02-22 12:08:58 +00:00
|
|
|
Device: &types.Device{
|
2020-01-10 21:03:49 +00:00
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Name: hostname,
|
2019-08-20 19:37:45 +00:00
|
|
|
CreationDate: t,
|
2019-02-22 12:08:58 +00:00
|
|
|
},
|
2019-12-07 15:53:49 +00:00
|
|
|
StorageEndpoint: "file:///var/log/flucky/logfile.csv",
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
err = config.Write(&cnf, configFile)
|
2019-02-22 12:08:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2018-11-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute a
|
2019-09-01 10:27:06 +00:00
|
|
|
func Execute(version *semver.Version) {
|
|
|
|
rootCmd.Version = version.String()
|
2018-11-07 19:07:15 +00:00
|
|
|
|
2019-06-16 11:00:50 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
|
|
|
|
|
2020-01-08 20:54:15 +00:00
|
|
|
completion.InitCmd(rootCmd)
|
2019-09-04 11:37:50 +00:00
|
|
|
compression.InitCmd(rootCmd, &configFile, version)
|
|
|
|
convert.InitCmd(rootCmd, &configFile, version)
|
|
|
|
daemon.InitCmd(rootCmd, &configFile, version)
|
|
|
|
humidity.InitCmd(rootCmd, &configFile, version)
|
|
|
|
pressure.InitCmd(rootCmd, &configFile, version)
|
|
|
|
rgbled.InitCmd(rootCmd, &configFile, version)
|
|
|
|
sensor.InitCmd(rootCmd, &configFile, version)
|
|
|
|
temperature.InitCmd(rootCmd, &configFile, version)
|
2018-11-07 19:07:15 +00:00
|
|
|
rootCmd.Execute()
|
|
|
|
}
|