fix(pkg/rgbled): use action maps to turn on/off led colors

This commit is contained in:
Markus Pesch 2019-06-24 22:57:29 +02:00
parent 02fa0cc931
commit c8a1dd2935
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
9 changed files with 284 additions and 157 deletions

View File

@ -47,6 +47,7 @@ flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
RGBLEDName: args[0], RGBLEDName: args[0],
RGBLEDLocation: location, RGBLEDLocation: location,
RGBLEDEnabled: enabled, RGBLEDEnabled: enabled,
ActionMapping: types.DefaultActionMapping,
BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{ BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{
types.BaseColorBlue: &gpioBlue, types.BaseColorBlue: &gpioBlue,
types.BaseColorGreen: &gpioGreen, types.BaseColorGreen: &gpioGreen,

View File

@ -32,7 +32,7 @@ flucky rgb-led on 1c5b9424-f6e9-4a37-be5c-77e531e94aab red`,
rgbLEDs = cnf.GetRGBLEDs(config.ENABLED) rgbLEDs = cnf.GetRGBLEDs(config.ENABLED)
} }
color, err := types.StrintToLEDColor(args[len(args)-1]) color, err := types.StringToLEDColor(args[len(args)-1])
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -184,10 +184,16 @@ func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
// headline // headline
fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled") fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled\taction")
for _, rgbled := range cnf.RGBLEDs { 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.BaseColorsToGPIO[types.BaseColorBlue], *rgbled.BaseColorsToGPIO[types.BaseColorGreen], *rgbled.BaseColorsToGPIO[types.BaseColorRed], rgbled.RGBLEDEnabled) fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t", rgbled.RGBLEDName, rgbled.RGBLEDLocation, *rgbled.BaseColorsToGPIO[types.BaseColorBlue], *rgbled.BaseColorsToGPIO[types.BaseColorGreen], *rgbled.BaseColorsToGPIO[types.BaseColorRed], rgbled.RGBLEDEnabled)
for action, color := range rgbled.ActionMapping {
fmt.Fprintf(tw, "%v=%v,", action, color)
}
fmt.Fprintf(tw, "\n")
} }
tw.Flush() tw.Flush()

View File

@ -26,45 +26,10 @@ var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
// Configuration of flucky // Configuration of flucky
type Configuration struct { type Configuration struct {
Device *types.Device `json:"device"` Device *types.Device `json:"device"`
LEDs []*types.LED `json:"leds"`
RGBLEDs []*types.RGBLED `json:"rgb_leds"` RGBLEDs []*types.RGBLED `json:"rgb_leds"`
Sensors []*types.Sensor `json:"sensors"` 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 // AddRGBLED add a new RGBLED
func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error { func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error {
@ -404,25 +369,6 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Tem
return c.convertTemperatureSensors(temperatureSensors) 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 // RemoveRGBLED deletes a LED by its name or its unique UUID
func (c *Configuration) RemoveRGBLED(name string) error { func (c *Configuration) RemoveRGBLED(name string) error {
for i, rgbLED := range c.RGBLEDs { for i, rgbLED := range c.RGBLEDs {
@ -435,7 +381,7 @@ func (c *Configuration) RemoveRGBLED(name string) error {
// remove machted uuid // remove machted uuid
if validUUID.MatchString(name) && if validUUID.MatchString(name) &&
rgbLED.RGBLEDID == name { rgbLED.RGBLEDID == name {
c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
return nil return nil
} }
} }
@ -507,7 +453,7 @@ func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED
leds := make([]rgbled.RGBLED, 0) leds := make([]rgbled.RGBLED, 0)
for _, rgbLED := range rgbLEDs { for _, rgbLED := range rgbLEDs {
leds = append(leds, &rgbled.LED{ leds = append(leds, &rgbled.DefaultRGBLED{
RGBLED: rgbLED, RGBLED: rgbLED,
}) })
} }

View File

@ -47,7 +47,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
for { for {
err := rgbled.Green(rgbLEDs) err := rgbled.Run(rgbLEDs)
if err != nil { if err != nil {
logger.Error("Can not turn on green info light: %v", err) logger.Error("Can not turn on green info light: %v", err)
} }
@ -57,7 +57,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
logger.Error("%v", err) logger.Error("%v", err)
err = rgbled.Red(rgbLEDs) err = rgbled.Error(rgbLEDs)
if err != nil { if err != nil {
logger.Error("Can not turn on red info light: %v", err) logger.Error("Can not turn on red info light: %v", err)
} }
@ -65,7 +65,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
case <-ticker: case <-ticker:
err := rgbled.Blue(rgbLEDs) err := rgbled.Logfile(rgbLEDs)
if err != nil { if err != nil {
logger.Error("Can not turn on blue info light: %v", err) logger.Error("Can not turn on blue info light: %v", err)
} }
@ -73,7 +73,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
err = logfile.AppendTemperatures(temperatureLogfile, compression, temperatureCache) err = logfile.AppendTemperatures(temperatureLogfile, compression, temperatureCache)
if err != nil { if err != nil {
err = rgbled.Red(rgbLEDs) err = rgbled.Error(rgbLEDs)
if err != nil { if err != nil {
logger.Error("Can not turn on red info light: %v", err) logger.Error("Can not turn on red info light: %v", err)
} }
@ -91,7 +91,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
cancel() cancel()
err := rgbled.Red(rgbLEDs) err := rgbled.Error(rgbLEDs)
if err != nil { if err != nil {
logger.Error("Can not turn on red info light: %v", err) logger.Error("Can not turn on red info light: %v", err)
} }

View File

@ -1,12 +1,25 @@
package rgbled package rgbled
type RGBLED interface { type LED interface {
Blue(on bool) error On() error
Green(on bool) error Off() error
}
type RGBLED interface {
Blue() error
Green() error
Purple() error
Red() error
Turquoise() error
White() error
Yellow() error
Error() error
Logfile() error
Run() error
Sync() error
Warn() error
On() error
Off() error Off() error
Purple(on bool) error
Red(on bool) error
Turquoise(on bool) error
White(on bool) error
Yellow(on bool) error
} }

View File

@ -7,37 +7,37 @@ import (
"github.com/stianeikeland/go-rpio" "github.com/stianeikeland/go-rpio"
) )
type LED struct { type DefaultRGBLED struct {
*types.RGBLED *types.RGBLED
} }
func (l *LED) Blue(on bool) error { func (rgbled *DefaultRGBLED) Blue() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorBlue],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) Green(on bool) error { func (rgbled *DefaultRGBLED) Green() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorGreen],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
@ -45,107 +45,131 @@ func (l *LED) Green(on bool) error {
} }
func (l *LED) Off() error { func (rgbled *DefaultRGBLED) Off() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorBlue],
l.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorGreen],
l.BaseColorsToGPIO[types.BaseColorRed], rgbled.BaseColorsToGPIO[types.BaseColorRed],
} }
if err := l.operate(gpios, false); err != nil { if err := rgbled.switchColors(gpios, false); err != nil {
return fmt.Errorf("Can not turn GPIOs off: %v", err) return fmt.Errorf("Can not turn GPIOs off: %v", err)
} }
return nil return nil
} }
func (l *LED) Purple(on bool) error { func (rgbled *DefaultRGBLED) On() error {
gpios := []*types.GPIO{ return rgbled.White()
l.BaseColorsToGPIO[types.BaseColorBlue],
l.BaseColorsToGPIO[types.BaseColorRed],
} }
if err := l.Off(); err != nil { func (rgbled *DefaultRGBLED) Purple() error {
gpios := []*types.GPIO{
rgbled.BaseColorsToGPIO[types.BaseColorBlue],
rgbled.BaseColorsToGPIO[types.BaseColorRed],
}
if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) Red(on bool) error { func (rgbled *DefaultRGBLED) Red() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorRed], rgbled.BaseColorsToGPIO[types.BaseColorRed],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) Turquoise(on bool) error { func (rgbled *DefaultRGBLED) Turquoise() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorBlue],
l.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorGreen],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) White(on bool) error { func (rgbled *DefaultRGBLED) White() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorBlue], rgbled.BaseColorsToGPIO[types.BaseColorBlue],
l.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorGreen],
l.BaseColorsToGPIO[types.BaseColorRed], rgbled.BaseColorsToGPIO[types.BaseColorRed],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) Yellow(on bool) error { func (rgbled *DefaultRGBLED) Yellow() error {
gpios := []*types.GPIO{ gpios := []*types.GPIO{
l.BaseColorsToGPIO[types.BaseColorGreen], rgbled.BaseColorsToGPIO[types.BaseColorGreen],
l.BaseColorsToGPIO[types.BaseColorRed], rgbled.BaseColorsToGPIO[types.BaseColorRed],
} }
if err := l.Off(); err != nil { if err := rgbled.Off(); err != nil {
return err return err
} }
if err := l.operate(gpios, on); err != nil { if err := rgbled.switchColors(gpios, true); err != nil {
return fmt.Errorf("Can not operate with GPIOs: %v", err) return fmt.Errorf("Can not operate with GPIOs: %v", err)
} }
return nil return nil
} }
func (l *LED) operate(gpios []*types.GPIO, on bool) error { func (rgbled *DefaultRGBLED) Error() error {
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionError])
}
func (rgbled *DefaultRGBLED) Logfile() error {
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionLogfile])
}
func (rgbled *DefaultRGBLED) Sync() error {
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionSync])
}
func (rgbled *DefaultRGBLED) Warn() error {
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionWarn])
}
func (rgbled *DefaultRGBLED) Run() error {
return rgbled.switchColorBasedOnAction(rgbled.ActionMapping[types.LEDActionRun])
}
func (rgbled *DefaultRGBLED) switchColors(gpios []*types.GPIO, on bool) error {
if err := rpio.Open(); err != nil { if err := rpio.Open(); err != nil {
return fmt.Errorf("Cam not open rpio connection: %v", err) return fmt.Errorf("Cam not open rpio connection: %v", err)
} }
@ -167,3 +191,26 @@ func (l *LED) operate(gpios []*types.GPIO, on bool) error {
} }
return nil return nil
} }
func (rgbled *DefaultRGBLED) switchColorBasedOnAction(action types.LEDColor) error {
switch action {
case types.LEDColorBlue:
return rgbled.Blue()
case types.LEDColorGreen:
return rgbled.Green()
case types.LEDColorNone:
return rgbled.Off()
case types.LEDColorPurple:
return rgbled.Purple()
case types.LEDColorRed:
return rgbled.Red()
case types.LEDColorTurquoise:
return rgbled.Turquoise()
case types.LEDColorWhite:
return rgbled.White()
case types.LEDColorYellow:
return rgbled.Yellow()
default:
return rgbled.Off()
}
}

View File

@ -11,7 +11,7 @@ import (
// Blue makes all RGB-LEDs which are passed as parameters light up blue. // Blue makes all RGB-LEDs which are passed as parameters light up blue.
func Blue(rgbLEDs []RGBLED) error { func Blue(rgbLEDs []RGBLED) error {
color := types.LEDColorBlue color := types.LEDColorBlue
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -20,7 +20,7 @@ func Blue(rgbLEDs []RGBLED) error {
// CustomColor makes all RGB-LEDs which are passed as parameters light up in // CustomColor makes all RGB-LEDs which are passed as parameters light up in
// custom color. // custom color.
func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error { func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error {
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -29,7 +29,7 @@ func CustomColor(rgbLEDs []RGBLED, color types.LEDColor) error {
// Green makes all RGB-LEDs which are passed as parameters light up in green. // Green makes all RGB-LEDs which are passed as parameters light up in green.
func Green(rgbLEDs []RGBLED) error { func Green(rgbLEDs []RGBLED) error {
color := types.LEDColorGreen color := types.LEDColorGreen
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -38,7 +38,7 @@ func Green(rgbLEDs []RGBLED) error {
// Off turns all RGB-LEDs which are passes as parameters off. // Off turns all RGB-LEDs which are passes as parameters off.
func Off(rgbLEDs []RGBLED) error { func Off(rgbLEDs []RGBLED) error {
color := types.LEDColorNone color := types.LEDColorNone
if err := operate(rgbLEDs, false, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -47,7 +47,7 @@ func Off(rgbLEDs []RGBLED) error {
// Purple makes all RGB-LEDs which are passed as parameters light up in purple. // Purple makes all RGB-LEDs which are passed as parameters light up in purple.
func Purple(rgbLEDs []RGBLED) error { func Purple(rgbLEDs []RGBLED) error {
color := types.LEDColorPurple color := types.LEDColorPurple
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -56,7 +56,7 @@ func Purple(rgbLEDs []RGBLED) error {
// Red makes all RGB-LEDs which are passed as parameters light up in red. // Red makes all RGB-LEDs which are passed as parameters light up in red.
func Red(rgbLEDs []RGBLED) error { func Red(rgbLEDs []RGBLED) error {
color := types.LEDColorRed color := types.LEDColorRed
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -66,7 +66,7 @@ func Red(rgbLEDs []RGBLED) error {
// turquoise. // turquoise.
func Turquoise(rgbLEDs []RGBLED) error { func Turquoise(rgbLEDs []RGBLED) error {
color := types.LEDColorTurquoise color := types.LEDColorTurquoise
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -75,7 +75,7 @@ func Turquoise(rgbLEDs []RGBLED) error {
// White makes all RGB-LEDs which are passed as parameters light up in white. // White makes all RGB-LEDs which are passed as parameters light up in white.
func White(rgbLEDs []RGBLED) error { func White(rgbLEDs []RGBLED) error {
color := types.LEDColorWhite color := types.LEDColorWhite
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
@ -84,13 +84,13 @@ func White(rgbLEDs []RGBLED) error {
// Yellow makes all RGB-LEDs which are passed as parameters light up in yellow. // Yellow makes all RGB-LEDs which are passed as parameters light up in yellow.
func Yellow(rgbLEDs []RGBLED) error { func Yellow(rgbLEDs []RGBLED) error {
color := types.LEDColorYellow color := types.LEDColorYellow
if err := operate(rgbLEDs, true, color); err != nil { if err := operate(rgbLEDs, color); err != nil {
return err return err
} }
return nil return nil
} }
func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error { func operate(rgbLEDs []RGBLED, color types.LEDColor) error {
errorChannel := make(chan error, len(rgbLEDs)) errorChannel := make(chan error, len(rgbLEDs))
@ -98,28 +98,28 @@ func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error {
wg.Add(len(rgbLEDs)) wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs { for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, color types.LEDColor, on bool, errorChannel chan<- error, wg *sync.WaitGroup) { go func(rgbLED RGBLED, color types.LEDColor, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
var err error var err error
switch color { switch color {
case types.LEDColorBlue: case types.LEDColorBlue:
err = rgbLED.Blue(on) err = rgbLED.Blue()
case types.LEDColorGreen: case types.LEDColorGreen:
err = rgbLED.Green(on) err = rgbLED.Green()
case types.LEDColorPurple: case types.LEDColorPurple:
err = rgbLED.Purple(on) err = rgbLED.Purple()
case types.LEDColorNone: case types.LEDColorNone:
err = rgbLED.Off() err = rgbLED.Off()
case types.LEDColorRed: case types.LEDColorRed:
err = rgbLED.Red(on) err = rgbLED.Red()
case types.LEDColorTurquoise: case types.LEDColorTurquoise:
err = rgbLED.Turquoise(on) err = rgbLED.Turquoise()
case types.LEDColorWhite: case types.LEDColorWhite:
err = rgbLED.White(on) err = rgbLED.White()
case types.LEDColorYellow: case types.LEDColorYellow:
err = rgbLED.Yellow(on) err = rgbLED.Yellow()
default: default:
err = rgbLED.Off() err = rgbLED.Off()
} }
@ -127,7 +127,132 @@ func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error {
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
} }
}(rgbLED, color, on, errorChannel, wg) }(rgbLED, color, errorChannel, wg)
}
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func Error(rgbLEDs []RGBLED) error {
errorChannel := make(chan error, len(rgbLEDs))
wg := new(sync.WaitGroup)
wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
err := rgbLED.Error()
if err != nil {
errorChannel <- err
}
}(rgbLED, errorChannel, wg)
}
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func Logfile(rgbLEDs []RGBLED) error {
errorChannel := make(chan error, len(rgbLEDs))
wg := new(sync.WaitGroup)
wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
err := rgbLED.Logfile()
if err != nil {
errorChannel <- err
}
}(rgbLED, errorChannel, wg)
}
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func Run(rgbLEDs []RGBLED) error {
errorChannel := make(chan error, len(rgbLEDs))
wg := new(sync.WaitGroup)
wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
err := rgbLED.Run()
if err != nil {
errorChannel <- err
}
}(rgbLED, errorChannel, wg)
}
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func Sync(rgbLEDs []RGBLED) error {
errorChannel := make(chan error, len(rgbLEDs))
wg := new(sync.WaitGroup)
wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
err := rgbLED.Sync()
if err != nil {
errorChannel <- err
}
}(rgbLED, errorChannel, wg)
}
wg.Wait()
errors := collect.Errors(errorChannel)
if len(errors) > 0 {
return prittyprint.FormatErrors(errors)
}
return nil
}
func Warn(rgbLEDs []RGBLED) error {
errorChannel := make(chan error, len(rgbLEDs))
wg := new(sync.WaitGroup)
wg.Add(len(rgbLEDs))
for _, rgbLED := range rgbLEDs {
go func(rgbLED RGBLED, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
err := rgbLED.Warn()
if err != nil {
errorChannel <- err
}
}(rgbLED, errorChannel, wg)
} }
wg.Wait() wg.Wait()

View File

@ -5,23 +5,12 @@ import (
"time" "time"
) )
type LED struct {
LEDID string `json:"led_id" xml:"led_id"`
LEDName string `json:"led_name" xml:"led_name"`
LEDLocation string `json:"led_location" xml:"led_location"`
GPIONumber *GPIO `json:"gpio_number" xml:"gpio_number"`
LEDEnabled bool `json:"led_enabled" xml:"led_enabled"`
LEDColor *LEDColor `json:"led_color" xml:"led_color"`
DeviceID string `json:"device_id" xml:"device_id"`
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
}
type RGBLED struct { type RGBLED struct {
RGBLEDID string `json:"rgbled_id" xml:"rgbled_id"` RGBLEDID string `json:"rgbled_id" xml:"rgbled_id"`
RGBLEDName string `json:"rgbled_name" xml:"rgbled_name"` RGBLEDName string `json:"rgbled_name" xml:"rgbled_name"`
RGBLEDLocation string `json:"rgbled_location" xml:"rgb_location"` RGBLEDLocation string `json:"rgbled_location" xml:"rgb_location"`
BaseColorsToGPIO map[BaseColor]*GPIO `json:"color_to_gpio" xml:"color_to_gpio"` BaseColorsToGPIO map[BaseColor]*GPIO `json:"base_colors_to_gpio" xml:"base_colors_to_gpio"`
ActionMapping map[LEDOption]LEDColor `json:"action_mapping" xml:"action_mapping"` ActionMapping map[LEDAction]LEDColor `json:"action_mapping" xml:"action_mapping"`
RGBLEDEnabled bool `json:"rgbled_enabled" xml:"rgb_enabled"` RGBLEDEnabled bool `json:"rgbled_enabled" xml:"rgb_enabled"`
DeviceID string `json:"device_id" xml:"device_id"` DeviceID string `json:"device_id" xml:"device_id"`
CreationDate time.Time `json:"creation_date" xml:"creation_date"` CreationDate time.Time `json:"creation_date" xml:"creation_date"`
@ -48,7 +37,7 @@ const (
LEDColorWhite LEDColor = "white" LEDColorWhite LEDColor = "white"
) )
func StrintToLEDColor(color string) (LEDColor, error) { func StringToLEDColor(color string) (LEDColor, error) {
switch color { switch color {
case "blue": case "blue":
return LEDColorBlue, nil return LEDColorBlue, nil
@ -71,20 +60,20 @@ func StrintToLEDColor(color string) (LEDColor, error) {
} }
} }
type LEDOption string type LEDAction string
const ( const (
LEDError LEDOption = "error" LEDActionError LEDAction = "error"
LEDWarn = "warn" LEDActionWarn = "warn"
LEDOk = "ok" LEDActionRun = "run"
LEDSync = "sync" LEDActionSync = "sync"
LEDLogfile = "logfile" LEDActionLogfile = "logfile"
) )
var DefaultActionMapping = map[LEDOption]LEDColor{ var DefaultActionMapping = map[LEDAction]LEDColor{
LEDError: LEDColorRed, LEDActionError: LEDColorRed,
LEDWarn: LEDColorYellow, LEDActionWarn: LEDColorYellow,
LEDOk: LEDColorGreen, LEDActionRun: LEDColorGreen,
LEDSync: LEDColorTurquoise, LEDActionSync: LEDColorTurquoise,
LEDLogfile: LEDColorBlue, LEDActionLogfile: LEDColorBlue,
} }