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:
parent
24b53b9446
commit
b6aae121bb
@ -437,9 +437,29 @@ 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:
|
||||
from scripts.discord_webhook import send_discord_message
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
{
|
||||
"timezone_offset": -5,
|
||||
"ac_target": 77.0,
|
||||
"ac_swing": 1.0,
|
||||
"heater_target": 72.0,
|
||||
|
||||
29
main.py
29
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()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user