feat(cmd/rgb-led): add subcommands to operate with rgb-leds

This commit is contained in:
2019-06-16 13:00:50 +02:00
parent c81cd2d21c
commit 49d66cfcbb
27 changed files with 713 additions and 40 deletions

View File

@ -6,6 +6,7 @@ import (
"time"
"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"
@ -15,7 +16,7 @@ import (
"github.com/spf13/cobra"
)
var configPath string
var configFile string
var rootCmd = &cobra.Command{
Use: "flucky",
@ -23,7 +24,7 @@ var rootCmd = &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("Can not locate the hostname: %v", err)
@ -39,7 +40,7 @@ var rootCmd = &cobra.Command{
},
}
err = config.Write(&cnf, configPath)
err = config.Write(&cnf, configFile)
if err != nil {
return err
}
@ -53,9 +54,11 @@ var rootCmd = &cobra.Command{
func Execute(version string) {
rootCmd.Version = version
rootCmd.PersistentFlags().StringVar(&configPath, "config", "/etc/flucky/config.json", "Config file")
daemon.InitCmd(rootCmd, configPath)
sensor.InitCmd(rootCmd, configPath)
temperature.InitCmd(rootCmd, configPath)
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
daemon.InitCmd(rootCmd, configFile)
rgbled.InitCmd(rootCmd, configFile)
sensor.InitCmd(rootCmd, configFile)
temperature.InitCmd(rootCmd, configFile)
rootCmd.Execute()
}

View File

@ -9,14 +9,14 @@ import (
)
var compression bool
var configPath string
var configFile string
var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Read continuously data from all enabled sensors",
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configPath)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
@ -29,8 +29,8 @@ var daemonCmd = &cobra.Command{
},
}
func InitCmd(cmd *cobra.Command, cnfPath string) {
configPath = cnfPath
func InitCmd(cmd *cobra.Command, cnfFile string) {
configFile = cnfFile
cmd.AddCommand(daemonCmd)
daemonCmd.Flags().BoolVarP(&compression, "compression", "c", true, "Compress measured values")

View File

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)
var configPath string
var configFile string
var humidityCmd = &cobra.Command{
Use: "humidity",
@ -12,8 +12,8 @@ var humidityCmd = &cobra.Command{
}
// Execute a
func InitCmd(cmd *cobra.Command, cnfPath string) {
configPath = cnfPath
func InitCmd(cmd *cobra.Command, cnfFile string) {
configFile = cnfFile
cmd.AddCommand(humidityCmd)

78
cmd/rgbled/add.go Normal file
View File

@ -0,0 +1,78 @@
package rgbled
import (
"fmt"
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra"
)
var enabled bool
var location, wireID, wirePath string
var addRgbLedCmd = &cobra.Command{
Use: "add",
Short: "Add a RGB-LED",
Aliases: []string{"append"},
Args: cobra.ExactArgs(4),
Example: fmt.Sprintf(`flucky rgb-led add <name> <gpio-for-blue> <gpio-for-green> <gpio-for-red>
flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// determine gpio port
gpioRed, err := types.StringToGPIO(args[1])
if err != nil {
log.Fatalln(err)
}
gpioGreen, err := types.StringToGPIO(args[2])
if err != nil {
log.Fatalln(err)
}
gpioBlue, err := types.StringToGPIO(args[3])
if err != nil {
log.Fatalln(err)
}
// create new sensor struct
rgbLED := &types.RGBLED{
RGBLEDName: args[0],
RGBLEDLocation: location,
RGBLEDEnabled: enabled,
RGBLEDColorToGPIO: map[types.RGBColor]*types.GPIO{
types.RGBLEDBlue: &gpioBlue,
types.RGBLEDGreen: &gpioGreen,
types.RGBLEDRed: &gpioRed,
},
}
// // add sensor entry to list
err = cnf.AddRGBLED(rgbLED)
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(addRgbLedCmd)
addRgbLedCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
addRgbLedCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
addRgbLedCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
addRgbLedCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
}

42
cmd/rgbled/disable.go Normal file
View File

@ -0,0 +1,42 @@
package rgbled
import (
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/spf13/cobra"
)
var disableRgbLedCmd = &cobra.Command{
Use: "disable",
Short: "Disable a RGB-LED",
Args: cobra.ExactArgs(1),
Example: `flucky rgb-led disable <name/uuid>
flucky rgb-led disable my-led
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// disable sensor entry to list
err = cnf.DisableRGBLED(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(disableRgbLedCmd)
}

41
cmd/rgbled/enable.go Normal file
View File

@ -0,0 +1,41 @@
package rgbled
import (
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/spf13/cobra"
)
var enableRgbLedCmd = &cobra.Command{
Use: "enable",
Short: "Enable a RGB-LED",
Example: `flucky rgb-led enable <name/uuid>
flucky rgb-led enable my-led
flucky rgb-led enable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// disable sensor entry to list
err = cnf.EnableRGBLED(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(enableRgbLedCmd)
}

30
cmd/rgbled/list.go Normal file
View File

@ -0,0 +1,30 @@
package rgbled
import (
"log"
"os"
"github.com/go-flucky/flucky/pkg/cli"
"github.com/go-flucky/flucky/pkg/config"
"github.com/spf13/cobra"
)
var listRgbLedCmd = &cobra.Command{
Use: "list",
Short: "List RGB-LEDs",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// print sensors on stdout
cli.PrintRGBLEDs(cnf, os.Stdout)
},
}
func init() {
rgbLedCmd.AddCommand(listRgbLedCmd)
}

41
cmd/rgbled/remove.go Normal file
View File

@ -0,0 +1,41 @@
package rgbled
import (
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/spf13/cobra"
)
var removeRgbLedCmd = &cobra.Command{
Use: "remove",
Short: "Remove a RGB-LED",
Example: `flucky rgb-led remove <name/uuid>
flucky rgb-led remove my-led
flucky rgb-led remove 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
Aliases: []string{"rm"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// // add remote entry to list
err = cnf.RemoveRGBLED(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(removeRgbLedCmd)
}

40
cmd/rgbled/rename.go Normal file
View File

@ -0,0 +1,40 @@
package rgbled
import (
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/spf13/cobra"
)
var renameRgbLedCmd = &cobra.Command{
Use: "rename",
Short: "Rename a RGB-LED",
Args: cobra.ExactArgs(2),
Example: `flucky rgb-led disable <name/uuid> <new-name>
flucky rgb-led disable my-led my-sweet-led
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d my-sweet-led`,
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// rename sensor
err = cnf.RenameRGBLED(args[0], args[1])
if err != nil {
log.Println(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
rgbLedCmd.AddCommand(renameRgbLedCmd)
}

20
cmd/rgbled/rgbled.go Normal file
View File

@ -0,0 +1,20 @@
package rgbled
import (
"github.com/spf13/cobra"
)
var configFile string
var rgbLedCmd = &cobra.Command{
Use: "rgb-led",
Short: "Manage RGB-LEDs",
}
// InitCmd da
func InitCmd(cmd *cobra.Command, cnfFile string) {
configFile = cnfFile
cmd.AddCommand(rgbLedCmd)
}

View File

@ -20,7 +20,7 @@ var addSensorCmd = &cobra.Command{
Example: fmt.Sprintf("flucky sensor add indoor DHT11 GPIO14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20 GPIO14"),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
@ -48,13 +48,13 @@ var addSensorCmd = &cobra.Command{
}
// // add sensor entry to list
err = fc.AddSensor(sensor)
err = cnf.AddSensor(sensor)
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -15,19 +15,19 @@ var disableSensorCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// disable sensor entry to list
err = fc.DisableSensor(args[0])
err = cnf.DisableSensor(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -15,19 +15,19 @@ var enableSensorCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// disable sensor entry to list
err = fc.EnableSensor(args[0])
err = cnf.EnableSensor(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -15,19 +15,19 @@ var listSensorCmd = &cobra.Command{
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// print sensors on stdout
err = cli.PrintSensors(fc, os.Stdout)
err = cli.PrintSensors(cnf, os.Stdout)
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -15,19 +15,19 @@ var rmSensorCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// // add remote entry to list
err = fc.RemoveSensor(args[0])
err = cnf.RemoveSensor(args[0])
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -15,19 +15,19 @@ var renameSensorCmd = &cobra.Command{
Example: fmt.Sprintf("flucky sensor rename indoor outdoor\nflucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor"),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// rename sensor
err = fc.RenameSensor(args[0], args[1])
err = cnf.RenameSensor(args[0], args[1])
if err != nil {
log.Println(err)
}
// save new configuration
err = config.Write(fc, cfg)
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
)
var cfg string
var configFile string
var sensorCmd = &cobra.Command{
Use: "sensor",
@ -12,8 +12,8 @@ var sensorCmd = &cobra.Command{
}
// InitCmd da
func InitCmd(cmd *cobra.Command, config string) {
cfg = config
func InitCmd(cmd *cobra.Command, cnfFile string) {
configFile = cnfFile
cmd.AddCommand(sensorCmd)

View File

@ -18,7 +18,7 @@ var listTemperatureCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configPath)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -22,7 +22,7 @@ var readTemperatureCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configPath)
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)
var configPath string
var configFile string
var temperatureCmd = &cobra.Command{
Use: "temperature",
@ -15,8 +15,8 @@ var temperatureCmd = &cobra.Command{
}
// Execute a
func InitCmd(cmd *cobra.Command, cnfPath string) {
configPath = cnfPath
func InitCmd(cmd *cobra.Command, cnfFile string) {
configFile = cnfFile
cmd.AddCommand(temperatureCmd)
}