implement client reconnect; remove some old code

This commit is contained in:
Jakob Ketterl 2019-05-07 15:21:16 +02:00
parent efb6e9c6cd
commit e937f2bca3
3 changed files with 37 additions and 132 deletions

View File

@ -52,6 +52,7 @@ var waterfall_setup_done=0;
var waterfall_queue = [];
var waterfall_timer;
var secondary_fft_size;
var audio_allowed;
/*function fade(something,from,to,time_ms,fps)
{
@ -1181,6 +1182,9 @@ function on_ws_recv(evt)
max_clients_num = config.max_clients;
waterfall_init();
audio_preinit();
if (audio_allowed) audio_init();
waterfall_clear();
break;
case "secondary_config":
window.secondary_fft_size = json.value.secondary_fft_size;
@ -1263,119 +1267,6 @@ function on_ws_recv(evt)
console.warn('unknown type of binary message: ' + type)
}
}
return
if(!(evt.data instanceof ArrayBuffer)) { divlog("on_ws_recv(): Not ArrayBuffer received...",1); return; }
//
debug_ws_data_received+=evt.data.byteLength/1000;
first4Chars=getFirstChars(evt.data,4);
first3Chars=first4Chars.slice(0,3);
if(first3Chars=="CLI")
{
}
if(first3Chars=="AUD")
{
var audio_data;
if(audio_compression=="adpcm") audio_data=new Uint8Array(evt.data,4)
else audio_data=new Int16Array(evt.data,4);
audio_prepare(audio_data);
audio_buffer_current_size_debug+=audio_data.length;
audio_buffer_all_size_debug+=audio_data.length;
if(!(ios||is_chrome) && (audio_initialized==0 && audio_prepared_buffers.length>audio_buffering_fill_to)) audio_init()
}
else if(first3Chars=="FFT")
{
//alert("Yupee! Doing FFT");
//if(first4Chars=="FFTS") console.log("FFTS");
if(fft_compression=="none") waterfall_add_queue(new Float32Array(evt.data,4));
else if(fft_compression="adpcm")
{
fft_codec.reset();
var waterfall_i16=fft_codec.decode(new Uint8Array(evt.data,4));
var waterfall_f32=new Float32Array(waterfall_i16.length-COMPRESS_FFT_PAD_N);
for(var i=0;i<waterfall_i16.length;i++) waterfall_f32[i]=waterfall_i16[i+COMPRESS_FFT_PAD_N]/100;
if(first4Chars=="FFTS") secondary_demod_waterfall_add_queue(waterfall_f32); //TODO digimodes
else waterfall_add_queue(waterfall_f32);
}
}
else if(first3Chars=="DAT")
{
//secondary_demod_push_binary_data(new Uint8Array(evt.data,4));
secondary_demod_push_data(arrayBufferToString(evt.data).substring(4));
//console.log("DAT");
}
else if(first3Chars=="MSG")
{
/*try
{*/
var stringData=arrayBufferToString(evt.data);
params=stringData.substring(4).split(" ");
for(i=0;i<params.length;i++)
{
param=params[i].split("=");
switch(param[0])
{
case "setup":
waterfall_init();
audio_preinit();
break;
case "bandwidth":
bandwidth=parseInt(param[1]);
break;
case "center_freq":
center_freq=parseInt(param[1]); //there was no ; and it was no problem... why?
break;
case "fft_size":
fft_size=parseInt(param[1]);
break;
case "secondary_fft_size":
secondary_fft_size=parseInt(param[1]);
break;
case "secondary_setup":
secondary_demod_init_canvases();
break;
case "if_samp_rate":
if_samp_rate=parseInt(param[1]);
break;
case "secondary_bw":
secondary_bw=parseFloat(param[1]);
break;
case "fft_fps":
fft_fps=parseInt(param[1]);
break;
case "audio_compression":
audio_compression=param[1];
divlog( "Audio stream is "+ ((audio_compression=="adpcm")?"compressed":"uncompressed")+"." )
break;
case "fft_compression":
fft_compression=param[1];
divlog( "FFT stream is "+ ((fft_compression=="adpcm")?"compressed":"uncompressed")+"." )
break;
case "cpu_usage":
var server_cpu_usage=parseInt(param[1]);
progressbar_set(e("openwebrx-bar-server-cpu"),server_cpu_usage/100,"Server CPU ["+param[1]+"%]",server_cpu_usage>85);
break;
case "clients":
var clients_num=parseInt(param[1]);
progressbar_set(e("openwebrx-bar-clients"),clients_num/max_clients_num,"Clients ["+param[1]+"]",clients_num>max_clients_num*0.85);
break;
case "max_clients":
max_clients_num=parseInt(param[1]);
break;
case "s":
smeter_level=parseFloat(param[1]);
setSmeterAbsoluteValue(smeter_level);
break;
}
}
/*}
catch(err)
{
divlog("Received invalid message over WebSocket.");
}*/
}
}
function add_problem(what)
@ -1799,7 +1690,9 @@ function on_ws_closed()
audio_node.disconnect();
}
catch (dont_care) {}
divlog("WebSocket has closed unexpectedly. Please reload the page.", 1);
divlog("WebSocket has closed unexpectedly. Attempting to reconnect in 5 seconds...", 1);
setTimeout(open_websocket, 5000);
}
function on_ws_error(event)
@ -2316,6 +2209,7 @@ function iosPlayButtonClick()
audio_init();
e("openwebrx-big-grey").style.opacity=0;
window.setTimeout(function(){ e("openwebrx-big-grey").style.display="none"; },1100);
audio_allowed = 1;
}
/*

View File

@ -46,7 +46,7 @@ class AssetsController(Controller):
filename = self.matches.group(1)
self.serve_file(filename)
class ClientDataForwarder(object):
class OpenWebRxClient(object):
def __init__(self, conn):
self.conn = conn
def write_spectrum_data(self, data):
@ -63,17 +63,23 @@ class ClientDataForwarder(object):
self.conn.send(bytes([0x04]) + data)
def write_secondary_dsp_config(self, cfg):
self.conn.send({"type":"secondary_config", "value":cfg})
def write_config(self, cfg):
self.conn.send({"type":"config","value":cfg})
def write_receiver_details(self, details):
self.conn.send({"type":"receiver_details","value":details})
class WebSocketMessageHandler(object):
def __init__(self):
self.handshake = None
self.forwarder = None
self.client = None
def handleTextMessage(self, conn, message):
if (message[:16] == "SERVER DE CLIENT"):
# maybe put some more info in there? nothing to store yet.
self.handshake = "completed"
self.client = OpenWebRxClient(conn)
pm = PropertyManager.getSharedInstance()
config_keys = ["waterfall_colors", "waterfall_min_level", "waterfall_max_level",
@ -82,19 +88,18 @@ class WebSocketMessageHandler(object):
"client_audio_buffer_size"]
config = dict((key, pm.getPropertyValue(key)) for key in config_keys)
config["start_offset_freq"] = pm.getPropertyValue("start_freq") - pm.getPropertyValue("center_freq")
conn.send({"type":"config","value":config})
self.client.write_config(config)
print("client connection intitialized")
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)
conn.send({"type":"receiver_details","value":receiver_details})
self.client.write_receiver_details(receiver_details)
self.forwarder = ClientDataForwarder(conn)
SpectrumThread.getSharedInstance().add_client(self.forwarder)
CpuUsageThread.getSharedInstance().add_client(self.forwarder)
SpectrumThread.getSharedInstance().add_client(self.client)
CpuUsageThread.getSharedInstance().add_client(self.client)
self.dsp = DspManager(self.forwarder)
self.dsp = DspManager(self.client)
return
@ -119,9 +124,9 @@ class WebSocketMessageHandler(object):
print("unsupported binary message, discarding")
def handleClose(self, conn):
if self.forwarder:
SpectrumThread.getSharedInstance().remove_client(self.forwarder)
CpuUsageThread.getSharedInstance().remove_client(self.forwarder)
if self.client:
SpectrumThread.getSharedInstance().remove_client(self.client)
CpuUsageThread.getSharedInstance().remove_client(self.client)
if self.dsp:
self.dsp.stop()

View File

@ -67,7 +67,9 @@ class SpectrumThread(threading.Thread):
bytes_to_read=int(dsp.get_fft_bytes_to_read())
while self.doRun:
data=dsp.read(bytes_to_read)
#print("gotcha",len(data),"bytes of spectrum data via spectrum_thread_function()")
if len(data) == 0:
self.shutdown()
else:
for c in self.clients:
c.write_spectrum_data(data)
@ -136,9 +138,11 @@ class DspManager(object):
if (pm.getPropertyValue("digimodes_enable")):
def set_secondary_mod(mod):
if mod == False: mod = None
if self.dsp.get_secondary_demodulator() == mod: return
self.stopSecondaryThreads()
self.dsp.stop()
if mod == False:
if mod is None:
self.dsp.set_secondary_demodulator(None)
else:
self.dsp.set_secondary_demodulator(mod)
@ -147,8 +151,6 @@ class DspManager(object):
"if_samp_rate":self.dsp.if_samp_rate(),
"secondary_bw":self.dsp.secondary_bw()
})
# TODO frontend will probably miss this
#rxws.send(self, "MSG secondary_fft_size={0} if_samp_rate={1} secondary_bw={2} secondary_setup".format(cfg.digimodes_fft_size, dsp.if_samp_rate(), dsp.secondary_bw()))
self.dsp.start()
if mod:
@ -180,6 +182,9 @@ class DspManager(object):
def readDspOutput(self):
while (self.doRun):
data = self.dsp.read(256)
if len(data) != 256:
time.sleep(1)
else:
self.handler.write_dsp_data(data)
def readSMeterOutput(self):
@ -199,6 +204,7 @@ class DspManager(object):
def stop(self):
self.doRun = False
self.runSecondary = False
self.dsp.stop()
def setProperty(self, prop, value):