Fix POST /api/color param checks

This commit is contained in:
2026-03-26 09:34:38 +01:00
parent 5361ca4db7
commit fe19a038ae
+25 -13
View File
@@ -86,6 +86,7 @@ class LedController:
async def _off(self): async def _off(self):
self.color = (0, 0, 0) self.color = (0, 0, 0)
self.previousStatus = self.status
self.status = "off" self.status = "off"
self._write(self.color) self._write(self.color)
@@ -121,7 +122,7 @@ class LedController:
self.status = self.previousStatus self.status = self.previousStatus
self._write(self.color) self._write(self.color)
async def send(self, cmd): async def sendCommand(self, cmd):
self._cmd = cmd self._cmd = cmd
self._flag.set() self._flag.set()
@@ -129,8 +130,7 @@ class LedController:
async def set_color(self, color, brightness=None): async def set_color(self, color, brightness=None):
if brightness is not None: if brightness is not None:
self.brightness = brightness self.brightness = brightness
await self.sendCommand({
await self.send({
"type": "solid", "type": "solid",
"color": color "color": color
}) })
@@ -142,16 +142,19 @@ class LedController:
if color is None: if color is None:
raise ValueError("invalid status") raise ValueError("invalid status")
await self.send({ await self.sendCommand({
"type": "solid", "type": "solid",
"color": color, "color": color,
"status": status.lower() "status": status.lower()
}) })
async def get_status(self):
return self.status
async def blink(self, frequency, duration): async def blink(self, frequency, duration):
self.previousStatus = self.status self.previousStatus = self.status
await self.send({ await self.sendCommand({
"type": "blink", "type": "blink",
"color": self.color, "color": self.color,
"frequency": frequency, "frequency": frequency,
@@ -160,7 +163,7 @@ class LedController:
}) })
async def stop(self): async def stop(self):
await self.send({"type": "off"}) await self.sendCommand({"type": "off"})
def get_state(self): def get_state(self):
if self.blinking and self.blinkDuration > 0: if self.blinking and self.blinkDuration > 0:
@@ -226,7 +229,6 @@ async def setBrightness(request):
await led.set_color(current_color, brightness) await led.set_color(current_color, brightness)
led.status = current_status led.status = current_status
return {'brightness': led.brightness} return {'brightness': led.brightness}
@@ -235,6 +237,7 @@ async def setColor(request):
r = request.json.get("r") r = request.json.get("r")
g = request.json.get("g") g = request.json.get("g")
b = request.json.get("b") b = request.json.get("b")
brightness = request.json.get("brightness")
if r is None or g is None or b is None: if r is None or g is None or b is None:
return {'error': 'missing color'}, 400 return {'error': 'missing color'}, 400
@@ -245,11 +248,17 @@ async def setColor(request):
if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255): if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255):
return {'error': 'color out of bound'}, 400 return {'error': 'color out of bound'}, 400
brightness = request.json.get("brightness") if brightness is not None:
if not isinstance(brightness, (int, float)):
return {'error': 'wrong brightness type'}, 400
if brightness < 0 or brightness > 1:
return {'error': 'brightness out of bound'}, 400
await led.set_color((r, g, b), brightness) await led.set_color((r, g, b), brightness)
status = await led.get_status()
return {'status': led.status} return {'status': status}
@app.get('/api/color') @app.get('/api/color')
@@ -260,7 +269,8 @@ async def getColor(request):
@app.get('/api/status') @app.get('/api/status')
async def getStatus(request): async def getStatus(request):
return {'status': led.status} status = await led.get_status()
return {'status': status}
@app.route('/api/status/<status>', methods=['GET', 'POST']) @app.route('/api/status/<status>', methods=['GET', 'POST'])
@@ -270,7 +280,8 @@ async def setStatus(request, status):
except ValueError: except ValueError:
return {'error': 'unknown status'}, 404 return {'error': 'unknown status'}, 404
return {'status': led.status} status = await led.get_status()
return {'status': status}
@app.post('/api/blink') @app.post('/api/blink')
@@ -307,7 +318,8 @@ async def getBlink(request):
async def blinkStop(request): async def blinkStop(request):
await led.stop() await led.stop()
await led.set_status(led.previousStatus) await led.set_status(led.previousStatus)
return {'status': led.status} status = await led.get_status()
return {'status': status}
@app.get('/api/debug') @app.get('/api/debug')
@@ -367,4 +379,4 @@ async def main():
await app.start_server(port=80) await app.start_server(port=80)
asyncio.run(main()) asyncio.run(main())