From ed9eeabbc3f50e295619f9187cb6ce2a6402a0e6 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Mon, 15 Mar 2021 16:41:50 +0100
Subject: [PATCH] Handle ignore period for cards better

---
 stechuhr_client/client.py | 20 ++++++++++++++++----
 stechuhr_client/config.py |  3 +++
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/stechuhr_client/client.py b/stechuhr_client/client.py
index 37e31c8..a4a0966 100644
--- a/stechuhr_client/client.py
+++ b/stechuhr_client/client.py
@@ -290,7 +290,7 @@ def read_key_input(input_queue, config, logger):
 
     # Buffer to store incoming keys till ENTER (keycode 28) is received
     keybuffer = []
-    last_string = None
+    last_seen_cards = None
 
     # Loop Forever over the Input
     while True:
@@ -318,10 +318,22 @@ def read_key_input(input_queue, config, logger):
                     # and reset the keybuffer
                     joined_string = "".join(keybuffer)
 
-                    # Don't add the string to the queue if is the same as before to avoid duplicates
-                    if last_string is None or joined_string != last_string:
+
+
+                    if last_seen_cards is None or len(last_seen_cards) == 0:
                         input_queue.put(joined_string)
-                    last_string = joined_string
+                        last_seen_cards.append({"id": joined_string, "time": time.time()})
+                    else:
+                        # Remove all cards from the list if they are past the threshold
+                        last_seen_cards = [c for c in last_seen_cards if time.time()-c["time"] > config["client"]["ignore_duration"] ]
+
+                        if not joined_string in [c["id"] for c in last_seen_cards]:
+                            input_queue.put(joined_string)
+                            last_seen_cards.append({"id": joined_string, "time": time.time()})
+                        else:
+                            card =  [c for c in last_seen_cards if joined_string == c["id"]][0]
+                            logger.info("Ignored duplicated register attempt of card {} because it was seen {} seconds ago".format(card["id"], card["time"]))
+                    
                     keybuffer = []
         except (KeyboardInterrupt, SystemExit, OSError) as e:
             cardreader.close()
diff --git a/stechuhr_client/config.py b/stechuhr_client/config.py
index 72507ac..bc512db 100644
--- a/stechuhr_client/config.py
+++ b/stechuhr_client/config.py
@@ -35,6 +35,9 @@ location = "lerchenfeld/mensa"
 entrance = "haupteingang"
 direction = "in"
 
+# Duration for which to ignore repeated register attempt for a card (in seconds)
+ignore_duration = 10
+
 # A list of possible python regex patterns for the id (logical OR!)
 id_patterns = [
     "^806[A-Z0-9]{9}04$",
-- 
GitLab