Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import MFRC522
- import RPi.GPIO as GPIO
- import datetime
- import time, signal, os
- tokenStatus = 0
- token_nr = 0
- index = 0
- logout = 0
- LEDList = [12,16,18,32,37]
- TasterList = [31,11,7,15,13]
- Sequenz = []
- def startup_LED():
- GPIO.output(LEDList, GPIO.LOW)
- GPIO.output(LEDList, GPIO.HIGH)
- time.sleep(0.4)
- GPIO.output(LEDList, GPIO.LOW)
- time.sleep(0.4)
- GPIO.output(LEDList, GPIO.HIGH)
- time.sleep(0.4)
- GPIO.output(LEDList, GPIO.LOW)
- time.sleep(0.4)
- def setup():
- GPIO.setmode(GPIO.BOARD)
- for pin in LEDList:
- GPIO.setup(pin, GPIO.OUT)
- for pin in TasterList:
- GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- startup_LED()
- #If a Button is pressed after login (Only gets called after successful login)
- def TasterDruck():
- if (TasterList[0] == True):
- (LEDList[0], GPIO.HIGH)
- time.sleep(0.5)
- (LEDList[0], GPIO.LOW)
- Sequenz.append("1")
- time.sleep(0.2)
- elif (TasterList[1] == True):
- (LEDList[1], GPIO.HIGH)
- time.sleep(0.5)
- (LEDList[1], GPIO.LOW)
- Sequenz.append("2")
- time.sleep(0.2)
- elif (TasterList[2] == True):
- (LEDList[2], GPIO.HIGH)
- time.sleep(0.5)
- (LEDList[2], GPIO.LOW)
- Sequenz.append("3")
- time.sleep(0.2)
- elif (TasterList[3] == True):
- (LEDList[3], GPIO.HIGH)
- time.sleep(0.5)
- (LEDList[3], GPIO.LOW)
- Sequenz.append("4")
- time.sleep(0.2)
- elif (TasterList[4] == True):
- (LEDList[4], GPIO.HIGH)
- time.sleep(0.5)
- (LEDList[4], GPIO.LOW)
- Sequenz.append("5")
- time.sleep(0.2)
- #Capture SIGINT for cleanup when the script is aborted
- def end_read(signal,frame):
- exit()
- #Hook the SIGINT
- signal.signal(signal.SIGINT, end_read)
- #Create an object of the class MFRC522
- MIFAREReader = MFRC522.MFRC522()
- def read_token():
- time.sleep(0.1)
- (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
- (status,uid) = MIFAREReader.MFRC522_Anticoll()
- time.sleep(0.1)
- if status == MIFAREReader.MI_OK:
- global tokenStatus, token_nr
- print ("Card read UID: %s%s%s%s") % (uid[0], uid[1], uid[2], uid[3])
- token_nr1, token_nr2, token_nr3, token_nr4 = uid[0], uid[1], uid[2], uid[3]
- token_nr = str(token_nr1)+str(token_nr2)+str(token_nr3)+str(token_nr4)
- time.sleep(1)
- def loop():
- while True:
- global tokenStatus, token_nr
- if tokenStatus == 0:
- read_token()
- array_token=[]
- if token_nr > 0:
- global index
- with open('/home/pi/MFRC522-python/tokens_mysql.txt') as data:
- for line in data:
- split_lines = line.split(',')
- array_token.append(split_lines)
- data.close()
- with open('/home/pi/MFRC522-python/dauer.txt') as data:
- Zeit = data.readline()
- data.close()
- Zeit = int(Zeit)
- if len(array_token) < index:
- index = 0
- if token_nr == array_token[0][index]:
- login_time = int(time.time())
- global login_timestamp
- login_timestamp = datetime.datetime.now()
- tokenStatus = tokenStatus +1
- token_login = token_nr
- token_nr = 0
- setup()
- else:
- index = index + 1
- else:
- read_token()
- #End of Rundgang
- if tokenStatus == 1:
- read_token()
- TasterDruck()
- time_difference = int(time.time()) - login_time
- #Exit Rundgang when over given time
- if time_difference > Zeit:
- logout_time = int(time.time())
- global logout_timestamp
- logout_timestamp = datetime.datetime.now()
- time_difference = logout_time - login_time
- token_nr = token_login
- os.system('sh /home/pi/MFRC522-python/./scriptsql.sh')
- destroy()
- #Checks if login is ok and the time is in its scope
- if token_nr > 0:
- if token_nr == token_login:
- logout_time = int(time.time())
- logout_timestamp = datetime.datetime.now()
- time_difference = logout_time - login_time
- os.system('sh /home/pi/MFRC522-python/./scriptsql.sh')
- destroy()
- else:
- token_nr = 0
- else:
- read_token()
- #When CTRL+C is Pressed to Finish the Programm
- def destroy():
- startup_LED()
- for pin in LEDList:
- GPIO.output(pin, GPIO.LOW)
- GPIO.cleanup()
- global index, login_timestamp, logout_timestamp, logout, token_nr, tokenStatus, login_timestamp, logout_timestamp, login_time, logout_time, Zeit, time_difference, Sequenz
- login_timestamp = str(login_timestamp)
- logout_timestamp = str(logout_timestamp)
- #Creates Files with the following data:
- #Login, Logout, Sequence,
- with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
- data.write(login_timestamp + ";")
- with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
- data.write(logout_timestamp + ";")
- with open('/home/pi/MFRC522-python/upload.txt', 'a') as file:
- for item in Sequenz:
- file.write(item)
- with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
- with open('/home/pi/MFRC522-python/rundgaengeNr_sql.txt', 'r') as file:
- rundgang = file.readline()
- data.write(";" + rundgang + ";")
- with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
- data.write(str(token_nr) + ",\n")
- index, login_timestamp, logout_timestamp, logout, token_nr, tokenStatus, login_time, logout_time, Zeit, time_difference = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- Sequenz = []
- loop()
- def ExitProgramm():
- for pin in LEDList:
- GPIO.output(pin, GPIO.LOW)
- GPIO.cleanup()
- exit()
- #Program start from here
- if __name__ == '__main__':
- try:
- loop()
- except KeyboardInterrupt:
- ExitProgramm()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement