diff --git a/timer_MBA.py b/timer_MBA.py index 89b9c2c..6c96e08 100644 --- a/timer_MBA.py +++ b/timer_MBA.py @@ -3,186 +3,95 @@ import json from socket import * from datetime import datetime import time -import os -# import logging import requests +import logging +import os -VERSION = "версия 2" # от 02.04.2024 - -host = "10.0.0.57" -port = 50002 +host = "10.4.100.17" +port = 4420 ADDR = (host, port) -PATH = ( - r"D:\ГРАФИКА\БАСКЕТБОЛ\ЕДИНАЯ ЛИГА ВТБ 2022-2023\python\JSON\timer_basketball.json" -) +PATH = r"D:\ГРАФИКА\БАСКЕТБОЛ\ЕДИНАЯ ЛИГА ВТБ 2022-2023\python\JSON\timer_basketball.json" -session = requests.Session() -session.headers.update({"Connection": "keep-alive"}) def save_log(name, data): current_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f") message_with_time = f"[{current_time}] {data}" - if not os.path.isdir("LOGS"): - os.mkdir("LOGS") - with open(f"LOGS/{name}", "a") as file: + with open(name, "a") as file: file.write(message_with_time + "\n") +session = requests.Session() +session.headers.update({"Connection": "keep-alive"}) + + def send_data(name, value): url = "http://127.0.0.1:8088/API/" par = "SetText" if name.split(".")[1] == "Text" else "SetImage" params = { "Function": par, - "Input": 41, - # "Input": "SCOREBUG", + "Input": "SCOREBUG", "SelectedName": name, "Value": value, } session.get(url, params=params) -def parse(data, score1, score2): - data = data.split("/") - - with open(PATH, "r", encoding="utf-8") as f: - new_data = json.load(f) - new_data = new_data[0] - path_pic = r"D:\графика\БАСКЕТБОЛ\ЕДИНАЯ ЛИГА ВТБ ФИНАЛ ЧЕТЫРЕХ\FOULS" - # print(data) - diff1, diff2 = 0, 0 - if len(data) > 10: - if data[4] == "NPR": - new_data["quarter"] = data[5] - if data[17] == "HCP": - new_data["points1"] = data[18] - if score1 != new_data["points1"]: - diff1 = int(new_data["points1"]) - int(score1) - score1 = new_data["points1"] - if data[20] == "GCP": - new_data["points2"] = data[21] - if score2 != new_data["points2"]: - diff2 = int(new_data["points2"]) - int(score2) - score2 = new_data["points2"] - if data[29] == "HCF": - new_data["foul1"] = data[30] - new_data["foul_pic1"] = path_pic + f"\\FOULS_{int(data[30])}.png" - if data[32] == "GCF": - new_data["foul2"] = data[33] - new_data["foul_pic2"] = path_pic + f"\\FOULS_{int(data[33])}.png" - else: - if data[1] == "TGI": - if data[2] != "0": - seconds = data[3] if len(data[3]) != 1 else f"0{data[3]}" - new_data["timeGFX"] = f"{data[2]}:{seconds}" - else: - millisec = data[4] - if not millisec.isdigit(): - millisec = "0" - new_data["timeGFX"] = f"{data[3]}.{millisec}" - elif data[1] == "NPR": - new_data["quarter"] = data[2] - elif data[1] == "HCP": - new_data["points1"] = data[2] - if score1 != new_data["points1"]: - diff1 = int(new_data["points1"]) - int(score1) - score1 = new_data["points1"] - elif data[1] == "GCP": - new_data["points2"] = data[2] - if score2 != new_data["points2"]: - diff2 = int(new_data["points2"]) - int(score2) - score2 = new_data["points2"] - elif data[1] == "HCF": - new_data["foul1"] = data[2] - new_data["foul_pic1"] = path_pic + f"\\FOULS_{int(data[2])}.png" - if len(data) > 4 and data[4] == "GCF": - new_data["foul2"] = data[2] - new_data["foul_pic2"] = path_pic + f"\\FOULS_{int(data[2])}.png" - elif data[1] == "GCF": - new_data["foul2"] = data[2] - new_data["foul_pic2"] = path_pic + f"\\FOULS_{int(data[2])}.png" - elif data[1] == "TAI": - if data[2] not in ["0", "-1"]: - new_data["time_attackGFX"] = str(int(data[2])) - elif data[2] == "-1": - new_data["time_attackGFX"] = "" - new_data["time_attac_pic"] = path_pic + "\\24Sec_empty.png" - if 0 < int(data[2]) <= 5: - new_data["time_attac_pic"] = path_pic + "\\24Sec_Red.png" - else: - if int(data[2]) in [0, -1]: - new_data["time_attac_pic"] = path_pic + "\\24Sec_empty.png" - else: - new_data["time_attac_pic"] = path_pic + "\\24Sec_Orange.png" - send_data("Timer.Text", new_data["timeGFX"]) - send_data("Score_Team1.Text", new_data["points1"]) - send_data("Score_Team2.Text", new_data["points2"]) - send_data("Timer24sec.Text", new_data["time_attackGFX"]) - send_data("FOULS_TEAM_A.Source", new_data["foul_pic1"]) - send_data("FOULS_TEAM_B.Source", new_data["foul_pic2"]) - - print(f'{diff1}, {diff2}, {new_data["timeGFX"]:<5}, {new_data["time_attackGFX"]:<3}, {new_data["points1"]:<3}, {new_data["points2"]:<3}, {new_data["foul1"]:<2}, {new_data["foul2"]:<2}') +def seconds(data): + if data.strip() == "": + return "" try: - with open(PATH, "w", encoding="utf-8") as file: - json.dump([new_data], file, ensure_ascii=False, indent=4) - # if ":" in new_data["timeGFX"]: - # time.sleep(.5) - # else: - # time.sleep(.1) + return str(int(data)) + except Exception as ex: + word_to_int = "ABCDEF" + if data[0] in word_to_int: + temp = f"{word_to_int.index(data[0])}.{data[1]}" + return "" if temp == "0.0" else temp if temp != "5.0" else "5" - except Exception: - pass - - return score1, score2 +def parse2(data): + # print(data) + timer = data[2:7].strip().replace("\x00","").replace('/', '') + if ":" in timer: + timer = f'{int(timer.split(":")[0])}:{timer.split(":")[1]}' + elif '.' in timer: + timer = f'{int(timer.split(".")[0])}.{timer.split(".")[1]}' + score1 = data[7:10].strip().replace("\x00","").replace('/', '') + score2 = data[10:13].strip().replace("\x00","").replace('/', '') + attack = seconds(data[23:25].strip()) + test = data[20] + send_data("TIMER.Text", timer) + send_data("SCORE1.Text", int(score1)) + send_data("SCORE2.Text", int(score2)) + send_data("24sec.Text", attack) + print(f"_{data}_", timer, int(score1), int(score2), attack) def read_logs(): - score1, score2 = 0, 0 - new_data = { - "timeGFX": "0:00", - "time_attackGFX": "", - "quarter": "0", - "points1": "0", - "points2": "0", - "foul1": "0", - "foul2": "0", - "foul_pic1": "", - "foul_pic2": "", - "time_attac_pic": "", - } - with open(PATH, "w", encoding="utf-8") as file: - json.dump([new_data], file, ensure_ascii=False, indent=4) - with open("D:\Графика\БАСКЕТБОЛ\ЕДИНАЯ ЛИГА ВТБ 2022-2023\python\logs_mba_3.txt", "r") as file: + with open("logs_mba_1.txt", "r") as file: for line in file: parts = line.strip().split("] ") if len(parts) == 2: timestamp = parts[0][1:] data = parts[1] - score1, score2 = parse(data, score1, score2) - time.sleep(.1) + + parse2(data) + + # if len(eval(data)) > 30 and eval(data)[-7] == "00": + # time.sleep(.1) + # else: + # time.sleep(1) def main(): - current_time = datetime.now().strftime("%d-%m-%Y_%H-%M-%S") - - new_data = { - "timeGFX": "0:00", - "time_attackGFX": "", - "quarter": "0", - "points1": "0", - "points2": "0", - "foul1": "0", - "foul2": "0", - "foul_pic1": "", - "foul_pic2": "", - "time_attac_pic": "", - } - with open(PATH, "w", encoding="utf-8") as file: - json.dump([new_data], file, ensure_ascii=False, indent=4) - + current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + if not os.path.isdir("LOGS"): + os.mkdir("LOGS") + tcp_socket = socket(AF_INET, SOCK_STREAM) tcp_socket.connect(ADDR) - score1, score2 = 0, 0 + logger = logging.getLogger(__name__) + LogFileName = f"LOGS/timer_MBA_{current_time}.log" + logging.basicConfig(filename=LogFileName, level=logging.INFO) try: while True: @@ -190,11 +99,13 @@ def main(): tcp_socket.send(data) data = bytes.decode(data) data = tcp_socket.recv(1024) - data = data.decode("utf-8", errors="ignore") - print(data) + # print(data) + # data = data.decode("utf-8", errors="ignore") + data = data.decode("utf-8", errors="ignore").strip() - save_log(f"logs_mba_{VERSION}_{current_time}.txt", data) - score1, score2 = parse(data, score1, score2) + # print(data) + logging.info(data) + parse2(data) except KeyboardInterrupt: tcp_socket.close() sys.exit(1) @@ -206,3 +117,4 @@ if __name__ == "__main__": # read_logs() except KeyboardInterrupt: sys.exit(1) +