WIP: add API driver in WebThrottle-EX

This commit is contained in:
2022-04-22 00:10:40 +02:00
parent f61c1a95ac
commit 0b97e6eab5
3 changed files with 35 additions and 2 deletions

View File

@@ -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()