@@ -22,25 +22,63 @@ async function fetchVehicles(): Object {
2222 return JSON . parse ( data ) ;
2323}
2424
25+ /**
26+ * We want to format the old data:
27+ * [
28+ * {
29+ * "stopID": "523",
30+ * "routeID": "15",
31+ * "tripIdentifiers": ["t607-b29-s1C"]
32+ * },
33+ * ...
34+ * ]
35+ * into the new data format type:
36+ * [
37+ * {
38+ * "routeID" : String,
39+ * "tripID" : String
40+ * },
41+ * ...
42+ * ]
43+ * @param {* } requestData
44+ */
45+ function formatOldRequestData ( requestData : Object ) : Object {
46+ // don't format requests that follow the new format
47+ if ( requestData == null || ! ( 'tripIdentifiers' in requestData [ 0 ] ) ) {
48+ return requestData ;
49+ }
50+ const formattedData = requestData . map ( ( data ) => {
51+ const { routeID } = data ;
52+ const { tripIdentifiers } = data ;
53+ return tripIdentifiers . map ( tripID => ( { routeID, tripID } ) ) ;
54+ } ) ;
55+ // flatten the data so that we don't have nested arrays
56+ return Array . prototype . concat . apply ( [ ] , formattedData ) ;
57+ }
58+
2559/**
2660 * Given an array of { routeID, tripID },
2761 * Return bus information
28- * Input:
29- [
30- {
31- “routeID” : String,
32- tripID : String
33- },
34- …
35- ]
62+ * Input:[
63+ * {
64+ * routeID : String,
65+ * tripID : String
66+ * },... ]
67+ * NOTE: Because we need to provide backwards compatibility with old iOS clients
68+ * we have to follow their janky way of routeID input is String but routeID
69+ * output is Number. This "routeID" is also named jankily, which is supposed to
70+ * be routeNumber from v2/route/. We cast this to Number in getVehicleInformation.
71+ *
72+ *
3673 */
3774async function getTrackingResponse ( requestData : Object ) : Object {
75+ const formattedData = formatOldRequestData ( requestData ) ;
3876 LogUtils . log ( { message : 'getTrackingResponse: entering function' } ) ;
3977 const vehicles = await fetchVehicles ( ) ;
4078
41- const trackingInformation = requestData . map ( ( data ) => {
42- const { routeNumber , tripID } = data ;
43- const vehicleData = getVehicleInformation ( routeNumber , tripID , vehicles ) ;
79+ const trackingInformation = formattedData . map ( ( data ) => {
80+ const { routeID , tripID } = data ;
81+ const vehicleData = getVehicleInformation ( routeID , tripID , vehicles ) ;
4482 if ( ! vehicleData ) {
4583 LogUtils . log ( { message : 'getVehicleResponse: noData' , vehicleData } ) ;
4684 return null ;
@@ -88,45 +126,83 @@ function getDelayInformation(
88126 } ;
89127}
90128
129+ /**
130+ *
131+ * @param {* } routeID
132+ * @param {* } tripID
133+ * @param {* } vehicles
134+ */
91135function getVehicleInformation (
92- routeNumber : ?Number ,
136+ routeID : ?String ,
93137 tripID : ?String ,
94138 vehicles : ?Object ,
95139) : ?Object {
96140 // vehicles param ensures the vehicle tracking information doesn't update in
97141 // the middle of execution
98- if ( ! routeNumber
142+ if ( ! routeID
99143 || ! tripID
100144 || ! vehicles
101145 || vehicles === { } ) {
102146 LogUtils . log ( {
103147 category : 'getVehicleInformation NULL' ,
104- routeNumber ,
148+ routeID ,
105149 tripID,
106150 } ) ;
107151 return null ;
108152 }
109-
110153 const vehicleData = Object . values ( vehicles ) . find (
111- v => parseInt ( v . routeID ) === routeNumber && v . tripID === tripID ,
154+ v => ( v . routeID === routeID ) && ( v . tripID === tripID ) ,
112155 ) ;
113156 if ( ! vehicleData ) {
114157 LogUtils . log ( {
115158 category : 'getVehicleInformation no data' ,
116- routeNumber ,
159+ routeID ,
117160 tripID,
118161 } ) ;
119- return null ;
162+ return {
163+ case : 'noData' ,
164+ delay : 0 ,
165+ destination : '' ,
166+ deviation : 0 ,
167+ direction : '' ,
168+ displayStatus : '' ,
169+ gpsStatus : 0 ,
170+ heading : 0 ,
171+ lastStop : '' ,
172+ lastUpdated : 0 ,
173+ latitude : 0 ,
174+ longitude : 0 ,
175+ name : '' ,
176+ opStatus : '' ,
177+ routeID : Number ( routeID ) , // although input is string, old clients expect a number
178+ runID : 0 ,
179+ speed : 0 ,
180+ tripID : 0 ,
181+ vehicleID : 0 ,
182+ congestionLevel : 0 ,
183+ } ;
120184 }
121185 return {
122- bearing : vehicleData . bearing ,
123- congestionLevel : vehicleData . congestionLevel ,
186+ case : 'validData' ,
187+ delay : 0 ,
188+ destination : '' ,
189+ deviation : 0 ,
190+ direction : '' ,
191+ displayStatus : '' ,
192+ gpsStatus : 0 ,
193+ heading : vehicleData . bearing ,
194+ lastStop : '' ,
195+ lastUpdated : vehicleData . timestamp ,
124196 latitude : vehicleData . latitude ,
125197 longitude : vehicleData . longitude ,
126- routeNumber,
127- speed : vehicleData . speed ,
128- timestamp : vehicleData . timestamp ,
129- tripID,
198+ name : '' ,
199+ opStatus : '' ,
200+ routeID : Number ( routeID ) , // although input is string, old clients expect a number
201+ runID : 0 ,
202+ speed : parseInt ( vehicleData . speed ) ,
203+ tripID : 0 ,
204+ vehicleID : Number ( vehicleData . vehicleID ) ,
205+ congestionLevel : vehicleData . congestionLevel ,
130206 } ;
131207}
132208
0 commit comments