generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from agiledev-students-fall2023/Jaden's-Branch
Manually merged @r8btx's changes with mine to resolve conflicts
- Loading branch information
Showing
5 changed files
with
115 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import axios from 'axios'; | ||
|
||
const EXCLUDED_ROUTES_QUERY_DELAY = 600000; // 10 mins | ||
const ALERTS_QUERY_DELAY = 120000; // 2 mins | ||
const MIN_QUERY_DELAY = 60000; // 1 min | ||
|
||
let lastExcludedRoutesQuery = -EXCLUDED_ROUTES_QUERY_DELAY; | ||
let lastAlertsQuery = -ALERTS_QUERY_DELAY; | ||
let lastQuery = -MIN_QUERY_DELAY; | ||
let lastResults = {}; | ||
|
||
// Query transportations (real-time update is done through the websocket) | ||
export async function queryTransportations(refresh) { | ||
// Prevent too frequent requests (rate limiting) | ||
if (performance.now() - lastQuery < MIN_QUERY_DELAY) { | ||
return lastResults; | ||
} | ||
|
||
// JSON | ||
const json = { | ||
s0: localStorage.agencyId, | ||
sA: 1, | ||
}; | ||
|
||
const subscribedRoutes = localStorage.subscribedRoutes ? localStorage.subscribedRoutes.split(',') : []; | ||
if (subscribedRoutes.length) { | ||
json.rA = subscribedRoutes.length; | ||
} | ||
subscribedRoutes.forEach((route, index) => { | ||
json['r' + index] = route; | ||
}); | ||
const formData = new URLSearchParams({}); | ||
formData.append('json', JSON.stringify(json)); | ||
|
||
// Params for URL | ||
const params = { | ||
getBuses: refresh || performance.now() - lastExcludedRoutesQuery > EXCLUDED_ROUTES_QUERY_DELAY ? 1 : 2, | ||
deviceId: localStorage.deviceId, | ||
}; | ||
|
||
// Optional parameters | ||
if (refresh) { | ||
params.speed = '1'; | ||
} | ||
if (performance.now() - lastAlertsQuery > ALERTS_QUERY_DELAY) { | ||
params.alertCRC = localStorage.alertCRC; | ||
lastAlertsQuery = performance.now(); | ||
} | ||
|
||
const urlParams = new URLSearchParams(params); | ||
const url = `${localStorage.serviceEndpointSub}/mapGetData.php?${urlParams.toString()}`; | ||
|
||
try { | ||
const response = await axios.post(url, formData); | ||
const data = response.data; | ||
if (!data) { | ||
throw new Error('empty response'); | ||
} | ||
if (data.excludedRoutes) { | ||
lastExcludedRoutesQuery = performance.now(); | ||
// place routes2 implementation here? | ||
} | ||
if (data && ((data.alertCRC && data.alertCRC != localStorage.alertCRC) || data.lastPushId > 0)) { | ||
// query alerts | ||
} | ||
|
||
const transportations = data.buses && Object.keys(data.buses).length ? data.buses : {}; | ||
|
||
lastResults = transportations; | ||
lastQuery = performance.now(); | ||
|
||
return transportations; | ||
} catch (error) { | ||
console.log('Transportations query error: ' + error.message); | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters