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-04 21:11:13 +00:00
|
|
|
from owrx.source import SpectrumThread, DspThread
|
2019-05-04 18:26:11 +00:00
|
|
|
import json
|
2019-05-03 20:59:24 +00:00
|
|
|
|
|
|
|
class Controller(object):
|
|
|
|
def __init__(self, handler, matches):
|
|
|
|
self.handler = handler
|
|
|
|
self.matches = matches
|
|
|
|
def send_response(self, content, code = 200, content_type = "text/html"):
|
|
|
|
self.handler.send_response(code)
|
|
|
|
if content_type is not None:
|
|
|
|
self.handler.send_header("Content-Type", content_type)
|
|
|
|
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):
|
|
|
|
self.send_response("you have reached the status page!")
|
|
|
|
|
|
|
|
class IndexController(Controller):
|
|
|
|
def handle_request(self):
|
|
|
|
self.render_template("index.wrx")
|
|
|
|
|
|
|
|
class AssetsController(Controller):
|
2019-05-04 14:56:23 +00:00
|
|
|
def serve_file(self, file):
|
|
|
|
try:
|
|
|
|
f = open('htdocs/' + file, 'rb')
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
(content_type, encoding) = mimetypes.MimeTypes().guess_type(file)
|
|
|
|
self.send_response(data, content_type = content_type)
|
|
|
|
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-04 18:26:11 +00:00
|
|
|
class SpectrumForwarder(object):
|
|
|
|
def __init__(self, conn):
|
|
|
|
self.conn = conn
|
|
|
|
def write_spectrum_data(self, data):
|
|
|
|
self.conn.send(bytes([0x01]) + data)
|
2019-05-04 21:11:13 +00:00
|
|
|
def write_dsp_data(self, data):
|
|
|
|
self.conn.send(bytes([0x02]) + data)
|
2019-05-04 14:56:23 +00:00
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
class WebSocketMessageHandler(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.forwarder = None
|
|
|
|
|
|
|
|
def handleTextMessage(self, conn, message):
|
|
|
|
if (message[:16] == "SERVER DE CLIENT"):
|
|
|
|
config = {}
|
|
|
|
pm = PropertyManager.getSharedInstance()
|
|
|
|
|
|
|
|
for key in ["waterfall_colors", "waterfall_min_level", "waterfall_max_level", "waterfall_auto_level_margin",
|
|
|
|
"shown_center_freq", "samp_rate", "fft_size", "fft_fps", "audio_compression", "fft_compression",
|
2019-05-04 21:11:13 +00:00
|
|
|
"max_clients", "start_mod", "client_audio_buffer_size"]:
|
2019-05-04 18:26:11 +00:00
|
|
|
|
2019-05-04 18:40:13 +00:00
|
|
|
config[key] = pm.getPropertyValue(key)
|
|
|
|
|
|
|
|
config["start_offset_freq"] = pm.getPropertyValue("start_freq") - pm.getPropertyValue("center_freq")
|
2019-05-04 14:56:23 +00:00
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
conn.send({"type":"config","value":config})
|
|
|
|
print("client connection intitialized")
|
2019-05-04 14:56:23 +00:00
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
self.forwarder = SpectrumForwarder(conn)
|
|
|
|
SpectrumThread.getSharedInstance().add_client(self.forwarder)
|
2019-05-04 14:56:23 +00:00
|
|
|
|
2019-05-04 21:11:13 +00:00
|
|
|
self.dsp = DspThread(self.forwarder)
|
|
|
|
|
2019-05-04 18:26:11 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
message = json.loads(message)
|
2019-05-04 21:11:13 +00:00
|
|
|
if message["type"] == "dspcontrol":
|
|
|
|
if "params" in message:
|
|
|
|
params = message["params"]
|
|
|
|
for key in params:
|
|
|
|
methodname = "set_" + key
|
|
|
|
if hasattr(self.dsp, methodname):
|
|
|
|
method = getattr(self.dsp, methodname)
|
|
|
|
if callable(method):
|
|
|
|
method(params[key])
|
|
|
|
else:
|
|
|
|
print("method {0} is not callable".format(methodname))
|
|
|
|
else:
|
|
|
|
print("dsp has no method {0}".format(methodname))
|
|
|
|
|
|
|
|
if "action" in message and message["action"] == "start":
|
|
|
|
self.dsp.start()
|
2019-05-04 18:26:11 +00:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
print("message is not json: {0}".format(message))
|
|
|
|
|
|
|
|
def handleBinaryMessage(self, conn, data):
|
|
|
|
print("unsupported binary message, discarding")
|
|
|
|
|
|
|
|
def handleClose(self, conn):
|
|
|
|
if self.forwarder:
|
|
|
|
SpectrumThread.getSharedInstance().remove_client(self.forwarder)
|
2019-05-04 21:11:13 +00:00
|
|
|
if self.dsp:
|
|
|
|
self.dsp.stop()
|
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()
|