import socket import time import json class TempWebServer: """Simple web server for viewing temperatures and adjusting settings.""" def __init__(self, port=80): self.port = port self.socket = None self.sensors = {} def start(self): """Start the web server (non-blocking).""" try: self.socket = socket.socket() self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind(('0.0.0.0', self.port)) self.socket.listen(1) self.socket.setblocking(False) print("Web server started on port {}".format(self.port)) except Exception as e: print("Failed to start web server: {}".format(e)) def check_requests(self, sensors, ac_monitor=None, heater_monitor=None, schedule_monitor=None): """Check for incoming requests (call in main loop).""" if not self.socket: return try: conn, addr = self.socket.accept() conn.settimeout(3.0) request = conn.recv(1024).decode('utf-8') # Check if this is a POST request (form submission) if 'POST /update' in request: response = self._handle_update(request, sensors, ac_monitor, heater_monitor, schedule_monitor) elif 'POST /schedule' in request: response = self._handle_schedule_update(request, sensors, ac_monitor, heater_monitor, schedule_monitor) else: # Regular GET request response = self._get_status_page(sensors, ac_monitor, heater_monitor) # Make sure we have a valid response if response is None: print("Error: response is None, generating default page") response = self._get_status_page(sensors, ac_monitor, heater_monitor) conn.send('HTTP/1.1 200 OK\r\n') conn.send('Content-Type: text/html; charset=utf-8\r\n') conn.send('Connection: close\r\n\r\n') conn.sendall(response.encode('utf-8')) conn.close() except OSError: pass except Exception as e: print("Web server error: {}".format(e)) import sys sys.print_exception(e) def _save_config_to_file(self, config): """Save configuration to config.json file.""" try: with open('config.json', 'w') as f: json.dump(config, f) print("Settings saved to config.json") return True except Exception as e: print("Error saving config: {}".format(e)) return False def _load_config(self): """Load configuration from file.""" try: with open('config.json', 'r') as f: return json.load(f) except: return { 'ac_target': 77.0, 'ac_swing': 1.0, 'heater_target': 80.0, 'heater_swing': 2.0, 'schedules': [], 'schedule_enabled': False } def _handle_schedule_update(self, request, sensors, ac_monitor, heater_monitor, schedule_monitor): """Handle schedule form submission.""" try: body = request.split('\r\n\r\n')[1] if '\r\n\r\n' in request else '' params = {} for pair in body.split('&'): if '=' in pair: key, value = pair.split('=', 1) params[key] = value.replace('+', ' ') # Load current config config = self._load_config() # Update schedule enabled status config['schedule_enabled'] = params.get('schedule_enabled') == 'on' # Parse schedules schedules = [] for i in range(4): time_key = 'schedule_{}_time'.format(i) name_key = 'schedule_{}_name'.format(i) ac_key = 'schedule_{}_ac'.format(i) heater_key = 'schedule_{}_heater'.format(i) if time_key in params and params[time_key]: schedule = { 'time': params[time_key], 'name': params.get(name_key, 'Schedule {}'.format(i+1)), 'ac_target': float(params.get(ac_key, 77.0)), 'heater_target': float(params.get(heater_key, 80.0)) } schedules.append(schedule) config['schedules'] = schedules # Save to file if self._save_config_to_file(config): print("Schedule settings saved") # Reload schedule monitor config if schedule_monitor: schedule_monitor.reload_config(config) # Send Discord notification try: from scripts.discord_webhook import send_discord_message status = "enabled" if config['schedule_enabled'] else "disabled" message = "📅 Schedules updated ({}) - {} schedules configured".format( status, len(schedules) ) send_discord_message(message) except: pass except Exception as e: print("Error updating schedule: {}".format(e)) import sys sys.print_exception(e) return self._get_status_page(sensors, ac_monitor, heater_monitor, show_success=True) def _handle_update(self, request, sensors, ac_monitor, heater_monitor, schedule_monitor): """Handle form submission and update settings.""" try: body = request.split('\r\n\r\n')[1] if '\r\n\r\n' in request else '' params = {} for pair in body.split('&'): if '=' in pair: key, value = pair.split('=', 1) params[key] = float(value) # Load current config config = self._load_config() # Update AC settings if 'ac_target' in params and ac_monitor: ac_monitor.target_temp = params['ac_target'] config['ac_target'] = params['ac_target'] print("AC target updated to {}°F".format(params['ac_target'])) if 'ac_swing' in params and ac_monitor: ac_monitor.temp_swing = params['ac_swing'] config['ac_swing'] = params['ac_swing'] print("AC swing updated to {}°F".format(params['ac_swing'])) # Update heater settings if 'heater_target' in params and heater_monitor: heater_monitor.target_temp = params['heater_target'] config['heater_target'] = params['heater_target'] print("Heater target updated to {}°F".format(params['heater_target'])) if 'heater_swing' in params and heater_monitor: heater_monitor.temp_swing = params['heater_swing'] config['heater_swing'] = params['heater_swing'] print("Heater swing updated to {}°F".format(params['heater_swing'])) # Save settings to file if self._save_config_to_file(config): print("Settings persisted to disk") # Send Discord notification try: from scripts.discord_webhook import send_discord_message ac_target_str = str(params.get('ac_target', 'N/A')) ac_swing_str = str(params.get('ac_swing', 'N/A')) heater_target_str = str(params.get('heater_target', 'N/A')) heater_swing_str = str(params.get('heater_swing', 'N/A')) message = "Settings Updated - AC: {}F +/- {}F | Heater: {}F +/- {}F".format( ac_target_str, ac_swing_str, heater_target_str, heater_swing_str ) send_discord_message(message) except Exception as discord_error: print("Discord notification failed: {}".format(discord_error)) except Exception as e: print("Error updating settings: {}".format(e)) import sys sys.print_exception(e) return self._get_status_page(sensors, ac_monitor, heater_monitor, show_success=True) def _get_status_page(self, sensors, ac_monitor, heater_monitor, show_success=False): """Generate HTML status page.""" try: # Get current temperatures inside_temps = sensors['inside'].read_all_temps(unit='F') outside_temps = sensors['outside'].read_all_temps(unit='F') inside_temp = list(inside_temps.values())[0] if inside_temps else "N/A" outside_temp = list(outside_temps.values())[0] if outside_temps else "N/A" # Get AC/Heater status ac_status = "ON" if ac_monitor and ac_monitor.ac.get_state() else "OFF" heater_status = "ON" if heater_monitor and heater_monitor.heater.get_state() else "OFF" # Get current time current_time = time.localtime() time_str = "{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format( current_time[0], current_time[1], current_time[2], current_time[3], current_time[4], current_time[5] ) # Load config config = self._load_config() # Build schedule display schedule_status = "ENABLED" if config.get('schedule_enabled') else "DISABLED" schedule_color = "#2ecc71" if config.get('schedule_enabled') else "#95a5a6" schedule_icon = "✅" if config.get('schedule_enabled') else "⚠️" # Build schedule cards schedule_cards = "" if config.get('schedules'): for schedule in config.get('schedules', []): schedule_cards += """
{}".format(str(e))
def _build_schedule_form(self, config):
"""Build the schedule editing form."""
schedules = config.get('schedules', [])
# Pad with empty schedules up to 4
while len(schedules) < 4:
schedules.append({'time': '', 'name': '', 'ac_target': 77.0, 'heater_target': 80.0})
enabled_checked = 'checked' if config.get('schedule_enabled') else ''
form = """
"""
return form