Skip to content

Commit 181aa44

Browse files
committed
Add Waitress
1 parent be9ede0 commit 181aa44

File tree

6 files changed

+37
-47
lines changed

6 files changed

+37
-47
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
#USERNAME: "username" # Optional
1717
#PWC_PASSWORD: "your-password" # Optional. Must be 8 characters or more.
1818

19-
## System Variables ##
19+
## Required System Variables ##
2020
DBUS_SYSTEM_BUS_ADDRESS: "unix:path=/host/run/dbus/system_bus_socket"
2121
build:
2222
context: .

src/common/errors.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
# Create custom logger
55
logger = logging.getLogger('syslog')
66
syslog = logging.StreamHandler()
7-
formatter = logging.Formatter('[%(asctime)s] - [%(levelname)s] - [%(module)s:'
8-
'%(lineno)d] - %(message)s', "%Y-%m-%d %H:%M:%S")
9-
syslog.setFormatter(formatter)
107
logger.addHandler(syslog)
11-
logger.setLevel(logging.INFO)
128

13-
# Change default logging mode when in development environmnets
14-
if config.dev_mode:
9+
# When in development mode provide details in log of each line of code
10+
# that is executing
11+
if config.dev_mode is True:
12+
formatter = logging.Formatter('[%(asctime)s] - [%(levelname)s] - '
13+
'[%(module)s:%(lineno)d] - %(message)s',
14+
'%Y-%m-%d %H:%M:%S')
1515
logger.setLevel(logging.DEBUG)
16+
else:
17+
formatter = logging.Formatter('[%(asctime)s] - [%(levelname)s] - '
18+
'%(message)s', '%Y-%m-%d %H:%M:%S')
19+
logger.setLevel(logging.INFO)
20+
21+
syslog.setFormatter(formatter)
1622

1723

1824
# Error classes for Flask-Restful

src/common/wifi.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def auto_connect(ssid=None,
6161
break
6262
else:
6363
logger.info('Auto-connect failed as the device could not find the '
64-
'specified network.')
64+
'specified network. Starting hotspot instead...')
6565
connect()
6666

6767

@@ -108,7 +108,7 @@ def connect(conn_type=config.type_hotspot,
108108

109109
try:
110110
NetworkManager.Settings.AddConnection(conn_dict)
111-
logger.info(f"Added connection of type {conn_type}")
111+
logger.debug(f"Added connection of type {conn_type}")
112112

113113
# Find this connection and its device
114114
connections = \
@@ -139,7 +139,7 @@ def connect(conn_type=config.type_hotspot,
139139
break
140140

141141
if dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED:
142-
logger.info("Connected.")
142+
logger.info("Active.")
143143
return True
144144
# If the current attempt is not already a hotspot attempt
145145
elif conn_type != config.type_hotspot:
@@ -170,14 +170,14 @@ def forget(create_new_hotspot=False, all_networks=False):
170170
# Delete the identified connection
171171
network_id = connection.GetSettings()["connection"]["id"]
172172
connection.Delete()
173-
logger.info(f"Deleted connection: {network_id}")
173+
logger.debug(f"Deleted connection: {network_id}")
174174
else:
175175
connection_ids = \
176176
dict([(x.GetSettings()['connection']['id'], x)
177177
for x in connections])
178178
if config.ap_name in connection_ids:
179179
connection_ids[config.ap_name].Delete()
180-
logger.info(f"Deleted connection: {config.ap_name}")
180+
logger.debug(f"Deleted connection: {config.ap_name}")
181181

182182
if create_new_hotspot:
183183
refresh_networks()

src/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Flask-RESTful
22
python-networkmanager
3+
waitress

src/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ six==1.16.0
2828
# via
2929
# flask-restful
3030
# python-networkmanager
31+
waitress==2.0.0
32+
# via -r requirements.in
3133
werkzeug==2.0.2
3234
# via flask

src/run.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import atexit
21
import config
3-
import signal
4-
import sys
52
import time
63
from common.errors import errors
74
from common.errors import logger
@@ -19,30 +16,27 @@
1916
from resources.wifi_routes import wifi_list_access_points
2017
from flask import Flask
2118
from flask_restful import Api
19+
from waitress import serve
2220

2321

24-
def handle_exit(*args):
25-
logger.info('Finshed the exit process.')
22+
# Create Flask app instance
23+
app = Flask(__name__)
2624

25+
# Load Flask-Restful API
26+
api = Api(app, errors=errors)
2727

28-
def handle_sigterm(*args):
29-
sys.exit(0)
28+
# Health check routes
29+
api.add_resource(system_health_check, '/healthcheck')
3030

31+
# Wi-Fi routes
32+
api.add_resource(wifi_connect, '/v1/connect')
33+
api.add_resource(wifi_connection_status, '/v1/connection_status')
34+
api.add_resource(wifi_forget, '/v1/forget')
35+
api.add_resource(wifi_list_access_points, '/v1/list_access_points')
3136

32-
# Startup process
3337
if __name__ == '__main__':
34-
logger.info('Starting...')
35-
# Load Flask-Restful API
36-
api = Api(errors=errors)
37-
38-
# Create Flask app instance
39-
app = Flask(__name__)
40-
41-
# Ensure soft shutdown to terminate wifi-connect
42-
atexit.register(handle_exit, None, None)
43-
signal.signal(signal.SIGHUP, handle_sigterm)
44-
signal.signal(signal.SIGINT, handle_sigterm)
45-
signal.signal(signal.SIGTERM, handle_sigterm)
38+
# Begin loading program
39+
logger.info('Checking for previously configured Wi-Fi connections...')
4640

4741
# Start dnsmasq service for assigning IPs to connected devices
4842
dnsmasq()
@@ -53,6 +47,7 @@ def handle_sigterm(*args):
5347
# If the Wi-Fi connection is not already active, start a hotspot
5448
if check_wifi_status():
5549
logger.info('Wi-Fi connection already established.')
50+
logger.info('Ready...')
5651
else:
5752
refresh_networks(retries=1)
5853
if config.auto_connect_kargs:
@@ -62,18 +57,4 @@ def handle_sigterm(*args):
6257
logger.info('Starting hotspot...')
6358
connect()
6459

65-
# Configure endpoints #
66-
67-
# Health check
68-
api.add_resource(system_health_check, '/healthcheck')
69-
70-
# Wi-Fi
71-
api.add_resource(wifi_connect, '/v1/connect')
72-
api.add_resource(wifi_connection_status, '/v1/connection_status')
73-
api.add_resource(wifi_forget, '/v1/forget')
74-
api.add_resource(wifi_list_access_points, '/v1/list_access_points')
75-
76-
# Initialise and start
77-
api.init_app(app)
78-
79-
app.run(port=port, host=host)
60+
serve(app, host=host, port=port)

0 commit comments

Comments
 (0)