From c8a1dd2935110e88df97f066aacafdc085200d09 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 24 Jun 2019 22:57:29 +0200 Subject: [PATCH] fix(pkg/rgbled): use action maps to turn on/off led colors --- cmd/rgbled/add.go | 1 + cmd/rgbled/on.go | 2 +- pkg/cli/cli.go | 10 ++- pkg/config/flucky.go | 58 +------------- pkg/daemon/daemon.go | 10 +-- pkg/rgbled/interfaces.go | 29 +++++-- pkg/rgbled/led.go | 127 ++++++++++++++++++++---------- pkg/rgbled/rgbled.go | 163 ++++++++++++++++++++++++++++++++++----- pkg/types/led.go | 41 ++++------ 9 files changed, 284 insertions(+), 157 deletions(-) diff --git a/cmd/rgbled/add.go b/cmd/rgbled/add.go index 43f5418..2c07f50 100644 --- a/cmd/rgbled/add.go +++ b/cmd/rgbled/add.go @@ -47,6 +47,7 @@ flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`), RGBLEDName: args[0], RGBLEDLocation: location, RGBLEDEnabled: enabled, + ActionMapping: types.DefaultActionMapping, BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{ types.BaseColorBlue: &gpioBlue, types.BaseColorGreen: &gpioGreen, diff --git a/cmd/rgbled/on.go b/cmd/rgbled/on.go index 34b5b22..4c7a0e5 100644 --- a/cmd/rgbled/on.go +++ b/cmd/rgbled/on.go @@ -32,7 +32,7 @@ flucky rgb-led on 1c5b9424-f6e9-4a37-be5c-77e531e94aab red`, rgbLEDs = cnf.GetRGBLEDs(config.ENABLED) } - color, err := types.StrintToLEDColor(args[len(args)-1]) + color, err := types.StringToLEDColor(args[len(args)-1]) if err != nil { log.Fatalln(err) } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 6505aa1..9b13313 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -184,10 +184,16 @@ func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) { tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) // 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 { - 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() diff --git a/pkg/config/flucky.go b/pkg/config/flucky.go index 267d7f3..7edaae4 100644 --- a/pkg/config/flucky.go +++ b/pkg/config/flucky.go @@ -26,45 +26,10 @@ 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 { @@ -404,25 +369,6 @@ 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 { @@ -435,7 +381,7 @@ func (c *Configuration) RemoveRGBLED(name string) error { // remove machted uuid if validUUID.MatchString(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 } } @@ -507,7 +453,7 @@ func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED leds := make([]rgbled.RGBLED, 0) for _, rgbLED := range rgbLEDs { - leds = append(leds, &rgbled.LED{ + leds = append(leds, &rgbled.DefaultRGBLED{ RGBLED: rgbLED, }) } diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index dd2d518..3a1b458 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -47,7 +47,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress for { - err := rgbled.Green(rgbLEDs) + err := rgbled.Run(rgbLEDs) if err != nil { 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) - err = rgbled.Red(rgbLEDs) + err = rgbled.Error(rgbLEDs) if err != nil { 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) case <-ticker: - err := rgbled.Blue(rgbLEDs) + err := rgbled.Logfile(rgbLEDs) if err != nil { 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) if err != nil { - err = rgbled.Red(rgbLEDs) + err = rgbled.Error(rgbLEDs) if err != nil { 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() - err := rgbled.Red(rgbLEDs) + err := rgbled.Error(rgbLEDs) if err != nil { logger.Error("Can not turn on red info light: %v", err) } diff --git a/pkg/rgbled/interfaces.go b/pkg/rgbled/interfaces.go index b5eb08f..fe38dc1 100644 --- a/pkg/rgbled/interfaces.go +++ b/pkg/rgbled/interfaces.go @@ -1,12 +1,25 @@ package rgbled -type RGBLED interface { - Blue(on bool) error - Green(on bool) error +type LED interface { + On() 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 - Purple(on bool) error - Red(on bool) error - Turquoise(on bool) error - White(on bool) error - Yellow(on bool) error } diff --git a/pkg/rgbled/led.go b/pkg/rgbled/led.go index 3967c9f..b1fd9a2 100644 --- a/pkg/rgbled/led.go +++ b/pkg/rgbled/led.go @@ -7,37 +7,37 @@ import ( "github.com/stianeikeland/go-rpio" ) -type LED struct { +type DefaultRGBLED struct { *types.RGBLED } -func (l *LED) Blue(on bool) error { +func (rgbled *DefaultRGBLED) Blue() error { 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 } - 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 nil } -func (l *LED) Green(on bool) error { +func (rgbled *DefaultRGBLED) Green() error { 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 } - 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) } @@ -45,107 +45,131 @@ func (l *LED) Green(on bool) error { } -func (l *LED) Off() error { +func (rgbled *DefaultRGBLED) Off() error { gpios := []*types.GPIO{ - l.BaseColorsToGPIO[types.BaseColorBlue], - l.BaseColorsToGPIO[types.BaseColorGreen], - l.BaseColorsToGPIO[types.BaseColorRed], + rgbled.BaseColorsToGPIO[types.BaseColorBlue], + rgbled.BaseColorsToGPIO[types.BaseColorGreen], + 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 nil } -func (l *LED) Purple(on bool) error { +func (rgbled *DefaultRGBLED) On() error { + return rgbled.White() +} + +func (rgbled *DefaultRGBLED) Purple() error { gpios := []*types.GPIO{ - l.BaseColorsToGPIO[types.BaseColorBlue], - l.BaseColorsToGPIO[types.BaseColorRed], + rgbled.BaseColorsToGPIO[types.BaseColorBlue], + rgbled.BaseColorsToGPIO[types.BaseColorRed], } - if err := l.Off(); err != nil { + if err := rgbled.Off(); err != nil { 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 nil } -func (l *LED) Red(on bool) error { +func (rgbled *DefaultRGBLED) Red() error { 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 } - 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 nil } -func (l *LED) Turquoise(on bool) error { +func (rgbled *DefaultRGBLED) Turquoise() error { gpios := []*types.GPIO{ - l.BaseColorsToGPIO[types.BaseColorBlue], - l.BaseColorsToGPIO[types.BaseColorGreen], + rgbled.BaseColorsToGPIO[types.BaseColorBlue], + rgbled.BaseColorsToGPIO[types.BaseColorGreen], } - if err := l.Off(); err != nil { + if err := rgbled.Off(); err != nil { 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 nil } -func (l *LED) White(on bool) error { +func (rgbled *DefaultRGBLED) White() error { gpios := []*types.GPIO{ - l.BaseColorsToGPIO[types.BaseColorBlue], - l.BaseColorsToGPIO[types.BaseColorGreen], - l.BaseColorsToGPIO[types.BaseColorRed], + rgbled.BaseColorsToGPIO[types.BaseColorBlue], + rgbled.BaseColorsToGPIO[types.BaseColorGreen], + rgbled.BaseColorsToGPIO[types.BaseColorRed], } - if err := l.Off(); err != nil { + if err := rgbled.Off(); err != nil { 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 nil } -func (l *LED) Yellow(on bool) error { +func (rgbled *DefaultRGBLED) Yellow() error { gpios := []*types.GPIO{ - l.BaseColorsToGPIO[types.BaseColorGreen], - l.BaseColorsToGPIO[types.BaseColorRed], + rgbled.BaseColorsToGPIO[types.BaseColorGreen], + rgbled.BaseColorsToGPIO[types.BaseColorRed], } - if err := l.Off(); err != nil { + if err := rgbled.Off(); err != nil { 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 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 { 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 } + +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() + } +} diff --git a/pkg/rgbled/rgbled.go b/pkg/rgbled/rgbled.go index f05dabe..12a5f8b 100644 --- a/pkg/rgbled/rgbled.go +++ b/pkg/rgbled/rgbled.go @@ -11,7 +11,7 @@ import ( // Blue makes all RGB-LEDs which are passed as parameters light up blue. func Blue(rgbLEDs []RGBLED) error { color := types.LEDColorBlue - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } return nil @@ -20,7 +20,7 @@ func Blue(rgbLEDs []RGBLED) error { // CustomColor makes all RGB-LEDs which are passed as parameters light up in // custom color. 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 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. func Green(rgbLEDs []RGBLED) error { color := types.LEDColorGreen - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } return nil @@ -38,7 +38,7 @@ func Green(rgbLEDs []RGBLED) error { // Off turns all RGB-LEDs which are passes as parameters off. func Off(rgbLEDs []RGBLED) error { color := types.LEDColorNone - if err := operate(rgbLEDs, false, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } 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. func Purple(rgbLEDs []RGBLED) error { color := types.LEDColorPurple - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } 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. func Red(rgbLEDs []RGBLED) error { color := types.LEDColorRed - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } return nil @@ -66,7 +66,7 @@ func Red(rgbLEDs []RGBLED) error { // turquoise. func Turquoise(rgbLEDs []RGBLED) error { color := types.LEDColorTurquoise - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } 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. func White(rgbLEDs []RGBLED) error { color := types.LEDColorWhite - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } 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. func Yellow(rgbLEDs []RGBLED) error { color := types.LEDColorYellow - if err := operate(rgbLEDs, true, color); err != nil { + if err := operate(rgbLEDs, color); err != nil { return err } 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)) @@ -98,28 +98,28 @@ func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error { wg.Add(len(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() var err error switch color { case types.LEDColorBlue: - err = rgbLED.Blue(on) + err = rgbLED.Blue() case types.LEDColorGreen: - err = rgbLED.Green(on) + err = rgbLED.Green() case types.LEDColorPurple: - err = rgbLED.Purple(on) + err = rgbLED.Purple() case types.LEDColorNone: err = rgbLED.Off() case types.LEDColorRed: - err = rgbLED.Red(on) + err = rgbLED.Red() case types.LEDColorTurquoise: - err = rgbLED.Turquoise(on) + err = rgbLED.Turquoise() case types.LEDColorWhite: - err = rgbLED.White(on) + err = rgbLED.White() case types.LEDColorYellow: - err = rgbLED.Yellow(on) + err = rgbLED.Yellow() default: err = rgbLED.Off() } @@ -127,7 +127,132 @@ func operate(rgbLEDs []RGBLED, on bool, color types.LEDColor) error { if err != nil { 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() diff --git a/pkg/types/led.go b/pkg/types/led.go index 5405e40..58e5fa5 100644 --- a/pkg/types/led.go +++ b/pkg/types/led.go @@ -5,23 +5,12 @@ import ( "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 { RGBLEDID string `json:"rgbled_id" xml:"rgbled_id"` RGBLEDName string `json:"rgbled_name" xml:"rgbled_name"` RGBLEDLocation string `json:"rgbled_location" xml:"rgb_location"` - BaseColorsToGPIO map[BaseColor]*GPIO `json:"color_to_gpio" xml:"color_to_gpio"` - ActionMapping map[LEDOption]LEDColor `json:"action_mapping" xml:"action_mapping"` + BaseColorsToGPIO map[BaseColor]*GPIO `json:"base_colors_to_gpio" xml:"base_colors_to_gpio"` + ActionMapping map[LEDAction]LEDColor `json:"action_mapping" xml:"action_mapping"` RGBLEDEnabled bool `json:"rgbled_enabled" xml:"rgb_enabled"` DeviceID string `json:"device_id" xml:"device_id"` CreationDate time.Time `json:"creation_date" xml:"creation_date"` @@ -48,7 +37,7 @@ const ( LEDColorWhite LEDColor = "white" ) -func StrintToLEDColor(color string) (LEDColor, error) { +func StringToLEDColor(color string) (LEDColor, error) { switch color { case "blue": return LEDColorBlue, nil @@ -71,20 +60,20 @@ func StrintToLEDColor(color string) (LEDColor, error) { } } -type LEDOption string +type LEDAction string const ( - LEDError LEDOption = "error" - LEDWarn = "warn" - LEDOk = "ok" - LEDSync = "sync" - LEDLogfile = "logfile" + LEDActionError LEDAction = "error" + LEDActionWarn = "warn" + LEDActionRun = "run" + LEDActionSync = "sync" + LEDActionLogfile = "logfile" ) -var DefaultActionMapping = map[LEDOption]LEDColor{ - LEDError: LEDColorRed, - LEDWarn: LEDColorYellow, - LEDOk: LEDColorGreen, - LEDSync: LEDColorTurquoise, - LEDLogfile: LEDColorBlue, +var DefaultActionMapping = map[LEDAction]LEDColor{ + LEDActionError: LEDColorRed, + LEDActionWarn: LEDColorYellow, + LEDActionRun: LEDColorGreen, + LEDActionSync: LEDColorTurquoise, + LEDActionLogfile: LEDColorBlue, }