(function () { var currentTime = 1721072111; var skw7430 = { "FlightInfoExResult": { "next_offset": -1, "flights": [ { "faFlightID": "SKW7430-1491197153-airline-0220", "ident": "SKW7430", "aircrafttype": "CRJ", "filed_ete": "01:07:00", "filed_time": 1491197153, "filed_departuretime": 1491451380, "filed_airspeed_kts": 408, "filed_airspeed_mach": "", "filed_altitude": 320, "route": "TWF3 TWF KU90K MQG", "actualdeparturetime": 0, "estimatedarrivaltime": 1491456000, "actualarrivaltime": 0, "diverted": "", "origin": "KSLC", "destination": "KLWS", "originName": "Salt Lake City Intl", "originCity": "Salt Lake City, UT", "destinationName": "Lewiston-Nez Perce County", "destinationCity": "Lewiston, ID" } ] } }; var skw7430 = { "FlightInfoExResult": { "next_offset": -1, "flights": [ { "faFlightID": "SKW7430-1491197153-airline-0220", "ident": "SKW7430", "aircrafttype": "CRJ", "filed_ete": "01:07:00", "filed_time": 1491197153, "filed_departuretime": 1491451380, "filed_airspeed_kts": 408, "filed_airspeed_mach": "", "filed_altitude": 320, "route": "TWF3 TWF KU90K MQG", "actualdeparturetime": 0, "estimatedarrivaltime": 1491456000, "actualarrivaltime": 0, "diverted": "", "origin": "KSLC", "destination": "KLWS", "originName": "Salt Lake City Intl", "originCity": "Salt Lake City, UT", "destinationName": "Lewiston-Nez Perce County", "destinationCity": "Lewiston, ID" } ] } }; var qxe2218 = { "FlightInfoExResult": { "next_offset": -1, "flights": [ { "faFlightID": "QXE2218-1491272700-schedule-0000", "ident": "QXE2218", "aircrafttype": "DH8D", "filed_ete": "00:38:00", "filed_time": 1491272700, "filed_departuretime": 1491445500, "filed_airspeed_kts": 351, "filed_airspeed_mach": "", "filed_altitude": 240, "route": "BOI LICKS PREIS MQG", "actualdeparturetime": 0, "estimatedarrivaltime": 1491448332, "actualarrivaltime": 0, "diverted": "", "origin": "KBOI", "destination": "KLWS", "originName": "Gowen Field", "originCity": "Boise, ID", "destinationName": "Lewiston-Nez Perce County", "destinationCity": "Lewiston, ID" } ] } }; var qxe2027 = { "FlightInfoExResult": { "next_offset": -1, "flights": [ { "faFlightID": "QXE2027-1491197153-airline-0048", "ident": "QXE2027", "aircrafttype": "DH8D", "filed_ete": "00:41:00", "filed_time": 1491197153, "filed_departuretime": 1491431700, "filed_airspeed_kts": 349, "filed_airspeed_mach": "", "filed_altitude": 210, "route": "SEA V2 ELN MQG", "actualdeparturetime": 0, "estimatedarrivaltime": 1491434760, "actualarrivaltime": 0, "diverted": "", "origin": "KSEA", "destination": "KLWS", "originName": "Seattle-Tacoma Intl", "originCity": "Seattle, WA", "destinationName": "Lewiston-Nez Perce County", "destinationCity": "Lewiston, ID" } ] } }; var skw7360 = { "FlightInfoExResult": { "next_offset": -1, "flights": [ { "faFlightID": "SKW7360-1491197153-airline-0026", "ident": "SKW7360", "aircrafttype": "CRJ", "filed_ete": "01:25:00", "filed_time": 1491197153, "filed_departuretime": 1491411600, "filed_airspeed_kts": 410, "filed_airspeed_mach": "", "filed_altitude": 320, "route": "TWF3 TWF KU90K MQG", "actualdeparturetime": 1491417551, "estimatedarrivaltime": 1491421560, "actualarrivaltime": 1491421560, "diverted": "", "origin": "KSLC", "destination": "KLWS", "originName": "Salt Lake City Intl", "originCity": "Salt Lake City, UT", "destinationName": "Lewiston-Nez Perce County", "destinationCity": "Lewiston, ID" } ] } }; // Check if the flight time is on the same day as today const isFlightToday = (flightTime) => { const currentTime = new Date(); const flightDate = new Date(flightTime); return ( currentTime.getFullYear() === flightDate.getFullYear() && currentTime.getMonth() === flightDate.getMonth() && currentTime.getDate() === flightDate.getDate() ); }; // Check if the flight has departed const hasDeparted = (time) => { const currentTime = Date.now(); const flightTime = new Date(time).getTime(); return currentTime > flightTime; }; // Check if flight is a known airline const isKnownAirline = (flightNumber) => { const airlineName = getAirlineName(flightNumber); return airlineName !== "unknown" ? true : false; }; function getAirlineName(flightNumber) { if (!flightNumber) return; if (flightNumber.includes("AS")) return "Alaska Airlines"; if (flightNumber.includes("DL")) return "Delta Airlines"; if (flightNumber.includes("UA")) return "United Airlines"; // If not found return Unknown return "unknown"; } var $ = jQuery; /** * baseUrl for the new rest api */ const baseUrl = "https://www.golws.com/wp-json/aeroapi/v4/flights"; /** * getArrivedData */ function getArrivedData() { $.ajax({ type: "GET", url: baseUrl + "/arrivals", success: function (result) { // if (result.error) { // alert("Failed to Get Arrived Information: " + result.error); // console.log(result.data); // return; // } console.log("Arrived Data"); console.log(result); setArrivalData(result); }, error: function (data) { console.error("arrivals", data); }, // dataType: "jsonp", // jsonp: "jsonp_callback", // xhrFields: { withCredentials: true }, }); } /** * getDepartedData */ function getDepartedData() { $.ajax({ type: "GET", url: baseUrl + "/departures", success: function (result) { // if (result.error) { // alert("Failed to Get Departed Information: " + result.error); // return; // } console.log("Departures Data", result); setDepartedData(result); }, error: function (data) { console.error("departures", data); }, // dataType: "jsonp", // jsonp: "jsonp_callback", // xhrFields: { withCredentials: true }, }); } /** * getEnrouteData */ function getEnrouteData() { $.ajax({ type: "GET", url: baseUrl + "/scheduled_arrivals", success: function (result) { // if (result.error) { // alert("Failed to Get Enroute Information: " + result.error); // return; // } console.log("scheduled_arrivals", result); setEnrouteData(result); }, error: function (data) { console.error("scheduled_arrivals", data); }, // dataType: "jsonp", // jsonp: "jsonp_callback", // xhrFields: { withCredentials: true }, }); } /** * getScheduledData */ function getScheduledData() { $.ajax({ type: "GET", url: baseUrl + "/scheduled_departures", success: function (result) { // if (result.error) { // alert("Failed to Get Scheduled Information: " + result.error); // return; // } console.log("scheduled_departures", result); setScheduledData(result); }, error: function (data) { console.error("scheduled_departures", data); }, // dataType: "jsonp", // jsonp: "jsonp_callback", // xhrFields: { withCredentials: true }, }); } /** * Section to load all of the files from cache * This is where all the variables get created and data is assigned * This is psuedo state management for all the various data */ // // Arrivals (v4) // `/airports/KLWS/flights/arrivals` // var arrivedData = {}; var loadArrivedDataFlag = 0; function loadArrivedData(filesArray) { arrivedFile = filesArray[0].substring(filesArray[0].lastIndexOf("/")); $.ajax({ type: "GET", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/data" + arrivedFile, dataType: "json", success: function (data) { // console.log(data); arrivedData = data; loadArrivedDataFlag = 1; buildFrontEnd(); }, }); } // // Scheduled Arrivals (v4) // `/airports/KLWS/flights/scheduled_arrivals` // var enrouteData = {}; var loadEnrouteDataFlag = 0; function loadEnrouteData(filesArray) { enrouteFile = filesArray[2].substring(filesArray[2].lastIndexOf("/")); $.ajax({ type: "GET", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/data" + enrouteFile, dataType: "json", success: function (data) { // console.log(data); enrouteData = data; loadEnrouteDataFlag = 1; buildFrontEnd(); }, }); } // // Departures (v4) // `/airports/KLWS/flights/departures` // var departedData = {}; var loadDepartedDataFlag = 0; function loadDepartedData(filesArray) { departedFile = filesArray[1].substring(filesArray[1].lastIndexOf("/")); $.ajax({ type: "GET", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/data" + departedFile, dataType: "json", success: function (data) { // console.log("fffff" + JSON.stringify(data)); departedData = data; loadDepartedDataFlag = 1; buildFrontEnd(); }, }); } // // Scheduled Departures (v4) // `/airports/KLWS/flights/scheduled_departures` // var scheduledData = {}; var loadScheduledDataFlag = 0; function loadScheduledData(filesArray) { enrouteFile = filesArray[3].substring(filesArray[3].lastIndexOf("/")); $.ajax({ type: "GET", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/data" + enrouteFile, dataType: "json", success: function (data) { // console.log(data); scheduledData = data; loadScheduledDataFlag = 1; buildFrontEnd(); }, }); } /* File to push all of the retrieved data to cache via setters-api.php */ var arrivalFlag = 0; function setArrivalData(result) { var arrivalsData = JSON.stringify(result); $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/setters-api.php", data: { arrivals: arrivalsData }, cache: false, success: function (data) { arrivalFlag = 1; // console.log('Arrivals Data Setter Worked'); if ( arrivalFlag == 1 && departedFlag == 1 && enrouteFlag == 1 && scheduledFlag == 1 ) { filesList(); } }, }); } var departedFlag = 0; function setDepartedData(result) { var departureData = JSON.stringify(result); $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/setters-api.php", data: { departed: departureData }, cache: false, success: function (data) { departedFlag = 1; // console.log('Departed Data Setter Worked'); if ( arrivalFlag == 1 && departedFlag == 1 && enrouteFlag == 1 && scheduledFlag == 1 ) { filesList(); } }, }); } var enrouteFlag = 0; function setEnrouteData(result) { var routeData = JSON.stringify(result); $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/setters-api.php", data: { enroute: routeData }, cache: false, success: function (data) { enrouteFlag = 1; // console.log('Enroute Data Setter Worked'); if ( arrivalFlag == 1 && departedFlag == 1 && enrouteFlag == 1 && scheduledFlag == 1 ) { filesList(); } }, }); } var scheduledFlag = 0; function setScheduledData(result) { var scheduleData = JSON.stringify(result); //console.log(departedData); $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/setters-api.php", data: { scheduled: scheduleData }, cache: false, success: function (data) { // console.log('Scheduled Data Setter Worked'); scheduledFlag = 1; if ( arrivalFlag == 1 && departedFlag == 1 && enrouteFlag == 1 && scheduledFlag == 1 ) { filesList(); } }, }); } // Change the last value to the amount of minutes you want to offset the time by. const EXPIRY_OFFSET_IN_SECONDS = 60 * 15; // 60 seconds in a minute, 15 minutes is our desired offset //Get all of the files from the data folder that are used for caching the data for this application starting the application. function filesList() { var filesArray = new Array(); $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/file-check.php", data: {}, cache: false, dataType: "json", success: function (data) { filesArray = data; checkFiles(data); }, }); } //Checks all the files for timestamp and if anything is out of date will download everything again or if not then it will build with the files it has cached. function checkFiles(filesArray) { const filesWithExpiredDates = []; // Loop over all the files found in the data folder from getCheckFilesData() function and verify if they have valid time stamps $(filesArray).each(function () { const endOfFileName = /[^_]*$/.exec(this)[0]; // We have to make sure we're comparing two numbers, or else we run into truthyness issues const fileCreationTimeAsNumber = Number( endOfFileName.substr(0, endOfFileName.lastIndexOf(".")) ); const currentTimeAsNumber = Number(currentTime); // compare the difference if ( currentTimeAsNumber > fileCreationTimeAsNumber + EXPIRY_OFFSET_IN_SECONDS ) { console.debug("Exec.js - At least one of the files is expired."); filesWithExpiredDates.push(fileCreationTimeAsNumber); } else { console.debug("Exec.js - None of the current files are expired."); } }); // If any files are expired OR any of the exsisting files are missing if (filesWithExpiredDates.length > 0 || filesArray.length !== 4) { console.debug("Exec.js - Getting new data."); // Delete Files as there are old files deleteFiles(); // Live Getters getArrivedData(); getDepartedData(); getEnrouteData(); getScheduledData(); } else { console.debug("Exec.js - Loading exsisting data."); loadArrivedData(filesArray); loadDepartedData(filesArray); loadEnrouteData(filesArray); loadScheduledData(filesArray); } } function deleteFiles() { $.ajax({ type: "POST", url: "https://www.golws.com/wp-content/themes/bb-child-theme-deluxe/setters-api.php", data: { deleteFiles: 1 }, cache: false, success: function (data) { // console.log('Files Deleted'); if ( arrivalFlag == 1 && departedFlag == 1 && enrouteFlag == 1 && scheduledFlag == 1 ) { filesList(); } }, }); } /* Build Functions */ function buildFrontEnd() { //Make sure everything is done and loaded then build front end. if ( loadArrivedDataFlag == 1 && loadDepartedDataFlag == 1 && loadEnrouteDataFlag == 1 && loadScheduledDataFlag == 1 ) { if ($("#arrivals-table").hasClass("fp-arrivals")) { buildFPArrivals(); buildFPDepartures(); } //TODO Full Set if to load. } } //Build Front Page Shortcode Arrivals. function buildFPArrivals() { var html = ""; var acutalEnrouteData = []; $(enrouteData.scheduled_arrivals).each(function () { if (this.codeshares_iata.length === 0) return; if (!isKnownAirline(this.codeshares_iata[0])) return; if (!isFlightToday(this.scheduled_in)) return; acutalEnrouteData.push(this); }); $(arrivedData.arrivals).each(function () { if (this.codeshares_iata.length === 0) return; if (!isKnownAirline(this.codeshares_iata[0])) return; acutalEnrouteData.push(this); }); acutalEnrouteData.sort(function (a, b) { return new Date(a.estimated_in) - new Date(b.estimated_in); }); console.debug('🚀 - acutalEnrouteData', acutalEnrouteData) // Build out the front end. if (acutalEnrouteData.length === 0) { html += 'No flights at this time.'; } else { $(acutalEnrouteData).each(function () { var ident = this.codeshares_iata[0]; const displayTime = this.actual_on ? (new Date(this.actual_on)) : (new Date(this.estimated_on)); //Output HTML html += ''; html += ""; //Flight No. html += ident; html += ""; html += ""; //Airline html += getAirlineName(ident); html += ""; html += ""; //Airport html += this.origin.city; html += ""; html += ""; //Time html += getDisplayTime(displayTime); html += ""; html += ''; //Status html += this.status; //Scheduled / On Time / Late(how much) / Landed html += ""; html += ""; }); // end of enroute loop } // end of if departedTotalNeeded if // Hide Preloader / Add HTML to DOM / Show. $(".preloader.two.arrivals").hide(); $("#arrivals-table thead").after(html); $("table#arrivals-table").delay(100).show(); } // End of buildFPArrivals(); //Build Front Page Shortcode Departures. function buildFPDepartures() { html = ""; var acutalDepartingData = []; $(scheduledData.scheduled_departures).each(function () { if (this.codeshares_iata.length === 0) return; if (!isKnownAirline(this.codeshares_iata[0])) return; if (!isFlightToday(this.scheduled_out)) return; acutalDepartingData.push(this); }); $(departedData.departures).each(function () { if (this.codeshares_iata.length === 0) return; if (!isKnownAirline(this.codeshares_iata[0])) return; acutalDepartingData.push(this); }); acutalDepartingData.sort(function (a, b) { return new Date(a.estimated_out) - new Date(b.estimated_out); }); // Build out the front end. if (acutalDepartingData.length === 0) { html += 'No flights at this time.'; } else { $(acutalDepartingData).each(function () { var ident = this.codeshares_iata[0]; html += ''; html += ""; //Flight No. html += ident; html += ""; html += ""; //Airline html += getAirlineName(ident); html += ""; html += ""; //Airport html += this.destination.city; html += ""; html += ""; //Time html += getDisplayTime(new Date(this.estimated_out)); html += ""; html += ''; //Status html += this.status; //Scheduled / On Time / Late(how much) / Landed html += ""; html += ""; }); // end of enroute loop } // end of if departedTotalNeeded if // Hide Preloader / Add HTML to DOM / Show. $(".preloader.two.departures").hide(); $("#departures-table thead").after(html); $("table#departures-table").delay(100).show(); //updateDeparture(); } // end of buildFPDepartures(); /* Utility Functions */ //If anonFlag is 0 then Flight is Scheduled. If anonFlag is 1 then Flight has departed. function updateArrival(result) { departureTotalOutput = computeOutput(result); var identity = result.FlightInfoExResult.flights[0].ident; $("#arrivals-table") .children("." + identity) .children(".status") .html(departureTotalOutput); } function updateDeparture(result) { departureTotalOutput = computeOutput(result); var identity = result.FlightInfoExResult.flights[0].ident; $("#departures-table") .children("." + identity) .children(".status") .html(departureTotalOutput); } function getDisplayTime(date, isDeparting = false) { var thisTimeHours = date.getHours(); var ampm = thisTimeHours >= 12 ? "PM" : "AM"; thisTimeHours = thisTimeHours > 12 ? thisTimeHours - 12 : thisTimeHours; thisTimeHours = thisTimeHours == 0 ? thisTimeHours + 12 : thisTimeHours; var thisTimeMin = date.getMinutes(); if (/^\d$/.test(thisTimeMin)) { thisTimeMin = "0" + thisTimeMin; } if (isDeparting) { thisTimeMin = thisTimeMin - 10; } return thisTimeHours + ":" + thisTimeMin + " " + ampm; } function computeHoursMin(input) { var realmin = Math.round(input % 60); var hours = Math.floor(input / 60); if (input < 60) { return realmin + " min"; } else { if (hours == 1) { return hours + " hour " + realmin + " min"; } else { return hours + " hours " + realmin + " min"; } } } function computeEarlyLate(input) { if (input > 0) { return "Early"; } else { return "Late"; } } //Figures out all of the math for the output to arrivals and departures table. function computeOutput(result) { var filedDeparture = result.FlightInfoExResult.flights[0].scheduled_off; var actualDeparture = result.FlightInfoExResult.flights[0].actual_off; var estimatedArrival = result.FlightInfoExResult.flights[0].estimated_on; var filedETE = result.FlightInfoExResult.flights[0].filed_ete; var departureTotal = filedDeparture - actualDeparture; var departureTotalOutput = null; var ontimeLate = null; //Get filed_ete in seconds var a = filedETE.split(":"); // split it at the colons // minutes are worth 60 seconds. Hours are worth 60 minutes. var filedETE = +a[0] * 60 * 60 + +a[1] * 60 + +a[2]; // If actual Departure is 0 then the flight hasn't departed and so run this else run the other block as it has departed. if (actualDeparture == 0) { //Filed Departure + Estimated Time for Arrival - Estimated Arrival time to get early or late. departureTotalOutput = filedDeparture + filedETE - estimatedArrival; ontimeLate = computeEarlyLate(departureTotalOutput); if (departureTotalOutput > 0) { departureTotalOutput = departureTotalOutput / 60; } else if (departureTotalOutput < 0) { departureTotalOutput = (departureTotalOutput * -1) / 60; } else if (departureTotalOutput == 0) { departureTotalOutput = "On Time"; } else { console.log("Unknown Departure Total Output"); } var departureTotalOutput = ontimeLate + '
( ' + computeHoursMin(departureTotalOutput) + " )"; } else { departureTotalOutput = departureTotal; ontimeLate = computeEarlyLate(departureTotalOutput); if (departureTotalOutput > 0) { departureTotalOutput = (departureTotalOutput * -1) / 60; } else if (departureTotalOutput < 0) { departureTotalOutput = departureTotalOutput / 60; } else if (departureTotalOutput == 0) { departureTotalOutput = "On Time"; } else { console.log("Unknown Departure Total Output"); } var departureTotalOutput = ontimeLate + '
( ' + computeHoursMin(departureTotalOutput) + " )"; } return departureTotalOutput; } $(document).ready(function(){ //Kick off the App by getting the list of cached files and checking if the timestamp is recent. filesList(); }); })();