diff --git a/src/nenuscanner/leds.py b/src/nenuscanner/leds.py index f5eb9b0..81952b2 100644 --- a/src/nenuscanner/leds.py +++ b/src/nenuscanner/leds.py @@ -15,6 +15,8 @@ class Leds: def off(self): pass + + class GpioLed: @@ -65,7 +67,18 @@ class GpioLeds(Leds): def on(self): for led in self.leds: led.on() + + def enter(self): + return self.__enter__() + def exit(self,*args): + self.__exit__(*args) + + def get_by_uuid(self, uuid: int) -> GpioLed: + for led in self.leds: + if led.gpio_pin == uuid: + return led + raise ValueError(f"No LED with UUID {uuid}") class DummyLed: def __init__(self, gpio_pin: int): @@ -110,10 +123,22 @@ class DummyLeds(Leds): def on(self): for led in self.leds: led.on() + + def enter(self): + return self.__enter__() + + def exit(self,*args): + self.__exit__(*args) + + def get_by_uuid(self, uuid: int) -> DummyLed: + for led in self.leds: + if led.gpio_pin == uuid: + return led + raise ValueError(f"No LED with UUID {uuid}") _leds = GpioLeds(config.GPIO_CHIP, config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS) def get() -> Leds: - return _leds + return _leds \ No newline at end of file diff --git a/src/nenuscanner/routes/leds.py b/src/nenuscanner/routes/leds.py index 16578f3..e8ceab2 100644 --- a/src/nenuscanner/routes/leds.py +++ b/src/nenuscanner/routes/leds.py @@ -1,13 +1,45 @@ -from flask import Blueprint, render_template, request, send_file, jsonify +from flask import Blueprint, render_template, request, send_file, jsonify, session, current_app import json import subprocess from .. import camera as C -from .. import leds - +from .. import leds,config blueprint = Blueprint('leds', __name__) +# WARNING: This is a temporary global variable to hold the state of the GPIO LEDs. +# This is necessary because the LED state must persist across multiple requests, +# and Flask does not maintain state between requests. +# A better solution would be to implement a proper state management system. + +def _get_gpio_leds(): + """Return a singleton leds controller stored in app.extensions.""" + app = current_app._get_current_object() + ext_key = 'nenuscanner_gpio_leds' + if ext_key not in app.extensions: + # create and store the resource + app.extensions[ext_key] = leds.get().enter() + return app.extensions[ext_key] + +@blueprint.record_once +def _register_cleanup(state): + """ + Register a teardown handler on the app that will try to exit the leds controller + when the app context is torn down. + """ + app = state.app + ext_key = 'nenuscanner_gpio_leds' + @app.teardown_appcontext + def _cleanup(exception=None): + gpio = app.extensions.get(ext_key) + if gpio: + try: + gpio.exit() + except Exception: + # best effort cleanup, don't raise during teardown + pass + + # Routes for object management @@ -16,9 +48,15 @@ def get(): """ Returns the pages showing all leds. """ + gpio_leds = _get_gpio_leds() + print(gpio_leds) + + for i, led in enumerate(gpio_leds.leds): + print(f"LED {i}: {led}, is_on={led.is_on}") return render_template( - 'leds.html') + 'leds.html', leds= config.LEDS_UUIDS) + @blueprint.route('/set', methods=['POST']) def set_led(): @@ -29,17 +67,18 @@ def set_led(): data = request.get_json() led = data.get('led') state = data.get('state') - ledId=int(led[3]) - - with leds.get() as gpio_leds: - print(gpio_leds.leds) - gpio_led=gpio_leds.leds[ledId] - if state == "on": + # get the controller (lazy, stored on app.extensions) + gpio_leds = _get_gpio_leds() + + try: + # parse led id/name according to your naming convention + gpio_led = gpio_leds.get_by_uuid(int(led)) + print(f"Setting {led} / {gpio_led} to {state}") + if state == "on": gpio_led.on() - else: + else: gpio_led.off() - - - print(f"Commande reçue pour {led} : {state}") + except Exception as e: + return jsonify({'status': 'error', 'error': str(e)}), 400 return jsonify({'status': 'ok', 'led': led, 'state': state}) \ No newline at end of file diff --git a/src/nenuscanner/templates/leds.html b/src/nenuscanner/templates/leds.html index 256be40..c40818e 100644 --- a/src/nenuscanner/templates/leds.html +++ b/src/nenuscanner/templates/leds.html @@ -2,73 +2,138 @@ {% block content %}
-
-

Conroler les LEDS

-
-
+
+

Contrôler les LEDS

-
-

LED 1

- - - -
- -
-

LED 2

- - - -
- -
-

LED 3

- - - -
-
-
- -
+
+ +
+
{% endblock content %} {% block extrajs %} - {% endblock extrajs %} \ No newline at end of file