Better error handling and logging

This commit is contained in:
Arti Zirk 2020-11-04 12:18:05 +02:00
parent b0a6de4a49
commit d9f578d0a9

View File

@ -15,7 +15,11 @@ except ImportError:
from .wiegand import Decoder
logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M'
)
def hash_uid(uid: str, salt: str) -> str:
@ -52,12 +56,14 @@ class Main:
def sync_cards(self):
logging.info("Downloading users list")
r = self.session.get(self.api_allowed, timeout=15)
try:
r = self.session.get(self.api_allowed, timeout=15)
allowed_uids = r.json()["allowed_uids"]
except JSONDecodeError as e:
logging.exception("Failed to decode allowed uids json")
return
except (requests.Timeout, requests.ConnectionError) as e:
logging.exception("Connection timeout/error in sync cards")
else:
uids = set()
for token in allowed_uids:
uids.add(token["token"]["uid_hash"].strip())
@ -78,10 +84,14 @@ class Main:
"uid": value,
"uid_hash": uid_h,
"door": self.door,
"success": success,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
}
if success:
data["success"] = success
try:
requests.post(self.api_swipe, data=data, timeout=15)
except (requests.Timeout, requests.ConnectionError) as e:
logging.exception("Connection timeout/error in post swipes")
def listen_notification(self):
while 1: