From e5e1aebd71bf716d4fef62d5f67013f2b00ea16e Mon Sep 17 00:00:00 2001 From: jnisbet Date: Thu, 9 May 2024 16:39:28 +0100 Subject: [PATCH] headless --- Headless.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Headless.py diff --git a/Headless.py b/Headless.py new file mode 100644 index 0000000..07106e9 --- /dev/null +++ b/Headless.py @@ -0,0 +1,58 @@ +import pyodbc +from datetime import datetime, timedelta + +SERVER = '' +DATABASE = '' +USERNAME = '' +PASSWORD = '' +PORT='1433' + +connection_string = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={SERVER};PORT={PORT};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD};TrustServerCertificate=yes' +db_connection = pyodbc.connect(connection_string) +cursor = db_connection.cursor() + +#Set default values +sign_in_date_time_check = datetime.fromtimestamp(0) +card_code_check = False + +while True: + #Get from card when inputed - card id will be a string + card_code_input = input() + + date_time_now = datetime.now() + time_now = date_time_now.time() + + #Find user in the database used only for checking that the user exists + cursor.execute('SELECT SignInTime FROM EmployeeRegister WHERE CardID = ?', card_code_input) + db_user_entry = cursor.fetchall() + db_sign_in_time = db_user_entry[0][0] + + #if no user info is returned and thus user does not exist reset loop + if not db_user_entry: + print('User does not exist') + continue + + #Prevent double tap of card + if card_code_input == card_code_check and sign_in_date_time_check > date_time_now: + print('Double tap prevented') + continue + + #Save sign in/sign out card code and time + 5 seconds + sign_in_date_time_check = date_time_now + timedelta(seconds = 50) + card_code_check = card_code_input + + + #If user is signed in sign them out + if db_sign_in_time: + print('Signed Out') + cursor.execute('UPDATE EmployeeRegister SET SignInTime = ? WHERE CardID = ?', None, card_code_input) + db_connection.commit() + continue + + print('Database updated') + #Update database with sign in time + cursor.execute('UPDATE EmployeeRegister SET SignInTime = ? WHERE CardID = ?', time_now, card_code_input) + db_connection.commit() + + + \ No newline at end of file