Advertisement
Guest User

ASd

a guest
May 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import MFRC522
  3. import RPi.GPIO as GPIO
  4. import datetime
  5. import time, signal, os
  6.  
  7. tokenStatus = 0
  8. token_nr = 0
  9. index = 0
  10. logout = 0
  11.  
  12. LEDList = [12,16,18,32,37]
  13. TasterList = [31,11,7,15,13]
  14. Sequenz = []
  15.  
  16. def startup_LED():
  17. GPIO.output(LEDList, GPIO.LOW)
  18. GPIO.output(LEDList, GPIO.HIGH)
  19. time.sleep(0.4)
  20. GPIO.output(LEDList, GPIO.LOW)
  21. time.sleep(0.4)
  22. GPIO.output(LEDList, GPIO.HIGH)
  23. time.sleep(0.4)
  24. GPIO.output(LEDList, GPIO.LOW)
  25. time.sleep(0.4)
  26.  
  27.  
  28. def setup():
  29. GPIO.setmode(GPIO.BOARD)
  30. for pin in LEDList:
  31. GPIO.setup(pin, GPIO.OUT)
  32. for pin in TasterList:
  33. GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  34. startup_LED()
  35.  
  36. #If a Button is pressed after login (Only gets called after successful login)
  37. def TasterDruck():
  38.  
  39. if (TasterList[0] == True):
  40. (LEDList[0], GPIO.HIGH)
  41. time.sleep(0.5)
  42. (LEDList[0], GPIO.LOW)
  43. Sequenz.append("1")
  44. time.sleep(0.2)
  45.  
  46. elif (TasterList[1] == True):
  47. (LEDList[1], GPIO.HIGH)
  48. time.sleep(0.5)
  49. (LEDList[1], GPIO.LOW)
  50. Sequenz.append("2")
  51. time.sleep(0.2)
  52.  
  53. elif (TasterList[2] == True):
  54. (LEDList[2], GPIO.HIGH)
  55. time.sleep(0.5)
  56. (LEDList[2], GPIO.LOW)
  57. Sequenz.append("3")
  58. time.sleep(0.2)
  59.  
  60. elif (TasterList[3] == True):
  61. (LEDList[3], GPIO.HIGH)
  62. time.sleep(0.5)
  63. (LEDList[3], GPIO.LOW)
  64. Sequenz.append("4")
  65. time.sleep(0.2)
  66.  
  67. elif (TasterList[4] == True):
  68. (LEDList[4], GPIO.HIGH)
  69. time.sleep(0.5)
  70. (LEDList[4], GPIO.LOW)
  71. Sequenz.append("5")
  72. time.sleep(0.2)
  73.  
  74. #Capture SIGINT for cleanup when the script is aborted
  75. def end_read(signal,frame):
  76. exit()
  77. #Hook the SIGINT
  78. signal.signal(signal.SIGINT, end_read)
  79. #Create an object of the class MFRC522
  80. MIFAREReader = MFRC522.MFRC522()
  81. def read_token():
  82. time.sleep(0.1)
  83. (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  84. (status,uid) = MIFAREReader.MFRC522_Anticoll()
  85. time.sleep(0.1)
  86. if status == MIFAREReader.MI_OK:
  87. global tokenStatus, token_nr
  88. print ("Card read UID: %s%s%s%s") % (uid[0], uid[1], uid[2], uid[3])
  89. token_nr1, token_nr2, token_nr3, token_nr4 = uid[0], uid[1], uid[2], uid[3]
  90. token_nr = str(token_nr1)+str(token_nr2)+str(token_nr3)+str(token_nr4)
  91. time.sleep(1)
  92.  
  93. def loop():
  94. while True:
  95. global tokenStatus, token_nr
  96. if tokenStatus == 0:
  97. read_token()
  98. array_token=[]
  99.  
  100. if token_nr > 0:
  101. global index
  102. with open('/home/pi/MFRC522-python/tokens_mysql.txt') as data:
  103. for line in data:
  104. split_lines = line.split(',')
  105. array_token.append(split_lines)
  106. data.close()
  107. with open('/home/pi/MFRC522-python/dauer.txt') as data:
  108. Zeit = data.readline()
  109. data.close()
  110. Zeit = int(Zeit)
  111. if len(array_token) < index:
  112. index = 0
  113. if token_nr == array_token[0][index]:
  114. login_time = int(time.time())
  115. global login_timestamp
  116. login_timestamp = datetime.datetime.now()
  117. tokenStatus = tokenStatus +1
  118. token_login = token_nr
  119. token_nr = 0
  120. setup()
  121. else:
  122. index = index + 1
  123.  
  124. else:
  125. read_token()
  126.  
  127. #End of Rundgang
  128. if tokenStatus == 1:
  129. read_token()
  130. TasterDruck()
  131. time_difference = int(time.time()) - login_time
  132.  
  133. #Exit Rundgang when over given time
  134. if time_difference > Zeit:
  135. logout_time = int(time.time())
  136. global logout_timestamp
  137. logout_timestamp = datetime.datetime.now()
  138. time_difference = logout_time - login_time
  139. token_nr = token_login
  140. os.system('sh /home/pi/MFRC522-python/./scriptsql.sh')
  141. destroy()
  142.  
  143. #Checks if login is ok and the time is in its scope
  144. if token_nr > 0:
  145. if token_nr == token_login:
  146. logout_time = int(time.time())
  147. logout_timestamp = datetime.datetime.now()
  148. time_difference = logout_time - login_time
  149. os.system('sh /home/pi/MFRC522-python/./scriptsql.sh')
  150. destroy()
  151. else:
  152. token_nr = 0
  153. else:
  154. read_token()
  155. #When CTRL+C is Pressed to Finish the Programm
  156. def destroy():
  157. startup_LED()
  158. for pin in LEDList:
  159. GPIO.output(pin, GPIO.LOW)
  160. GPIO.cleanup()
  161.  
  162. global index, login_timestamp, logout_timestamp, logout, token_nr, tokenStatus, login_timestamp, logout_timestamp, login_time, logout_time, Zeit, time_difference, Sequenz
  163.  
  164. login_timestamp = str(login_timestamp)
  165. logout_timestamp = str(logout_timestamp)
  166.  
  167. #Creates Files with the following data:
  168. #Login, Logout, Sequence,
  169. with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
  170. data.write(login_timestamp + ";")
  171.  
  172. with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
  173. data.write(logout_timestamp + ";")
  174.  
  175. with open('/home/pi/MFRC522-python/upload.txt', 'a') as file:
  176. for item in Sequenz:
  177. file.write(item)
  178.  
  179. with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
  180. with open('/home/pi/MFRC522-python/rundgaengeNr_sql.txt', 'r') as file:
  181. rundgang = file.readline()
  182. data.write(";" + rundgang + ";")
  183.  
  184. with open('/home/pi/MFRC522-python/upload.txt', 'a') as data:
  185. data.write(str(token_nr) + ",\n")
  186.  
  187. 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
  188. Sequenz = []
  189. loop()
  190.  
  191. def ExitProgramm():
  192. for pin in LEDList:
  193. GPIO.output(pin, GPIO.LOW)
  194. GPIO.cleanup()
  195. exit()
  196.  
  197. #Program start from here
  198. if __name__ == '__main__':
  199. try:
  200. loop()
  201. except KeyboardInterrupt:
  202. ExitProgramm()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement