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