import { reactive } from 'vue' import { fromLonLat } from 'ol/proj.js'; import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions"; import app from '../api/firebase'; export const store = reactive({ // map and insights data insights: {}, latestTrain: {}, earliestTrain: {}, orderedTrains: [], selectedTrain: {}, selectedStation: {}, allTrainsJSON: {}, // side bar displaySelectedTrain: false, displayedSelectedStation: false, // login status loggedIn: false, isWaitingForLoginStatus: true, setOrderedTrains(unorderedTrains) { // sort in ascending order unorderedTrains.sort((a, b) => { return a.time - b.time }) this.orderedTrains = unorderedTrains }, // method to populate the database for local testing postTrainAndStationData(host) { const functions = getFunctions(app); if (host === '127.0.0.1' || host === 'localhost') { connectFunctionsEmulator(functions, host, 5001); } const postTrainData = httpsCallable(functions, 'postLiveTrainData'); postTrainData().then(() => { const postStationData = httpsCallable(functions, 'postStationData'); postStationData().then(() => { this.getTrainAndStationData(host) }) }) .catch((error) => { console.log(error.message) //this.showToast(error.message, "red") }) }, // method to fetch live train and station data from Firestore getTrainAndStationData(host) { const functions = getFunctions(app); if (host === '127.0.0.1' || host == 'localhost') { connectFunctionsEmulator(functions, host, 5001); } const getTrainData = httpsCallable(functions, 'getLiveTrainData'); getTrainData().then((response) => { try { if (!response.data) throw new Error("Error fetching live train data from the database") var insights = { "totalNumTrains": 0, "numRunningTrains": 0, "numLateRunningTrains": 0, "numTrains": 0, "numDarts": 0, "totalNumStations": 0, "numTrainStations": 0, "numDartStations": 0 }; var unorderedTrains = []; var currentMessages = []; var latest = null; var earliest = null; var currLatestTime = null; var currEarliestTime = null; for (var i=0; i currLatestTime) { latest = train; currLatestTime = num; } } // train is early or ontime else { if (!earliest) earliest = train; // check for a new earliest train (early trains are -x mins late) if (num < currEarliestTime) { earliest = train; currEarliestTime = num; } } } } // assign results after looping through JSON insights["percentageLate"] = ((insights["numLateRunningTrains"] / insights["numRunningTrains"]) * 100).toFixed(2); insights["percentageNotLate"] = (100 - insights["percentageLate"]).toFixed(2); insights["latestTime"] = currLatestTime; insights["earliestTime"] = currEarliestTime; this.publicMessages = currentMessages; // assign the results to the Vue Store store.setEarliestTrain(earliest); store.setLatestTrain(latest); store.setRawData(response.data); store.setOrderedTrains(unorderedTrains); const getStationData = httpsCallable(functions, 'getStationData'); getStationData().then((response) => { if (!response.data) throw new Error("Error fetching station from the database"); for (var i=0; i { //loader.hide() //this.showToast("Error fetching live data", "red") }) } })