Loading poolpay/card/Pirc522CardReader.py +9 −0 Original line number Diff line number Diff line Loading @@ -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): Loading @@ -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() Loading poolpay/card/raise_on_error_e1.py 0 → 100644 +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 Loading
poolpay/card/Pirc522CardReader.py +9 −0 Original line number Diff line number Diff line Loading @@ -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): Loading @@ -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() Loading
poolpay/card/raise_on_error_e1.py 0 → 100644 +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