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
This commit is contained in:
Aaron 2025-11-09 09:24:21 -05:00
parent 24b53b9446
commit b6aae121bb
3 changed files with 43 additions and 7 deletions

View File

@ -437,8 +437,28 @@ class TempWebServer:
if self._save_config_to_file(config): if self._save_config_to_file(config):
print("Schedule configuration saved") 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: if schedule_monitor:
schedule_monitor.reload_config(config) 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 # Send Discord notification
try: try:

View File

@ -1,4 +1,5 @@
{ {
"timezone_offset": -5,
"ac_target": 77.0, "ac_target": 77.0,
"ac_swing": 1.0, "ac_swing": 1.0,
"heater_target": 72.0, "heater_target": 72.0,

29
main.py
View File

@ -47,6 +47,7 @@ def load_config():
print("No saved config found, creating default config.json...") print("No saved config found, creating default config.json...")
default_config = { 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_target': 75.0, # Default AC target temp
'ac_swing': 1.0, # Default AC tolerance (+/- degrees) 'ac_swing': 1.0, # Default AC tolerance (+/- degrees)
'heater_target': 72.0, # Default heater target temp 'heater_target': 72.0, # Default heater target temp
@ -96,6 +97,9 @@ def load_config():
# Load configuration from file # Load configuration from file
config = load_config() 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 ===== # ===== START: Reset hold modes on startup =====
# Always reset to automatic mode on boot (don't persist hold states) # Always reset to automatic mode on boot (don't persist hold states)
if 'schedule_enabled' in config: if 'schedule_enabled' in config:
@ -179,11 +183,18 @@ if wifi and wifi.isconnected():
finally: finally:
s.close() s.close()
# Use patched version # Get UTC time from NTP
ntptime.time = time_with_timeout utc_timestamp = time_with_timeout()
ntptime.settime()
# 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 ntp_synced = True
print("Time synced with NTP server") print("Time synced with NTP server (UTC{:+d})".format(TIMEZONE_OFFSET))
except Exception as e: except Exception as e:
print("Initial NTP sync failed: {}".format(e)) print("Initial NTP sync failed: {}".format(e))
@ -369,13 +380,17 @@ while True:
s.sendto(NTP_QUERY, addr) s.sendto(NTP_QUERY, addr)
msg = s.recv(48) msg = s.recv(48)
val = struct.unpack("!I", msg[40:44])[0] 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)) RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
ntp_synced = True 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: finally:
s.close() s.close()