openwebrx-clone/owrx/controllers.py

92 lines
3.6 KiB
Python
Raw Normal View History

import os
2019-05-03 20:59:24 +00:00
import mimetypes
from datetime import datetime
2019-05-04 14:56:23 +00:00
from owrx.websocket import WebSocketConnection
from owrx.config import PropertyManager
2019-05-12 16:10:24 +00:00
from owrx.source import ClientRegistry
from owrx.connection import WebSocketMessageHandler
from owrx.version import openwebrx_version
2019-05-03 20:59:24 +00:00
2019-05-10 19:50:58 +00:00
import logging
logger = logging.getLogger(__name__)
2019-05-03 20:59:24 +00:00
class Controller(object):
def __init__(self, handler, matches):
self.handler = handler
self.matches = matches
2019-05-09 14:12:05 +00:00
def send_response(self, content, code = 200, content_type = "text/html", last_modified: datetime = None, max_age = None):
2019-05-03 20:59:24 +00:00
self.handler.send_response(code)
if content_type is not None:
self.handler.send_header("Content-Type", content_type)
2019-05-09 14:12:05 +00:00
if last_modified is not None:
self.handler.send_header("Last-Modified", last_modified.strftime("%a, %d %b %Y %H:%M:%S GMT"))
if max_age is not None:
self.handler.send_header("Cache-Control", "max-age: {0}".format(max_age))
2019-05-03 20:59:24 +00:00
self.handler.end_headers()
if (type(content) == str):
content = content.encode()
self.handler.wfile.write(content)
def render_template(self, template, **variables):
f = open('htdocs/' + template)
data = f.read()
f.close()
self.send_response(data)
class StatusController(Controller):
def handle_request(self):
2019-05-10 21:00:18 +00:00
pm = PropertyManager.getSharedInstance()
# TODO keys that have been left out since they are no longer simple strings: sdr_hw, bands, antenna
vars = {
"status": "active",
"name": pm["receiver_name"],
"op_email": pm["receiver_admin"],
2019-05-12 16:10:24 +00:00
"users": ClientRegistry.getSharedInstance().clientCount(),
2019-05-10 21:00:18 +00:00
"users_max": pm["max_clients"],
"gps": pm["receiver_gps"],
"asl": pm["receiver_asl"],
"loc": pm["receiver_location"],
"sw_version": openwebrx_version,
2019-05-10 21:00:18 +00:00
"avatar_ctime": os.path.getctime("htdocs/gfx/openwebrx-avatar.png")
}
self.send_response("\n".join(["{key}={value}".format(key = key, value = value) for key, value in vars.items()]))
2019-05-03 20:59:24 +00:00
class AssetsController(Controller):
2019-05-09 14:12:05 +00:00
def serve_file(self, file, content_type = None):
2019-05-04 14:56:23 +00:00
try:
2019-05-09 14:12:05 +00:00
modified = datetime.fromtimestamp(os.path.getmtime('htdocs/' + file))
if "If-Modified-Since" in self.handler.headers:
client_modified = datetime.strptime(self.handler.headers["If-Modified-Since"], "%a, %d %b %Y %H:%M:%S %Z")
if modified <= client_modified:
self.send_response("", code = 304)
return
2019-05-04 14:56:23 +00:00
f = open('htdocs/' + file, 'rb')
data = f.read()
f.close()
2019-05-09 14:12:05 +00:00
if content_type is None:
(content_type, encoding) = mimetypes.MimeTypes().guess_type(file)
self.send_response(data, content_type = content_type, last_modified = modified, max_age = 3600)
2019-05-04 14:56:23 +00:00
except FileNotFoundError:
self.send_response("file not found", code = 404)
2019-05-03 20:59:24 +00:00
def handle_request(self):
filename = self.matches.group(1)
2019-05-04 14:56:23 +00:00
self.serve_file(filename)
2019-05-09 14:12:05 +00:00
class IndexController(AssetsController):
def handle_request(self):
self.serve_file("index.html", content_type = "text/html")
2019-05-09 14:12:05 +00:00
2019-07-01 14:49:39 +00:00
class MapController(AssetsController):
def handle_request(self):
2019-07-01 17:49:58 +00:00
#TODO check if we have a google maps api key first?
2019-07-01 14:49:39 +00:00
self.serve_file("map.html", content_type = "text/html")
2019-05-04 18:26:11 +00:00
class WebSocketController(Controller):
def handle_request(self):
conn = WebSocketConnection(self.handler, WebSocketMessageHandler())
# enter read loop
conn.read_loop()