-
-
Notifications
You must be signed in to change notification settings - Fork 142
Bjorn.py
infinition edited this page Jul 5, 2024
·
1 revision
This document describes the detailed step-by-step process of how the Bjorn.py
python file works, including the specific methods, classes, and functions used at each step.
This is the main Python File of Bjorn.
-
Modules:
-
threading
,signal
,logging
,time
,sys
,subprocess
- Custom modules:
shared_data
,Display
,handle_exit_display
,Commentaireia
,web_thread
,handle_exit_web
,Orchestrator
,NetworkScanner
,Logger
-
-
Code:
bjorn = Bjorn(shared_data)
-
Purpose: Initializes the
Bjorn
class with shared data.
-
Code:
display_thread = threading.Thread(target=display.run) bjorn_thread = threading.Thread(target=bjorn.run)
- Purpose: Creates and starts threads for the display and Bjorn's main operations.
-
Code:
signal.signal(signal.SIGINT, lambda sig, frame: handle_exit(sig, frame, display_thread, bjorn_thread)) signal.signal(signal.SIGTERM, lambda sig, frame: handle_exit_webserver(sig, frame))
- Purpose: Sets up signal handlers to ensure a clean exit when the application is terminated.
-
Purpose: Initializes the main components of the application, including the
Commentaireia
instance and a semaphore to limit concurrent threads.
-
Purpose: Main loop for the
Bjorn
application. -
Key Steps:
-
Wait for Startup Delay:
-
Condition:
if hasattr(self.shared_data, 'startup_delay') and self.shared_data.startup_delay > 0:
-
Action:
time.sleep(self.shared_data.startup_delay)
-
Condition:
-
Check Wi-Fi Connection:
-
Loop:
while not self.shared_data.should_exit and not is_wifi_connected():
-
Action:
time.sleep(5)
-
Loop:
-
Start Network Scanner and Orchestrator:
-
Code:
self.scanner = NetworkScanner(self.shared_data) with self.semaphore: self.scanner.scan() self.orchestrator = Orchestrator() with self.semaphore: self.orchestrator.run()
-
Code:
-
Wait for Startup Delay:
- Purpose: Handles the termination of the main and display threads.
-
Parameters:
sig
,frame
,display_thread
,bjorn_thread
-
Key Steps:
-
Set Exit Flag:
shared_data.should_exit = True
-
Handle Display Exit:
handle_exit_display(sig, frame, display_thread)
-
Join Threads:
if display_thread.is_alive(): display_thread.join() if bjorn_thread.is_alive(): bjorn_thread.join()
-
Set Exit Flag:
- Purpose: Handles the termination of the web server thread.
-
Parameters:
sig
,frame
-
Key Steps:
-
Set Exit Flag:
shared_data.should_exit = True
-
Handle Web Exit:
handle_exit_web(sig, frame)
-
Join Web Thread:
if web_thread.is_alive(): web_thread.join()
-
Set Exit Flag:
-
Purpose: Checks for Wi-Fi connectivity using the
nmcli
command. -
Code:
result = subprocess.run(['nmcli', '-t', '-f', 'active', 'dev', 'wifi'], stdout=subprocess.PIPE, text=True) return 'yes' in result.stdout
-
Start Threads:
-
Loading Configuration:
shared_data.load_config()
-
Starting Display Thread:
display = Display(shared_data) display_thread.start()
-
Starting Bjorn Thread:
bjorn_thread.start()
-
Starting Web Server Thread (if enabled):
if shared_data.config["websrv"]: web_thread.start()
-
Loading Configuration:
-
Handle Signals:
-
SIGINT:
signal.signal(signal.SIGINT, lambda sig, frame: handle_exit(sig, frame, display_thread, bjorn_thread))
-
SIGTERM:
signal.signal(signal.SIGTERM, lambda sig, frame: handle_exit_webserver(sig, frame))
-
SIGINT:
The Bjorn
application orchestrates various components including network scanning, display, and web server functionalities. The main operations are managed by the Bjorn
class, which ensures proper startup, operation, and clean termination of the application. Helper functions handle signal-based exits, ensuring that all threads are properly terminated.