mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 05:07:50 +02:00
Customize the Web Throttle to use RestAPI
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
<script type="text/javascript" src="js/commandController.js"></script>
|
||||
<script type="text/javascript" src="js/storageController.js"></script>
|
||||
<script type="text/javascript" src="js/addloco.js"></script>
|
||||
<script type="text/javascript" src="js/emulator.js"></script>
|
||||
<script type="text/javascript" src="js/restapi.js"></script>
|
||||
<script type="text/javascript" src="js/exwebthrottle.js"></script>
|
||||
<script type="text/javascript" src="js/pwa.js"></script>
|
||||
|
||||
@@ -120,8 +120,7 @@
|
||||
</div>
|
||||
<div class="server-button column-5">
|
||||
<select id="select-method" class="select-control select-xl" name="selectMethod" title="Change the connection method">
|
||||
<option value="serial">Serial</option>
|
||||
<option value="emulator">Emulator</option>
|
||||
<option value="rest">RestAPI</option>
|
||||
</select>
|
||||
<button type="button" class="btn-default btn" title="Connect to the Command Station" aria-state="connected" name="button-connect" access="false" id="button-connect">
|
||||
<span class="con-ind"></span>Connect DCC++ EX
|
||||
|
@@ -10,7 +10,7 @@
|
||||
$(document).ready(function(){
|
||||
console.log("Command Controller loaded");
|
||||
uiDisable(true)
|
||||
emulatorClass = new RestAPI({logger: displayLog});
|
||||
restClass = new RestAPI({logger: displayLog});
|
||||
});
|
||||
|
||||
// - Request a port and open an asynchronous connection,
|
||||
@@ -64,11 +64,10 @@ async function connectServer() {
|
||||
return false;
|
||||
}
|
||||
} else{
|
||||
// If using the emulator
|
||||
emulatorMode = true;
|
||||
restMode = true;
|
||||
// Displays dummy hardware message
|
||||
displayLog("\n[CONNECTION] Emulator connected")
|
||||
displayLog("[RECEIVE] DCC++ EX COMMAND STATION FOR EMULATOR / EMULATOR MOTOR SHIELD: V-1.0.0 / Feb 30 2020 13:10:04")
|
||||
displayLog("\n[CONNECTION] Connected to "+window.location.host);
|
||||
restClass.write("<s>");
|
||||
uiDisable(false)
|
||||
return true;
|
||||
}
|
||||
@@ -95,8 +94,7 @@ async function readLoop() {
|
||||
}
|
||||
|
||||
function writeToStream(...lines) {
|
||||
// Stops data being written to nonexistent port if using emulator
|
||||
let stream = emulatorClass
|
||||
let stream = restClass
|
||||
if (port) {
|
||||
stream = outputStream.getWriter();
|
||||
}
|
||||
@@ -188,8 +186,8 @@ async function disconnectServer() {
|
||||
displayLog("[CONNECTION] Serial disconnected");
|
||||
} else {
|
||||
// Disables emulator
|
||||
emulatorMode = undefined;
|
||||
displayLog("[CONNECTION] Emulator disconnected");
|
||||
restMode = undefined;
|
||||
displayLog("[CONNECTION] "+window.location.host+" disconnected");
|
||||
}
|
||||
// Allows a new method to be chosen
|
||||
selectMethod.disabled = false;
|
||||
@@ -198,7 +196,7 @@ async function disconnectServer() {
|
||||
// Connect or disconnect from the command station
|
||||
async function toggleServer(btn) {
|
||||
// If already connected, disconnect
|
||||
if (port || emulatorMode) {
|
||||
if (port || restMode) {
|
||||
await disconnectServer();
|
||||
btn.attr('aria-state','Disconnected');
|
||||
btn.html('<span class="con-ind"></span>Connect DCC++ EX'); //<span id="con-ind"></span>Connect DCC++ EX
|
||||
@@ -218,8 +216,8 @@ async function toggleServer(btn) {
|
||||
|
||||
// Display log of events
|
||||
function displayLog(data){
|
||||
data = data.replace("<"," ");
|
||||
data = data.replace(">"," ");
|
||||
data = data.replaceAll("<"," ");
|
||||
data = data.replaceAll(">"," ");
|
||||
$("#log-box").append("<br>"+data.toString()+"<br>");
|
||||
$("#log-box").scrollTop($("#log-box").prop("scrollHeight"));
|
||||
}
|
||||
|
@@ -28,40 +28,6 @@ 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()
|
||||
|
@@ -58,7 +58,7 @@ window.functions = {
|
||||
};
|
||||
window.isStopped = true;
|
||||
let port;
|
||||
let emulatorMode;
|
||||
let restMode;
|
||||
let reader;
|
||||
let inputDone;
|
||||
let outputDone;
|
||||
|
33
ram/driver/static/wt/js/restapi.js
Normal file
33
ram/driver/static/wt/js/restapi.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user