Use consistent variable and method naming
This commit is contained in:
parent
838efe083e
commit
0804b865f9
6
main.py
6
main.py
|
@ -45,7 +45,7 @@ class Marshaller(Server):
|
||||||
self.leds_off()
|
self.leds_off()
|
||||||
super().work()
|
super().work()
|
||||||
|
|
||||||
if util.secondsElapsed(ticks, self.ntp_ticks) > self.ntp_interval_in_seconds:
|
if util.seconds_elapsed(ticks, self.ntp_ticks) > self.ntp_interval_in_seconds:
|
||||||
self.ntp_ticks = ticks
|
self.ntp_ticks = ticks
|
||||||
ntp.sync()
|
ntp.sync()
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class Marshaller(Server):
|
||||||
return self.activation_switch.value() == 0 or self.is_http_activation
|
return self.activation_switch.value() == 0 or self.is_http_activation
|
||||||
|
|
||||||
def is_activation_expired(self, ticks):
|
def is_activation_expired(self, ticks):
|
||||||
return self.is_http_activation and util.secondsElapsed(ticks, self.last_http_activation_ticks) > self.http_activation_interval_in_seconds
|
return self.is_http_activation and util.seconds_elapsed(ticks, self.last_http_activation_ticks) > self.http_activation_interval_in_seconds
|
||||||
|
|
||||||
def show_color(self):
|
def show_color(self):
|
||||||
distance_in_inches = self.get_buffered_distance_in_inches()
|
distance_in_inches = self.get_buffered_distance_in_inches()
|
||||||
|
@ -88,7 +88,7 @@ class Marshaller(Server):
|
||||||
def get_distance_in_inches(self):
|
def get_distance_in_inches(self):
|
||||||
return self.distance_reading.read_u16() / 65535 * 1024 * 5 * 0.03937008
|
return self.distance_reading.read_u16() / 65535 * 1024 * 5 * 0.03937008
|
||||||
|
|
||||||
def handlePath(self, path):
|
def handle_path(self, path):
|
||||||
if (path == 'on'):
|
if (path == 'on'):
|
||||||
logging.info('activated via http')
|
logging.info('activated via http')
|
||||||
return self.http_activation()
|
return self.http_activation()
|
||||||
|
|
14
net/http.py
14
net/http.py
|
@ -1,7 +1,7 @@
|
||||||
okResponse = 'HTTP/1.1 200 OK\r\ncontent-type: text/html\r\n\r\n'.encode('ascii')
|
ok_response = 'HTTP/1.1 200 OK\r\ncontent-type: text/html\r\n\r\n'.encode('ascii')
|
||||||
okTextResponse = 'HTTP/1.1 200 OK\r\ncontent-type: text/plain\r\n\r\n'.encode('ascii')
|
ok_text_response = 'HTTP/1.1 200 OK\r\ncontent-type: text/plain\r\n\r\n'.encode('ascii')
|
||||||
okJsonResponse = 'HTTP/1.1 200 OK\r\ncontent-type: application/json\r\n\r\n'.encode('ascii')
|
ok_json_response = 'HTTP/1.1 200 OK\r\ncontent-type: application/json\r\n\r\n'.encode('ascii')
|
||||||
okIconResponse = 'HTTP/1.1 200 OK\r\ncontent-type: image/x-icon\r\n\r\n'.encode('ascii')
|
ok_icon_response = 'HTTP/1.1 200 OK\r\ncontent-type: image/x-icon\r\n\r\n'.encode('ascii')
|
||||||
noContentResponse = 'HTTP/1.1 204 No Content\r\n\r\n'.encode('ascii')
|
no_content_response = 'HTTP/1.1 204 No Content\r\n\r\n'.encode('ascii')
|
||||||
notFoundResponse = 'HTTP/1.1 404 Not Found\r\n\r\n'.encode('ascii')
|
not_found_response = 'HTTP/1.1 404 Not Found\r\n\r\n'.encode('ascii')
|
||||||
serverErrorResponse = 'HTTP/1.1 500 Internal Server Error\r\n\r\n'.encode('ascii')
|
server_error_response = 'HTTP/1.1 500 Internal Server Error\r\n\r\n'.encode('ascii')
|
||||||
|
|
|
@ -32,11 +32,11 @@ class Server:
|
||||||
self.serve()
|
self.serve()
|
||||||
self.work()
|
self.work()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logException(e)
|
self.log_exception(e)
|
||||||
finally:
|
finally:
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
|
|
||||||
def logException(self, e):
|
def log_exception(self, e):
|
||||||
buf = io.StringIO()
|
buf = io.StringIO()
|
||||||
sys.print_exception(e, buf)
|
sys.print_exception(e, buf)
|
||||||
logging.debug(f'exception:', buf.getvalue())
|
logging.debug(f'exception:', buf.getvalue())
|
||||||
|
@ -58,42 +58,42 @@ class Server:
|
||||||
logging.info(f'client connected from {addr}')
|
logging.info(f'client connected from {addr}')
|
||||||
request = conn.recv(1024).decode('utf-8').strip()
|
request = conn.recv(1024).decode('utf-8').strip()
|
||||||
|
|
||||||
self.handleRequest(conn, request)
|
self.handle_request(conn, request)
|
||||||
except:
|
except:
|
||||||
conn.write(http.serverErrorResponse)
|
conn.write(http.server_error_response)
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
def handleRequest(self, conn, request):
|
def handle_request(self, conn, request):
|
||||||
[method, path, _protocol] = request.partition('\n')[0].split()
|
[method, path, _protocol] = request.partition('\n')[0].split()
|
||||||
|
|
||||||
logging.info(f'{method} {path}')
|
logging.info(f'{method} {path}')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if method == 'GET':
|
if method == 'GET':
|
||||||
response = self.handlePath(path.strip('/'))
|
response = self.handle_path(path.strip('/'))
|
||||||
|
|
||||||
conn.write(self.getContentType(path, response))
|
conn.write(self.get_content_type(path, response))
|
||||||
conn.write(response)
|
conn.write(response)
|
||||||
else:
|
else:
|
||||||
conn.write(http.notFoundResponse)
|
conn.write(http.not_found_response)
|
||||||
except OSError:
|
except OSError:
|
||||||
conn.write(http.notFoundResponse)
|
conn.write(http.not_found_response)
|
||||||
|
|
||||||
def getContentType(self, path, response):
|
def get_content_type(self, path, response):
|
||||||
if path.endswith('.txt'):
|
if path.endswith('.txt'):
|
||||||
return http.okTextResponse
|
return http.ok_text_response
|
||||||
elif path.endswith('.json'):
|
elif path.endswith('.json'):
|
||||||
return http.okJsonResponse
|
return http.ok_json_response
|
||||||
elif path.endswith('.ico'):
|
elif path.endswith('.ico'):
|
||||||
return http.okIconResponse
|
return http.ok_icon_response
|
||||||
elif response == '':
|
elif response == '':
|
||||||
return http.noContentResponse
|
return http.no_content_response
|
||||||
|
|
||||||
return http.okResponse
|
return http.ok_response
|
||||||
|
|
||||||
def handlePath(self, _path):
|
def handle_path(self, _path):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def work(self):
|
def work(self):
|
||||||
|
|
|
@ -20,7 +20,7 @@ def render(template, **kwargs):
|
||||||
result = (
|
result = (
|
||||||
result +
|
result +
|
||||||
data[tokenCaret:start] +
|
data[tokenCaret:start] +
|
||||||
replaceToken(token, kwargs)
|
replace_token(token, kwargs)
|
||||||
)
|
)
|
||||||
|
|
||||||
tokenCaret = end + endLength
|
tokenCaret = end + endLength
|
||||||
|
@ -30,7 +30,7 @@ def render(template, **kwargs):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def replaceToken(token, values):
|
def replace_token(token, values):
|
||||||
result = str(values[token]) if token in values else ''
|
result = str(values[token]) if token in values else ''
|
||||||
result = result.replace('&', '&')
|
result = result.replace('&', '&')
|
||||||
result = result.replace('"', '"')
|
result = result.replace('"', '"')
|
||||||
|
|
|
@ -8,14 +8,15 @@ def datetime():
|
||||||
return '{0:04d}-{1:02d}-{2:02d} {4:02d}:{5:02d}:{6:02d} UTC'.format(*dt)
|
return '{0:04d}-{1:02d}-{2:02d} {4:02d}:{5:02d}:{6:02d} UTC'.format(*dt)
|
||||||
|
|
||||||
|
|
||||||
def datetimeISO8601():
|
def datetime_iso8601():
|
||||||
dt = machine.RTC().datetime()
|
dt = machine.RTC().datetime()
|
||||||
|
|
||||||
return '{0:04d}-{1:02d}-{2:02d}T{4:02d}:{5:02d}:{6:02d}Z'.format(*dt)
|
return '{0:04d}-{1:02d}-{2:02d}T{4:02d}:{5:02d}:{6:02d}Z'.format(*dt)
|
||||||
|
|
||||||
|
|
||||||
def secondsElapsed(ticks1, ticks2):
|
def seconds_elapsed(ticks1, ticks2):
|
||||||
return time.ticks_diff(ticks1, ticks2) / 1000
|
return time.ticks_diff(ticks1, ticks2) / 1000
|
||||||
|
|
||||||
def millisecondsElapsed(ticks1, ticks2):
|
|
||||||
|
def milliseconds_elapsed(ticks1, ticks2):
|
||||||
return time.ticks_diff(ticks1, ticks2)
|
return time.ticks_diff(ticks1, ticks2)
|
||||||
|
|
|
@ -14,13 +14,13 @@ class WifiConnectionError(RuntimeError):
|
||||||
def connect():
|
def connect():
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
return connectToWifi()
|
return connect_to_wifi()
|
||||||
except WifiConnectionError as e:
|
except WifiConnectionError as e:
|
||||||
logging.error(e.value)
|
logging.error(e.value)
|
||||||
time.sleep(180)
|
time.sleep(180)
|
||||||
|
|
||||||
|
|
||||||
def connectToWifi():
|
def connect_to_wifi():
|
||||||
rp2.country('US')
|
rp2.country('US')
|
||||||
network.hostname(config['hostname'])
|
network.hostname(config['hostname'])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue