diff --git a/auto_rx/autorx/scan.py b/auto_rx/autorx/scan.py index 598efb3..47e0a66 100644 --- a/auto_rx/autorx/scan.py +++ b/auto_rx/autorx/scan.py @@ -524,19 +524,28 @@ class SondeScanner(object): # Append on any frequencies in the supplied greylist peak_frequencies = np.append(np.array(self.greylist)*1e6, peak_frequencies) - # Emit a notification to the client that a scan is complete. - # We need to format our peak results in an odd manner for chart.js to read them. + # Get the level of our peak search results, to send to the web client. + # This is actually a bit of a pain to do... _peak_freq = [] _peak_lvl = [] for _peak in peak_frequencies: try: + # Find the index of the peak within our decimated frequency array. + _peak_power_idx = np.argmin(np.abs(scan_result['freq']-_peak/1e6)) + # Because we've decimated the freq & power data, the peak location may + # not be exactly at this frequency, so we take the maximum of an area + # around this location. + _peak_search_min = max(0,_peak_power_idx-5) + _peak_search_max = min(len(scan_result['freq'])-1, _peak_power_idx+5) + # Grab the maximum value, and append it and the frequency to the output arrays + _peak_lvl.append(max(scan_result['power'][_peak_search_min:_peak_search_max])) _peak_freq.append(_peak/1e6) - _peak_lvl.append(power[np.argmin(np.abs(freq-_peak))]) except: pass - + # Add the peak results to our global scan result dictionary. scan_result['peak_freq'] = _peak_freq scan_result['peak_lvl'] = _peak_lvl + # Tell the web client we have new data. flask_emit_event('scan_event') if len(peak_frequencies) == 0: diff --git a/auto_rx/autorx/templates/index.html b/auto_rx/autorx/templates/index.html index 18786cc..8036316 100644 --- a/auto_rx/autorx/templates/index.html +++ b/auto_rx/autorx/templates/index.html @@ -47,13 +47,6 @@ // http[s]://:[/] var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace); - // Event handler for new connections. - // The callback function is invoked when a connection with the - // server is established. - socket.on('connect', function() { - socket.emit('my_event', {data: 'I\'m connected!'}); - }); - // Grab the System config. $.ajax({ url: "/get_config", @@ -148,15 +141,21 @@ attribution: '© OpenStreetMap contributors' }).addTo(sondemap); // Add a new marker, but set it to hidden initially. - var marker = L.marker(sondemap.getCenter()).addTo(sondemap); + var home_marker = L.marker(sondemap.getCenter()).addTo(sondemap); + var sonde_marker = L.marker([0.0,0.0]).addTo(sondemap); socket.on('telemetry_event', function(msg) { $('#telemetry').html('ID: ' + msg.id + ' Frame: ' + msg.frame + ' Position: ' + msg.lat + ',' + msg.lon + ' Alt: ' + msg.alt); - marker.setLatLng([msg.lat,msg.lon]).update(); + sonde_marker.setLatLng([msg.lat,msg.lon]).update(); sondemap.setView([msg.lat, msg.lon], 10); }); + // Tell the server we are connected and ready for data. + socket.on('connect', function() { + socket.emit('client_connected', {data: 'I\'m connected!'}); + }); + }); diff --git a/auto_rx/autorx/web.py b/auto_rx/autorx/web.py index 482caef..3be98e2 100644 --- a/auto_rx/autorx/web.py +++ b/auto_rx/autorx/web.py @@ -94,6 +94,23 @@ def shutdown_flask(shutdown_key): return "" +# +# Globally called 'emit' function +# +def flask_emit_event(event_name="none", data={}): + """ Emit a socketio event to any clients. """ + socketio.emit(event_name, data, namespace='/update_status') + + +@socketio.on('client_connected', namespace='/update_status') +def refresh_client(arg1): + """ A client has connected, let them know to grab data.""" + logging.info("Flask - New Web Client connected!") + # Tell them to get a copy of the latest scan results. + flask_emit_event('scan_event') + # TODO: Send last few log entries + + # # Flask Startup & Shutdown Helper Scripts # @@ -125,12 +142,6 @@ def stop_flask(host='0.0.0.0', port=5000): # TODO: Cleanup errors traceback.print_exc() -# -# Globally called 'emit' function -# -def flask_emit_event(event_name="none", data={}): - """ Emit a socketio event to any clients. """ - socketio.emit(event_name, data, namespace='/update_status') class WebHandler(logging.Handler):