-1

I have an rfid reader which when scanned returns a UID as "backData"

I have my users (username and UID) stored in a text file.

I've managed to read the text file into a python dictionary, but when i scan my card, it is only accepting the last UID in the text file.

Text File:

164 168 124 90 42, user1
114 156 203 196 225, user2

Python Code:

for line in uid_file:
    info = line.split(",")
    key = info[0]
    uname = info[1]
    c = len(uname)-1
    uname = uname[0:c]
    uid_dict[key] = uname
    USER = [int(i) for i in key.split()]

    if backData == USER:
        f = open("/mnt/lock_logs/lock_log.csv", "a");
        print f
        value = ('\n') + uname
        myString = str(value)
        f.write(myString)
        f.close()
     else:
        print "Access Denied"

So if I scan the card assigned to user2, it works, but if I scan the card assigned to user1, I get Access Denied.

If i print the variable USER, it returns both UIDs from the text file. Any ideas on what I need to change??

2 Answers2

0

I think this is because you continue to evaluate your for loop even after the user 1 is matched. Add a 'break' to the bottom of your if statement.

if backData == USER:
    f = open("/mnt/lock_logs/lock_log.csv", "a");
    print f
    value = ('\n') + uname
    myString = str(value)
    f.write(myString)
    f.close()
    break

I recreated your file and am able to get predicted behavior with the following code. Not that I hard coded backData and have tried it with both USER lists with success.

filename = 'uid.txt'

with open(filename) as uid_file:
    uid_dict={}

    backData = [164, 168, 124, 90, 42]
    no_matches = True

    for line in uid_file:
        info = line.split(",")
        key = info[0]
        uname = info[1]
        print info[0], info[1]
        c = len(uname)-1
        uname = uname[0:c]
        uid_dict[key] = uname
        USER = [int(i) for i in key.split()]

        if backData == USER:
            print "user matches"
            no_matches = False          
            break

    if no_matches:
        print "Access Denied"
carruthd
  • 341
  • 1
  • 3
  • 8
-1

Reanalyzing the code a bit, I am assuming that backData is an array, such as [114, 156, 203, 196, 225].

Your code is going to loop through each line, checking the array against the stored key. It won't stop if it finds a match, and it will print Access Denied each time it doesn't match (not the same as if it had not found a match at all.)

A more detailed explanation:

If backData matched with user1, the following will happen in order:

  1. Data for user1 is loaded.
  2. backData is checked against the key for user1, matching.
  3. \nuser1 is appended to lock_log.csv
  4. Data for user2 is loaded.
  5. backData is checked against the key for user2, not matching.
  6. Access Denied is printed.

If backData matched with user2, the following will happen in order:

  1. Data for user1 is loaded.
  2. backData is checked against the key for user1, not matching.
  3. Access Denied is printed.
  4. Data for user2 is loaded.
  5. backData is checked against the key for user2, matching.
  6. \nuser2 is appended to lock_log.csv

If backData matched with neither, the following will happen in order:

  1. Data for user1 is loaded.
  2. backData is checked against the key for user1, not matching.
  3. Access Denied is printed.
  4. Data for user2 is loaded.
  5. backData is checked against the key for user2, not matching.
  6. Access Denied is printed again.
Kal Zekdor
  • 1,172
  • 2
  • 13
  • 23
  • Sorry, i forgot to include this.. uid_dict = {} uid_file = open('/home/pi/users.txt',"r") – craisondigital Apr 27 '16 at 20:07
  • Basically, I'm trying to store all my users in the text file, and if the scanned cards UID is equal to one in that text file, the username will be output to a csv file. – craisondigital Apr 27 '16 at 20:11
  • you are correct about backData. Not sure what you mean, but if I scan the user2 card, it successfully writes the username to my csv file, if I scan user1 card, I get Access Denied. If I print USER, both UIDs are printed – craisondigital Apr 27 '16 at 20:21
  • Can you double check the indentation on your post? I'm assuming the `if backData == USER:` line is at the same level as the `USER = [int(i) for i in key.split()]` line. – Kal Zekdor Apr 27 '16 at 20:23
  • You are correct. I apologize. I fixed the indentation – craisondigital Apr 27 '16 at 20:26
  • I understand now, thank you, how do you suggest printing the Access Denied message. – craisondigital Apr 27 '16 at 20:58