feat(cmd/rgb-led): add subcommands to operate with rgb-leds
This commit is contained in:
parent
c81cd2d21c
commit
49d66cfcbb
17
cmd/cmd.go
17
cmd/cmd.go
@ -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()
|
||||
}
|
||||
|
@ -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")
|
||||
|
||||
|
@ -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
78
cmd/rgbled/add.go
Normal 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
42
cmd/rgbled/disable.go
Normal 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
41
cmd/rgbled/enable.go
Normal 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
30
cmd/rgbled/list.go
Normal 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
41
cmd/rgbled/remove.go
Normal 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
40
cmd/rgbled/rename.go
Normal 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
20
cmd/rgbled/rgbled.go
Normal 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)
|
||||
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -9,6 +9,7 @@ require (
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
github.com/spf13/cobra v0.0.3
|
||||
github.com/spf13/pflag v1.0.3 // indirect
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible
|
||||
github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -13,6 +13,8 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
|
||||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo=
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8=
|
||||
github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624 h1:bePzgtpuLSl+F9aacwuaquuoOyKfMKuJORq2CvPPJK4=
|
||||
github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624/go.mod h1:MqFju5qeLDFh+S9PqxYT7TEla8xeW7bgGr/69q3oki0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
|
@ -68,6 +68,21 @@ func PrintHumidities(humidities []*types.Humidity, cnf *config.Configuration, w
|
||||
tw.Flush()
|
||||
}
|
||||
|
||||
func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
|
||||
|
||||
// declare tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
// headline
|
||||
fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled")
|
||||
|
||||
for _, rgbled := range cnf.RGBLEDs {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", rgbled.RGBLEDName, rgbled.RGBLEDLocation, *rgbled.RGBLEDColorToGPIO[types.RGBLEDBlue], *rgbled.RGBLEDColorToGPIO[types.RGBLEDGreen], *rgbled.RGBLEDColorToGPIO[types.RGBLEDRed], rgbled.RGBLEDEnabled)
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
}
|
||||
|
||||
// PrintSensors displays a list with all configured sensors
|
||||
func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
||||
|
||||
|
@ -25,9 +25,79 @@ var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
||||
// Configuration of flucky
|
||||
type Configuration struct {
|
||||
Device *types.Device `json:"device"`
|
||||
LEDs []*types.LED `json:"leds"`
|
||||
RGBLEDs []*types.RGBLED `json:"rgb_leds"`
|
||||
Sensors []*types.Sensor `json:"sensors"`
|
||||
}
|
||||
|
||||
// AddLED add a new LED
|
||||
func (c *Configuration) AddLED(led *types.LED) error {
|
||||
|
||||
// check if LEDID is a valid UUID string
|
||||
if !validUUID.MatchString(led.LEDID) {
|
||||
led.LEDID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, l := range c.LEDs {
|
||||
if l.LEDName == led.LEDName {
|
||||
return fmt.Errorf("LED %v already exists", led.LEDName)
|
||||
}
|
||||
|
||||
if l.LEDID == led.LEDID {
|
||||
return fmt.Errorf("LED %v with UUID %v already exists", led.LEDName, led.LEDID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check if sensor has a valid device id
|
||||
if led.DeviceID != c.Device.DeviceID {
|
||||
led.DeviceID = c.Device.DeviceID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
led.CreationDate = time.Now()
|
||||
|
||||
// check
|
||||
c.LEDs = append(c.LEDs, led)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRGBLED add a new RGBLED
|
||||
func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error {
|
||||
|
||||
// check if RGBLEDID is a valid UUID string
|
||||
if !validUUID.MatchString(rgbLED.RGBLEDID) {
|
||||
rgbLED.RGBLEDID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, l := range c.RGBLEDs {
|
||||
if l.RGBLEDName == rgbLED.RGBLEDName {
|
||||
return fmt.Errorf("RGBLED %v already exists", rgbLED.RGBLEDName)
|
||||
}
|
||||
|
||||
if l.RGBLEDID == rgbLED.RGBLEDID {
|
||||
return fmt.Errorf("RGBLED %v with UUID %v already exists", rgbLED.RGBLEDName, rgbLED.RGBLEDID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check if sensor has a valid device id
|
||||
if rgbLED.DeviceID != c.Device.DeviceID {
|
||||
rgbLED.DeviceID = c.Device.DeviceID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
rgbLED.CreationDate = time.Now()
|
||||
|
||||
// check
|
||||
c.RGBLEDs = append(c.RGBLEDs, rgbLED)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddSensor add a new sensor
|
||||
func (c *Configuration) AddSensor(sensor *types.Sensor) error {
|
||||
|
||||
@ -68,6 +138,36 @@ func (c *Configuration) AddSensor(sensor *types.Sensor) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableRGBLED enables a rgb led by its name or its unique UUID
|
||||
func (c *Configuration) DisableRGBLED(name string) error {
|
||||
found := false
|
||||
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDName == name {
|
||||
rgbled.RGBLEDEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// disable sensor matched by uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDID == name {
|
||||
rgbled.RGBLEDEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found RGB-LED %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableSensor disables a sensor by its name or its unique UUID
|
||||
func (c *Configuration) DisableSensor(name string) error {
|
||||
found := false
|
||||
@ -98,6 +198,36 @@ func (c *Configuration) DisableSensor(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableRGBLED enables a rgb led by its name or its unique UUID
|
||||
func (c *Configuration) EnableRGBLED(name string) error {
|
||||
found := false
|
||||
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDName == name {
|
||||
rgbled.RGBLEDEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// disable sensor matched by uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDID == name {
|
||||
rgbled.RGBLEDEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found RGB-LED %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableSensor enables a sensor by its name or its unique UUID
|
||||
func (c *Configuration) EnableSensor(name string) error {
|
||||
found := false
|
||||
@ -228,6 +358,44 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Tem
|
||||
return c.convertTemperatureSensors(temperatureSensors)
|
||||
}
|
||||
|
||||
// RemoveLED deletes a LED by its name or its unique UUID
|
||||
func (c *Configuration) RemoveLED(name string) error {
|
||||
for i, led := range c.LEDs {
|
||||
// remove machted name
|
||||
if !validUUID.MatchString(name) &&
|
||||
led.LEDName == name {
|
||||
c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
led.LEDID == name {
|
||||
c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Can not find LED %v", name)
|
||||
}
|
||||
|
||||
// RemoveRGBLED deletes a LED by its name or its unique UUID
|
||||
func (c *Configuration) RemoveRGBLED(name string) error {
|
||||
for i, rgbLED := range c.RGBLEDs {
|
||||
// remove machted name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbLED.RGBLEDName == name {
|
||||
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbLED.RGBLEDID == name {
|
||||
c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Can not find RGBLED %v", name)
|
||||
}
|
||||
|
||||
// RemoveSensor deletes a sensor by its name or its unique UUID
|
||||
func (c *Configuration) RemoveSensor(name string) error {
|
||||
for i, sensor := range c.Sensors {
|
||||
@ -247,6 +415,18 @@ func (c *Configuration) RemoveSensor(name string) error {
|
||||
return fmt.Errorf("Can not find sensor %v", name)
|
||||
}
|
||||
|
||||
// RenameRGBLED renames a sensor identified by the name or the UUID
|
||||
func (c *Configuration) RenameRGBLED(oldName, newName string) error {
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
if rgbled.RGBLEDName == oldName ||
|
||||
rgbled.RGBLEDID == oldName {
|
||||
rgbled.RGBLEDName = newName
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Could not find rgb-led %v to replace into with %v", oldName, newName)
|
||||
}
|
||||
|
||||
// RenameSensor renames a sensor identified by the name or the UUID
|
||||
func (c *Configuration) RenameSensor(oldName, newName string) error {
|
||||
for _, sensor := range c.Sensors {
|
||||
|
71
pkg/led/led.go
Normal file
71
pkg/led/led.go
Normal file
@ -0,0 +1,71 @@
|
||||
package led
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/stianeikeland/go-rpio"
|
||||
)
|
||||
|
||||
type LED interface {
|
||||
On() error
|
||||
Off() error
|
||||
Toggel() error
|
||||
}
|
||||
|
||||
type RGBLED struct {
|
||||
*types.LED
|
||||
}
|
||||
|
||||
func (l *RGBLED) On() error {
|
||||
if err := rpio.Open(); err != nil {
|
||||
return fmt.Errorf("Can not open rpio: %v", err)
|
||||
}
|
||||
defer rpio.Close()
|
||||
|
||||
gpio, err := types.GPIOToInt(*l.GPIONumber)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
|
||||
}
|
||||
|
||||
pin := rpio.Pin(gpio)
|
||||
pin.High()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *RGBLED) Off() error {
|
||||
|
||||
if err := rpio.Open(); err != nil {
|
||||
return fmt.Errorf("Can not open rpio: %v", err)
|
||||
}
|
||||
defer rpio.Close()
|
||||
|
||||
gpio, err := types.GPIOToInt(*l.GPIONumber)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
|
||||
}
|
||||
|
||||
pin := rpio.Pin(gpio)
|
||||
pin.Low()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *RGBLED) Toggel() error {
|
||||
|
||||
if err := rpio.Open(); err != nil {
|
||||
return fmt.Errorf("Can not open rpio: %v", err)
|
||||
}
|
||||
defer rpio.Close()
|
||||
|
||||
gpio, err := types.GPIOToInt(*l.GPIONumber)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not determine %v into integer: %v", l.GPIONumber, err)
|
||||
}
|
||||
|
||||
pin := rpio.Pin(gpio)
|
||||
pin.Toggle()
|
||||
|
||||
return nil
|
||||
}
|
@ -91,6 +91,63 @@ func GPIOToString(gpio GPIO) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func GPIOToInt(gpio GPIO) (int, error) {
|
||||
switch gpio {
|
||||
case GPIO02:
|
||||
return 2, nil
|
||||
case GPIO03:
|
||||
return 3, nil
|
||||
case GPIO04:
|
||||
return 4, nil
|
||||
case GPIO05:
|
||||
return 5, nil
|
||||
case GPIO06:
|
||||
return 6, nil
|
||||
case GPIO07:
|
||||
return 7, nil
|
||||
case GPIO08:
|
||||
return 8, nil
|
||||
case GPIO10:
|
||||
return 10, nil
|
||||
case GPIO11:
|
||||
return 11, nil
|
||||
case GPIO12:
|
||||
return 12, nil
|
||||
case GPIO13:
|
||||
return 13, nil
|
||||
case GPIO14:
|
||||
return 14, nil
|
||||
case GPIO15:
|
||||
return 15, nil
|
||||
case GPIO16:
|
||||
return 16, nil
|
||||
case GPIO17:
|
||||
return 17, nil
|
||||
case GPIO18:
|
||||
return 18, nil
|
||||
case GPIO19:
|
||||
return 19, nil
|
||||
case GPIO20:
|
||||
return 20, nil
|
||||
case GPIO21:
|
||||
return 21, nil
|
||||
case GPIO22:
|
||||
return 22, nil
|
||||
case GPIO23:
|
||||
return 23, nil
|
||||
case GPIO24:
|
||||
return 24, nil
|
||||
case GPIO25:
|
||||
return 25, nil
|
||||
case GPIO26:
|
||||
return 26, nil
|
||||
case GPIO27:
|
||||
return 27, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("Can not determine gpio %v", gpio)
|
||||
}
|
||||
}
|
||||
|
||||
func StringToGPIO(gpio string) (GPIO, error) {
|
||||
switch gpio {
|
||||
case "GPIO02":
|
||||
|
52
pkg/types/led.go
Normal file
52
pkg/types/led.go
Normal file
@ -0,0 +1,52 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
|
||||
type LED struct {
|
||||
LEDID string `json:"led_id"`
|
||||
LEDName string `json:"led_name"`
|
||||
LEDLocation string `json:"led_location"`
|
||||
GPIONumber *GPIO `json:"gpio_number"`
|
||||
LEDEnabled bool `json:"led_enabled"`
|
||||
LEDColor *LEDColor `json:"led_color"`
|
||||
DeviceID string `json:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
type RGBLED struct {
|
||||
RGBLEDID string `json:"rgbled_id"`
|
||||
RGBLEDName string `json:"rgbled_name"`
|
||||
RGBLEDLocation string `json:"rgbled_location"`
|
||||
RGBLEDColorToGPIO map[RGBColor]*GPIO `json:"color_to_gpio"`
|
||||
RGBLEDEnabled bool `json:"rgbled_enabled"`
|
||||
DeviceID string `json:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
type RGBColor string
|
||||
|
||||
const (
|
||||
RGBLEDBlue RGBColor = "blue"
|
||||
RGBLEDRed = "red"
|
||||
RGBLEDGreen = "green"
|
||||
)
|
||||
|
||||
type LEDColor string
|
||||
|
||||
const (
|
||||
LEDBlue LEDColor = "blue"
|
||||
LEDRed = "red"
|
||||
LEDGreen = "green"
|
||||
LEDPurple = "purple"
|
||||
LEDTurquoiseGravel = "turquoise gravel"
|
||||
LEDYellow = "yellow"
|
||||
LEDWhite = "white"
|
||||
)
|
||||
|
||||
type LEDOption string
|
||||
|
||||
const (
|
||||
LEDError LEDOption = "error"
|
||||
LEDWarn = "warn"
|
||||
LEDOk = "ok"
|
||||
)
|
Loading…
Reference in New Issue
Block a user