diff --git a/ram/driver/static/wt/js/commandController.js b/ram/driver/static/wt/js/commandController.js index abb2271..b20fa2f 100644 --- a/ram/driver/static/wt/js/commandController.js +++ b/ram/driver/static/wt/js/commandController.js @@ -10,7 +10,7 @@ $(document).ready(function(){ console.log("Command Controller loaded"); uiDisable(true) - emulatorClass = new Emulator({logger: displayLog}); + emulatorClass = new RestAPI({logger: displayLog}); }); // - Request a port and open an asynchronous connection, diff --git a/ram/driver/static/wt/js/emulator.js b/ram/driver/static/wt/js/emulator.js index 7f4c1da..11990e3 100644 --- a/ram/driver/static/wt/js/emulator.js +++ b/ram/driver/static/wt/js/emulator.js @@ -28,6 +28,40 @@ function extractPacketKey(packet) { return cleanedPacket.find(char => char !== " "); } +class RestAPI { + constructor({logger}) { + this.csrftoken = this.getCookie('csrftoken'); + this.logger = logger + } + + getCookie(name) { + let cookieValue = null; + if (document.cookie && document.cookie !== '') { + const cookies = document.cookie.split(';'); + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim(); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + + write(packet) { + $.ajax({ + type: "PUT", + url: "/api/v1/dcc/command", + data: packet, + success: function (data) { displayLog('[RECEIVE] '+data.response.replace(/\n/g,"")); }, + contentType: "text/plain", + headers: {'X-CSRFToken': this.csrftoken} + }); + } +} + class Emulator { constructor({logger}) { this.turnoutEmulator = new TurnoutEmulator() diff --git a/ram/driver/static/wt/sw.js b/ram/driver/static/wt/sw.js index 0959455..2d76e79 100644 --- a/ram/driver/static/wt/sw.js +++ b/ram/driver/static/wt/sw.js @@ -1,6 +1,5 @@ var cacheName = 'WebThrottle-EX'; var filesToCache = [ - '/WebThrottle-EX/', 'index.html', 'images/favicon.ico', 'images/carbon_fibre.png',