2019-05-03 20:59:24 +00:00
|
|
|
import mimetypes
|
2019-05-04 14:56:23 +00:00
|
|
|
from owrx.websocket import WebSocketConnection
|
|
|
|
from owrx.config import PropertyManager
|
2019-05-10 20:47:40 +00:00
|
|
|
from owrx.source import DspManager, CpuUsageThread, SdrService, ClientReporterThread
|
2019-05-11 12:18:43 +00:00
|
|
|
from owrx.version import openwebrx_version
|
2019-05-04 18:26:11 +00:00
|
|
|
import json
|
2019-05-09 14:12:05 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
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"],
|
|
|
|
"users": ClientReporterThread.getSharedInstance().clientCount(),
|
|
|
|
"users_max": pm["max_clients"],
|
|
|
|
"gps": pm["receiver_gps"],
|
|
|
|
"asl": pm["receiver_asl"],
|
|
|
|
"loc": pm["receiver_location"],
|
2019-05-11 12:18:43 +00:00
|
|
|
"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.wrx", "text/html")
|
|
|
|
|
2019-05-07 13:21:16 +00:00
|
|
|
class OpenWebRxClient(object):
|
2019-05-09 20:44:29 +00:00
|
|
|
config_keys = ["waterfall_colors", "waterfall_min_level", "waterfall_max_level",
|
|
|
|
"waterfall_auto_level_margin", "lfo_offset", "samp_rate", "fft_size", "fft_fps",
|
|
|
|
"audio_compression", "fft_compression", "max_clients", "start_mod",
|
2019-05-11 15:55:32 +00:00
|
|
|
"client_audio_buffer_size", "start_freq", "center_freq", "mathbox_waterfall_colors",
|
|
|
|
"mathbox_waterfall_history_length", "mathbox_waterfall_frequency_resolution"]
|
2019-05-04 18:26:11 +00:00
|
|
|
def __init__(self, conn):
|
|
|
|
self.conn = conn
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2019-05-10 20:47:40 +00:00
|
|
|
ClientReporterThread.getSharedInstance().addClient(self)
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
self.dsp = None
|
|
|
|
self.sdr = None
|
|
|
|
self.configProps = None
|
|
|
|
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
|
|
|
|
|
|
|
self.setSdr()
|
|
|
|
|
|
|
|
# send receiver info
|
|
|
|
receiver_keys = ["receiver_name", "receiver_location", "receiver_qra", "receiver_asl", "receiver_gps",
|
|
|
|
"photo_title", "photo_desc"]
|
|
|
|
receiver_details = dict((key, pm.getPropertyValue(key)) for key in receiver_keys)
|
|
|
|
self.write_receiver_details(receiver_details)
|
|
|
|
|
2019-05-10 14:14:16 +00:00
|
|
|
profiles = [{"name": s.getName() + " " + p["name"], "id":sid + "|" + pid} for (sid, s) in SdrService.getSources().items() for (pid, p) in s.getProfiles().items()]
|
|
|
|
self.write_profiles(profiles)
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
CpuUsageThread.getSharedInstance().add_client(self)
|
|
|
|
|
|
|
|
def sendConfig(self, key, value):
|
|
|
|
config = dict((key, self.configProps[key]) for key in OpenWebRxClient.config_keys)
|
|
|
|
# TODO mathematical properties? hmmmm
|
|
|
|
config["start_offset_freq"] = self.configProps["start_freq"] - self.configProps["center_freq"]
|
|
|
|
self.write_config(config)
|
|
|
|
def setSdr(self, id = None):
|
2019-05-10 22:38:46 +00:00
|
|
|
next = SdrService.getSource(id)
|
|
|
|
if (next == self.sdr):
|
|
|
|
return
|
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
self.stopDsp()
|
|
|
|
|
|
|
|
if self.configProps is not None:
|
|
|
|
self.configProps.unwire(self.sendConfig)
|
|
|
|
|
2019-05-10 22:38:46 +00:00
|
|
|
self.sdr = next
|
2019-05-09 20:44:29 +00:00
|
|
|
|
|
|
|
# send initial config
|
|
|
|
self.configProps = self.sdr.getProps().collect(*OpenWebRxClient.config_keys).defaults(PropertyManager.getSharedInstance())
|
|
|
|
|
|
|
|
self.configProps.wire(self.sendConfig)
|
|
|
|
self.sendConfig(None, None)
|
|
|
|
|
2019-05-10 18:59:06 +00:00
|
|
|
self.sdr.addSpectrumClient(self)
|
2019-05-09 20:44:29 +00:00
|
|
|
|
|
|
|
def startDsp(self):
|
|
|
|
if self.dsp is None:
|
|
|
|
self.dsp = DspManager(self, self.sdr)
|
|
|
|
self.dsp.start()
|
|
|
|
|
2019-05-10 18:08:22 +00:00
|
|
|
def close(self):
|
|
|
|
self.stopDsp()
|
|
|
|
CpuUsageThread.getSharedInstance().remove_client(self)
|
2019-05-10 20:47:40 +00:00
|
|
|
try:
|
|
|
|
ClientReporterThread.getSharedInstance().removeClient(self)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("connection closed")
|
2019-05-10 18:08:22 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
def stopDsp(self):
|
|
|
|
if self.dsp is not None:
|
|
|
|
self.dsp.stop()
|
|
|
|
self.dsp = None
|
|
|
|
if self.sdr is not None:
|
2019-05-10 18:59:06 +00:00
|
|
|
self.sdr.removeSpectrumClient(self)
|
2019-05-09 20:44:29 +00:00
|
|
|
|
|
|
|
def setParams(self, params):
|
|
|
|
# only the keys in the protected property manager can be overridden from the web
|
|
|
|
protected = self.sdr.getProps().collect("samp_rate", "center_freq", "rf_gain", "type") \
|
|
|
|
.defaults(PropertyManager.getSharedInstance())
|
|
|
|
for key, value in params.items():
|
|
|
|
protected[key] = value
|
|
|
|
|
|
|
|
def setDspProperties(self, params):
|
|
|
|
for key, value in params.items():
|
|
|
|
self.dsp.setProperty(key, value)
|
|
|
|
|
2019-05-10 18:08:22 +00:00
|
|
|
def protected_send(self, data):
|
|
|
|
try:
|
|
|
|
self.conn.send(data)
|
|
|
|
# these exception happen when the socket is closed
|
|
|
|
except OSError:
|
|
|
|
self.close()
|
|
|
|
except ValueError:
|
|
|
|
self.close()
|
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
def write_spectrum_data(self, data):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send(bytes([0x01]) + data)
|
2019-05-04 21:11:13 +00:00
|
|
|
def write_dsp_data(self, data):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send(bytes([0x02]) + data)
|
2019-05-05 14:17:55 +00:00
|
|
|
def write_s_meter_level(self, level):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"smeter","value":level})
|
2019-05-05 15:34:40 +00:00
|
|
|
def write_cpu_usage(self, usage):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"cpuusage","value":usage})
|
2019-05-10 20:47:40 +00:00
|
|
|
def write_clients(self, clients):
|
|
|
|
self.protected_send({"type":"clients","value":clients})
|
2019-05-05 20:09:48 +00:00
|
|
|
def write_secondary_fft(self, data):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send(bytes([0x03]) + data)
|
2019-05-05 20:09:48 +00:00
|
|
|
def write_secondary_demod(self, data):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send(bytes([0x04]) + data)
|
2019-05-05 20:09:48 +00:00
|
|
|
def write_secondary_dsp_config(self, cfg):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"secondary_config", "value":cfg})
|
2019-05-07 13:21:16 +00:00
|
|
|
def write_config(self, cfg):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"config","value":cfg})
|
2019-05-07 13:21:16 +00:00
|
|
|
def write_receiver_details(self, details):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"receiver_details","value":details})
|
2019-05-10 14:14:16 +00:00
|
|
|
def write_profiles(self, profiles):
|
2019-05-10 18:08:22 +00:00
|
|
|
self.protected_send({"type":"profiles","value":profiles})
|
2019-05-04 14:56:23 +00:00
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
class WebSocketMessageHandler(object):
|
|
|
|
def __init__(self):
|
2019-05-05 18:12:36 +00:00
|
|
|
self.handshake = None
|
2019-05-07 13:21:16 +00:00
|
|
|
self.client = None
|
2019-05-07 16:19:53 +00:00
|
|
|
self.dsp = None
|
2019-05-04 18:26:11 +00:00
|
|
|
|
|
|
|
def handleTextMessage(self, conn, message):
|
|
|
|
if (message[:16] == "SERVER DE CLIENT"):
|
2019-05-05 18:12:36 +00:00
|
|
|
# maybe put some more info in there? nothing to store yet.
|
|
|
|
self.handshake = "completed"
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.debug("client connection intitialized")
|
2019-05-05 18:12:36 +00:00
|
|
|
|
2019-05-07 13:21:16 +00:00
|
|
|
self.client = OpenWebRxClient(conn)
|
|
|
|
|
2019-05-05 18:12:36 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not self.handshake:
|
2019-05-10 20:17:53 +00:00
|
|
|
logger.warning("not answering client request since handshake is not complete")
|
2019-05-05 18:12:36 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = json.loads(message)
|
2019-05-09 20:44:29 +00:00
|
|
|
if "type" in message:
|
|
|
|
if message["type"] == "dspcontrol":
|
|
|
|
if "action" in message and message["action"] == "start":
|
|
|
|
self.client.startDsp()
|
|
|
|
|
|
|
|
if "params" in message:
|
|
|
|
params = message["params"]
|
|
|
|
self.client.setDspProperties(params)
|
|
|
|
|
|
|
|
if message["type"] == "config":
|
|
|
|
if "params" in message:
|
|
|
|
self.client.setParams(message["params"])
|
|
|
|
if message["type"] == "setsdr":
|
|
|
|
if "params" in message:
|
|
|
|
self.client.setSdr(message["params"]["sdr"])
|
2019-05-10 14:14:16 +00:00
|
|
|
if message["type"] == "selectprofile":
|
|
|
|
if "params" in message and "profile" in message["params"]:
|
|
|
|
profile = message["params"]["profile"].split("|")
|
|
|
|
self.client.setSdr(profile[0])
|
|
|
|
self.client.sdr.activateProfile(profile[1])
|
2019-05-09 20:44:29 +00:00
|
|
|
else:
|
2019-05-10 20:17:53 +00:00
|
|
|
logger.warning("received message without type: {0}".format(message))
|
2019-05-07 15:09:29 +00:00
|
|
|
|
2019-05-05 18:12:36 +00:00
|
|
|
except json.JSONDecodeError:
|
2019-05-10 20:17:53 +00:00
|
|
|
logger.warning("message is not json: {0}".format(message))
|
2019-05-04 18:26:11 +00:00
|
|
|
|
|
|
|
def handleBinaryMessage(self, conn, data):
|
2019-05-10 19:50:58 +00:00
|
|
|
logger.error("unsupported binary message, discarding")
|
2019-05-04 18:26:11 +00:00
|
|
|
|
|
|
|
def handleClose(self, conn):
|
2019-05-07 13:21:16 +00:00
|
|
|
if self.client:
|
2019-05-10 18:08:22 +00:00
|
|
|
self.client.close()
|
2019-05-04 18:26:11 +00:00
|
|
|
|
|
|
|
class WebSocketController(Controller):
|
|
|
|
def handle_request(self):
|
|
|
|
conn = WebSocketConnection(self.handler, WebSocketMessageHandler())
|
|
|
|
conn.send("CLIENT DE SERVER openwebrx.py")
|
|
|
|
# enter read loop
|
|
|
|
conn.read_loop()
|