From b6aae121bbf3122c65e9080a4738ae6fde2857a8 Mon Sep 17 00:00:00 2001 From: sickprodigy Date: Sun, 9 Nov 2025 09:24:21 -0500 Subject: [PATCH] feat: Add dynamic config reload and timezone offset handling in web server Fixes #18 Already Fixed #13 after long hours of tedious back and forth coding to figure out wtf was happening jesus --- Scripts/web_server.py | 20 ++++++++++++++++++++ config.json | 1 + main.py | 29 ++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Scripts/web_server.py b/Scripts/web_server.py index 34af881..d248b32 100644 --- a/Scripts/web_server.py +++ b/Scripts/web_server.py @@ -437,8 +437,28 @@ class TempWebServer: if self._save_config_to_file(config): print("Schedule configuration saved") + # ===== ADD THIS: Reload config into memory immediately ===== + try: + with open('config.json', 'r') as f: + updated_config = json.load(f) + # Update the passed-in config dict (updates reference, not copy) + config.clear() + config.update(updated_config) + print("✅ Config reloaded into memory") + except Exception as e: + print("⚠️ Warning: Could not reload config: {}".format(e)) + # ===== END: Reload config ===== + if schedule_monitor: schedule_monitor.reload_config(config) + + # Update AC and heater monitors with new targets from config + if ac_monitor: + ac_monitor.target_temp = config['ac_target'] + ac_monitor.temp_swing = config['ac_swing'] + if heater_monitor: + heater_monitor.target_temp = config['heater_target'] + heater_monitor.temp_swing = config['heater_swing'] # Send Discord notification try: diff --git a/config.json b/config.json index 0ced3da..1673f7c 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,5 @@ { + "timezone_offset": -5, "ac_target": 77.0, "ac_swing": 1.0, "heater_target": 72.0, diff --git a/main.py b/main.py index 9fd088e..31c0344 100644 --- a/main.py +++ b/main.py @@ -47,6 +47,7 @@ def load_config(): print("No saved config found, creating default config.json...") default_config = { + 'timezone_offset': -6, # Timezone offset from UTC (CST=-6, EST=-5, MST=-7, PST=-8, add 1 for DST) 'ac_target': 75.0, # Default AC target temp 'ac_swing': 1.0, # Default AC tolerance (+/- degrees) 'heater_target': 72.0, # Default heater target temp @@ -96,6 +97,9 @@ def load_config(): # Load configuration from file config = load_config() +# Get timezone offset from config (with fallback to -6 if not present) +TIMEZONE_OFFSET = config.get('timezone_offset', -6) + # ===== START: Reset hold modes on startup ===== # Always reset to automatic mode on boot (don't persist hold states) if 'schedule_enabled' in config: @@ -179,11 +183,18 @@ if wifi and wifi.isconnected(): finally: s.close() - # Use patched version - ntptime.time = time_with_timeout - ntptime.settime() + # Get UTC time from NTP + utc_timestamp = time_with_timeout() + + # Apply timezone offset + local_timestamp = utc_timestamp + (TIMEZONE_OFFSET * 3600) + + # Set RTC with local time + tm = time.gmtime(local_timestamp) + RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) + ntp_synced = True - print("Time synced with NTP server") + print("Time synced with NTP server (UTC{:+d})".format(TIMEZONE_OFFSET)) except Exception as e: print("Initial NTP sync failed: {}".format(e)) @@ -369,13 +380,17 @@ while True: s.sendto(NTP_QUERY, addr) msg = s.recv(48) val = struct.unpack("!I", msg[40:44])[0] - t = val - NTP_DELTA + utc_timestamp = val - NTP_DELTA - tm = time.gmtime(t) + # Apply timezone offset + local_timestamp = utc_timestamp + (TIMEZONE_OFFSET * 3600) + + # Set RTC with local time + tm = time.gmtime(local_timestamp) RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) ntp_synced = True - print("NTP sync succeeded on retry #{}".format(retry_ntp_attempts + 1)) + print("NTP sync succeeded on retry #{} (UTC{:+d})".format(retry_ntp_attempts + 1, TIMEZONE_OFFSET)) finally: s.close()