78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
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 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
|
|
gpioBlue, err := types.StringToGPIO(args[1])
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
gpioGreen, err := types.StringToGPIO(args[2])
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
gpioRed, 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,
|
|
ActionMapping: types.DefaultActionMapping,
|
|
BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{
|
|
types.BaseColorBlue: &gpioBlue,
|
|
types.BaseColorGreen: &gpioGreen,
|
|
types.BaseColorRed: &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")
|
|
}
|