Send to a single json endpoint
This commit is contained in:
parent
afaf4c02e4
commit
17d33d98e2
|
@ -4,144 +4,140 @@ let colorTimeout;
|
||||||
let color2Timeout;
|
let color2Timeout;
|
||||||
|
|
||||||
async function post(path, data) {
|
async function post(path, data) {
|
||||||
console.log(`POST to ${path}`, data);
|
console.log(`POST to ${path}`, data);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(path, {
|
const response = await fetch(path, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(data) // Convert data to JSON string
|
body: JSON.stringify(data), // Convert data to JSON string
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error during POST request:', error);
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during POST request:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get(path) {
|
async function get(path) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(path);
|
const response = await fetch(path);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
}
|
|
||||||
return await response.json(); // Assuming you are expecting JSON response
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error during GET request:', error);
|
|
||||||
}
|
}
|
||||||
|
return await response.json(); // Assuming you are expecting JSON response
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during GET request:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateColor(event) {
|
async function updateColor(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearTimeout(colorTimeout);
|
clearTimeout(colorTimeout);
|
||||||
colorTimeout = setTimeout(async function() {
|
colorTimeout = setTimeout(async function () {
|
||||||
const color = document.getElementById('color').value;
|
const color = document.getElementById("color").value;
|
||||||
await post("/color", { color }); // Send as JSON
|
await post("settings", { color1: color }); // Send as JSON
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateColor2(event) {
|
async function updateColor2(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearTimeout(color2Timeout);
|
clearTimeout(color2Timeout);
|
||||||
color2Timeout = setTimeout(async function() {
|
color2Timeout = setTimeout(async function () {
|
||||||
const color = document.getElementById('color2').value;
|
const color = document.getElementById("color2").value;
|
||||||
await post("/color2", { color }); // Send as JSON
|
await post("/settings", { color2: color }); // Send as JSON
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updatePattern(pattern) {
|
async function updatePattern(pattern) {
|
||||||
event.preventDefault();
|
await post("/settings", { pattern: pattern }); // Send as JSON
|
||||||
await post("/pattern", { pattern }); // Send as JSON
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateBrightness(event) {
|
async function updateBrightness(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearTimeout(brightnessTimeout);
|
clearTimeout(brightnessTimeout);
|
||||||
brightnessTimeout = setTimeout(async function() {
|
brightnessTimeout = setTimeout(async function () {
|
||||||
const brightness = document.getElementById('brightness').value;
|
const brightness = document.getElementById("brightness").value;
|
||||||
await post('/brightness', { brightness }); // Send as JSON
|
await post("/settings", { brightness: brightness }); // Send as JSON
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateDelay(event) {
|
async function updateDelay(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearTimeout(delayTimeout);
|
clearTimeout(delayTimeout);
|
||||||
delayTimeout = setTimeout(async function() {
|
delayTimeout = setTimeout(async function () {
|
||||||
const delay = document.getElementById('delay').value;
|
const delay = document.getElementById("delay").value;
|
||||||
await post('/delay', { delay }); // Send as JSON
|
await post("/settings", { delay: delay }); // Send as JSON
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateNumLeds(event) {
|
async function updateNumLeds(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const numLeds = document.getElementById('num_leds').value;
|
const numLeds = document.getElementById("num_leds").value;
|
||||||
await post('/num_leds', { num_leds: numLeds }); // Send as JSON
|
await post("/settings", { num_leds: parseInt(numLeds) }); // Send as JSON
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateWifi(event) {
|
async function updateName(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const ssid = document.getElementById('ssid').value;
|
const name = document.getElementById("name").value;
|
||||||
const password = document.getElementById('password').value;
|
await post("/settings", { name: name }); // Send as JSON
|
||||||
const ip = document.getElementById('ip').value;
|
|
||||||
const gateway = document.getElementById('gateway').value;
|
|
||||||
|
|
||||||
const wifiSettings = { ssid, password, ip, gateway }; // Create JSON object
|
|
||||||
console.log(wifiSettings);
|
|
||||||
const response = await post('/wifi_settings', wifiSettings); // Send as JSON
|
|
||||||
if (response === 500) {
|
|
||||||
alert("Failed to connect to Wi-Fi");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPatternButtons(patterns) {
|
function createPatternButtons(patterns) {
|
||||||
const container = document.getElementById('pattern_buttons');
|
const container = document.getElementById("pattern_buttons");
|
||||||
container.innerHTML = ''; // Clear previous buttons
|
container.innerHTML = ""; // Clear previous buttons
|
||||||
|
|
||||||
patterns.forEach(pattern => {
|
patterns.forEach((pattern) => {
|
||||||
const button = document.createElement('button');
|
const button = document.createElement("button");
|
||||||
button.type = 'button'; // Use 'button' instead of 'submit'
|
button.type = "button"; // Use 'button' instead of 'submit'
|
||||||
button.textContent = pattern;
|
button.textContent = pattern;
|
||||||
button.value = pattern;
|
button.value = pattern;
|
||||||
button.addEventListener('click', async function(event) {
|
button.addEventListener("click", async function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await updatePattern(pattern);
|
await updatePattern(pattern);
|
||||||
});
|
|
||||||
container.appendChild(button);
|
|
||||||
});
|
});
|
||||||
|
container.appendChild(button);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', async function() {
|
document.addEventListener("DOMContentLoaded", async function () {
|
||||||
document.getElementById('color').addEventListener('input', updateColor);
|
document.getElementById("color").addEventListener("input", updateColor);
|
||||||
document.getElementById('color2').addEventListener('input', updateColor2);
|
document.getElementById("color2").addEventListener("input", updateColor2);
|
||||||
document.getElementById('delay').addEventListener('input', updateDelay);
|
document.getElementById("delay").addEventListener("input", updateDelay);
|
||||||
document.getElementById('brightness').addEventListener('input', updateBrightness);
|
document
|
||||||
document.getElementById('num_leds_form').addEventListener('submit', updateNumLeds);
|
.getElementById("brightness")
|
||||||
document.getElementById('wifi_form').addEventListener('submit', updateWifi);
|
.addEventListener("input", updateBrightness);
|
||||||
document.getElementById('delay').addEventListener('touchend', updateDelay);
|
document
|
||||||
document.getElementById('brightness').addEventListener('touchend', updateBrightness);
|
.getElementById("num_leds_form")
|
||||||
|
.addEventListener("submit", updateNumLeds);
|
||||||
|
document.getElementById("name_form").addEventListener("submit", updateName);
|
||||||
|
document.getElementById("delay").addEventListener("touchend", updateDelay);
|
||||||
|
document
|
||||||
|
.getElementById("brightness")
|
||||||
|
.addEventListener("touchend", updateBrightness);
|
||||||
|
|
||||||
document.querySelectorAll(".pattern_button").forEach(button => {
|
document.querySelectorAll(".pattern_button").forEach((button) => {
|
||||||
console.log(button.value);
|
console.log(button.value);
|
||||||
button.addEventListener('click', async event => {
|
button.addEventListener("click", async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await updatePattern(button.value);
|
await updatePattern(button.value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Function to toggle the display of the settings menu
|
// Function to toggle the display of the settings menu
|
||||||
function selectSettings() {
|
function selectSettings() {
|
||||||
const settingsMenu = document.getElementById('settings_menu');
|
const settingsMenu = document.getElementById("settings_menu");
|
||||||
controls = document.getElementById('controls');
|
controls = document.getElementById("controls");
|
||||||
settingsMenu.style.display = 'block';
|
settingsMenu.style.display = "block";
|
||||||
controls.style.display = 'none';
|
controls.style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectControls() {
|
function selectControls() {
|
||||||
const settingsMenu = document.getElementById('settings_menu');
|
const settingsMenu = document.getElementById("settings_menu");
|
||||||
controls = document.getElementById('controls');
|
controls = document.getElementById("controls");
|
||||||
settingsMenu.style.display = 'none';
|
settingsMenu.style.display = "none";
|
||||||
controls.style.display = 'block';
|
controls.style.display = "block";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue