@@ -3233,6 +3233,7 @@ function refresh() {
3233
3233
document . getElementById ( "timeperiod" ) . disabled = false ;
3234
3234
}
3235
3235
clientActive = true ;
3236
+ //console.log("WebSockets - Resuming Websockets updates after poll.")
3236
3237
clearTimeout ( periodical ) ;
3237
3238
ajax_inprogress = false ;
3238
3239
}
@@ -3403,26 +3404,43 @@ function liveData() {
3403
3404
3404
3405
} else {
3405
3406
// Message is payload telemetry
3407
+
3408
+ // Single frame, as we are not using amateur-batch
3406
3409
var frame = [ { "1" :JSON . parse ( message . payloadString . toString ( ) ) } ] ;
3407
- if ( frame . length == null ) {
3408
- var tempDate = new Date ( frame . time_received ) . getTime ( ) ;
3409
- } else {
3410
- var tempDate = new Date ( frame [ frame . length - 1 ] [ "1" ] . time_received ) . getTime ( )
3411
- }
3412
- if ( ( dateNow - tempDate ) < 30000 ) {
3413
- var test = formatData ( frame ) ;
3414
- if ( clientActive ) {
3415
- live_data_buffer . positions . position . push . apply ( live_data_buffer . positions . position , test . positions . position ) ;
3416
- }
3410
+ // If we switch to amateur-batch, we should use
3411
+ // var frame = JSON.parse(message.payloadString.toString());
3412
+
3413
+ var test = formatData ( frame ) ;
3414
+ if ( clientActive ) {
3415
+ live_data_buffer . positions . position . push . apply ( live_data_buffer . positions . position , test . positions . position )
3417
3416
$ ( "#stTimer" ) . attr ( "data-timestamp" , dateNow ) ;
3418
3417
$ ( "#stText" ) . text ( "websocket |" ) ;
3419
- } else if ( ( dateNow - new Date ( frame [ 0 ] [ "1" ] . time_received ) . getTime ( ) ) > 150000 ) {
3420
- $ ( "#stText" ) . text ( "error |" ) ;
3421
- refresh ( ) ;
3422
- } else {
3423
- $ ( "#stText" ) . text ( "error |" ) ;
3424
3418
}
3419
+
3420
+ // The old way, with data age checks.
3421
+ //
3422
+ // var frame = [{"1":JSON.parse(message.payloadString.toString())}];
3423
+ // if (frame.length == null) {
3424
+ // var tempDate = new Date(frame.time_received).getTime();
3425
+ // } else {
3426
+ // var tempDate = new Date(frame[frame.length - 1]["1"].time_received).getTime()
3427
+ // }
3428
+ // if ((dateNow - tempDate) < 30000) {
3429
+ // var test = formatData(frame);
3430
+ // if (clientActive) {
3431
+ // live_data_buffer.positions.position.push.apply(live_data_buffer.positions.position,test.positions.position);
3432
+ // }
3433
+ // $("#stTimer").attr("data-timestamp", dateNow);
3434
+ // $("#stText").text("websocket |");
3435
+ // } else if ((dateNow - new Date(frame[0]["1"].time_received).getTime()) > 150000) {
3436
+ // $("#stText").text("error |");
3437
+ // refresh();
3438
+ // } else {
3439
+ // $("#stText").text("error |");
3440
+ // }
3425
3441
}
3442
+ } else {
3443
+ console . log ( "WebSockets - Discarding Message, not ready yet." )
3426
3444
}
3427
3445
}
3428
3446
catch ( err ) { }
@@ -3432,7 +3450,10 @@ function liveData() {
3432
3450
setInterval ( function ( ) {
3433
3451
update ( live_data_buffer ) ;
3434
3452
live_data_buffer . positions . position = [ ] ;
3435
- } , 200 )
3453
+ } , 500 )
3454
+
3455
+ // Event listener to update on page resume from suspend.
3456
+ document . addEventListener ( 'resume' , refresh ) ;
3436
3457
3437
3458
function refreshPatreons ( ) {
3438
3459
@@ -3885,93 +3906,85 @@ function update(response, none) {
3885
3906
lastPPointer : lastPositions . positions . position ,
3886
3907
idx : 0 ,
3887
3908
max : response . positions . position . length ,
3888
- step : function ( ctx ) {
3889
- var draw_idx = - 1 ;
3909
+ run : function ( ctx ) {
3910
+ while ( ctx . idx < ctx . max ) {
3911
+ var draw_idx = - 1 ;
3890
3912
3891
- var i = ctx . idx ;
3892
- var max = i + 5000 ;
3893
- max = ( max >= ctx . max ) ? ctx . max : max ;
3913
+ var i = ctx . idx ;
3914
+ var max = i + 5000 ;
3915
+ max = ( max >= ctx . max ) ? ctx . max : max ;
3894
3916
3895
- for ( ; i < max ; i ++ ) {
3896
- var row = ctx . positions [ i ] ;
3917
+ for ( ; i < max ; i ++ ) {
3918
+ var row = ctx . positions [ i ] ;
3897
3919
3898
- // set the position based on the last record (oldest) returned from the server. Only provide minute accuracy to allow better hit rate with cloudfront
3899
- this_position_id = new Date ( row . gps_time ) ;
3920
+ // set the position based on the last record (oldest) returned from the server. Only provide minute accuracy to allow better hit rate with cloudfront
3921
+ this_position_id = new Date ( row . gps_time ) ;
3900
3922
3901
- if ( new Date ( position_id ) < this_position_id || position_id == 0 ) {
3902
- if ( new Date ( ) > this_position_id ) {
3903
- this_position_id . setSeconds ( 0 )
3904
- this_position_id . setMilliseconds ( 0 )
3905
- position_id = this_position_id . toISOString ( )
3923
+ if ( new Date ( position_id ) < this_position_id || position_id == 0 ) {
3924
+ if ( new Date ( ) > this_position_id ) {
3925
+ this_position_id . setSeconds ( 0 )
3926
+ this_position_id . setMilliseconds ( 0 )
3927
+ position_id = this_position_id . toISOString ( )
3928
+ }
3906
3929
}
3907
- }
3908
3930
3909
- if ( ! row . picture ) {
3910
- addPosition ( row ) ;
3911
- got_positions = true ;
3931
+ if ( ! row . picture ) {
3932
+ addPosition ( row ) ;
3933
+ got_positions = true ;
3934
+ }
3912
3935
}
3913
- }
3914
3936
3915
- ctx . idx = max ;
3937
+ ctx . idx = max ;
3916
3938
3917
- if ( ctx . idx < ctx . max ) {
3918
- setTimeout ( function ( ) { ctx . step ( ctx ) ; } , 4 ) ;
3919
- } else {
3920
- ctx . list = Object . keys ( vehicles ) ;
3921
- setTimeout ( function ( ) { ctx . draw ( ctx ) ; } , 16 ) ;
3922
- }
3923
- } ,
3924
- draw : function ( ctx ) {
3925
- if ( ctx . list . length < 1 ) {
3926
- setTimeout ( function ( ) { ctx . end ( ctx ) ; } , 16 ) ;
3927
- return ;
3928
3939
}
3929
3940
3930
- // pop a callsign from the top
3931
- var vcallsign = ctx . list . shift ( ) ;
3932
- var vehicle = vehicles [ vcallsign ] ;
3941
+ ctx . list = Object . keys ( vehicles ) ;
3933
3942
3934
- if ( vehicle === undefined ) return ;
3943
+ // draw loop
3944
+ while ( ctx . list . length >= 1 ) {
3945
+ // pop a callsign from the top
3946
+ var vcallsign = ctx . list . shift ( ) ;
3947
+ var vehicle = vehicles [ vcallsign ] ;
3935
3948
3936
- if ( vehicle . updated ) {
3937
- updatePolyline ( vcallsign ) ;
3938
-
3939
- updateVehicleInfo ( vcallsign , vehicle . curr_position ) ;
3949
+ if ( vehicle === undefined ) return ;
3940
3950
3941
- // remember last position for each vehicle
3942
- ctx . lastPPointer . push ( vehicle . curr_position ) ;
3951
+ if ( vehicle . updated ) {
3952
+ updatePolyline ( vcallsign ) ;
3953
+
3954
+ updateVehicleInfo ( vcallsign , vehicle . curr_position ) ;
3955
+
3956
+ // remember last position for each vehicle
3957
+ ctx . lastPPointer . push ( vehicle . curr_position ) ;
3943
3958
3944
- if ( listScroll ) listScroll . refresh ( ) ;
3945
- if ( zoomed_in && follow_vehicle == vcallsign && ! manual_pan ) panTo ( follow_vehicle ) ;
3946
- if ( follow_vehicle == vcallsign ) {
3947
- update_lookangles ( follow_vehicle ) ;
3948
- drawLOSPaths ( vcallsign ) ;
3959
+ if ( listScroll ) listScroll . refresh ( ) ;
3960
+ if ( zoomed_in && follow_vehicle == vcallsign && ! manual_pan ) panTo ( follow_vehicle ) ;
3961
+ if ( follow_vehicle == vcallsign ) {
3962
+ update_lookangles ( follow_vehicle ) ;
3963
+ drawLOSPaths ( vcallsign ) ;
3964
+ }
3949
3965
}
3950
3966
}
3967
+ // ctx.end
3951
3968
3952
- // step to the next callsign
3953
- setTimeout ( function ( ) { ctx . draw ( ctx ) ; } , 16 ) ;
3954
- } ,
3955
- end : function ( ctx ) {
3956
-
3957
- // update graph is current vehicles is followed
3958
- if ( follow_vehicle !== null &&
3959
- vehicles . hasOwnProperty ( follow_vehicle ) &&
3960
- vehicles [ follow_vehicle ] . graph_data_updated ) updateGraph ( follow_vehicle , false ) ;
3969
+ // update graph is current vehicles is followed
3970
+ if ( follow_vehicle !== null &&
3971
+ vehicles . hasOwnProperty ( follow_vehicle ) &&
3972
+ vehicles [ follow_vehicle ] . graph_data_updated ) updateGraph ( follow_vehicle , false ) ;
3961
3973
3962
- if ( got_positions && ! zoomed_in && Object . keys ( vehicles ) . length ) {
3974
+ if ( got_positions && ! zoomed_in && Object . keys ( vehicles ) . length ) {
3963
3975
if ( vehicles . hasOwnProperty ( wvar . query ) && wvar . query !== "" ) {
3964
3976
zoom_on_payload ( ) ;
3965
3977
}
3966
3978
// TODO: Zoom to geolocation position
3967
3979
3968
- }
3980
+ }
3981
+
3982
+ if ( periodical_predictions === null ) refreshPredictions ( ) ;
3983
+ } ,
3969
3984
3970
- if ( periodical_predictions === null ) refreshPredictions ( ) ;
3971
- }
3972
3985
} ;
3973
3986
3974
- ctx_init . step ( ctx_init ) ;
3987
+ ctx_init . run ( ctx_init ) ;
3975
3988
}
3976
3989
3977
3990
function zoom_on_payload ( ) {
0 commit comments