reduce warning and info messages about the rfid not connected
This commit is contained in:
parent
55e82cc3fe
commit
d085e1d90e
@ -25,19 +25,39 @@ class RFID_PN532(Component):
|
|||||||
self.is_win = platform.system().lower() == "windows"
|
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.dev_list = [f"{'com'if self.is_win else 'tty'}:{self.config['fixture_rfid']['port']}:pn532"]
|
||||||
self._period = 1
|
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):
|
def open_device(self):
|
||||||
self.clf = src.lib.nfc.ContactlessFrontend()
|
self.clf = src.lib.nfc.ContactlessFrontend()
|
||||||
for dev in self.dev_list:
|
for dev in self.dev_list:
|
||||||
self.connected = self.clf.open(dev)
|
self.connected = self.clf.open(dev)
|
||||||
if self.connected:
|
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}")
|
self.log.info(f"CONNECTED TO {dev}")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# 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.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):
|
def close_device(self):
|
||||||
|
try:
|
||||||
|
if self.clf is not None:
|
||||||
self.clf.close()
|
self.clf.close()
|
||||||
|
except Exception as e:
|
||||||
|
self.log.debug(f"Error while closing RFID device: {e}")
|
||||||
|
finally:
|
||||||
|
self.clf = None
|
||||||
|
|
||||||
@pyqtSlot()
|
@pyqtSlot()
|
||||||
def start(self):
|
def start(self):
|
||||||
@ -58,6 +78,9 @@ class RFID_PN532(Component):
|
|||||||
tag = src.lib.nfc.tag.activate(self.clf, target)
|
tag = src.lib.nfc.tag.activate(self.clf, target)
|
||||||
if tag is not None:
|
if tag is not None:
|
||||||
self.log.debug("tag present")
|
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:
|
if tag.ndef is not None:
|
||||||
tag_content=tag.ndef.records[0].text
|
tag_content=tag.ndef.records[0].text
|
||||||
if tag_content!=self.current_data:
|
if tag_content!=self.current_data:
|
||||||
|
|||||||
@ -35,6 +35,13 @@ import threading
|
|||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
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):
|
def print_data(data):
|
||||||
return 'None' if data is None else binascii.hexlify(data).decode('latin')
|
return 'None' if data is None else binascii.hexlify(data).decode('latin')
|
||||||
@ -73,6 +80,10 @@ class ContactlessFrontend(object):
|
|||||||
self.device = None
|
self.device = None
|
||||||
self.target = None
|
self.target = None
|
||||||
self.lock = threading.Lock()
|
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):
|
if path and not self.open(path):
|
||||||
raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
|
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
|
# Close current device driver if this is not the first
|
||||||
# open. This allows to use several devices sequentially or
|
# open. This allows to use several devices sequentially or
|
||||||
# re-initialize a device.
|
# re-initialize a device.
|
||||||
|
global _OPEN_FAIL_LOG_COUNT, _OPEN_FAIL_LOG_LIMIT, _OPEN_FAIL_LOG_SUPPRESSED
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
# Acquire the lock and search for a device on *path*
|
# Acquire the lock and search for a device on *path*
|
||||||
with self.lock:
|
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)
|
self.device = device.connect(path)
|
||||||
if self.device:
|
if self.device:
|
||||||
log.info("using {0}".format(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:
|
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)
|
return bool(self.device)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user