mirror of
https://github.com/infinition/Bjorn.git
synced 2025-12-06 06:11:46 +00:00
Clone
1
webapp.py
infinition edited this page 2024-07-06 01:17:23 +02:00
Table of Contents
- webapp.py WebApp
- Initialization and Start of WebApp
- CustomHandler Class
- __init__ Method
- log_message Method
- do_GET Method
- do_POST Method
- serve_file Method
- serve_current_config Method
- restore_default_config Method
- serve_image Method
- scan_wifi Method
- parse_scan_result Method
- connect_wifi Method
- update_nmconnection Method
- save_configuration Method
- WebThread Class
- handle_exit_web Function
- Execution Flow when webapp.py is Run
- Step 1: Initialize Web Thread
- Step 2: Start Main Loop
- Step 3: Handle Requests
- Step 4: Handle Exit Signal
- Summary
webapp.py WebApp
This document describes the detailed step-by-step process of how the webapp.py script works, including the specific methods, classes, and functions used at each step.
Initialization and Start of WebApp
Importing Modules
- The
webapp.pyscript imports several modules, including standard libraries (threading,http.server,socketserver,logging,sys,signal,os,json,subprocess) and custom modules (Logger,shared_data).
Global Variables
should_exit: A flag used to signal when the server should shut down.
Logger Initialization
- The logger is initialized to capture events and errors.
CustomHandler Class
__init__ Method
- Purpose: Initializes the request handler with shared data.
- Key Steps:
- Initialize Shared Data:
- Sets up
self.shared_datawith shared data.
- Sets up
- Call Parent Constructor:
- Calls the parent constructor to complete initialization.
- Initialize Shared Data:
log_message Method
- Purpose: Overrides the default logging behavior to suppress logging of GET requests.
- Key Steps:
- Log Non-GET Requests:
- Logs messages that are not GET requests.
- Log Non-GET Requests:
do_GET Method
- Purpose: Handles GET requests to serve HTML files, configuration data, and the display image.
- Key Steps:
- Serve HTML Files:
- Calls
serve_fileforindex.htmlandconfig.html.
- Calls
- Serve Configuration Data:
- Calls
serve_current_configto serve the current configuration as JSON.
- Calls
- Restore Default Configuration:
- Calls
restore_default_configto reset configurations to default.
- Calls
- Scan Wi-Fi Networks:
- Calls
scan_wifito scan for available Wi-Fi networks.
- Calls
- Serve Display Image:
- Calls
serve_imageto serve the current display image.
- Calls
- Serve HTML Files:
do_POST Method
- Purpose: Handles POST requests to save configuration data and connect to Wi-Fi.
- Key Steps:
- Save Configuration:
- Calls
save_configurationto save posted configuration data.
- Calls
- Connect to Wi-Fi:
- Calls
connect_wifito connect to a specified Wi-Fi network.
- Calls
- Save Configuration:
serve_file Method
- Purpose: Serves an HTML file with dynamic content injection.
- Key Steps:
- Read and Serve File:
- Reads the file, injects dynamic content, and serves it.
- Read and Serve File:
serve_current_config Method
- Purpose: Serves the current configuration as JSON.
- Key Steps:
- Read and Serve Configuration:
- Reads the configuration file and sends it as JSON.
- Read and Serve Configuration:
restore_default_config Method
- Purpose: Restores the default configuration and saves it to JSON.
- Key Steps:
- Restore and Save Configuration:
- Resets configurations to default, saves them, and serves the updated configuration.
- Restore and Save Configuration:
serve_image Method
- Purpose: Handles requests to serve the display image.
- Key Steps:
- Read and Serve Image:
- Reads and serves the current display image file.
- Read and Serve Image:
scan_wifi Method
- Purpose: Scans for available Wi-Fi networks and returns the results as JSON.
- Key Steps:
- Scan Networks:
- Runs the
iwlistcommand to scan for Wi-Fi networks.
- Runs the
- Parse and Serve Results:
- Parses the scan results and serves them as JSON.
- Scan Networks:
parse_scan_result Method
- Purpose: Parses the scan output from
iwlistto extract SSIDs. - Key Steps:
- Extract SSIDs:
- Parses the scan output to extract and return a list of SSIDs.
- Extract SSIDs:
connect_wifi Method
- Purpose: Connects to a specified Wi-Fi network using the provided SSID and password.
- Key Steps:
- Read Credentials:
- Reads the SSID and password from the POST request.
- Update Network Configuration:
- Updates the NetworkManager configuration with the new credentials.
- Connect to Network:
- Runs the
nmclicommand to connect to the network.
- Runs the
- Read Credentials:
update_nmconnection Method
- Purpose: Updates the
preconfigured.nmconnectionfile with the new SSID and password. - Key Steps:
- Write Configuration:
- Writes the new network configuration to the file.
- Set Permissions:
- Sets the correct permissions on the configuration file.
- Reload Connection:
- Reloads the NetworkManager connections.
- Write Configuration:
save_configuration Method
- Purpose: Saves the configuration posted from the client.
- Key Steps:
- Read and Update Configuration:
- Reads the posted configuration data, updates the current configuration, and saves it.
- Reload Configuration:
- Reloads the configuration to apply the changes.
- Read and Update Configuration:
WebThread Class
__init__ Method
- Purpose: Initializes the web server thread with the specified handler class and port.
- Key Steps:
- Initialize Attributes:
- Sets up
self.shared_data,self.port, andself.handler_class.
- Sets up
- Initialize Attributes:
run Method
- Purpose: Runs the web server in a separate thread.
- Key Steps:
- Start Server:
- Starts the web server and handles requests until
should_exitis set toTrue.
- Starts the web server and handles requests until
- Handle Port Conflicts:
- If the port is already in use, increments the port number and retries.
- Graceful Shutdown:
- Ensures the server is closed properly when shutting down.
- Start Server:
shutdown Method
- Purpose: Shuts down the web server gracefully.
- Key Steps:
- Shutdown Server:
- Shuts down and closes the server.
- Shutdown Server:
handle_exit_web Function
Purpose
- Handles exit signals to shut down the web server cleanly.
- Key Steps:
- Set Exit Flag:
- Sets
should_exittoTrueto signal the server to stop.
- Sets
- Shutdown Server:
- Calls
shutdownon the web server thread and waits for it to finish.
- Calls
- Set Exit Flag:
Execution Flow when webapp.py is Run
Step 1: Initialize Web Thread
- An instance of the
WebThreadclass is created with theCustomHandlerand the specified port.
Step 2: Start Main Loop
- The main loop is started in a separate thread to handle web server requests.
Step 3: Handle Requests
- The web server handles GET and POST requests, serving HTML files, configuration data, and images, and processing configuration updates and Wi-Fi connections.
Step 4: Handle Exit Signal
- When an exit signal is received, the
handle_exit_webfunction sets the exit flag and shuts down the server cleanly.
Summary
The webapp.py script manages the web interface of the e-ink display, serving HTML interface pages and the EPD image, handling GET and POST requests, and running a multithreaded web server for concurrent request handling. It ensures a clean exit on receiving system signals and captures events and errors for maintainability and ease of debugging.