Files
prezenta_work/app.py
2025-02-24 12:31:57 +00:00

152 lines
5.2 KiB
Python

import rdm6300
import os, time
from gpiozero import OutputDevice
from multiprocessing import Process
import requests
import subprocess
import urllib3
import threading
import urllib.parse
urllib3.disable_warnings()
def config():
import config
def post_backup_data():
print("Reading backup data from tag.txt...")
try:
with open("./data/tag.txt", "r") as file:
lines = file.readlines()
remaining_lines = lines[:]
for line in lines:
line = line.strip()
if line:
print(f"Posting backup data: {line}")
try:
response = requests.post(line, verify=False, timeout=3)
print("Request sent, awaiting response...")
response.raise_for_status() # Raise an error for bad status codes
print("Data posted successfully:", response.text)
remaining_lines.remove(line + "\n")
except requests.exceptions.Timeout:
print("Request timed out.")
break
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
break
with open("./data/tag.txt", "w") as file:
file.writelines(remaining_lines)
print("Backup data updated.")
except FileNotFoundError:
print("No backup file found.")
except Exception as e:
print(f"An error occurred while reading the backup file: {e}")
def check_internet_connection():
hostname = "10.76.140.17" # "https://google.com"
cmd_block_wifi = 'sudo rfkill block wifi'
cmd_unblock_wifi = 'sudo rfkill unblock wifi'
print('Internet connection check loaded')
def manage_wifi_connection():
if var1 == 0:
print("Internet is up! Whaiting 45 minutes.")
post_backup_data()
time.sleep(2700)
else:
os.system(cmd_block_wifi)
print('System is rebooting WiFi, please wait until it finishes the job 20 minutes')
time.sleep(1200) # 20 minutes
os.system(cmd_unblock_wifi)
while True:
response = os.system("ping -c 1 " + hostname)
if response == 0:
var1 = 0
manage_wifi_connection()
else:
var1 = 1
manage_wifi_connection()
# Start the internet connection check in a separate process
internet_check_process = Process(target=check_internet_connection)
internet_check_process.start()
url = "10.76.140.17/iweb_v2/index.php/traceability/production" # pentru cazul in care raspberiul nu are sistem de prezenta
# Launch Chromium with the specified URLs
subprocess.Popen(["chromium", "--test-type", "--noerrors", "--kiosk", "--start-fullscreen", "--unsafely-treat-insecure-origin-as-secure=http://10.76.140.17", url], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL, start_new_session=True)
info = "0"
def post_info(info):
print("Starting to post data...")
info1 = info.strip() # Remove any leading/trailing whitespace, including newlines
try:
response = requests.post(info1, verify=False, timeout=3)
print("Request sent, awaiting response...")
response.raise_for_status() # Raise an error for bad status codes
print("Data posted successfully:", response.text)
except requests.exceptions.Timeout:
print("Request timed out. Saving data to backup file.")
with open("./data/tag.txt", "a") as file: # Open in append mode
file.write(info)
print("Value", info, "was saved to tag.txt")
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
with open("./data/tag.txt", "a") as file: # Open in append mode
file.write(info)
print("Value", info, "was saved to tag.txt")
def post_info_thread(info):
thread = threading.Thread(target=post_info, args=(info,))
thread.start()
led1 = OutputDevice(23)
led2 = OutputDevice(24)
name = "Ionut"
print(name)
f = open("./data/idmasa.txt", "r") # deschid fisierul
name = f.readline().strip() # citesc toate liniile
f.close() # inchid fisierul
print(name)
class Reader(rdm6300.BaseReader):
global info
def card_inserted(self, card):
if card.value == 12886709:
print("Inserting Config Card")
return
# config()
afisare = time.strftime("%Y-%m-%d&%H:%M:%S")
date = 'https://dataswsibiusb01.sibiusb.harting.intra/RO_Quality_PRD/api/record/' + name + '/' + str(card.value) + "/1/" + afisare + "\n"
info = date
print(info)
post_info_thread(info)
led1.on()
print(f"card inserted {card}")
def card_removed(self, card):
if card.value == 12886709:
print("Remouving Config card")
return
# config()
afisare=time.strftime("%Y-%m-%d&%H:%M:%S")
date='https://dataswsibiusb01.sibiusb.harting.intra/RO_Quality_PRD/api/record/'+name+'/'+str(card.value)+"/0/"+afisare+"\n"
info = date
print(info)
post_info_thread(info)
led1.off()
print("Card remove")
try:
r = Reader('/dev/ttyS0')
except:
r = Reader('/dev/ttyAMA0')
r.start()