Fix station preferences mapping

This commit is contained in:
Conor McNamara
2023-03-16 10:30:04 +00:00
parent f3ea627bab
commit b4733d07e8
5 changed files with 48 additions and 74 deletions

View File

@ -49,8 +49,8 @@ exports.postStationData = functions.https.onRequest((request, response) => {
cors(request, response, () => { cors(request, response, () => {
var batchWrite = db.batch(); var batchWrite = db.batch();
jsonData.forEach((doc) => { jsonData.forEach((doc) => {
// append if the dartCodes hashset is empty or the current station is not present // append if the dartCodes hashset is empty or the current station is not present, and ignoring positions of zero
if (dartCodes.size == 0 || !dartCodes.has(doc["StationCode"][0])) { if ((dartCodes.size == 0 || !dartCodes.has(doc["StationCode"][0])) && !(doc["StationLongitude"] == 0 || doc["StationLatitude"] == 0)) {
doc["StationType"] = [stationTypeCode] doc["StationType"] = [stationTypeCode]
var docID = db.collection('stations').doc(doc["StationCode"][0]) var docID = db.collection('stations').doc(doc["StationCode"][0])
batchWrite.set(docID, doc); batchWrite.set(docID, doc);

View File

@ -56,15 +56,6 @@ export default {
created() { created() {
this.user = auth.currentUser this.user = auth.currentUser
const functions = getFunctions(app)
let host = window.location.hostname
if (host === '127.0.0.1' || host === 'localhost') {
connectFunctionsEmulator(functions, host, 5001);
}
const secureFunction = httpsCallable(functions, 'securefunction')
secureFunction().then((response) => {
console.log(response);
})
}, },
methods: { methods: {

View File

@ -60,7 +60,7 @@ export default {
signInWithEmailAndPassword(auth, this.email, this.password) signInWithEmailAndPassword(auth, this.email, this.password)
.then((userCredential) => { .then((userCredential) => {
const user = userCredential.user const user = userCredential.user
this.$router.push({path:'/account'}) this.$router.push({path:'/'})
}) })
.catch((error) => { .catch((error) => {
this.FirebaseError = error.message this.FirebaseError = error.message

View File

@ -45,7 +45,7 @@
<!-- train overlay --> <!-- train overlay -->
<template v-for="coordinate, i in trainCoordinates" :position="inline-block"> <template v-for="coordinate, i in trainCoordinates" :position="inline-block">
<ol-overlay v-if="showTrains[i]" :position="coordinate" :positioning="center-center" :offset="[-14,-16]"> <ol-overlay v-if="showTrains[i]" :position="coordinate" :offset="[-14,-16]">
<div class="overlay-content" @click="getSelectedTrain(i)"> <div class="overlay-content" @click="getSelectedTrain(i)">
<div v-if="getTrainType(i) === 'DART'"> <div v-if="getTrainType(i) === 'DART'">
<img v-if="isTrainRunning(i) && isTrainLate(i)" src="../assets/red-train-tram-solid.png" class="trainMapIcon" alt="Late DART Icon"> <img v-if="isTrainRunning(i) && isTrainLate(i)" src="../assets/red-train-tram-solid.png" class="trainMapIcon" alt="Late DART Icon">
@ -63,7 +63,7 @@
<!-- station overlay --> <!-- station overlay -->
<template v-for="coordinate, i in stationCoordinates" :position="inline-block"> <template v-for="coordinate, i in stationCoordinates" :position="inline-block">
<ol-overlay v-if="showStations[i]" :position="coordinate" :positioning="center-center" :offset="[-14,-16]"> <ol-overlay v-if="showStations[i]" :position="coordinate" :offset="[-14,-16]">
<div class="overlay-content" @click="getSelectedStation(i)"> <div class="overlay-content" @click="getSelectedStation(i)">
<img src="../assets/station.png" class="stationMapIcon" alt="Station Icon"> <img src="../assets/station.png" class="stationMapIcon" alt="Station Icon">
</div> </div>
@ -94,24 +94,11 @@ export default {
name: "MapPage", name: "MapPage",
data() { data() {
const center = ref(fromLonLat([-7.5029786, 53.4494762]));
const projection = ref('EPSG:3857');
const zoom = ref(7);
const rotation = ref(0);
const radius = ref(10);
const strokeWidth = ref(1);
const strokeColor = ref('black');
const fillColor = ref('red');
return { return {
center, center: fromLonLat([-7.5029786, 53.4494762]),
projection, projection: ref('EPSG:3857'),
zoom, zoom: ref(7),
rotation, rotation: ref(0),
radius,
strokeWidth,
strokeColor,
fillColor,
showTrains: [], showTrains: [],
showStations: [], showStations: [],
@ -151,17 +138,17 @@ export default {
this.getTrainAndStationData(); this.getTrainAndStationData();
} }
// request new data every 60 seconds // request new data every 60 seconds
// window.setInterval(this.getLiveTrainData, 60000); // window.setInterval(this.getTrainAndStationData, 60000);
}, },
methods: { methods: {
getPreferences() { getPreferences() {
if (!store.loggedIn) return if (!store.loggedIn) return
const functions = getFunctions(app); const functions = getFunctions(app);
let host = window.location.hostname let host = window.location.hostname
if (host === '127.0.0.1' || host == 'localhost') { if (host === '127.0.0.1' || host == 'localhost') {
connectFunctionsEmulator(functions, host, 5001); connectFunctionsEmulator(functions, host, 5001);
} }
const getPreferencesData = httpsCallable(functions, 'getPreferences') const getPreferencesData = httpsCallable(functions, 'getPreferences')
getPreferencesData().then((response) => { getPreferencesData().then((response) => {
@ -176,9 +163,9 @@ export default {
this.showTerminated = response.data.data["showTerminated"] this.showTerminated = response.data.data["showTerminated"]
this.showNotYetRunning = response.data.data["showNotYetRunning"] this.showNotYetRunning = response.data.data["showNotYetRunning"]
// update the map with these preferences // update the map with the user's preferences
this.decideShowStations()
this.decideShowTrains() this.decideShowTrains()
this.decideShowStations()
} }
}) })
.catch((error) => { .catch((error) => {
@ -203,7 +190,7 @@ export default {
const functions = getFunctions(app); const functions = getFunctions(app);
let host = window.location.hostname let host = window.location.hostname
if (host === '127.0.0.1' || host == 'localhost') { if (host === '127.0.0.1' || host == 'localhost') {
connectFunctionsEmulator(functions, host, 5001); connectFunctionsEmulator(functions, host, 5001);
} }
const postPreferencesData = httpsCallable(functions, 'postPreferences') const postPreferencesData = httpsCallable(functions, 'postPreferences')
@ -217,10 +204,8 @@ export default {
// method to determine whether or not to show each train // method to determine whether or not to show each train
decideShowTrains() { decideShowTrains() {
for (let i = 0; i < this.showTrains.length; i++) { for (let i=0; i<this.showTrains.length; i++) {t
// determine whether or not the tain is a DART or not
let isDART = this.getTrainType(i) == "DART"; let isDART = this.getTrainType(i) == "DART";
if ((this.showRunning && this.allTrains[i]["TrainStatus"][0] == "R") || (this.showTerminated && this.allTrains[i]["TrainStatus"][0] == "T") || this.showNotYetRunning && this.allTrains[i]["TrainStatus"][0] == "N") { if ((this.showRunning && this.allTrains[i]["TrainStatus"][0] == "R") || (this.showTerminated && this.allTrains[i]["TrainStatus"][0] == "T") || this.showNotYetRunning && this.allTrains[i]["TrainStatus"][0] == "N") {
if ((this.showDART && isDART) || (this.showMainland && !isDART)) { if ((this.showDART && isDART) || (this.showMainland && !isDART)) {
this.showTrains[i] = (this.showLate && this.isTrainLate(i)) || (this.showOnTime && !this.isTrainLate(i)); // || (this.showMainland && !isDART) || (this.showDART && isDART); this.showTrains[i] = (this.showLate && this.isTrainLate(i)) || (this.showOnTime && !this.isTrainLate(i)); // || (this.showMainland && !isDART) || (this.showDART && isDART);
@ -237,10 +222,10 @@ export default {
// method to determine whether or not to show each station // method to determine whether or not to show each station
decideShowStations() { decideShowStations() {
for (let i = 0; i < this.showStations.length; i++) { for (var i=0; i<this.showStations.length; i++) {
let isDARTStation = this.allStations[i]["StationType"][0] == "DART"; let isDARTStation = this.getStationType(j) == "DART";
this.showStations[i] = (this.showDARTStations && isDARTStation) || (this.showMainlandStations && !isDARTStation); this.showStations[i] = (this.showDARTStations && isDARTStation) || (this.showMainlandStations && !isDARTStation);
} }
}, },
// method to display a selected train // method to display a selected train
@ -274,12 +259,8 @@ export default {
// method to determine whether or not a selected train is running // method to determine whether or not a selected train is running
isTrainRunning(i) { isTrainRunning(i) {
if (this.allTrains[i]["TrainStatus"][0] == "R") { if (this.allTrains[i]["TrainStatus"][0] == "R") return true;
return true; else return false;
}
else {
return false;
}
}, },
// method that returns the type of train (either "Train" or "DART") // method that returns the type of train (either "Train" or "DART")
@ -287,6 +268,11 @@ export default {
return this.allTrains[i]["TrainType"][0]; return this.allTrains[i]["TrainType"][0];
}, },
// method that returns the type of station (either "Train" or "DART")
getStationType(i) {
return this.allStations[i]["StationType"][0]
},
// method to fetch live train and station data from the database // method to fetch live train and station data from the database
getTrainAndStationData() { getTrainAndStationData() {
const functions = getFunctions(app); const functions = getFunctions(app);
@ -305,15 +291,16 @@ export default {
try { try {
if (!response.data) throw new Error("Error fetching live train data from the database"); if (!response.data) throw new Error("Error fetching live train data from the database");
var insights = { var insights = {
"totalNumTrains": 0, "totalNumTrains": 0,
"numRunningTrains": 0, "numRunningTrains": 0,
"numLateRunningTrains": 0, "numLateRunningTrains": 0,
"numTrains": 0, "numTrains": 0,
"numDarts": 0, "numDarts": 0,
"totalNumStations": 0, "totalNumStations": 0,
"numTrainStations": 0, "numTrainStations": 0,
"numDartStations": 0 "numDartStations": 0
}; };
var unorderedTrains = []; var unorderedTrains = [];
var currentMessages = []; var currentMessages = [];
var latest = null; var latest = null;
@ -325,13 +312,11 @@ export default {
for (var i=0; i<response.data.length; i++) { for (var i=0; i<response.data.length; i++) {
let train = response.data[i]; let train = response.data[i];
this.allTrains[i] = train; this.allTrains[i] = train;
this.trainCoordinates[i] = fromLonLat([train["TrainLongitude"][0], train["TrainLatitude"][0]])
// filling showTrains witht the default value true
this.showTrains[i] = true;
this.trainCoordinates[i] = ref(fromLonLat([train["TrainLongitude"][0], train["TrainLatitude"][0]]))
insights["totalNumTrains"] += 1 insights["totalNumTrains"] += 1
// filling showTrains with the default value - true
this.showTrains[i] = true;
if (train["TrainType"][0] == "Train") insights["numTrains"] += 1; if (train["TrainType"][0] == "Train") insights["numTrains"] += 1;
else if (train["TrainType"][0] == "DART") insights["numDarts"] += 1; else if (train["TrainType"][0] == "DART") insights["numDarts"] += 1;
@ -362,7 +347,6 @@ export default {
// train is early or ontime // train is early or ontime
else { else {
if (!earliest) earliest = train; if (!earliest) earliest = train;
// check for a new earliest train (early trains are -x mins late) // check for a new earliest train (early trains are -x mins late)
if (num < currEarliestTime) { if (num < currEarliestTime) {
earliest = train; earliest = train;
@ -390,26 +374,25 @@ export default {
for (var i=0; i<response.data.length; i++) { for (var i=0; i<response.data.length; i++) {
let station = response.data[i]; let station = response.data[i];
this.allStations[i] = station; this.allStations[i] = station;
this.stationCoordinates[i] = fromLonLat([station["StationLongitude"][0], station["StationLatitude"][0]])
insights["totalNumStations"] += 1
// setting the station to show on the map by default - true // setting the station to show on the map by default - true
this.showStations[i] = true; this.showStations[i] = true;
this.stationCoordinates[i] = ref(fromLonLat([station["StationLongitude"][0], station["StationLatitude"][0]]))
insights["totalNumStations"] += 1
if (station["StationType"][0] == "DART") insights["numDartStations"] += 1; if (station["StationType"][0] == "DART") insights["numDartStations"] += 1;
else if (station["StationType"][0] == "Train") insights["numTrainStations"] += 1; else if (station["StationType"][0] == "Train") insights["numTrainStations"] += 1;
} }
store.setInsights(insights); store.setInsights(insights);
loader.hide(); loader.hide();
// request the user's preferences
this.getPreferences()
}) })
} }
catch (error) { catch (error) {
console.log(error.message) console.log(error.message)
loader.hide(); loader.hide();
} }
this.getPreferences()
}) })
}, },
@ -420,6 +403,7 @@ export default {
if (host === '127.0.0.1' || host === 'localhost') { if (host === '127.0.0.1' || host === 'localhost') {
connectFunctionsEmulator(functions, host, 5001); connectFunctionsEmulator(functions, host, 5001);
} }
// post live train data // post live train data
const postTrainData = httpsCallable(functions, 'postLiveTrainData'); const postTrainData = httpsCallable(functions, 'postLiveTrainData');
postTrainData().then((response) => { postTrainData().then((response) => {

View File

@ -2,7 +2,6 @@ import {getAuth, onAuthStateChanged} from "firebase/auth"
import app from '../api/firebase'; import app from '../api/firebase';
function isAuth(to, from, next) { function isAuth(to, from, next) {
console.log("Checking auth")
const auth = getAuth(app) const auth = getAuth(app)
onAuthStateChanged(auth, (user) => { onAuthStateChanged(auth, (user) => {
// user is logged in, continue to page // user is logged in, continue to page