@@ -3,140 +3,53 @@ import { PYTHON_APP } from './EnvUtils';
33import Constants from './Constants' ;
44import LogUtils from './LogUtils' ;
55import RequestUtils from './RequestUtils' ;
6- import TokenUtils from './TokenUtils' ;
76
87async function fetchRTF ( ) : Object {
98 const options = {
109 ...Constants . GET_OPTIONS ,
1110 url : `http://${ PYTHON_APP || 'localhost' } :5000/rtf` ,
1211 } ;
13- const data = await RequestUtils . createRequest ( options , 'Tracking request failed' ) ;
12+ const data = await RequestUtils . createRequest ( options , 'RTF request failed' ) ;
13+ return JSON . parse ( data ) ;
14+ }
15+
16+ async function fetchVehicles ( ) : Object {
17+ const options = {
18+ ...Constants . GET_OPTIONS ,
19+ url : `http://${ PYTHON_APP || 'localhost' } :5000/vehicles` ,
20+ } ;
21+ const data = await RequestUtils . createRequest ( options , 'Vehicles request failed' ) ;
1422 return JSON . parse ( data ) ;
1523}
1624
1725/**
18- * Given an array of { stopID, routeID, [ tripID] },
19- * Return bus and tracking data including location
26+ * Given an array of { routeID, tripID },
27+ * Return bus information
2028 * Input:
2129 [
2230 {
23- “stopID” : String,
2431 “routeID” : String,
25- “tripIdentifiers” : [ String]
32+ tripID : String
2633 },
2734 …
2835 ]
2936 */
3037async function getTrackingResponse ( requestData : Object ) : Object {
3138 LogUtils . log ( { message : 'getTrackingResponse: entering function' } ) ;
32-
33- const trackingInformation = [ ] ;
34- const rtf = await fetchRTF ( ) ; // ensures the realtimeFeed doesn't update in the middle of execution
35-
36- // for each input
37- await Promise . all ( requestData . map ( async ( data ) : Promise < boolean > => {
38- const { stopID, routeID, tripIdentifiers } = data ;
39- const realtimeDelayData = getDelayInformation ( stopID , tripIdentifiers [ 0 ] , rtf ) ;
40-
41- if ( realtimeDelayData ) {
42- const authHeader = await TokenUtils . fetchAuthHeader ( ) ;
43- const options = {
44- method : 'GET' ,
45- url : 'https://gateway.api.cloud.wso2.com:443/t/mystop/tcat/v1/rest/Vehicles/GetAllVehiclesForRoute' ,
46- headers : {
47- Authorization : authHeader ,
48- 'Cache-Control' : 'no-cache' ,
49- 'Postman-Token' : 'b688b636-87ea-4e04-9f3e-ba34e811e639' ,
50- } ,
51- qs : { routeID } ,
52- } ;
53-
54- const trackingRequest = await RequestUtils . createRequest ( options , 'Tracking request failed' ) ;
55- if ( ! trackingRequest ) return false ;
56-
57- /**
58- * Parse request to object and map valid realtime data to info for each bus
59- */
60- const busFound = JSON . parse ( trackingRequest ) // parse the request
61- . find ( // find the first tracking data matching the vehicleId
62- busInfo => busInfo . VehicleId === realtimeDelayData . vehicleId ,
63- ) ;
64-
65- if ( ! busFound ) return false ;
66-
67- const trackingData = ( ( busInfo ) => { // create object and return
68- let lastUpdated = busInfo . LastUpdated ;
69- const firstParan = lastUpdated . indexOf ( '(' ) + 1 ;
70- const secondParan = lastUpdated . indexOf ( '-' ) ;
71- lastUpdated = parseInt ( lastUpdated . slice ( firstParan , secondParan ) ) ;
72- return {
73- case : 'validData' ,
74- commStatus : busInfo . CommStatus ,
75- delay : realtimeDelayData . delay ,
76- destination : busInfo . Destination ,
77- deviation : busInfo . Deviation ,
78- direction : busInfo . Direction ,
79- displayStatus : busInfo . DisplayStatus ,
80- gpsStatus : busInfo . GPSStatus ,
81- heading : busInfo . Heading ,
82- lastStop : busInfo . LastStop ,
83- lastUpdated,
84- latitude : busInfo . Latitude ,
85- longitude : busInfo . Longitude ,
86- name : busInfo . Name ,
87- opStatus : busInfo . OpStatus ,
88- routeID : busInfo . RouteId ,
89- runID : busInfo . RunId ,
90- speed : busInfo . Speed ,
91- tripID : busInfo . TripId ,
92- vehicleID : busInfo . VehicleId ,
93- } ;
94- } ) ( busFound ) ;
95-
96- LogUtils . log ( { message : 'getTrackingResponse: validData' , trackingData } ) ;
97-
98- // we have tracking data for the bus
99- if ( trackingData ) {
100- trackingInformation . push ( trackingData ) ;
101- } else {
102- return false ;
103- }
104- return true ;
39+ const vehicles = await fetchVehicles ( ) ;
40+ console . log ( 'vehicles' , vehicles ) ;
41+
42+ const trackingInformation = requestData . map ( ( data ) => {
43+ const { routeID, tripID } = data ;
44+ const vehicleData = getVehicleInformation ( routeID , tripID , vehicles ) ;
45+ if ( ! vehicleData ) {
46+ LogUtils . log ( { message : 'getVehicleResponse: noData' , vehicleData } ) ;
47+ return null ;
10548 }
106- return false ;
107- } ) ) . catch ( ( err ) => {
108- LogUtils . logErr ( err , requestData , 'Tracking error' ) ;
109- throw err ;
110- } ) ;
111-
112- if ( trackingInformation . length > 0 ) {
113- return trackingInformation ;
114- }
49+ return vehicleData ;
50+ } ) . filter ( Boolean ) ;
11551
116- LogUtils . log ( { message : 'getTrackingResponse: noData' , trackingInformation } ) ;
117- // TODO: change this to non-dummy data once client fix is out
118- return [ {
119- case : 'noData' ,
120- commStatus : '' ,
121- delay : 0 ,
122- destination : '' ,
123- deviation : 0 ,
124- direction : '' ,
125- displayStatus : '' ,
126- gpsStatus : 0 ,
127- heading : 0 ,
128- lastStop : '' ,
129- lastUpdated : 0 ,
130- latitude : 0 ,
131- longitude : 0 ,
132- name : '' ,
133- opStatus : '' ,
134- routeID : parseInt ( requestData [ 0 ] . routeID ) ,
135- runID : 0 ,
136- speed : 0 ,
137- tripID : 0 ,
138- vehicleID : 0 ,
139- } ] ;
52+ return trackingInformation ;
14053}
14154
14255/**
@@ -146,7 +59,11 @@ async function getTrackingResponse(requestData: Object): Object {
14659 * @param rtf
14760 * @returns Object
14861 */
149- function getDelayInformation ( stopID : String , tripID : String , rtf : Object ) : ?Object {
62+ function getDelayInformation (
63+ stopID : ?String ,
64+ tripID : ?String ,
65+ rtf : ?Object ,
66+ ) : ?Object {
15067 // rtf param ensures the realtimeFeed doesn't update in the middle of execution
15168 // if invalid params or the trip is inactive
15269 if ( ! stopID
@@ -172,8 +89,32 @@ function getDelayInformation(stopID: String, tripID: String, rtf: Object): ?Obje
17289 } ;
17390}
17491
92+ function getVehicleInformation (
93+ routeID : ?String ,
94+ tripID : ?String ,
95+ vehicles : ?Object ,
96+ ) : ?Object {
97+ // vehicles param ensures the vehicle tracking information doesn't update in
98+ // the middle of execution
99+ if ( ! routeID
100+ || ! tripID
101+ || ! vehicles
102+ || vehicles === { } ) {
103+ LogUtils . log ( {
104+ category : 'getVehicleInformation NULL' ,
105+ routeID,
106+ tripID,
107+ } ) ;
108+ return null ;
109+ }
110+
111+ return Object . values ( vehicles ) . find ( v => v . routeID === routeID && v . tripID === tripID ) ;
112+ }
113+
175114export default {
176115 fetchRTF,
116+ fetchVehicles,
177117 getDelayInformation,
118+ getVehicleInformation,
178119 getTrackingResponse,
179120} ;
0 commit comments