-
Notifications
You must be signed in to change notification settings - Fork 0
Backend
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
/* Internal use functions */
function calculateDistance(lat1, lon1, lat2, lon2) { const R = 3958.8; // Radius of the Earth in miles const dLat = toRadians(lat2 - lat1); const dLon = toRadians(lon2 - lon1); const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); const distance = R * c; return distance; }
function toRadians(degrees) { return degrees * (Math.PI / 180); }
/* Scheduled Functions */
exports.resetCloudVariables = functions.pubsub.schedule('0 0 * * *').timeZone('UTC').onRun((context) => { const db = admin.database(); const ref = db.ref('cloud_variables');
// Reset all cloud variables to their default values
const updates = {
chapelWayCAP: 0,
ryanCenterCAP: 0,
flaggRdCAP: 0,
fineArtsCAP: 0,
keaneyCAP: 0,
leavingMarkers: {
"chapelWay": {
},
"fineArts": {
},
"keaney":{
},
"flaggRd":{
},
"ryanCenter":{
}
}
};
return ref.update(updates)
.then(() => {
console.log('Cloud variables reset successfully');
return null;
})
.catch((error) => {
console.error('Error resetting cloud variables:', error);
throw new functions.https.HttpsError('internal', 'Failed to reset cloud variables');
});
});
/* Cloud Functions */
exports.getDistance = functions.https.onRequest((request, response) => { const coordinates = [ { lat: 41.48553, lon: -71.52263 }, // Behind Emporium (Lot 4) { lat: 41.48831, lon: -71.52231 }, // Fine Arts (Lot 7) { lat: 41.49031, lon: -71.53864 }, // Plains Road (Lot 25) { lat: 41.48340, lon: -71.53595 }, // Keaney (Lot 26) { lat: 41.48895, lon: -71.54081 }, // Plains Road South (Lot 31) { lat: 41.49067, lon: -71.52812 }, // Flagg Road Street Parking ];
const distances = coordinates.map((coord) => {
const distance = calculateDistance(
request.query.lat,
request.query.lon,
coord.lat,
coord.lon
);
return distance;
});
response.json(distances);
});
In the Easy Parking app, Cloud Functions play an important role in calculating distances between the user's current location and available parking spots. These functions are able to use Firebase's infrastructure to execute backend logic in response to HTTP requests.
Firstly, the app's HTTP Cloud Function, called getDistance, is triggered when a user sends a request with their latitude and longitude coordinates as query parameters. Within this function, the calculateDistance function is utilized. This function employs the Haversine formula to accurately compute the distance between the user's location and a predefined set of parking spots. The result is then formatted as a JSON response and sent back to the user, providing them with the distances to nearby parking options. This process is seamlessly integrated into the app's frontend, enabling users to quickly find and assess available parking spaces based on their current position.
The function, named resetCloudVariables, is a scheduled Cloud Function in Firebase that runs at midnight UTC (00:00 UTC) every day. Its purpose is to reset certain values stored in the Firebase Realtime Database under the cloud_variables node to their default values.
Overall, Cloud Functions act as the backbone of the Easy Parking app's backend infrastructure, facilitating real-time distance calculations and enhancing the user experience by delivering pertinent information promptly. This integration of Cloud Functions with Firebase not only streamlines the development process but also ensures efficient and scalable performance, essential for a dynamic and responsive parking application.
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "chapelWayJSON": { "type": "string" }, "fineArtsJSON": { "type": "string" }, "flaggRdJSON": { "type": "string" }, "keaneyJSON": { "type": "string" }, "ryanCenterJSON": { "type": "string" }, "Metrics": { "type": "object", "properties": { "NPSScore": { "type": "number" }, "detractors": { "type": "integer" }, "passives": { "type": "integer" }, "promoters": { "type": "integer" }, "surveyCount": { "type": "integer" } }, "required": [ "NPSScore", "detractors", "passives", "promoters", "surveyCount" ] }, "Users": { "type": "array", "items": { "type": "string" } }, "capacity": { "type": "object", "properties": { "chapelWayCAP": { "type": "integer" }, "fineArtsCAP": { "type": "integer" }, "flaggRdCAP": { "type": "integer" }, "keaneyCAP": { "type": "integer" }, "ryanCenterCAP": { "type": "integer" } }, "required": [ "chapelWayCAP", "fineArtsCAP", "flaggRdCAP", "keaneyCAP", "ryanCenterCAP" ] }, "leavingMarkers": { "type": "object", "properties": { "chapelWay": { "type": "null" } }, "required": [ "chapelWay" ] } }, "required": [ "chapelWayJSON", "fineArtsJSON", "flaggRdJSON", "keaneyJSON", "ryanCenterJSON", "Metrics", "Users", "capacity", "leavingMarkers" ] }
--