(function () { var currentTime = 1738731326; 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 * 45; // 60 seconds in a minute, 45 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 += '