mirror of
https://github.com/infinition/Bjorn.git
synced 2025-12-05 22:01:45 +00:00
Clone
1
Bjorn.py
infinition edited this page 2024-07-06 01:00:45 +02:00
Bjorn.py
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.
Initialization and Start of Bjorn
Importing Modules
- Modules:
threading,signal,logging,time,sys,subprocess- Custom modules:
shared_data,Display,handle_exit_display,Commentaireia,web_thread,handle_exit_web,Orchestrator,NetworkScanner,Logger
Creating Bjorn Instance
- Code:
bjorn = Bjorn(shared_data) - Purpose: Initializes the
Bjornclass with shared data.
Starting Threads
- 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.
Handling Signals
- 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.
Bjorn Class
__init__ Method
- Purpose: Initializes the main components of the application, including the
Commentaireiainstance and a semaphore to limit concurrent threads.
run Method
- Purpose: Main loop for the
Bjornapplication. - 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:
Helper Functions
handle_exit
- 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:
handle_exit_webserver
- 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:
is_wifi_connected
- Purpose: Checks for Wi-Fi connectivity using the
nmclicommand. - Code:
result = subprocess.run(['nmcli', '-t', '-f', 'active', 'dev', 'wifi'], stdout=subprocess.PIPE, text=True) return 'yes' in result.stdout
Execution Flow
-
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:
Summary
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.