-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_app.html
164 lines (149 loc) · 8.18 KB
/
doc_app.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>app.py - Documentation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>app.py - Documentation</h1>
<p>This HTML document provides detailed documentation for the Python script app.py.</p>
</header>
<section id="flow">
<h2>Flow</h2>
<p>
The `app.py` script follows a specific flow consisting of several steps:
</p>
<ol>
<li>
<strong>Initialize Camera:</strong>
<p>
At the beginning of the script, the camera is initialized using the `initialise_camera` function. This step involves the following:
<ul>
<li>Initialization of the IDS Peak library for camera communication.</li>
<li>Discovery and selection of available camera devices.</li>
<li>Configuration of camera settings such as exposure time, gain, and image cropping.</li>
<li>Setting up software triggering for image capture.</li>
</ul>
Any errors during this initialization process are logged and handled.
</p>
</li>
<li>
<strong>Start Background Thread for Barcode Scanning:</strong>
<p>
While the camera is initialized, the script may also start a background thread for barcode scanning. However, this part of the flow is not explicitly described in the code provided. Barcode scanning can be implemented separately and integrated into the script.
</p>
</li>
<li>
<section id="flask-app">
<h3>Start Flask App to Listen for Triggers</h3>
<p>
This section focuses on the steps involved in starting the Flask web application to listen for triggers, providing an interface for controlling the camera and managing firmware updates.
</p>
<ol>
<li>
<strong>Flask Web Application:</strong>
<p>
The script initiates a Flask web application to handle HTTP requests. Flask is a Python web framework that simplifies the creation of web-based applications. In this context, the Flask app serves as a bridge between external triggers and camera actions.
</p>
</li>
<li>
<strong>HTTP Routes:</strong>
<p>
The Flask app defines specific HTTP routes or endpoints that correspond to different functionalities:
<ul>
<li><code>GET /</code>: The root route serves as a health check endpoint, responding with a JSON object indicating the application's health status.</li>
<li><code>POST /</code>: This route is used to trigger camera capture. When a POST request is received, the script captures an image using the camera and processes it.</li>
<li><code>GET /ota</code>: This route handles requests related to Over-The-Air (OTA) firmware updates. It checks the current device firmware version and provides information about available firmware updates.</li>
<li><code>GET /files/firmware.py</code>: This route serves the firmware file for download if an update is available.</li>
<li><code>POST /logs</code> and <code>POST /log</code>: These routes receive logs from the device's firmware. The script logs messages of different severity levels (info, debug, warn, error).</li>
</ul>
</p>
</li>
<li>
<strong>Listening for Requests:</strong>
<p>
Once the Flask app is set up with its routes, it starts listening for incoming HTTP requests. The <code>app.run</code> method specifies the host and port on which the app will listen for requests. In this case, it is configured to listen on all available network interfaces ("<code>0.0.0.0</code>") and port <code>8000</code>.
</p>
</li>
</ol>
<p>
The Flask web application serves as the primary interface for controlling camera capture, checking firmware updates, and managing logs. It responds to various HTTP endpoints, allowing external systems to interact with the camera and firmware functionalities.
</p>
</section>
</li>
</ol>
<p>
The flow of `app.py` is designed to provide a web-based interface for triggering camera capture and managing firmware updates while concurrently handling camera initialization and potentially barcode scanning in the background.
</p>
</section>
<section id="imports">
<h2>Imports</h2>
<pre><code>
import logging
import json
import typer
import flask
import semver
from os import path, getcwd
from camera.camera_ids_cli import close_cam, initialise_camera, capture_optimised
from api_client import validate_image
from mdns import init_service
</code></pre>
<p>
The script imports various Python modules and libraries necessary for its functionality:
<ul>
<li><code>logging</code>: Used for logging events and information.</li>
<li><code>json</code>: Enables JSON data handling.</li>
<li><code>typer</code>: A library for building CLI applications.</li>
<li><code>flask</code>: A web framework for creating web applications and RESTful APIs.</li>
<li><code>semver</code>: Handles semantic versioning.</li>
<li><code>os.path</code>: Handles file paths and directories.</li>
<li><code>getcwd</code>: Retrieves the current working directory.</li>
<li><code>close_cam</code>: A function from the camera module to close the camera.</li>
<li><code>initialise_camera</code>: A function for camera initialization.</li>
<li><code>capture_optimised</code>: A function for optimized image capture from the camera.</li>
<li><code>validate_image</code>: A function from the API client module for image validation.</li>
<li><code>init_service</code>: Initializes a service for mDNS discovery.</li>
</ul>
</p>
</section>
<section id="variables">
<h2>Global Variables</h2>
<p>
The script defines several global variables that are used throughout the code:
<ul>
<li><code>cam</code>: Represents the camera object.</li>
<li><code>stream</code>: Represents the camera stream object.</li>
<li><code>exiting</code>: A boolean flag to control script termination.</li>
<li><code>firmware_fname</code>: The filename of the firmware script.</li>
<li><code>firmware_version</code>: Stores the firmware version extracted from the firmware script.</li>
</ul>
</p>
</section>
<section id="main-function">
<h2>Main Function</h2>
<pre><code>
def main(
device: int = 0,
logfile: str = "accumen_junior.log",
host: str = "0.0.0.0",
port: int = 8000,
skip: int = 2,
):
# Main function code...
if __name__ == "__main__":
typer.run(main)
</code></pre>
<p>
The <code>main</code> function serves as the entry point of the script. It accepts command-line arguments for configuration and performs the following tasks:
<ul>
<li>Initializes the camera and sets up a Flask app.</li>
<li>Configures logging and runs the app.</li>
</ul>
</p>
</section>
</body>
</html>