mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
WIP: add API driver in WebThrottle-EX
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
console.log("Command Controller loaded");
|
console.log("Command Controller loaded");
|
||||||
uiDisable(true)
|
uiDisable(true)
|
||||||
emulatorClass = new Emulator({logger: displayLog});
|
emulatorClass = new RestAPI({logger: displayLog});
|
||||||
});
|
});
|
||||||
|
|
||||||
// - Request a port and open an asynchronous connection,
|
// - Request a port and open an asynchronous connection,
|
||||||
|
@@ -28,6 +28,40 @@ function extractPacketKey(packet) {
|
|||||||
return cleanedPacket.find(char => char !== " ");
|
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 {
|
class Emulator {
|
||||||
constructor({logger}) {
|
constructor({logger}) {
|
||||||
this.turnoutEmulator = new TurnoutEmulator()
|
this.turnoutEmulator = new TurnoutEmulator()
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
var cacheName = 'WebThrottle-EX';
|
var cacheName = 'WebThrottle-EX';
|
||||||
var filesToCache = [
|
var filesToCache = [
|
||||||
'/WebThrottle-EX/',
|
|
||||||
'index.html',
|
'index.html',
|
||||||
'images/favicon.ico',
|
'images/favicon.ico',
|
||||||
'images/carbon_fibre.png',
|
'images/carbon_fibre.png',
|
||||||
|
Reference in New Issue
Block a user