|
| 1 | + |
| 2 | +var local_livedata = "ws://sonde.bitnet.be:9001/"; |
| 3 | +var local_clientID = "SondeHub-Tracker-" + Math.floor(Math.random() * 10000000000); |
| 4 | +var local_client = new Paho.Client(local_livedata, local_clientID); |
| 5 | +var local_clientConnected = false; |
| 6 | +var local_clientActive = false; |
| 7 | +var local_clientTopic; |
| 8 | +var local_messageRate = 0; |
| 9 | +var local_messageRateAverage = 10; |
| 10 | + |
| 11 | + |
| 12 | +local_live_data_buffer = {positions:{position:[]}} |
| 13 | +function local_liveData() { |
| 14 | + local_client.onConnectionLost = local_onConnectionLost; |
| 15 | + local_client.onMessageArrived = local_onMessageArrived; |
| 16 | + |
| 17 | + local_client.connect({onSuccess:local_onConnect,onFailure:local_connectionError,reconnect:true}); |
| 18 | + |
| 19 | + function local_onConnect() { |
| 20 | + if (wvar.query && sondePrefix.indexOf(wvar.query) == -1) { |
| 21 | + var topic = "sondes/" + wvar.query; |
| 22 | + local_client.subscribe(topic); |
| 23 | + local_clientTopic = topic; |
| 24 | + } else { |
| 25 | + local_client.subscribe("batch"); |
| 26 | + local_clientTopic = "batch"; |
| 27 | + } |
| 28 | + // Also subscribe to listener data, for listener and chase-car telemetry. |
| 29 | + // To revert listener-via-websockets change, comment out this line, |
| 30 | + // and un-comment the 'Disable periodical listener refresh' lines further below. |
| 31 | + local_client.subscribe("listener/#"); |
| 32 | + |
| 33 | + local_clientConnected = true; |
| 34 | + $("#stText").text("websocket |"); |
| 35 | + }; |
| 36 | + |
| 37 | + function local_connectionError(error) { |
| 38 | + $("#stText").text("conn. error |"); |
| 39 | + local_clientConnected = false; |
| 40 | + local_clientActive = false; |
| 41 | + if (!document.getElementById("stTimer").classList.contains('friendly-dtime') ) { |
| 42 | + document.getElementById("stTimer").classList.add('friendly-dtime'); |
| 43 | + $("#updatedText").text(" Updated: "); |
| 44 | + } |
| 45 | + refresh(); |
| 46 | + }; |
| 47 | + |
| 48 | + function local_onConnectionLost(responseObject) { |
| 49 | + if (responseObject.errorCode !== 0) { |
| 50 | + local_clientConnected = false; |
| 51 | + local_clientActive = false; |
| 52 | + if (!document.getElementById("stTimer").classList.contains('friendly-dtime') ) { |
| 53 | + document.getElementById("stTimer").classList.add('friendly-dtime'); |
| 54 | + $("#updatedText").text(" Updated: "); |
| 55 | + } |
| 56 | + refresh(); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + function local_onMessageArrived(message) { |
| 61 | + local_messageRate += 1; |
| 62 | + setTimeout(function(){ |
| 63 | + local_messageRate -= 1; |
| 64 | + }, (1000 * local_messageRateAverage)); |
| 65 | + if ( document.getElementById("stTimer").classList.contains('friendly-dtime') ) { |
| 66 | + document.getElementById("stTimer").classList.remove('friendly-dtime'); |
| 67 | + } |
| 68 | + $("#stTimer").text(Math.round(local_messageRate/10) + " msg/s"); |
| 69 | + $("#updatedText").text(" "); |
| 70 | + var dateNow = new Date().getTime(); |
| 71 | + try { |
| 72 | + if (local_clientActive) { |
| 73 | + if(message.topic.startsWith("listener")){ |
| 74 | + // Message is Listener / Chase-Car information |
| 75 | + var frame = JSON.parse(message.payloadString.toString()); |
| 76 | + // We need to convert this into the right format for feeding into the receiver / chase car update functions. |
| 77 | + // Probably a cleaner way of doing this. |
| 78 | + // Format needs to be {callsign : {timestamp: frame}} |
| 79 | + var formatted_frame = {}; |
| 80 | + formatted_frame[frame.uploader_callsign] = {}; |
| 81 | + formatted_frame[frame.uploader_callsign][frame.ts] = frame; |
| 82 | + |
| 83 | + // Send frames with mobile present and true onto the chase-car updater, |
| 84 | + // otherwise, send them to the receiver updater. |
| 85 | + // Do this on a per-update bases, since listener / chase car updates shouldn't |
| 86 | + // be as frequent. |
| 87 | + if(frame.hasOwnProperty('mobile')) { |
| 88 | + if(frame.mobile == true) { |
| 89 | + updateChase(formatted_frame); |
| 90 | + } else { |
| 91 | + updateReceivers(formatted_frame, single=true); |
| 92 | + } |
| 93 | + } else { |
| 94 | + updateReceivers(formatted_frame, single=true); |
| 95 | + } |
| 96 | + |
| 97 | + } else { |
| 98 | + var frame = JSON.parse(message.payloadString.toString()); |
| 99 | + |
| 100 | + if (wvar.query == "" || sondePrefix.indexOf(wvar.query) > -1 || wvar.query == frame.serial) { |
| 101 | + |
| 102 | + var test = formatData(frame, true); |
| 103 | + if (local_clientActive) { |
| 104 | + local_live_data_buffer.positions.position.push.apply(local_live_data_buffer.positions.position,test.positions.position) |
| 105 | + $("#stTimer").attr("data-timestamp", dateNow); |
| 106 | + $("#stText").text("websocket |"); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } else { |
| 111 | + console.log("WebSockets - Discarding Message, not ready yet.") |
| 112 | + } |
| 113 | + } |
| 114 | + catch(err) {} |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +// Interval to read in the live data buffer and update the page. |
| 120 | +setInterval(function(){ |
| 121 | + update(local_live_data_buffer); |
| 122 | + local_live_data_buffer.positions.position=[]; |
| 123 | +}, 500) |
| 124 | + |
0 commit comments