diff --git a/nuclearesrpc.py b/nuclearesrpc.py index 99ed051..9ef2668 100644 --- a/nuclearesrpc.py +++ b/nuclearesrpc.py @@ -77,25 +77,41 @@ def find_nucleares(): presence.connect() print("Connected. Press Ctrl+C to Exit") while 1: - dvars = get_all_vars(url) - if dvars["CORE_TEMP"] <= 50: - details = "Reactor Offline" - else: - details = f"Reactor Online: {round(dvars['CORE_TEMP'])}C" - pwr = dvars["GENERATOR_0_KW"] + dvars["GENERATOR_1_KW"] + dvars["GENERATOR_2_KW"] - if pwr > 0: - status = f"Producing {pwr} kW" - else: - status = "Generator Offline" - if dvars["CORE_IMMINENT_FUSION"] == "TRUE": - details = "Imminent Meltdown" - presence.update( - pid=proc.pid, - start=round(starttime), - details=details, - state=status, - ) - #print( - # f"Sent Update: Core = {dvars['CORE_TEMP']} - Total Pwr = {pwr} - Panic = {dvars['CORE_IMMINENT_FUSION']}" - #) - time.sleep(15) \ No newline at end of file + try: + dvars = get_all_vars(url) + if dvars["CORE_TEMP"] <= 50: + details = "Reactor Offline" + else: + details = f"Reactor Online: {round(dvars['CORE_TEMP'])}C" + pwr = round(dvars["GENERATOR_0_KW"] + dvars["GENERATOR_1_KW"] + dvars["GENERATOR_2_KW"]) + if pwr > 0: + status = f"Producing {pwr} kW" + else: + status = "Generator Offline" + if dvars["CORE_IMMINENT_FUSION"] == "TRUE": + details = "Imminent Meltdown" + presence.update( + pid=proc.pid, + start=round(starttime), + details=details, + state=status, + large_image="nucleares" + ) + #print( + # f"Sent Update: Core = {dvars['CORE_TEMP']} - Total Pwr = {pwr} - Panic = {dvars['CORE_IMMINENT_FUSION']}" + #) + time.sleep(15) + except requests.ConnectionError: + print("Webserver connection lost, trying to re-establish...") + if find_nucleares() is None: + print("Nucleares is closed, RPC will close...") + exit(0) + while 1: + try: + # Try to query data until it succeeds + requests.get(url, {"Variable": "CORE_TEMP"}) + break + except requests.ConnectionError as e: + time.sleep(5) + print("Connected!") + continue