diff --git a/GoNOW/app/scripts/Polling.ts b/GoNOW/app/scripts/Polling.ts index b0a5114..bb577c3 100644 --- a/GoNOW/app/scripts/Polling.ts +++ b/GoNOW/app/scripts/Polling.ts @@ -1,9 +1,9 @@ -import { RouteRequestBody } from '../../../backend/index'; +import { travelTimeRequestBody } from '../../../backend/index'; import { getNextEvent } from './Event'; import { getMyLocation } from './Geo'; interface routeResponse { - travelTime: number; + travelTime: number; } export const POLLING_INTERVAL_MIN = 15; // 15 minutes, minimum recommended interval for background fetch is 15 minutes @@ -37,9 +37,9 @@ export const poll = async (): Promise => { return; } else{ - const requestBody: RouteRequestBody = { - event: next_event, - coordinates: current_location.coordinates, + const requestBody: travelTimeRequestBody = { + event: next_event, + coordinates: current_location.coordinates, } try { diff --git a/backend/index.ts b/backend/index.ts index 13bd6de..d44c391 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -13,11 +13,17 @@ app.use(bodyParser.json()) const apiKey = process.env.API_KEY??''; -export interface RouteRequestBody { +export interface travelTimeRequestBody { event:Event, coordinates:Coordinates; } +interface RouteRequestBody { + origin: Coordinates, + destination: Coordinates, + travelMode: string +} + interface AutoscheduleRequestBody { events:Event[], workflow: Workflow, @@ -64,6 +70,51 @@ app.get('/api/poll', async (req: Request, res: Response) => { + //TODO + //parse incoming task for location + console.log(req.body) + const { origin, destination, travelMode } = req.body; + + //construct request + const url = 'https://routes.googleapis.com/directions/v2:computeRoutes'; + + const headers = new Headers({ + 'Content-Type': 'application/json', + 'X-Goog-Api-Key': apiKey, + 'X-Goog-FieldMask': '*' + }); + + const body = JSON.stringify({ + origin: { + location: { latLng: { latitude: origin.latitude, longitude: origin.longitude } } + }, + destination: { + location: { latLng: { latitude: destination.latitude, longitude: destination.longitude } } + }, + travelMode: travelMode + }); + + try { + const response = await fetch(url, { + method: 'POST', + headers: headers, + body: body + }); + + if (!response.ok) throw new Error(`HTTP Error: ${String(response.status)}`); + //const responseText = await response.text(); + //console.log('Raw response:', responseText); + const responseData = await response.json(); + console.log('response data', responseData); + res.status(200).send(responseData); + + } catch (error) { + console.error('Error fetching route:', error); + res.status(500).send(''); + } +}); + app.listen(PORT,() => { console.log(`Backend listening on port ${String(PORT)}`);