From fe19a038ae222434dc7fc67a2d880cbfa04416c5 Mon Sep 17 00:00:00 2001 From: iGoX Date: Thu, 26 Mar 2026 09:34:38 +0100 Subject: [PATCH] Fix `POST /api/color` param checks --- ESP32/main.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/ESP32/main.py b/ESP32/main.py index aefe8e8..5edb8ca 100644 --- a/ESP32/main.py +++ b/ESP32/main.py @@ -86,6 +86,7 @@ class LedController: async def _off(self): self.color = (0, 0, 0) + self.previousStatus = self.status self.status = "off" self._write(self.color) @@ -121,7 +122,7 @@ class LedController: self.status = self.previousStatus self._write(self.color) - async def send(self, cmd): + async def sendCommand(self, cmd): self._cmd = cmd self._flag.set() @@ -129,8 +130,7 @@ class LedController: async def set_color(self, color, brightness=None): if brightness is not None: self.brightness = brightness - - await self.send({ + await self.sendCommand({ "type": "solid", "color": color }) @@ -142,16 +142,19 @@ class LedController: if color is None: raise ValueError("invalid status") - await self.send({ + await self.sendCommand({ "type": "solid", "color": color, "status": status.lower() }) + + async def get_status(self): + return self.status async def blink(self, frequency, duration): self.previousStatus = self.status - await self.send({ + await self.sendCommand({ "type": "blink", "color": self.color, "frequency": frequency, @@ -160,7 +163,7 @@ class LedController: }) async def stop(self): - await self.send({"type": "off"}) + await self.sendCommand({"type": "off"}) def get_state(self): if self.blinking and self.blinkDuration > 0: @@ -226,7 +229,6 @@ async def setBrightness(request): await led.set_color(current_color, brightness) led.status = current_status - return {'brightness': led.brightness} @@ -235,6 +237,7 @@ async def setColor(request): r = request.json.get("r") g = request.json.get("g") b = request.json.get("b") + brightness = request.json.get("brightness") if r is None or g is None or b is None: 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): 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) + status = await led.get_status() - return {'status': led.status} + return {'status': status} @app.get('/api/color') @@ -260,7 +269,8 @@ async def getColor(request): @app.get('/api/status') async def getStatus(request): - return {'status': led.status} + status = await led.get_status() + return {'status': status} @app.route('/api/status/', methods=['GET', 'POST']) @@ -270,7 +280,8 @@ async def setStatus(request, status): except ValueError: return {'error': 'unknown status'}, 404 - return {'status': led.status} + status = await led.get_status() + return {'status': status} @app.post('/api/blink') @@ -307,7 +318,8 @@ async def getBlink(request): async def blinkStop(request): await led.stop() await led.set_status(led.previousStatus) - return {'status': led.status} + status = await led.get_status() + return {'status': status} @app.get('/api/debug') @@ -367,4 +379,4 @@ async def main(): await app.start_server(port=80) -asyncio.run(main()) \ No newline at end of file +asyncio.run(main())