Files
TIMERS/timer_app2.py

327 lines
12 KiB
Python

import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import threading
import socket
import time
import binascii
def hexspace(data, n):
return " ".join([data[i:i + n] for i in range(0, len(data), n)])
def send_data(key, value):
print(f"Sending {key}: {value}")
class DataReceiver:
def __init__(self):
self.running = False
self.file_mode = False
self.file_path = None
def start_tcp(self, host, port, callback):
self.running = True
def run():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
with conn:
while self.running:
data = conn.recv(1024)
if data:
callback(data.decode('utf-8'))
threading.Thread(target=run, daemon=True).start()
def start_file(self, callback):
self.running = True
def run():
if not self.file_path:
return
with open(self.file_path, 'r') as f:
while self.running:
line = f.readline()
if line:
callback(line.strip())
else:
time.sleep(0.5)
threading.Thread(target=run, daemon=True).start()
def stop(self):
self.running = False
class DataSender:
def __init__(self):
self.targets = []
def add_target(self, host, port):
self.targets.append((host, port))
# def send(self, data):
# for host, port in self.targets:
# try:
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.connect((host, port))
# s.sendall(data.encode('utf-8'))
# except Exception as e:
# print(f"Error sending data to {host}:{port}: {e}")
class DataProcessor:
def process_protocol_a(self, data):
return data.upper()
def process_protocol_b(self, data):
return data[::-1]
def process_protocol_saratov(self, data):
if data == "\xff":
return
cdata = binascii.hexlify(data.encode("utf-8"))
ddata = cdata.decode("utf-8").upper()
edata = hexspace(ddata, 2)
temp = edata.split("FF 52")
results = []
for i in temp:
try:
minutes = int(i.split()[5], 16)
seconds = int(i.split()[6], 16)
timer_str = f"{minutes}:{seconds:02d}" if seconds < 60 else f"{minutes}.{seconds-128}"
seconds_24 = int(i.split()[7], 16)
timer_attack = ""
timer_pic = "D:\\Graphics\\Basketball\\24Sec_Orange.png"
if seconds_24 != 255:
if seconds_24 > 127:
seconds_24 -= 128
timer_attack = f"{seconds_24//10}.{seconds_24%10}"
timer_pic = "D:\\Graphics\\Basketball\\24Sec_Red.png"
else:
timer_attack = seconds_24
quarter = int(i.split()[14][1], 16)
timeout1 = int(i.split()[10], 16)
timeout2 = int(i.split()[11], 16)
score1 = int(i.split()[8], 16)
score2 = int(i.split()[9], 16)
foul1 = int(i.split()[12], 16)
foul2 = int(i.split()[13], 16)
data = {
"TIMER.Text": timer_str,
"ATTACK.Text": timer_attack,
"Score_Home.Text": score1,
"Score_Away.Text": score2,
"fouls1.Source": f"D:\\Graphics\\Basketball\\Home_{foul1}.png",
"fouls2.Source": f"D:\\Graphics\\Basketball\\Away_{foul2}.png",
"24SECBACKGROUND.Source": timer_pic,
}
for key, value in data.items():
send_data(key, value)
results.append(data)
except (IndexError, ValueError):
continue
return results
class App:
def __init__(self, root):
self.receiver = DataReceiver()
self.sender = DataSender()
self.processor = DataProcessor()
self.protocol_var = tk.StringVar(value="Protocol A")
self.mode_var = tk.StringVar(value="Log File")
self.create_ui(root)
def display_data(self, data):
protocol = self.protocol_var.get()
if protocol == "Protocol A":
processed_data = self.processor.process_protocol_a(data)
elif protocol == "Protocol B":
processed_data = self.processor.process_protocol_b(data)
elif protocol == "Saratov":
processed_data = self.processor.process_protocol_saratov(data)
else:
processed_data = data
if processed_data:
self.update_send_status(True)
self.sender.send(str(processed_data))
self.update_send_status(False)
def create_ui(self, root):
root.title("Data Processing Application")
self.notebook = tk.ttk.Notebook(root)
self.notebook.pack(fill="both", expand=True)
self.settings_frame = tk.Frame(self.notebook)
self.game_frame = tk.Frame(self.notebook)
self.notebook.add(self.settings_frame, text="Settings")
self.notebook.add(self.game_frame, text="Game")
self.create_settings_ui(self.settings_frame)
self.create_game_ui(self.game_frame)
def create_settings_ui(self, frame):
# Frame for receiving data
frame_receive = tk.LabelFrame(frame, text="Receive Data")
frame_receive.pack(fill="x", padx=5, pady=5)
tk.Label(frame_receive, text="Select Mode:").pack(side="left", padx=5, pady=5)
tk.OptionMenu(frame_receive, self.mode_var, "Log File", "TCP Connection", command=self.update_mode).pack(side="left", padx=5, pady=5)
self.file_button = tk.Button(frame_receive, text="Select Log File", command=self.select_file)
self.tcp_frame = tk.Frame(frame_receive)
tk.Label(self.tcp_frame, text="IP:").pack(side="left")
self.tcp_ip_entry = tk.Entry(self.tcp_frame)
self.tcp_ip_entry.pack(side="left", padx=5)
tk.Label(self.tcp_frame, text="Port:").pack(side="left")
self.tcp_port_entry = tk.Entry(self.tcp_frame)
self.tcp_port_entry.pack(side="left", padx=5)
self.start_button = tk.Button(frame_receive, text="Start", command=self.start_receiving)
self.start_button.pack(side="left", padx=5, pady=5)
tk.Button(frame_receive, text="Stop", command=self.stop_receiving).pack(side="left", padx=5, pady=5)
# Frame for processing data
frame_process = tk.LabelFrame(frame, text="Process Data")
frame_process.pack(fill="x", padx=5, pady=5)
tk.Label(frame_process, text="Select Protocol:").pack(side="left", padx=5, pady=5)
tk.OptionMenu(frame_process, self.protocol_var, "Protocol A", "Protocol B", "Saratov").pack(side="left", padx=5, pady=5)
# Frame for sending data
frame_send = tk.LabelFrame(frame, text="Send Data")
frame_send.pack(fill="x", padx=5, pady=5)
self.target_count_var = tk.IntVar(value=1)
tk.Label(frame_send, text="Number of Targets:").pack(side="left", padx=5, pady=5)
tk.Spinbox(frame_send, from_=1, to=10, textvariable=self.target_count_var, command=self.update_targets).pack(side="left", padx=5, pady=5)
self.targets_frame = tk.Frame(frame_send)
self.targets_frame.pack(fill="x", padx=5, pady=5)
self.target_entries = []
self.update_targets()
tk.Button(frame_send, text="Send Data", command=self.send_data).pack(side="left", padx=5, pady=5)
self.update_mode(self.mode_var.get())
def create_game_ui(self, frame):
frame_status = tk.LabelFrame(frame, text="Status")
frame_status.pack(fill="x", padx=5, pady=5)
self.data_receive_status = tk.Label(frame_status, text="Receiving Data: Not Active", fg="red")
self.data_receive_status.pack(pady=5)
self.data_send_status = tk.Label(frame_status, text="Sending Data: Not Active", fg="red")
self.data_send_status.pack(pady=5)
def update_mode(self, mode):
if mode == "Log File":
self.file_button.pack(side="left", padx=5, pady=5)
self.tcp_frame.pack_forget()
self.start_button.config(state="normal")
else:
self.file_button.pack_forget()
self.tcp_frame.pack(side="left", padx=5, pady=5)
self.start_button.config(state="normal")
def select_file(self):
self.receiver.file_path = filedialog.askopenfilename()
def start_receiving(self):
self.start_button.config(state="disabled")
mode = self.mode_var.get()
if mode == "Log File":
if self.receiver.file_path:
self.receiver.start_file(self.display_data)
self.update_receive_status(True)
else:
messagebox.showerror("Error", "No file selected.")
elif mode == "TCP Connection":
host = self.tcp_ip_entry.get()
port = self.tcp_port_entry.get()
if host and port.isdigit():
self.receiver.start_tcp(host, int(port), self.display_data)
self.update_receive_status(True)
else:
messagebox.showerror("Error", "Invalid IP or port.")
self.start_button.config(state="normal")
def stop_receiving(self):
self.receiver.stop()
self.start_button.config(state="normal")
self.update_receive_status(False)
def display_data(self, data):
protocol = self.protocol_var.get()
if protocol == "Protocol A":
processed_data = self.processor.process_protocol_a(data)
else:
processed_data = self.processor.process_protocol_b(data)
self.update_send_status(True)
self.sender.send(processed_data)
self.update_send_status(False)
def update_receive_status(self, active):
if active:
self.data_receive_status.config(text="Receiving Data: Active", fg="green")
else:
self.data_receive_status.config(text="Receiving Data: Not Active", fg="red")
def update_send_status(self, active):
if active:
self.data_send_status.config(text="Sending Data: Active", fg="green")
else:
self.data_send_status.config(text="Sending Data: Not Active", fg="red")
def update_targets(self):
for widget in self.targets_frame.winfo_children():
widget.destroy()
self.target_entries = []
for i in range(self.target_count_var.get()):
target_frame = tk.Frame(self.targets_frame)
target_frame.pack(fill="x", pady=2)
tk.Label(target_frame, text=f"Target {i+1} Host:").pack(side="left")
host_entry = tk.Entry(target_frame)
host_entry.pack(side="left", padx=5)
tk.Label(target_frame, text="Port:").pack(side="left")
port_entry = tk.Entry(target_frame)
port_entry.pack(side="left", padx=5)
self.target_entries.append((host_entry, port_entry))
def send_data(self):
data = "Sample data to send"
if not data:
messagebox.showerror("No Data", "No data to send.")
return
self.sender.targets = []
for host_entry, port_entry in self.target_entries:
host = host_entry.get()
port = port_entry.get()
if host and port.isdigit():
self.sender.add_target(host, int(port))
self.update_send_status(True)
self.sender.send(data)
self.update_send_status(False)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()