Memory Optimizations
This commit is contained in:
66
web/index.html
Normal file
66
web/index.html
Normal file
@@ -0,0 +1,66 @@
|
||||
<!DOCTYPE HTML><html>
|
||||
<head>
|
||||
<title>HannaBox</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta charset="UTF-8">
|
||||
<link rel='stylesheet' href='/style.css' type='text/css' media='all' />
|
||||
</head>
|
||||
<body>
|
||||
<h1>🎵 HannaBox 🎵</h1>
|
||||
<span id="state"></span><br/><br/>
|
||||
<span id="voltage"></span><br/>
|
||||
<span id="uid"></span><br/>
|
||||
<span id="heap"></span><br/>
|
||||
|
||||
<div>
|
||||
<button class="prev-button" onclick="simpleGetCall('previous');"></button>
|
||||
<button class="play-button" onclick="simpleGetCall('toggleplaypause');"></button>
|
||||
<button class="next-button" onclick="simpleGetCall('next');"></button><br/><br/>
|
||||
</div>
|
||||
<div class="slidecontainer">
|
||||
|
||||
<input name="progress" type="range" min="0" max="100" value="0" class="slider" id="progressSlider"
|
||||
onchange="postValue('progress',document.getElementById('progressSlider').value);lastChange = Date.now();userIsInteracting = false;"
|
||||
oninput="userIsInteracting = true;"
|
||||
>
|
||||
<label for="progress" id="progressLabel"></label>
|
||||
</div>
|
||||
<div class="slidecontainer">
|
||||
<input name="volume" type="range" min="0" max="15" value="7" class="slider" id="volumeSlider"
|
||||
onchange="postValue('volume',document.getElementById('volumeSlider').value);lastChange = Date.now()"
|
||||
>
|
||||
<label for="volume">Vol</label>
|
||||
</div>
|
||||
<!--
|
||||
<button onmouseup="simpleGetCall('stop');" ontouchend="simpleGetCall('stop');">Stop</button>
|
||||
-->
|
||||
|
||||
<p class="playlist-container">
|
||||
<h2>🎶 Playlist 🎶</h2>
|
||||
%DIRECTORY%
|
||||
</p>
|
||||
|
||||
<form id="uploadForm" method="POST" action="/upload" enctype="multipart/form-data">
|
||||
<input type="file" name="data" id="uploadFile" accept=".mp3,.wav,.flac,.m4a,.ogg"/>
|
||||
<input type="submit" name="upload" value="Upload" title="Upload Audio File" id="uploadButton"/>
|
||||
<div id="uploadStatus"></div>
|
||||
<div id="uploadProgress" style="display: none;">
|
||||
<div class="progress-bar">
|
||||
<div class="progress-fill" id="progressFill"></div>
|
||||
</div>
|
||||
<span id="progressText">0%</span>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<h2>Edit RFID Mapping</h2>
|
||||
<form id="editMappingForm">
|
||||
<label for="rfid">RFID:</label>
|
||||
<input type="text" id="rfid" name="rfid" required><br>
|
||||
<label for="song">Song:</label>
|
||||
<input type="text" id="song" name="song" required><br>
|
||||
<button type="button" onclick="editMapping()">Update Mapping</button>
|
||||
</form>
|
||||
|
||||
<script src="/script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
262
web/script.js
Normal file
262
web/script.js
Normal file
@@ -0,0 +1,262 @@
|
||||
setInterval(getState, 4000);
|
||||
setInterval(updateProgress, 500); // Update progress every second
|
||||
|
||||
// Get the <li> elements
|
||||
var liElements = document.querySelectorAll('ul li');
|
||||
|
||||
var lastChange = 0;
|
||||
|
||||
var lastStateUpdateTime = Date.now();
|
||||
var songStartTime = 0;
|
||||
var currentSongLength = 0;
|
||||
var isPlaying = false;
|
||||
var userIsInteracting = false; // Flag to track user interaction with the slider
|
||||
|
||||
// Add click event listener to each <li> element
|
||||
liElements.forEach(function(li) {
|
||||
li.addEventListener('click', function() {
|
||||
//var liText = this.innerText;
|
||||
var id = this.dataset.id;
|
||||
playSongById(id);
|
||||
});
|
||||
});
|
||||
|
||||
function simpleGetCall(endpoint) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/" + endpoint, true);
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
getState(); // Fetch the latest state right after the button action
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function postValue(endpoint,value) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/" + endpoint, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
xhr.send("value="+encodeURIComponent(value));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
getState(); // Fetch the latest state right after the button action
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function getState() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
var state = JSON.parse(xhr.response);
|
||||
isPlaying = state['playing'];
|
||||
if (isPlaying) {
|
||||
songStartTime = Date.now() - state['time'] * 1000;
|
||||
currentSongLength = state['length'] * 1000;
|
||||
}
|
||||
lastStateUpdateTime = Date.now();
|
||||
displayState(state);
|
||||
}
|
||||
}
|
||||
xhr.open("GET","/state", true);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function updateProgress() {
|
||||
if (isPlaying && !userIsInteracting) { // Check if user is not interacting
|
||||
var elapsedTime = Date.now() - songStartTime;
|
||||
if (elapsedTime >= currentSongLength) {
|
||||
elapsedTime = currentSongLength;
|
||||
isPlaying = false; // Stop updating if the song has ended
|
||||
}
|
||||
var progressElement = document.getElementById('progressSlider');
|
||||
progressElement.value = elapsedTime / 1000; // Convert to seconds
|
||||
document.getElementById("progressLabel").innerHTML = Math.floor(elapsedTime / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function displayState(state) {
|
||||
document.getElementById("state").innerHTML = state['title'];
|
||||
document.getElementById("progressLabel").innerHTML = state['time'];
|
||||
document.getElementById("voltage").innerHTML = state['voltage']+' mV';
|
||||
document.getElementById("heap").innerHTML = state['heap']+' bytes free heap';
|
||||
document.getElementById("uid").innerHTML = 'Last NFC ID: '+state['uid'];
|
||||
var elements = document.getElementsByClassName('play-button');
|
||||
var btn = elements[0];
|
||||
|
||||
if (state['playing']) {
|
||||
btn.classList.add('paused');
|
||||
} else {
|
||||
btn.classList.remove('paused');
|
||||
}
|
||||
|
||||
if (Date.now()-lastChange>1200) {
|
||||
var progress = document.getElementById('progressSlider');
|
||||
progress.value = state['time'];
|
||||
progress.max = state['length'];
|
||||
|
||||
var volume = document.getElementById('volumeSlider');
|
||||
volume.value = state['volume'];
|
||||
}
|
||||
updateProgress();
|
||||
}
|
||||
|
||||
function playSongById(id) {
|
||||
var url = "/playbyid";
|
||||
var params = "id="+id;
|
||||
var http = new XMLHttpRequest();
|
||||
|
||||
http.open("GET", url+"?"+params, true);
|
||||
http.onreadystatechange = function()
|
||||
{
|
||||
if(http.readyState == 4 && http.status == 200) {
|
||||
getState();
|
||||
}
|
||||
}
|
||||
http.send(null);
|
||||
}
|
||||
|
||||
function playNamedSong(song) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/playnamed");
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
//application/x-www-form-urlencoded
|
||||
var body = song;
|
||||
xhr.send("title="+encodeURIComponent(body));
|
||||
}
|
||||
|
||||
function editMapping() {
|
||||
var rfid = document.getElementById('rfid').value;
|
||||
var song = document.getElementById('song').value;
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/edit_mapping", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
xhr.send("rfid=" + encodeURIComponent(rfid) + "&song=" + encodeURIComponent(song));
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||
alert("Mapping updated successfully!");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Validate file before upload
|
||||
function validateFile(file) {
|
||||
var maxSize = 50 * 1024 * 1024; // 50MB limit
|
||||
var allowedTypes = ['audio/mpeg', 'audio/wav', 'audio/flac', 'audio/mp4', 'audio/ogg'];
|
||||
var allowedExtensions = ['.mp3', '.wav', '.flac', '.m4a', '.ogg'];
|
||||
|
||||
if (file.size > maxSize) {
|
||||
return 'File too large. Maximum size is 50MB.';
|
||||
}
|
||||
|
||||
var fileName = file.name.toLowerCase();
|
||||
var hasValidExtension = allowedExtensions.some(ext => fileName.endsWith(ext));
|
||||
|
||||
if (!hasValidExtension) {
|
||||
return 'Invalid file type. Only audio files (.mp3, .wav, .flac, .m4a, .ogg) are allowed.';
|
||||
}
|
||||
|
||||
return null; // No error
|
||||
}
|
||||
|
||||
// Handle form submission with AJAX to show upload status
|
||||
document.getElementById('uploadForm').addEventListener('submit', function(event) {
|
||||
event.preventDefault(); // Prevent the default form submit
|
||||
|
||||
var fileInput = document.getElementById('uploadFile');
|
||||
var file = fileInput.files[0];
|
||||
|
||||
if (!file) {
|
||||
alert('Please select a file to upload.');
|
||||
return;
|
||||
}
|
||||
|
||||
var validationError = validateFile(file);
|
||||
if (validationError) {
|
||||
alert(validationError);
|
||||
return;
|
||||
}
|
||||
|
||||
var form = event.target;
|
||||
var formData = new FormData(form);
|
||||
var uploadButton = document.getElementById('uploadButton');
|
||||
var uploadStatus = document.getElementById('uploadStatus');
|
||||
var uploadProgress = document.getElementById('uploadProgress');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
var progressText = document.getElementById('progressText');
|
||||
|
||||
// Disable upload button and show progress
|
||||
uploadButton.disabled = true;
|
||||
uploadButton.value = 'Uploading...';
|
||||
uploadProgress.style.display = 'block';
|
||||
uploadStatus.innerHTML = 'Preparing upload...';
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/upload', true);
|
||||
|
||||
xhr.upload.onloadstart = function() {
|
||||
uploadStatus.innerHTML = 'Upload started...';
|
||||
};
|
||||
|
||||
xhr.upload.onprogress = function(event) {
|
||||
if (event.lengthComputable) {
|
||||
var percentComplete = Math.round((event.loaded / event.total) * 100);
|
||||
progressFill.style.width = percentComplete + '%';
|
||||
progressText.innerHTML = percentComplete + '%';
|
||||
uploadStatus.innerHTML = 'Uploading: ' + percentComplete + '% (' +
|
||||
Math.round(event.loaded / 1024) + 'KB / ' +
|
||||
Math.round(event.total / 1024) + 'KB)';
|
||||
}
|
||||
};
|
||||
|
||||
xhr.upload.onerror = function() {
|
||||
uploadStatus.innerHTML = 'Upload failed due to network error.';
|
||||
resetUploadForm();
|
||||
};
|
||||
|
||||
xhr.upload.onabort = function() {
|
||||
uploadStatus.innerHTML = 'Upload was cancelled.';
|
||||
resetUploadForm();
|
||||
};
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) { // Request is done
|
||||
if (xhr.status >= 200 && xhr.status < 300) { // Success status code range
|
||||
uploadStatus.innerHTML = 'Upload completed successfully!';
|
||||
progressFill.style.width = '100%';
|
||||
progressText.innerHTML = '100%';
|
||||
|
||||
setTimeout(function() {
|
||||
alert('File uploaded successfully!');
|
||||
location.reload(); // Reload to get updated playlist
|
||||
}, 1000);
|
||||
} else {
|
||||
var errorMsg = xhr.responseText || 'Unknown error occurred';
|
||||
uploadStatus.innerHTML = 'Upload failed: ' + errorMsg;
|
||||
alert('Upload failed: ' + errorMsg);
|
||||
resetUploadForm();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(formData); // Send the form data using XMLHttpRequest
|
||||
});
|
||||
|
||||
function resetUploadForm() {
|
||||
var uploadButton = document.getElementById('uploadButton');
|
||||
var uploadProgress = document.getElementById('uploadProgress');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
var progressText = document.getElementById('progressText');
|
||||
|
||||
uploadButton.disabled = false;
|
||||
uploadButton.value = 'Upload';
|
||||
uploadProgress.style.display = 'none';
|
||||
progressFill.style.width = '0%';
|
||||
progressText.innerHTML = '0%';
|
||||
|
||||
// Clear file input
|
||||
document.getElementById('uploadFile').value = '';
|
||||
}
|
||||
229
web/style.css
Normal file
229
web/style.css
Normal file
@@ -0,0 +1,229 @@
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
text-align: center; /* Center align elements */
|
||||
background-color: #f4f4f4; /* Light background */
|
||||
}
|
||||
|
||||
.playlist-container {
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
margin-top: 20px;
|
||||
text-align: left; /* Align playlist text to the left */
|
||||
}
|
||||
|
||||
li {
|
||||
cursor: pointer;
|
||||
list-style-type: none;
|
||||
}
|
||||
|
||||
.play-button, .next-button, .prev-button {
|
||||
border: 0;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s ease; /* Smooth transition for hover */
|
||||
}
|
||||
|
||||
.play-button {
|
||||
box-sizing: border-box;
|
||||
margin: 5% auto;
|
||||
height: 50px; /* Consistent size for play button */
|
||||
border-color: transparent transparent transparent #007bff;
|
||||
border-style: solid;
|
||||
border-width: 25px 0 25px 40px; /* Adjusted size */
|
||||
}
|
||||
|
||||
.play-button.paused {
|
||||
border-style: double;
|
||||
border-width: 25px 0 25px 40px; /* Same size for pause button */
|
||||
height: 50px; /* Consistent height */
|
||||
}
|
||||
|
||||
.play-button:hover {
|
||||
border-color: transparent transparent transparent #0056b3; /* Darker blue on hover */
|
||||
}
|
||||
|
||||
.next-button, .prev-button {
|
||||
padding: 0;
|
||||
margin: 10px;
|
||||
border-color: transparent #007bff transparent #007bff;
|
||||
border-style: solid;
|
||||
}
|
||||
|
||||
.next-button {
|
||||
border-width: 15px 0 15px 25px;
|
||||
box-shadow: 8px 0 0 0 #007bff;
|
||||
}
|
||||
|
||||
.next-button:hover {
|
||||
border-color: transparent #0056b3 transparent #0056b3;
|
||||
box-shadow: 8px 0 0 0 #0056b3;
|
||||
}
|
||||
|
||||
.prev-button {
|
||||
border-width: 15px 25px 15px 0;
|
||||
box-shadow: -8px 0 0 0 #007bff;
|
||||
}
|
||||
|
||||
.prev-button:hover {
|
||||
border-color: transparent #0056b3 transparent #0056b3;
|
||||
box-shadow: -8px 0 0 0 #0056b3;
|
||||
}
|
||||
|
||||
.slider {
|
||||
width: 90%; /* Make slider wider for easier interaction */
|
||||
margin: 10px auto; /* Center align the slider */
|
||||
}
|
||||
|
||||
.slidecontainer {
|
||||
margin: 20px 0; /* Space out elements */
|
||||
}
|
||||
|
||||
/* Upload progress bar styles */
|
||||
.progress-bar {
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
background-color: #e0e0e0;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
margin: 10px 0;
|
||||
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
background: linear-gradient(90deg, #007bff, #0056b3);
|
||||
width: 0%;
|
||||
transition: width 0.3s ease;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
#uploadProgress {
|
||||
margin: 15px 0;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#progressText {
|
||||
font-weight: bold;
|
||||
color: #007bff;
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
#uploadStatus {
|
||||
margin: 10px 0;
|
||||
padding: 8px;
|
||||
border-radius: 4px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
#uploadStatus:not(:empty) {
|
||||
background-color: #e7f3ff;
|
||||
border: 1px solid #007bff;
|
||||
color: #0056b3;
|
||||
}
|
||||
|
||||
/* Form styling improvements */
|
||||
#uploadForm {
|
||||
background-color: white;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
#uploadButton {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
margin-left: 10px;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
#uploadButton:hover:not(:disabled) {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
#uploadButton:disabled {
|
||||
background-color: #6c757d;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
#uploadFile {
|
||||
padding: 8px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
/* RFID mapping form styling */
|
||||
#editMappingForm {
|
||||
background-color: white;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
margin: 20px 0;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
#editMappingForm label {
|
||||
display: block;
|
||||
margin: 10px 0 5px 0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
#editMappingForm input[type="text"] {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 10px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#editMappingForm button {
|
||||
background-color: #28a745;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
margin-top: 10px;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
#editMappingForm button:hover {
|
||||
background-color: #218838;
|
||||
}
|
||||
|
||||
/* Responsive design improvements */
|
||||
@media (max-width: 600px) {
|
||||
body {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.slider {
|
||||
width: 95%;
|
||||
}
|
||||
|
||||
#uploadForm, #editMappingForm {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
#uploadButton, #editMappingForm button {
|
||||
width: 100%;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
#uploadFile {
|
||||
width: 100%;
|
||||
margin: 10px 0;
|
||||
}
|
||||
}
|
||||
262
web/system_script.js
Normal file
262
web/system_script.js
Normal file
@@ -0,0 +1,262 @@
|
||||
setInterval(getState, 4000);
|
||||
setInterval(updateProgress, 500); // Update progress every second
|
||||
|
||||
// Get the <li> elements
|
||||
var liElements = document.querySelectorAll('ul li');
|
||||
|
||||
var lastChange = 0;
|
||||
|
||||
var lastStateUpdateTime = Date.now();
|
||||
var songStartTime = 0;
|
||||
var currentSongLength = 0;
|
||||
var isPlaying = false;
|
||||
var userIsInteracting = false; // Flag to track user interaction with the slider
|
||||
|
||||
// Add click event listener to each <li> element
|
||||
liElements.forEach(function(li) {
|
||||
li.addEventListener('click', function() {
|
||||
//var liText = this.innerText;
|
||||
var id = this.dataset.id;
|
||||
playSongById(id);
|
||||
});
|
||||
});
|
||||
|
||||
function simpleGetCall(endpoint) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/" + endpoint, true);
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
getState(); // Fetch the latest state right after the button action
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function postValue(endpoint,value) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/" + endpoint, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
xhr.send("value="+encodeURIComponent(value));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
getState(); // Fetch the latest state right after the button action
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function getState() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
var state = JSON.parse(xhr.response);
|
||||
isPlaying = state['playing'];
|
||||
if (isPlaying) {
|
||||
songStartTime = Date.now() - state['time'] * 1000;
|
||||
currentSongLength = state['length'] * 1000;
|
||||
}
|
||||
lastStateUpdateTime = Date.now();
|
||||
displayState(state);
|
||||
}
|
||||
}
|
||||
xhr.open("GET","/state", true);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function updateProgress() {
|
||||
if (isPlaying && !userIsInteracting) { // Check if user is not interacting
|
||||
var elapsedTime = Date.now() - songStartTime;
|
||||
if (elapsedTime >= currentSongLength) {
|
||||
elapsedTime = currentSongLength;
|
||||
isPlaying = false; // Stop updating if the song has ended
|
||||
}
|
||||
var progressElement = document.getElementById('progressSlider');
|
||||
progressElement.value = elapsedTime / 1000; // Convert to seconds
|
||||
document.getElementById("progressLabel").innerHTML = Math.floor(elapsedTime / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function displayState(state) {
|
||||
document.getElementById("state").innerHTML = state['title'];
|
||||
document.getElementById("progressLabel").innerHTML = state['time'];
|
||||
document.getElementById("voltage").innerHTML = state['voltage']+' mV';
|
||||
document.getElementById("heap").innerHTML = state['heap']+' bytes free heap';
|
||||
document.getElementById("uid").innerHTML = 'Last NFC ID: '+state['uid'];
|
||||
var elements = document.getElementsByClassName('play-button');
|
||||
var btn = elements[0];
|
||||
|
||||
if (state['playing']) {
|
||||
btn.classList.add('paused');
|
||||
} else {
|
||||
btn.classList.remove('paused');
|
||||
}
|
||||
|
||||
if (Date.now()-lastChange>1200) {
|
||||
var progress = document.getElementById('progressSlider');
|
||||
progress.value = state['time'];
|
||||
progress.max = state['length'];
|
||||
|
||||
var volume = document.getElementById('volumeSlider');
|
||||
volume.value = state['volume'];
|
||||
}
|
||||
updateProgress();
|
||||
}
|
||||
|
||||
function playSongById(id) {
|
||||
var url = "/playbyid";
|
||||
var params = "id="+id;
|
||||
var http = new XMLHttpRequest();
|
||||
|
||||
http.open("GET", url+"?"+params, true);
|
||||
http.onreadystatechange = function()
|
||||
{
|
||||
if(http.readyState == 4 && http.status == 200) {
|
||||
getState();
|
||||
}
|
||||
}
|
||||
http.send(null);
|
||||
}
|
||||
|
||||
function playNamedSong(song) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/playnamed");
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
//application/x-www-form-urlencoded
|
||||
var body = song;
|
||||
xhr.send("title="+encodeURIComponent(body));
|
||||
}
|
||||
|
||||
function editMapping() {
|
||||
var rfid = document.getElementById('rfid').value;
|
||||
var song = document.getElementById('song').value;
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/edit_mapping", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
xhr.send("rfid=" + encodeURIComponent(rfid) + "&song=" + encodeURIComponent(song));
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||
alert("Mapping updated successfully!");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Validate file before upload
|
||||
function validateFile(file) {
|
||||
var maxSize = 50 * 1024 * 1024; // 50MB limit
|
||||
var allowedTypes = ['audio/mpeg', 'audio/wav', 'audio/flac', 'audio/mp4', 'audio/ogg'];
|
||||
var allowedExtensions = ['.mp3', '.wav', '.flac', '.m4a', '.ogg'];
|
||||
|
||||
if (file.size > maxSize) {
|
||||
return 'File too large. Maximum size is 50MB.';
|
||||
}
|
||||
|
||||
var fileName = file.name.toLowerCase();
|
||||
var hasValidExtension = allowedExtensions.some(ext => fileName.endsWith(ext));
|
||||
|
||||
if (!hasValidExtension) {
|
||||
return 'Invalid file type. Only audio files (.mp3, .wav, .flac, .m4a, .ogg) are allowed.';
|
||||
}
|
||||
|
||||
return null; // No error
|
||||
}
|
||||
|
||||
// Handle form submission with AJAX to show upload status
|
||||
document.getElementById('uploadForm').addEventListener('submit', function(event) {
|
||||
event.preventDefault(); // Prevent the default form submit
|
||||
|
||||
var fileInput = document.getElementById('uploadFile');
|
||||
var file = fileInput.files[0];
|
||||
|
||||
if (!file) {
|
||||
alert('Please select a file to upload.');
|
||||
return;
|
||||
}
|
||||
|
||||
var validationError = validateFile(file);
|
||||
if (validationError) {
|
||||
alert(validationError);
|
||||
return;
|
||||
}
|
||||
|
||||
var form = event.target;
|
||||
var formData = new FormData(form);
|
||||
var uploadButton = document.getElementById('uploadButton');
|
||||
var uploadStatus = document.getElementById('uploadStatus');
|
||||
var uploadProgress = document.getElementById('uploadProgress');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
var progressText = document.getElementById('progressText');
|
||||
|
||||
// Disable upload button and show progress
|
||||
uploadButton.disabled = true;
|
||||
uploadButton.value = 'Uploading...';
|
||||
uploadProgress.style.display = 'block';
|
||||
uploadStatus.innerHTML = 'Preparing upload...';
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/upload', true);
|
||||
|
||||
xhr.upload.onloadstart = function() {
|
||||
uploadStatus.innerHTML = 'Upload started...';
|
||||
};
|
||||
|
||||
xhr.upload.onprogress = function(event) {
|
||||
if (event.lengthComputable) {
|
||||
var percentComplete = Math.round((event.loaded / event.total) * 100);
|
||||
progressFill.style.width = percentComplete + '%';
|
||||
progressText.innerHTML = percentComplete + '%';
|
||||
uploadStatus.innerHTML = 'Uploading: ' + percentComplete + '% (' +
|
||||
Math.round(event.loaded / 1024) + 'KB / ' +
|
||||
Math.round(event.total / 1024) + 'KB)';
|
||||
}
|
||||
};
|
||||
|
||||
xhr.upload.onerror = function() {
|
||||
uploadStatus.innerHTML = 'Upload failed due to network error.';
|
||||
resetUploadForm();
|
||||
};
|
||||
|
||||
xhr.upload.onabort = function() {
|
||||
uploadStatus.innerHTML = 'Upload was cancelled.';
|
||||
resetUploadForm();
|
||||
};
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) { // Request is done
|
||||
if (xhr.status >= 200 && xhr.status < 300) { // Success status code range
|
||||
uploadStatus.innerHTML = 'Upload completed successfully!';
|
||||
progressFill.style.width = '100%';
|
||||
progressText.innerHTML = '100%';
|
||||
|
||||
setTimeout(function() {
|
||||
alert('File uploaded successfully!');
|
||||
location.reload(); // Reload to get updated playlist
|
||||
}, 1000);
|
||||
} else {
|
||||
var errorMsg = xhr.responseText || 'Unknown error occurred';
|
||||
uploadStatus.innerHTML = 'Upload failed: ' + errorMsg;
|
||||
alert('Upload failed: ' + errorMsg);
|
||||
resetUploadForm();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(formData); // Send the form data using XMLHttpRequest
|
||||
});
|
||||
|
||||
function resetUploadForm() {
|
||||
var uploadButton = document.getElementById('uploadButton');
|
||||
var uploadProgress = document.getElementById('uploadProgress');
|
||||
var progressFill = document.getElementById('progressFill');
|
||||
var progressText = document.getElementById('progressText');
|
||||
|
||||
uploadButton.disabled = false;
|
||||
uploadButton.value = 'Upload';
|
||||
uploadProgress.style.display = 'none';
|
||||
progressFill.style.width = '0%';
|
||||
progressText.innerHTML = '0%';
|
||||
|
||||
// Clear file input
|
||||
document.getElementById('uploadFile').value = '';
|
||||
}
|
||||
Reference in New Issue
Block a user