Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import MFRC522
  6. import signal
  7. import mysql.connector
  8. from mysql.connector import Error
  9. #import threading
  10. #import thread
  11.  
  12.  
  13. #class read(threading.Thread):
  14. class read():
  15.     continue_reading = True
  16.     pins = []
  17.    
  18.     def __init__(self):
  19.       #threading.Thread.__init__(self)
  20.       signal.signal(signal.SIGINT, self.end_read)
  21.       self.getCodes()
  22.       self.read()
  23.    
  24.     def end_read(self,signal,frame):
  25.         self.continue_reading
  26.         print "Ctrl+C captured, ending read."
  27.         self.continue_reading = False
  28.         GPIO.cleanup()
  29.  
  30.     #def run(self):
  31.     def read(self):
  32.         MIFAREReader = MFRC522.MFRC522()
  33.         while self.continue_reading:
  34.    
  35.     # Scan for cards    
  36.             (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  37.  
  38.     # If a card is found
  39.             if status == MIFAREReader.MI_OK:
  40.                 print "Card detected"
  41.    
  42.     # Get the UID of the card
  43.             (status,uid) = MIFAREReader.MFRC522_Anticoll()
  44.     # If we have the UID, continue
  45.             if status == MIFAREReader.MI_OK:
  46.  
  47.         # Print UID
  48.                 print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
  49.    
  50.         # This is the default key for authentication
  51.                 key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  52.        
  53.         # Select the scanned tag
  54.                 MIFAREReader.MFRC522_SelectTag(uid)
  55.  
  56.         # Authenticate
  57.                 status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
  58.         # Check if authenticated
  59.                 if status == MIFAREReader.MI_OK:
  60.                     pin = str(uid[0])
  61.                     if pin == self.pins[0] or pin == self.pins[1]:
  62.                         print("Access granted")
  63.                     else:
  64.                         print("Access denied")
  65.                     MIFAREReader.MFRC522_StopCrypto1()
  66.                 else:
  67.                     print "Authentication error"
  68.  
  69.                    
  70.     def getCodes(self):
  71.         try:
  72.             connection = mysql.connector.connect(host='localhost', database='zugang',user='root', password='abcD123')
  73.             query = 'SELECT pin FROM rolle'
  74.             cursor = connection.cursor()
  75.             cursor.execute(query)
  76.             records = cursor.fetchall()
  77.            
  78.             for row in records:
  79.                 self.pins.append(row[0])
  80.            
  81.             cursor.close()
  82.        
  83.         except Error as e:
  84.             print("Error", e)
  85.        
  86.         finally:
  87.             if(connection.is_connected()):
  88.                 connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement