@@ -46,59 +46,94 @@ function App() {
4646 const [ coordinate , setCoordinate ] = useState < LatLng [ ] > ( [ ] ) ;
4747
4848 useEffect ( ( ) => {
49- const interval = setInterval ( ( ) => {
50- fetch ( "/api/plane/telemetry?id=33&field=lat,lon,alt,relative_alt" )
49+ let isCancelled = false ;
50+ let planeTimeoutId : number | null = null ;
51+ let statusTimeoutId : number | null = null ;
52+
53+ const pollPlaneTelemetry = async ( ) => {
54+ await fetch ( "/api/plane/telemetry?id=33&field=lat,lon,alt,relative_alt" )
5155 . then ( ( resp ) => resp . json ( ) )
5256 . then ( ( json ) => {
5357 const lat = parseFloat ( json [ "lat" ] ) / 10e6 ;
5458 const lng = parseFloat ( json [ "lon" ] ) / 10e6 ;
55- setPlaneLatLng ( [ lat , lng ] ) ;
59+ if ( ! isCancelled ) {
60+ setPlaneLatLng ( [ lat , lng ] ) ;
61+ }
62+ } )
63+ . catch ( ( error ) => {
64+ console . error ( "Error fetching plane telemetry:" , error ) ;
5665 } ) ;
57- } , 500 ) ;
5866
59- const interval_long = setInterval ( ( ) => {
60- // I hate this but don't feel motivated to refactor it, sorry ~Tyler 4/13/24
61- fetch ( "/api/connections" )
62- . then ( ( resp ) => resp . json ( ) )
63- . then ( ( json ) => {
64- setStatuses ( ( old ) =>
65- old . map ( ( status : ConnectionStatus ) => {
66- switch ( status . name ) {
67- case "Antenna Tracker" :
68- return {
69- isActive : json [ "antenna_tracker" ] ,
70- type : status . type ,
71- name : status . name ,
72- } as ConnectionStatus ;
73- case "Onboard Computer" :
74- return {
75- isActive : json [ "plane_obc" ] ,
76- type : status . type ,
77- name : status . name ,
78- } as ConnectionStatus ;
79- case "Radio Mavlink" :
80- return {
81- isActive : json [ "radio_mavlink" ] ,
82- type : status . type ,
83- name : status . name ,
84- } as ConnectionStatus ;
85- default :
86- return { } as ConnectionStatus ;
87- }
88- } ) ,
89- ) ;
90- } ) ;
91- fetch ( "/api/obc_connection" )
92- . then ( ( resp ) => resp . json ( ) )
93- . then ( ( json ) => {
94- // these keys are defined in the /connections route of the backend
95- localStorage . setItem ( "obc_conn_status" , JSON . stringify ( json ) ) ;
96- } ) ;
97- } , 2000 ) ;
67+ if ( ! isCancelled ) {
68+ planeTimeoutId = window . setTimeout ( ( ) => {
69+ void pollPlaneTelemetry ( ) ;
70+ } , 500 ) ;
71+ }
72+ } ;
73+
74+ const pollConnectionStatus = async ( ) => {
75+ await Promise . allSettled ( [
76+ fetch ( "/api/connections" )
77+ . then ( ( resp ) => resp . json ( ) )
78+ . then ( ( json ) => {
79+ if ( isCancelled ) {
80+ return ;
81+ }
82+ setStatuses ( ( old ) =>
83+ old . map ( ( status : ConnectionStatus ) => {
84+ switch ( status . name ) {
85+ case "Antenna Tracker" :
86+ return {
87+ isActive : json [ "antenna_tracker" ] ,
88+ type : status . type ,
89+ name : status . name ,
90+ } as ConnectionStatus ;
91+ case "Onboard Computer" :
92+ return {
93+ isActive : json [ "plane_obc" ] ,
94+ type : status . type ,
95+ name : status . name ,
96+ } as ConnectionStatus ;
97+ case "Radio Mavlink" :
98+ return {
99+ isActive : json [ "radio_mavlink" ] ,
100+ type : status . type ,
101+ name : status . name ,
102+ } as ConnectionStatus ;
103+ default :
104+ return { } as ConnectionStatus ;
105+ }
106+ } ) ,
107+ ) ;
108+ } ) ,
109+ fetch ( "/api/obc_connection" )
110+ . then ( ( resp ) => resp . json ( ) )
111+ . then ( ( json ) => {
112+ if ( ! isCancelled ) {
113+ // these keys are defined in the /connections route of the backend
114+ localStorage . setItem ( "obc_conn_status" , JSON . stringify ( json ) ) ;
115+ }
116+ } ) ,
117+ ] ) ;
118+
119+ if ( ! isCancelled ) {
120+ statusTimeoutId = window . setTimeout ( ( ) => {
121+ void pollConnectionStatus ( ) ;
122+ } , 2000 ) ;
123+ }
124+ } ;
125+
126+ void pollPlaneTelemetry ( ) ;
127+ void pollConnectionStatus ( ) ;
98128
99129 return ( ) => {
100- clearInterval ( interval ) ;
101- clearInterval ( interval_long ) ;
130+ isCancelled = true ;
131+ if ( planeTimeoutId !== null ) {
132+ window . clearTimeout ( planeTimeoutId ) ;
133+ }
134+ if ( statusTimeoutId !== null ) {
135+ window . clearTimeout ( statusTimeoutId ) ;
136+ }
102137 } ;
103138 } , [ ] ) ;
104139
0 commit comments