Verified Commit 3fc63f01 authored by Jakob Moser's avatar Jakob Moser
Browse files

Throw proper exception on Error E1

parent 86186731
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -4,8 +4,10 @@ from time import sleep

import RPi.GPIO as GPIO
from pirc522 import RFID
from pirc522.rfid import logger as pirc522_logger  # We need this for some ugly monkey-patching, see below

from poolpay.card.CardReader import CardReader
from poolpay.card.raise_on_error_e1 import raise_on_error_e1


class Pirc522CardReader(CardReader):
@@ -21,6 +23,13 @@ class Pirc522CardReader(CardReader):
        self._reader = RFID(bus=1, device=0, pin_irq=None, pin_rst=33)
        self._handle_removal: Callable[[CardReader], None] | None = None

        # Ugly monkey-patching: There is a crash scenario in which the card reader no longer returns anything,
        # even when a card is presented (the rest of the application keeps running, although that is not particularly
        # relevant). The only indication of this happening is an "Error E1" logged as warning to the logger in the
        # pirc522 module. We replace the logger.warning function with one that is almost identical, but throws
        # an exception if the message is "Error E1", so that we can handle the crash properly.
        pirc522_logger.warning = raise_on_error_e1(pirc522_logger.warning)

    def read_id(self) -> int | None:
        self._reader.anticoll()
        uid = self._reader.read_id()
+17 −0
Original line number Diff line number Diff line
import functools
from collections.abc import Callable

from poolpay.card.CardReaderException import CardReaderException


def raise_on_error_e1[T, **P](function: Callable[P, T]) -> Callable[P, T]:

    @functools.wraps(function)
    def inner(*args: P.args, **kwargs: P.kwargs) -> T:
        result = function(*args, **kwargs)
        if len(args) > 0 and args[0] == "Error E1":
            raise CardReaderException(*args)
        else:
            return result

    return inner