diff --git a/main.py b/main.py index 7325c3e..c175332 100644 --- a/main.py +++ b/main.py @@ -45,7 +45,7 @@ class Marshaller(Server): self.leds_off() 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 ntp.sync() @@ -53,7 +53,7 @@ class Marshaller(Server): return self.activation_switch.value() == 0 or self.is_http_activation 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): distance_in_inches = self.get_buffered_distance_in_inches() @@ -88,7 +88,7 @@ class Marshaller(Server): def get_distance_in_inches(self): return self.distance_reading.read_u16() / 65535 * 1024 * 5 * 0.03937008 - def handlePath(self, path): + def handle_path(self, path): if (path == 'on'): logging.info('activated via http') return self.http_activation() diff --git a/net/http.py b/net/http.py index 12fdbcc..c4ee68e 100644 --- a/net/http.py +++ b/net/http.py @@ -1,7 +1,7 @@ -okResponse = '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') -okJsonResponse = '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') -noContentResponse = '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') -serverErrorResponse = 'HTTP/1.1 500 Internal Server Error\r\n\r\n'.encode('ascii') +ok_response = 'HTTP/1.1 200 OK\r\ncontent-type: text/html\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') +ok_json_response = 'HTTP/1.1 200 OK\r\ncontent-type: application/json\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') +no_content_response = 'HTTP/1.1 204 No Content\r\n\r\n'.encode('ascii') +not_found_response = 'HTTP/1.1 404 Not Found\r\n\r\n'.encode('ascii') +server_error_response = 'HTTP/1.1 500 Internal Server Error\r\n\r\n'.encode('ascii') diff --git a/net/server.py b/net/server.py index c1ea1dd..e098aa2 100644 --- a/net/server.py +++ b/net/server.py @@ -32,11 +32,11 @@ class Server: self.serve() self.work() except Exception as e: - self.logException(e) + self.log_exception(e) finally: self.cleanup() - def logException(self, e): + def log_exception(self, e): buf = io.StringIO() sys.print_exception(e, buf) logging.debug(f'exception:', buf.getvalue()) @@ -58,42 +58,42 @@ class Server: logging.info(f'client connected from {addr}') request = conn.recv(1024).decode('utf-8').strip() - self.handleRequest(conn, request) + self.handle_request(conn, request) except: - conn.write(http.serverErrorResponse) + conn.write(http.server_error_response) raise finally: conn.close() - def handleRequest(self, conn, request): + def handle_request(self, conn, request): [method, path, _protocol] = request.partition('\n')[0].split() logging.info(f'{method} {path}') try: 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) else: - conn.write(http.notFoundResponse) + conn.write(http.not_found_response) 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'): - return http.okTextResponse + return http.ok_text_response elif path.endswith('.json'): - return http.okJsonResponse + return http.ok_json_response elif path.endswith('.ico'): - return http.okIconResponse + return http.ok_icon_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 '' def work(self): diff --git a/net/templates.py b/net/templates.py index f2be3d5..4802930 100644 --- a/net/templates.py +++ b/net/templates.py @@ -20,7 +20,7 @@ def render(template, **kwargs): result = ( result + data[tokenCaret:start] + - replaceToken(token, kwargs) + replace_token(token, kwargs) ) tokenCaret = end + endLength @@ -30,7 +30,7 @@ def render(template, **kwargs): return result -def replaceToken(token, values): +def replace_token(token, values): result = str(values[token]) if token in values else '' result = result.replace('&', '&') result = result.replace('"', '"') diff --git a/net/util.py b/net/util.py index 4170548..490df2a 100644 --- a/net/util.py +++ b/net/util.py @@ -8,14 +8,15 @@ def datetime(): 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() 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 -def millisecondsElapsed(ticks1, ticks2): + +def milliseconds_elapsed(ticks1, ticks2): return time.ticks_diff(ticks1, ticks2) diff --git a/net/wifi.py b/net/wifi.py index eec5445..acf1853 100644 --- a/net/wifi.py +++ b/net/wifi.py @@ -14,13 +14,13 @@ class WifiConnectionError(RuntimeError): def connect(): while True: try: - return connectToWifi() + return connect_to_wifi() except WifiConnectionError as e: logging.error(e.value) time.sleep(180) -def connectToWifi(): +def connect_to_wifi(): rp2.country('US') network.hostname(config['hostname'])