/************************************************************************************************************************* File: alternateConnections.js Purpose: This file provides interface and utiltiy methods for communicating with the server as a sketch for what a mobile cab could look like. It serves to illustrate that the simulation can be controlled from a separate place than the main view webpage. This script also populates the controls on alternatePage.html. Written by John McGorey - Spring 2019 - Using Microsoft Visual Studio Code 2019 For use in the St. Norbert College CSCI460 Senior Capstone Experience **************************************************************************************************************************/ const url = "http://localhost:3000/"; // Change this to reflect where the server is being hosted // URL Parameter Constants // These are used to send information to the server via the querystring const TRAIN_SPEED = "speed"; const TRAIN_ID = "train_id"; const TURNOUT_ID = "turnout_id"; // Response Status Constants const ERROR_STATUS = "ERROR"; // The requested operation was a failure const SUCCESS_STATUS = "SUCCESS"; // The requested operation was a success //---------------------------------------------------------------------------------------------------- // Script execution starts here //---------------------------------------------------------------------------------------------------- getTurnouts(function(turnouts) { let element = document.getElementById('turnoutId'); if (element) { for (let i = 0; i < turnouts.length; i++) { element.options[i] = new Option("Turnout " + i, i); } } }); getTrains(function(trains) { let element = document.getElementById('trainId'); if (element) { for (let i = 0; i < trains.length; i++) { element.options[i] = new Option("Train " + i, i); } } }); //---------------------------------------------------------------------------------------------------- // Function Definitions //---------------------------------------------------------------------------------------------------- function sendRequest(requestUrl, callback) { /*************************************************************************************** Function: sendRequest Purpose: Sends an HTTP request to the URL specified by requestUrl. When the request returns with a response, execute the specified callback with status of the response and the response body. Input: requestUrl - The URL to send the http request to. If the request operation requires parameters, those should be added to the URL as a queryString before the URL is passed to this function. callback - The function to call when the request returns a response Output: (Via callback) status - Either ERROR_STATUS or SUCCESS_STATUS, depending if the response body contained the word 'Error'. request.response - The body of the response, containing the info the server sends back ****************************************************************************************/ let request = new XMLHttpRequest(); request.timeout = 10000; // Timeout of 10 seconds request.open('GET', requestUrl); request.responseType = 'text'; request.onload = function() { // Determine if the request was successful let status; if (request.response.includes('Error')) status = ERROR_STATUS; else status = SUCCESS_STATUS; callback(status , request.response); }; // This function will be called when the HTTP request gets a response request.send(); // Send out the request and wait for a response } function setTrainSpeed(train_id, speed) { /*************************************************************************************** Function: setTrainSpeed Purpose: This function takes in a train id number and a speed value and sets the speed of the specified train to that value via an HTTP request to the server. Input: train_id - The id of the train whose speed to modify speed - The speed value to set the train to Note: There is nothing of import in the response to this request, so this function only logs if something went wrong. ****************************************************************************************/ let request_url = url + "setTrainSpeed/?"; request_url += TRAIN_ID + "=" + train_id; request_url += "&" + TRAIN_SPEED + "=" + speed; sendRequest(request_url, function(status, response) { if (status == "SUCCESS") { // Train speed updated successfully } else { console.log(response); // If something goes wrong, log it } }); } function getTrains(callback) { /*************************************************************************************** Function: getTrains Purpose: This function sends a request to the server to get a list of the trains in the simiulation & their statuses. The response from the server contains a JSON string containing the train information, which is parsed into an array of objects and passed on to the callback. Input: callback - The function to call when the server returns the list of trains ****************************************************************************************/ let request_url = url + "getTrains/"; sendRequest(request_url, function(status, response) { if (status == SUCCESS_STATUS) { // In this case, the response is a JSON string containing the trains in the simulation let train_objs = JSON.parse(response); if (callback) // Send the resultant object to the callback for processing callback(train_objs); } }); } function getTurnouts(callback) { /*************************************************************************************** Function: getTurnouts Purpose: This function sends a request to the server to get a list of the turnouts in the simulation & their statuses. The response from the server contains a JSON string describing the turnouts' information. This info is then parsed into an array of objects and passed on to the callback for processing Input: callback - The function to call when the server returns the list of turnouts ****************************************************************************************/ let request_url = url + "getTurnouts/"; sendRequest(request_url, function(status, response) { if (status == SUCCESS_STATUS) { // In this case, the response is a JSON string contaning all of the turnouts in the simulation let turnout_objs = JSON.parse(response); if (callback) callback(turnout_objs); } }); } function switchTurnout(turnout_id) { /*************************************************************************************** Function: switchTurnout Purpose: This function takes in a turnout ID number and sends a request to the server to switch that particular turnout. Input: turnout_id - The id number of the turnout to switch ****************************************************************************************/ let request_url = url + "switchTurnout/?" + TURNOUT_ID + "=" + turnout_id; sendRequest(request_url, function(status, response) { console.log("Switching Turnout " + turnout_id + ": ", response); }); }