import sys import os import json import requests import time from datetime import datetime from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QHBoxLayout, QFrame, QPushButton, QGridLayout from PyQt5.QtGui import QPixmap, QPalette, QBrush from PyQt5.QtCore import Qt, QTimer, QDateTime from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply import RPi.GPIO as GPIO from mfrc522 import SimpleMFRC522 GPIO.setwarnings(False) class KartOkumaSistemi(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Kart Okuma Sistemi") self.setWindowFlags(Qt.FramelessWindowHint) self.showFullScreen() self.setFocusPolicy(Qt.StrongFocus) self.setGeometry(0,0,1200,950) # Dosya ve API bilgileri self.dosya_adi = "giris_kayitlari.json" self.api_url = "https://pdks.damargrup.com/api/kayitoku" if not os.path.exists(self.dosya_adi): with open(self.dosya_adi, "w") as dosya: json.dump([], dosya) # GPIO ayarları self.buzzer_pin = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(self.buzzer_pin, GPIO.OUT) # Arka plan ve UI ayarları self.set_background("placeholder_background.jpg") self.fetch_background() self.time_label = QLabel("", self) self.time_label.setAlignment(Qt.AlignRight) self.time_label.setStyleSheet("font-size: 18px; color: white; padding: 10px;") header_layout = QHBoxLayout() header_layout.addWidget(self.time_label) self.kart_bilgi_label = QLabel("Lütfen kartınızı okutunuz...", self) self.kart_bilgi_label.setAlignment(Qt.AlignCenter) self.kart_bilgi_label.setStyleSheet("font-size: 18px; font-weight: bold; color: white;") self.card_frame = QFrame(self) self.card_frame.setStyleSheet("border: 2px solid white; border-radius: 10px; padding: 15px; background-color: rgba(0, 0, 0, 0.6);") self.card_frame.setFixedSize(350, 500) self.profile_image_label = QLabel(self.card_frame) self.profile_image_label.setFixedSize(150, 150) self.profile_image_label.setStyleSheet("border-radius: 10px; border: 2px solid white;") self.profile_image_label.setAlignment(Qt.AlignCenter) self.isim_label = QLabel("İsim: -", self.card_frame) self.birim_label = QLabel("Birim: -", self.card_frame) self.kart_id_label = QLabel("Kart ID: -", self.card_frame) self.giris_saati_label = QLabel("Giriş Saati: -", self.card_frame) card_layout = QVBoxLayout(self.card_frame) card_layout.addWidget(self.profile_image_label, alignment=Qt.AlignCenter) for label in [self.isim_label, self.birim_label, self.kart_id_label, self.giris_saati_label]: label.setStyleSheet("font-size: 14px; color: white;") card_layout.addWidget(label) self.exit_button = QPushButton("Çıkış", self) self.exit_button.setStyleSheet("font-size: 16px; font-weight: bold; color: white; background-color: red; border: none; border-radius: 5px; padding: 10px 20px;") self.exit_button.clicked.connect(self.close) main_layout = QGridLayout() main_layout.addLayout(header_layout, 0, 0, 1, 2) main_layout.addWidget(self.kart_bilgi_label, 1, 0, 1, 2) main_layout.addWidget(self.card_frame, 2, 0, 1, 2, alignment=Qt.AlignCenter) main_layout.addWidget(self.exit_button, 3, 1, alignment=Qt.AlignBottom | Qt.AlignRight) main_layout.setContentsMargins(10, 10, 10, 10) self.setLayout(main_layout) self.clock_timer = QTimer() self.clock_timer.timeout.connect(self.update_time) self.clock_timer.start(1000) self.reader = SimpleMFRC522() self.kart_okuma_timer = QTimer() self.kart_okuma_timer.timeout.connect(self.kart_okuma) self.kart_okuma_timer.start(2000) def set_background(self, image_path): """Arka plan görüntüsünü ayarla.""" background_label = QLabel(self) pixmap = QPixmap(image_path) background_label.setPixmap(pixmap.scaled(self.size())) background_label.setGeometry(self.rect()) background_label.lower() background_label.show() def fetch_background(self): """API üzerinden arka plan görüntüsünü indir ve uygula.""" api_url = "https://pdks.damargrup.com/api/background" try: response = requests.get(api_url) if response.status_code == 200: # Yanıt JSON olarak ayrıştırılır. data = response.json() if data.get("status") == "success": image_url = data.get("message") # Görüntü URL'sini al image_response = requests.get(image_url) if image_response.status_code == 200: with open("background.jpg", "wb") as f: f.write(image_response.content) self.set_background("background.jpg") else: print(f"Görsel indirme hatası: {image_response.status_code}") else: print(f"API yanıtı başarısız: {data.get('message')}") else: print(f"API isteği başarısız: {response.status_code}") except requests.exceptions.RequestException as e: print("Arka plan API hatası:", e) def update_time(self): current_time = QDateTime.currentDateTime().toString("hh:mm:ss") self.time_label.setText(current_time) def kart_okuma(self): try: id, veri = self.reader.read() mevcut_zaman = datetime.now() isim, birim = veri.split("-", 1) if "-" in veri else (veri, "Birim bilgisi yok") self.save_to_json(id, isim, birim, mevcut_zaman) self.send_to_api(id, mevcut_zaman) self.buzzer_beep() except Exception as e: print("Kart okuma hatası:", e) def save_to_json(self, id, isim, birim, mevcut_zaman): yeni_kayit = {"id": id, "İsim": isim, "Birim": birim, "time": mevcut_zaman.strftime("%Y-%m-%d %H:%M:%S")} try: with open(self.dosya_adi, "r+") as dosya: try: kayitlar = json.load(dosya) except json.JSONDecodeError: kayitlar = [] kayitlar.append(yeni_kayit) dosya.seek(0) json.dump(kayitlar, dosya, indent=4) except Exception as e: print("JSON dosyasına yazma hatası:", e) def send_to_api(self, id, mevcut_zaman): data = {"id": id, "time": mevcut_zaman.strftime("%Y-%m-%d %H:%M:%S")} try: response = requests.post(self.api_url, data=data) if response.status_code == 200: json_data = json.loads(response.content) print(json_data['status']) print(json_data['message']) print(json_data['personel']) print(json_data['birim']) adi = json_data['personel'] birim = json_data['birim'] kart_id = id profile_image = json_data['profile_photo'] print(profile_image) self.isim_label.setText(f"İsim: {adi}") self.birim_label.setText(f"Birim: {birim}") self.kart_id_label.setText(f"Kart ID: {id}") self.giris_saati_label.setText(f"Giriş Saati: {mevcut_zaman.strftime('%Y-%m-%d %H:%M:%S')}") image_response = requests.get(profile_image) if image_response.status_code == 200: # Resmi dosyaya kaydet with open("profile_image.jpg", "wb") as f: f.write(image_response.content) # Görseli QPixmap ile yükleyin pixmap = QPixmap("profile_image.jpg") if pixmap.isNull(): print("Görsel yüklenemedi.") else: self.profile_image_label.setStyleSheet("border-radius: 10px; border: 2px solid white;") self.profile_image_label.setPixmap(pixmap.scaled(150, 150, Qt.KeepAspectRatio)) print("Veri API'ye başarıyla gönderildi.") else: print("Görsel indirme hatası:", image_response.status_code) else: print("API gönderim hatası:", response.status_code) except requests.exceptions.RequestException as e: print("API bağlantı hatası:", e) def buzzer_beep(self): GPIO.output(self.buzzer_pin, GPIO.HIGH) time.sleep(0.2) GPIO.output(self.buzzer_pin, GPIO.LOW) def closeEvent(self, event): GPIO.cleanup() event.accept() if __name__ == "__main__": app = QApplication(sys.argv) sistem = KartOkumaSistemi() sistem.show() sys.exit(app.exec_())