diff --git a/tytocare_logs_extractor.py b/tytocare_logs_extractor.py index a725d32..afbbe15 100644 --- a/tytocare_logs_extractor.py +++ b/tytocare_logs_extractor.py @@ -45,7 +45,17 @@ def extract_data_from_file(file_path): except Exception as e: print(f"Error occurred while reading file {file_path}: {e}") return None, None + +def check_record_exists(conn, cardno, station, machine, status, lastupdate): + query = """ + SELECT count(*) as record_count FROM log_pass + WHERE cardno = ? AND station = ? AND machine = ? AND status = ? AND lastupdate = ? + """ + params = (f"{cardno}_1", station, machine, status, lastupdate) + serial_check = pd.read_sql_query(query, conn, params=params) + return int(serial_check['record_count'].iloc[0]) + def main_loop(): try: global cursor @@ -78,25 +88,34 @@ def main_loop(): log_pass_lastupdate = str(new_date_str_end[:-3]) else: log_pass_lastupdate = None - - # Insert data into the database - insert_logpass = "INSERT INTO dbo.log_pass (account, cardno, line, sequence, station, machine, status, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(insert_logpass, ('G5', cardno + '_1', '', '', stage, machine, database_status, log_pass_lastupdate, 'cats')) - conn.commit() - print('History inserted successfully') - - # Insert data into log_repair if failed - if status == 'FAILED': - check_defect = pd.read_sql_query(f"SELECT defectCode FROM dbo.defectProd2 WHERE description = '{symptoms}'", conn) - defect_code = check_defect.iloc[0]['defectCode'] - insert_log_repair = "INSERT INTO dbo.log_repair (account, cardno, stage, machine, category, defect, location, details, status, addinfo, serialAffected, component, remarks, rejectDate, rejectUser, repairDate, repairUser, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" - cursor.execute(insert_log_repair, ('G5', cardno + "_1", stage, machine, "Testing Defects", defect_code, "", symptoms, "0", "", "", "", "", log_pass_lastupdate, lastupdatedby, "", "", log_pass_lastupdate, lastupdatedby)) + exists = check_record_exists(conn, cardno, stage, machine, database_status, log_pass_lastupdate) + + if exists > 0: + print(f"Record already exists for cardno: {cardno}, stage: {stage}, machine: {machine}, status: {database_status}, lastupdate: {log_pass_lastupdate}") + shutil.move(file, DESTINATION_DIR + os.path.basename(file)) + print('File Moved Successfully!') + + else: + + # Insert data into the database + insert_logpass = "INSERT INTO dbo.log_pass (account, cardno, line, sequence, station, machine, status, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(insert_logpass, ('G5', cardno + '_1', '', '', stage, machine, database_status, log_pass_lastupdate, 'cats')) conn.commit() - print('Reject Details inserted successfully') + print('History inserted successfully') - shutil.move(file, DESTINATION_DIR + os.path.basename(file)) - print('File Moved Successfully') + # Insert data into log_repair if failed + if status == 'FAILED': + check_defect = pd.read_sql_query(f"SELECT defectCode FROM dbo.defectProd2 WHERE description = '{symptoms}'", conn) + defect_code = check_defect.iloc[0]['defectCode'] + + insert_log_repair = "INSERT INTO dbo.log_repair (account, cardno, stage, machine, category, defect, location, details, status, addinfo, serialAffected, component, remarks, rejectDate, rejectUser, repairDate, repairUser, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + cursor.execute(insert_log_repair, ('G5', cardno + "_1", stage, machine, "Testing Defects", defect_code, "", symptoms, "0", "", "", "", "", log_pass_lastupdate, lastupdatedby, "", "", log_pass_lastupdate, lastupdatedby)) + conn.commit() + print('Reject Details inserted successfully') + + shutil.move(file, DESTINATION_DIR + os.path.basename(file)) + print('File Moved Successfully!') else: print('No .txt files in the directory found!') else: