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