-
-
Notifications
You must be signed in to change notification settings - Fork 135
webapp.py
infinition edited this page Jul 5, 2024
·
1 revision
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.
- The
webapp.py
script imports several modules, including standard libraries (threading
,http.server
,socketserver
,logging
,sys
,signal
,os
,json
,subprocess
) and custom modules (Logger
,shared_data
).
-
should_exit
: A flag used to signal when the server should shut down.
- The logger is initialized to capture events and errors.
- Purpose: Initializes the request handler with shared data.
-
Key Steps:
-
Initialize Shared Data:
- Sets up
self.shared_data
with shared data.
- Sets up
-
Call Parent Constructor:
- Calls the parent constructor to complete initialization.
-
Initialize Shared Data:
- 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:
- Purpose: Handles GET requests to serve HTML files, configuration data, and the display image.
-
Key Steps:
-
Serve HTML Files:
- Calls
serve_file
forindex.html
andconfig.html
.
- Calls
-
Serve Configuration Data:
- Calls
serve_current_config
to serve the current configuration as JSON.
- Calls
-
Restore Default Configuration:
- Calls
restore_default_config
to reset configurations to default.
- Calls
-
Scan Wi-Fi Networks:
- Calls
scan_wifi
to scan for available Wi-Fi networks.
- Calls
-
Serve Display Image:
- Calls
serve_image
to serve the current display image.
- Calls
-
Serve HTML Files:
- Purpose: Handles POST requests to save configuration data and connect to Wi-Fi.
-
Key Steps:
-
Save Configuration:
- Calls
save_configuration
to save posted configuration data.
- Calls
-
Connect to Wi-Fi:
- Calls
connect_wifi
to connect to a specified Wi-Fi network.
- Calls
-
Save Configuration:
- 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:
- 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:
- 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:
- 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:
- Purpose: Scans for available Wi-Fi networks and returns the results as JSON.
-
Key Steps:
-
Scan Networks:
- Runs the
iwlist
command to scan for Wi-Fi networks.
- Runs the
-
Parse and Serve Results:
- Parses the scan results and serves them as JSON.
-
Scan Networks:
-
Purpose: Parses the scan output from
iwlist
to extract SSIDs. -
Key Steps:
-
Extract SSIDs:
- Parses the scan output to extract and return a list of SSIDs.
-
Extract SSIDs:
- 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
nmcli
command to connect to the network.
- Runs the
-
Read Credentials:
-
Purpose: Updates the
preconfigured.nmconnection
file 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:
- 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:
- 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:
- Purpose: Runs the web server in a separate thread.
-
Key Steps:
-
Start Server:
- Starts the web server and handles requests until
should_exit
is 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:
- Purpose: Shuts down the web server gracefully.
-
Key Steps:
-
Shutdown Server:
- Shuts down and closes the server.
-
Shutdown Server:
- Handles exit signals to shut down the web server cleanly.
-
Key Steps:
-
Set Exit Flag:
- Sets
should_exit
toTrue
to signal the server to stop.
- Sets
-
Shutdown Server:
- Calls
shutdown
on the web server thread and waits for it to finish.
- Calls
-
Set Exit Flag:
- An instance of the
WebThread
class is created with theCustomHandler
and the specified port.
- The main loop is started in a separate thread to handle web server requests.
- The web server handles GET and POST requests, serving HTML files, configuration data, and images, and processing configuration updates and Wi-Fi connections.
- When an exit signal is received, the
handle_exit_web
function sets the exit flag and shuts down the server cleanly.
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.