Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import hashlib
  5. from pyfingerprint.pyfingerprint import PyFingerprint
  6. import json
  7. import datetime
  8. import time
  9. from time import sleep, strftime
  10. import mysql.connector
  11. import Adafruit_GPIO.SPI as SPI
  12. import Adafruit_SSD1306
  13. from PIL import Image
  14. from PIL import ImageDraw
  15. from PIL import ImageFont
  16. import subprocess
  17. import os
  18. from pad4pi import rpi_gpio
  19. import webbrowser
  20. import subprocess
  21. import sys
  22. import RPi.GPIO as GPIO
  23. GPIO.setwarnings(False)
  24.  
  25. KEYPAD = [
  26. ["1","2","3"],
  27. ["4","5","6"],
  28. ["7","8","9"],
  29. ["*","0","#"]
  30. ]
  31.  
  32. ROW_PINS = [11,23,24,25] # BCM numbering
  33. COL_PINS = [17,27,22] # BCM numbering
  34.  
  35. factory = rpi_gpio.KeypadFactory()
  36.  
  37. keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
  38.  
  39.  
  40. RST = 0
  41. DC = 23
  42. SPI_PORT = 0
  43. SPI_DEVICE = 0
  44. disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
  45. disp.clear()
  46. disp.begin()
  47. width = disp.width
  48. height = disp.height
  49. image = Image.new('1', (width, height))
  50. draw = ImageDraw.Draw(image)
  51. draw.rectangle((0,0,width,height), outline=0, fill=0)
  52. padding = -2
  53. top = padding
  54. bottom = height-padding
  55. x = 0
  56. font = ImageFont.load_default()
  57. mydb = mysql.connector.connect(
  58. host="localhost",
  59. user="root",
  60. passwd="root",
  61. database="COMPANY"
  62. )
  63. mycursor = mydb.cursor()
  64. def sql_update(Emp_no):
  65.  
  66. time = datetime.datetime.now().strftime ("%H:%M:%S")
  67. date = datetime.datetime.now().strftime ("%Y-%m-%d")
  68. sql = "SELECT * FROM ATTENDENCE WHERE SERIAL_NO = %s AND DATE= %s AND OUT_TIME is null"
  69. val = (str(positionNumber), date)
  70. mycursor.execute(sql, val)
  71. myresult = mycursor.fetchall()
  72. print(myresult)
  73. tick = len(myresult)
  74. if tick:
  75. sql = "UPDATE ATTENDENCE SET OUT_TIME = %s WHERE SERIAL_NO = %s AND DATE= %s AND OUT_TIME is null"
  76. val = (time, str(positionNumber), date)
  77. mycursor.execute(sql, val)
  78. mydb.commit()
  79.  
  80. if not tick:
  81.  
  82. sql = "INSERT INTO ATTENDENCE (SERIAL_NO,EMP_ID, DATE, IN_TIME) VALUES (%s,%s, %s, %s)"
  83. val = (str(positionNumber), str(Emp_no),date, time )
  84. mycursor.execute(sql, val)
  85.  
  86. mydb.commit()
  87.  
  88. def sql_extract(positionNumber):
  89.  
  90. sql = "SELECT EMP_NO FROM EMPLOYEE WHERE SERIAL_NO=" + str(positionNumber)
  91.  
  92. mycursor.execute(sql)
  93.  
  94. myresult = mycursor.fetchall()
  95.  
  96. print(myresult)
  97.  
  98. emp_no = myresult[0][0]
  99.  
  100. print(emp_no)
  101.  
  102. return emp_no
  103. def printKey(key):
  104. print(key)
  105. if (key=="#"):
  106. sql_update(Emp_No)
  107. while 1:
  108. disp.clear()
  109. disp.display()
  110. draw.rectangle((0,0,width,height), outline=0, fill=0)
  111. draw.text((x, top ), str(Emp_No) , font=font, fill=255)
  112. draw.text((x, top + 8), 'Attendance Done' , font=font, fill=255)
  113. disp.image(image)
  114. disp.display()
  115. os.system("python ~/Desktop/GIH/search.py
  116.  
  117. elif (key == "*") :
  118. disp.clear()
  119. disp.display()
  120. draw.rectangle((0,0,width,height), outline=0, fill=0)
  121. draw.text((x, top ), str(Emp_No) , font=font, fill=255)
  122. draw.text((x, top + 16), ' OK, YOU CAN LEAVE!' , font=font, fill=255)
  123. disp.image(image)
  124. disp.display()
  125. print(Emp_No)
  126. print("Ok You can Leave?")
  127. os.system("python ~/Desktop/GIH/search.py")
  128. else :
  129. disp.clear()
  130. disp.display()
  131. draw.rectangle((0,0,width,height), outline=0, fill=0)
  132. draw.text((x, top ), str(Emp_No) , font=font, fill=255)
  133. draw.text((x, top + 16), ' Incorrect input!' , font=font, fill=255)
  134. disp.image(image)
  135. disp.display()
  136. print(Emp_No)
  137. print("Incorrect input")
  138. os.system("python ~/Desktop/GIH/search.py")
  139.  
  140. try:
  141. f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000)
  142.  
  143. if ( f.verifyPassword() == False ):
  144. raise ValueError('The given fingerprint sensor password is wrong!')
  145.  
  146. except Exception as e:
  147. print('The fingerprint sensor could not be initialized!')
  148. print('Exception message: ' + str(e))
  149. os.system("python search.py")
  150.  
  151. print('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ str(f.getStorageCapacity()))
  152.  
  153. while 1:
  154. disp.clear()
  155. disp.display()
  156. draw.rectangle((0,0,width,height), outline=0, fill=0)
  157. draw.text((x, top), 'Waiting for finger...' , font=font, fill=255)
  158. draw.text((0, top + 20), strftime('%a, %b %d %Y'), font=font, fill=1)
  159. print('Waiting for finger...')
  160. disp.image(image)
  161. disp.display()
  162. print("Waiting for Finger")
  163.  
  164. while ( f.readImage() == False ):
  165. pass
  166.  
  167. f.convertImage(0x01)
  168. f.createTemplate()
  169. temp = f.createTemplate()
  170. result = f.searchTemplate()
  171.  
  172. positionNumber = result[0]
  173.  
  174. if ( positionNumber == -1 ):
  175. disp.clear()
  176. disp.display()
  177. draw.rectangle((0,0,width,height), outline=0, fill=0)
  178. draw.text((x, top), 'No match found' , font=font, fill=255)
  179. disp.image(image)
  180. disp.display()
  181. print('No match found!')
  182. os.system("python ~/Desktop/GIH/search.py")
  183. exit(0)
  184. else:
  185. print('Found template at position #' + str(positionNumber))
  186.  
  187. Emp_No = sql_extract(positionNumber)
  188.  
  189. webbrowser.open("/home/pi/Desktop/GIH/fingerprint/hello.mp3")
  190.  
  191. disp.clear()
  192. disp.display()
  193. draw.rectangle((0,0,width,height), outline=0, fill=0)
  194. draw.text((x, top), 'Are You Sure!' , font=font, fill=255)
  195. draw.text((x, top + 10), 'press # if : yes' , font=font, fill=255)
  196. draw.text((x, top + 20), 'press * if : no' , font=font, fill=255)
  197. disp.image(image)
  198. disp.display()
  199. print("# if : Yes")
  200. print("* if: NO")
  201. keypad.registerKeyPressHandler(printKey)
  202. time.sleep(1000000)
  203.  
  204. f.loadTemplate(positionNumber, 0x01)
  205. characterics = str(f.downloadCharacteristics(0x01)).encode('utf-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement