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

This commit is contained in:
2019-06-24 22:57:29 +02:00
parent 02fa0cc931
commit c8a1dd2935
9 changed files with 284 additions and 157 deletions

View File

@ -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,
})
}