reduce warning and info messages about the rfid not connected

This commit is contained in:
edo-neo 2025-09-11 13:20:08 +02:00
parent 55e82cc3fe
commit d085e1d90e
2 changed files with 50 additions and 4 deletions

View File

@ -25,19 +25,39 @@ class RFID_PN532(Component):
self.is_win = platform.system().lower() == "windows"
self.dev_list = [f"{'com'if self.is_win else 'tty'}:{self.config['fixture_rfid']['port']}:pn532"]
self._period = 1
# Limit info-level logging for repeated connection failures
self._fail_log_count = 0
self._fail_log_limit = 10
self._fail_log_suppressed = False
def open_device(self):
self.clf = src.lib.nfc.ContactlessFrontend()
for dev in self.dev_list:
self.connected = self.clf.open(dev)
if self.connected:
# Reset failure log suppression on successful connection
self._fail_log_count = 0
self._fail_log_suppressed = False
self.log.info(f"CONNECTED TO {dev}")
break
else:
self.log.info(f"UNABLE TO CONNECT TO {dev}")
# Log at most _fail_log_limit times, then suppress until reset
if not self._fail_log_suppressed:
if self._fail_log_count < self._fail_log_limit:
self.log.info(f"UNABLE TO CONNECT TO {dev}")
self._fail_log_count += 1
if self._fail_log_count == self._fail_log_limit:
self.log.info("RFID connection errors exceeded limit; further messages suppressed")
self._fail_log_suppressed = True
def close_device(self):
self.clf.close()
try:
if self.clf is not None:
self.clf.close()
except Exception as e:
self.log.debug(f"Error while closing RFID device: {e}")
finally:
self.clf = None
@pyqtSlot()
def start(self):
@ -58,6 +78,9 @@ class RFID_PN532(Component):
tag = src.lib.nfc.tag.activate(self.clf, target)
if tag is not None:
self.log.debug("tag present")
# Reset suppression when a valid NFC tag is detected
self._fail_log_count = 0
self._fail_log_suppressed = False
if tag.ndef is not None:
tag_content=tag.ndef.records[0].text
if tag_content!=self.current_data:

View File

@ -35,6 +35,13 @@ import threading
import logging
log = logging.getLogger(__name__)
# Module-level counters to rate-limit repeated open() failure logs across
# all ContactlessFrontend instances. This ensures suppression even if new
# instances are created on each retry by higher-level code.
_OPEN_FAIL_LOG_COUNT = 0
_OPEN_FAIL_LOG_LIMIT = 10
_OPEN_FAIL_LOG_SUPPRESSED = False
def print_data(data):
return 'None' if data is None else binascii.hexlify(data).decode('latin')
@ -73,6 +80,10 @@ class ContactlessFrontend(object):
self.device = None
self.target = None
self.lock = threading.Lock()
# Counters to rate-limit repeated open() failure logs
self._fail_open_log_count = 0
self._fail_open_log_limit = 10
self._fail_open_log_suppressed = False
if path and not self.open(path):
raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
@ -142,16 +153,28 @@ class ContactlessFrontend(object):
# Close current device driver if this is not the first
# open. This allows to use several devices sequentially or
# re-initialize a device.
global _OPEN_FAIL_LOG_COUNT, _OPEN_FAIL_LOG_LIMIT, _OPEN_FAIL_LOG_SUPPRESSED
self.close()
# Acquire the lock and search for a device on *path*
with self.lock:
log.info("searching for reader on path " + path)
# Reduce noise: searching message at DEBUG level
log.debug("searching for reader on path " + path)
self.device = device.connect(path)
if self.device:
log.info("using {0}".format(self.device))
# Reset global failure log suppression on successful open
_OPEN_FAIL_LOG_COUNT = 0
_OPEN_FAIL_LOG_SUPPRESSED = False
else:
log.error("no reader available on path " + path)
# Log failure at most _OPEN_FAIL_LOG_LIMIT times, then suppress (module-level)
if not _OPEN_FAIL_LOG_SUPPRESSED:
if _OPEN_FAIL_LOG_COUNT < _OPEN_FAIL_LOG_LIMIT:
log.warning("no reader available on path " + path)
_OPEN_FAIL_LOG_COUNT += 1
if _OPEN_FAIL_LOG_COUNT == _OPEN_FAIL_LOG_LIMIT:
log.info("nfc.clf connection errors exceeded limit; further messages suppressed")
_OPEN_FAIL_LOG_SUPPRESSED = True
return bool(self.device)
def close(self):