feat: Improve schedule parsing and validation in web server

Fixes #12
This commit is contained in:
Aaron 2025-11-08 17:51:19 -05:00
parent 6ac3407cc2
commit 9c7ca86d86

View File

@ -194,34 +194,40 @@ class TempWebServer:
# ===== END: Handle mode actions ===== # ===== END: Handle mode actions =====
# ===== START: Handle schedule configuration save ===== # ===== START: Handle schedule configuration save =====
# Parse schedules from form # Parse schedules (4 slots)
schedules = [] schedules = []
for i in range(4): for i in range(4):
time_key = 'schedule_{}_time'.format(i) time_key = 'schedule{}_time'.format(i)
name_key = 'schedule_{}_name'.format(i) name_key = 'schedule{}_name'.format(i)
ac_key = 'schedule_{}_ac'.format(i) ac_key = 'schedule{}_ac'.format(i)
heater_key = 'schedule_{}_heater'.format(i) heater_key = 'schedule{}_heater'.format(i)
if time_key in params and params[time_key]: if time_key in params and params[time_key]:
# Validate time format (HH:MM) # URL decode the time (converts %3A back to :)
time_val = params[time_key] schedule_time = params[time_key].replace('%3A', ':')
if ':' not in time_val or len(time_val.split(':')) != 2:
print("Invalid time format: {}".format(time_val)) # Validate time format
if ':' not in schedule_time or len(schedule_time.split(':')) != 2:
print("Invalid time format: {}".format(schedule_time))
return 'HTTP/1.1 303 See Other\r\nLocation: /schedule\r\n\r\n' return 'HTTP/1.1 303 See Other\r\nLocation: /schedule\r\n\r\n'
try: try:
hours, mins = time_val.split(':') hours, mins = schedule_time.split(':')
if not (0 <= int(hours) <= 23 and 0 <= int(mins) <= 59): if not (0 <= int(hours) <= 23 and 0 <= int(mins) <= 59):
raise ValueError raise ValueError
except: except:
print("Invalid time value: {}".format(time_val)) print("Invalid time value: {}".format(schedule_time))
return 'HTTP/1.1 303 See Other\r\nLocation: /schedule\r\n\r\n' return 'HTTP/1.1 303 See Other\r\nLocation: /schedule\r\n\r\n'
# URL decode the name too
schedule_name = params.get(name_key, 'Schedule {}'.format(i+1)).replace('+', ' ')
# Create schedule entry
schedule = { schedule = {
'time': params[time_key], 'time': schedule_time,
'name': params.get(name_key, 'Schedule {}'.format(i+1)), 'name': schedule_name,
'ac_target': float(params.get(ac_key, 77.0)), 'ac_target': float(params.get(ac_key, 75.0)),
'heater_target': float(params.get(heater_key, 80.0)) 'heater_target': float(params.get(heater_key, 72.0))
} }
schedules.append(schedule) schedules.append(schedule)
@ -264,8 +270,8 @@ class TempWebServer:
pass pass
# ===== END: Handle schedule configuration save ===== # ===== END: Handle schedule configuration save =====
# Redirect back to schedule page # Redirect back to Dashboard
return 'HTTP/1.1 303 See Other\r\nLocation: /schedule\r\n\r\n' return 'HTTP/1.1 303 See Other\r\nLocation: /\r\n\r\n'
except Exception as e: except Exception as e:
print("Error updating schedule: {}".format(e)) print("Error updating schedule: {}".format(e))