Skip to content

Commit

Permalink
Moves logic into staged functions and adds a history tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
sgowie committed Nov 13, 2023
1 parent 7019c76 commit c75641c
Showing 1 changed file with 63 additions and 26 deletions.
89 changes: 63 additions & 26 deletions reader/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,74 @@

reader = SimpleMFRC522()


# Tag read history structure
# tagHistory is a dict, expected keys being the tag IDs of scanned tags
# HISTORY_DEPTH is a constant value that defines the depth of the list used as a ring for timestamps of the scan events
# a Reset Intent is defined as INTENT_RATE read events for the same within 10 seconds

HISTORY_DEPTH = 4
INTENT_RATE = 3
YODA_URL = 'http://yoda:3001/play'
YODA_TIMEOUT_RETRY = 5
tagHistory = {}

def cardRead(tagID,tagText):
systemd.journal.send(f"Tag ID: {tagID}")
updateTagHistory(tagID)
if detectResetIntent(tagID):
systemd.journal.send(f"Tag {tagID} playback reset request")
yodaPlay(tagID)

def yodaPlay(tagID):
data = {'id': str(tagID)}
while True:
try:
# Send the POST request with JSON data
response = requests.post(YODA_URL, json=data, timeout=5, headers={'Content-Type': 'application/json'})
if response.status_code == 200:
systemd.journal.send("Data sent successfully.")
break
else:
systemd.journal.send(f"Failed to send data. Status code: {response.status_code}")
systemd.journal.send(f"Response body: {response.text}")
break
except ConnectionError as e:
systemd.journal.send(f"Connection error: {e}")
time.sleep(YODA_TIMEOUT_RETRY) # Wait for a moment before retrying

def updateTagHistory(tagID):
if not tagID in tagHistory:
tagHistory[tagID] = [time.struct_time]*HISTORY_DEPTH
insertIndex = 0
insertTime = time.gmtime()
oldest = time.mktime(insertTime)
for index in range(len(tagHistory[tagID])):
if not isinstance(tagHistory[tagID][index], time.struct_time):
insertIndex = index
break;
if time.mktime(tagHistory[tagID][index]) < oldest:
oldest = time.mktime(tagHistory[tagID][index])
insertIndex = index
tagHistory[tagID][insertIndex] = insertTime

def detectResetIntent(tagID):
floorTime=time.gmtime()-10
count=0
for index in range(len(tagHistory[tagID])):
if time.mktime(tagHistory[tagID][index]) > floorTime:
count++
if count>=INTENT_RATE:
return True
return False


while True:
try:
systemd.journal.send("Place an RFID tag near the reader...")
id, text = reader.read()
systemd.journal.send(f"Tag ID: {id}")

# Define the URL to which you want to send the data
url = 'http://yoda:3001/play' # Update the URL as needed

# Create a dictionary with the RFID ID to send as JSON data
data = {'id': str(id)}

while True:
try:
# Send the POST request with JSON data
response = requests.post(url, json=data, timeout=5, headers={'Content-Type': 'application/json'})

if response.status_code == 200:
systemd.journal.send("Data sent successfully.")
break
else:
systemd.journal.send(f"Failed to send data. Status code: {response.status_code}")
systemd.journal.send(f"Response body: {response.text}")
break

except ConnectionError as e:
systemd.journal.send(f"Connection error: {e}")
time.sleep(5) # Wait for a moment before retrying

cardRead(id, text)
time.sleep(2)

except KeyboardInterrupt:
GPIO.cleanup()
break

0 comments on commit c75641c

Please sign in to comment.