feat(cmd/rgb-led): add subcommands to operate with rgb-leds
This commit is contained in:
@ -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"
|
||||
)
|
Reference in New Issue
Block a user