2023-05-05 08:21:09 +02:00
|
|
|
function addTabListener() {
|
|
|
|
try {
|
|
|
|
var tabMain = document.getElementById("tab-lights");
|
|
|
|
var tabConfig = document.getElementById("tab-config");
|
|
|
|
var tabTDE = document.getElementById("tab-tde");
|
|
|
|
var amain = document.getElementById("tab-a-lights");
|
|
|
|
var acfg = document.getElementById("tab-a-config");
|
|
|
|
var atde = document.getElementById("tab-a-tde");
|
|
|
|
amain.addEventListener("click", function() {
|
|
|
|
tabMain.classList.add("visible");
|
|
|
|
tabConfig.classList.remove("visible");
|
|
|
|
tabTDE.classList.remove("visible");
|
|
|
|
amain.classList.add("pure-button-primary");
|
|
|
|
acfg.classList.remove("pure-button-primary");
|
|
|
|
atde.classList.remove("pure-button-primary");
|
2023-05-07 09:45:18 +02:00
|
|
|
loadGraphData();
|
2023-05-05 08:21:09 +02:00
|
|
|
});
|
|
|
|
acfg.addEventListener("click", function() {
|
|
|
|
tabMain.classList.remove("visible");
|
|
|
|
tabConfig.classList.add("visible");
|
|
|
|
tabTDE.classList.remove("visible");
|
|
|
|
amain.classList.remove("pure-button-primary");
|
|
|
|
acfg.classList.add("pure-button-primary");
|
|
|
|
atde.classList.remove("pure-button-primary");
|
|
|
|
});
|
|
|
|
atde.addEventListener("click", function() {
|
|
|
|
tabMain.classList.remove("visible");
|
|
|
|
tabConfig.classList.remove("visible");
|
|
|
|
tabTDE.classList.add("visible");
|
|
|
|
amain.classList.remove("pure-button-primary");
|
|
|
|
acfg.classList.remove("pure-button-primary");
|
|
|
|
atde.classList.add("pure-button-primary");
|
2023-05-05 10:45:56 +02:00
|
|
|
createTable();
|
2023-05-05 11:22:09 +02:00
|
|
|
fillTableFromJson();
|
2023-05-05 08:21:09 +02:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2023-05-05 10:15:29 +02:00
|
|
|
console.log("Error: load listener of the tab action listener management: " + error.message);
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener('load', function() {
|
|
|
|
addTabListener();
|
|
|
|
});
|
|
|
|
function loadGraphData() {
|
|
|
|
console.log('----> generate graph <----');
|
|
|
|
$.getJSON('/tc_data_blocks_read', function(data) {
|
|
|
|
var currenttime = [];
|
|
|
|
var time = [];
|
|
|
|
var channel1 = [];
|
|
|
|
var channel2 = [];
|
|
|
|
var channel3 = [];
|
|
|
|
var channel4 = [];
|
|
|
|
for (var i = 0; i < data['tcdata'].length; i++) {
|
|
|
|
time.push(data['tcdata'][i]['hour'] + ':' + (data['tcdata'][i]['min'] < 10 ? '0' : '') + data['tcdata'][i]['min']);
|
2023-05-07 09:45:18 +02:00
|
|
|
channel1.push(data['tcdata'][i]['ch1'] * 100 / 255);
|
|
|
|
channel2.push(data['tcdata'][i]['ch2'] * 100 / 255);
|
|
|
|
channel3.push(data['tcdata'][i]['ch3'] * 100 / 255);
|
|
|
|
channel4.push(data['tcdata'][i]['ch4'] * 100 / 255);
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
|
|
|
currenttime.push(data['currenttime']['hour']);
|
|
|
|
currenttime.push(data['currenttime']['min']);
|
|
|
|
var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];
|
|
|
|
var index = time.indexOf(currentTimeStr);
|
|
|
|
if (index === -1) {
|
|
|
|
var lowerIndex = -1;
|
|
|
|
var upperIndex = -1;
|
|
|
|
for (var i = 0; i < time.length - 1; i++) {
|
|
|
|
const currentDate = new Date();
|
|
|
|
const year = currentDate.getFullYear();
|
|
|
|
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
const day = currentDate.getDate().toString().padStart(2, '0');
|
|
|
|
const dateString = `${year}-${month}-${day}`;
|
|
|
|
const start = moment(dateString + ' ' + time[i], 'YYYY-MM-DD HH:mm');
|
|
|
|
const curr = moment(dateString + ' ' + currentTimeStr, 'YYYY-MM-DD HH:mm');
|
|
|
|
const end = moment(dateString + ' ' + time[i + 1], 'YYYY-MM-DD HH:mm');
|
|
|
|
if (curr.isBetween(start, end)) {
|
|
|
|
lowerIndex = i;
|
|
|
|
upperIndex = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lowerIndex === -1 || upperIndex === -1) {
|
2023-05-05 10:15:29 +02:00
|
|
|
console.log("Error: Current time not found in time array and not between two elements in time array. Fixing it...");
|
2023-05-05 08:21:09 +02:00
|
|
|
lowerIndex = 0;
|
|
|
|
upperIndex = 1;
|
|
|
|
var tmp1 = time[0].split(':');
|
|
|
|
currenttime[0] = tmp1[0];
|
|
|
|
currenttime[1] = tmp1[1];
|
|
|
|
}
|
|
|
|
var lowerTime = time[lowerIndex].split(":");
|
|
|
|
var upperTime = time[upperIndex].split(":");
|
|
|
|
var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[1] - lowerTime[1]) / 60);
|
|
|
|
var indexFloat = lowerIndex + timeDiff / ((upperTime[0] - lowerTime[0]) + ((upperTime[1] - lowerTime[1]) / 60));
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
if (indexFloat > index) {
|
|
|
|
index = indexFloat;
|
|
|
|
}
|
|
|
|
var trace1 = {
|
|
|
|
x: time,
|
|
|
|
y: channel1,
|
|
|
|
name: 'Channel 1',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace2 = {
|
|
|
|
x: time,
|
|
|
|
y: channel2,
|
|
|
|
name: 'Channel 2',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace3 = {
|
|
|
|
x: time,
|
|
|
|
y: channel3,
|
|
|
|
name: 'Channel 3',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace4 = {
|
|
|
|
x: time,
|
|
|
|
y: channel4,
|
|
|
|
name: 'Channel 4',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var layout = {
|
|
|
|
title: 'Timing Control Data Blocks',
|
|
|
|
xaxis: {
|
|
|
|
title: 'Time',
|
|
|
|
tickangle: -45,
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
title: 'Brightness',
|
2023-05-07 09:45:18 +02:00
|
|
|
range: [0, 100],
|
2023-05-05 08:21:09 +02:00
|
|
|
},
|
|
|
|
shapes: [{
|
|
|
|
type: 'line',
|
|
|
|
x0: index,
|
|
|
|
y0: 0,
|
|
|
|
x1: index,
|
|
|
|
y1: 255,
|
|
|
|
line: {
|
|
|
|
color: 'lightgrey',
|
|
|
|
width: 3,
|
|
|
|
dash: 'dot'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setInterval(loadGraphData, 10000);
|
|
|
|
loadGraphData();
|
2023-05-07 11:31:52 +02:00
|
|
|
function loadTCGraphData() {
|
|
|
|
console.log('----> generate tc data graph <----');
|
|
|
|
var data = JSON.parse(createJsonFromTable());
|
|
|
|
var currenttime = [];
|
|
|
|
var time = [];
|
|
|
|
var channel1 = [];
|
|
|
|
var channel2 = [];
|
|
|
|
var channel3 = [];
|
|
|
|
var channel4 = [];
|
|
|
|
for (var i = 0; i < data['tcdata'].length; i++) {
|
|
|
|
time.push(data['tcdata'][i]['hour'] + ':' + (data['tcdata'][i]['min'] < 10 ? '0' : '') + data['tcdata'][i]['min']);
|
|
|
|
channel1.push(data['tcdata'][i]['ch1'] * 100 / 255);
|
|
|
|
channel2.push(data['tcdata'][i]['ch2'] * 100 / 255);
|
|
|
|
channel3.push(data['tcdata'][i]['ch3'] * 100 / 255);
|
|
|
|
channel4.push(data['tcdata'][i]['ch4'] * 100 / 255);
|
|
|
|
}
|
|
|
|
currenttime.push(data['currenttime']['hour']);
|
|
|
|
currenttime.push(data['currenttime']['min']);
|
|
|
|
var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];
|
|
|
|
var index = time.indexOf(currentTimeStr);
|
|
|
|
if (index === -1) {
|
|
|
|
var lowerIndex = -1;
|
|
|
|
var upperIndex = -1;
|
|
|
|
for (var i = 0; i < time.length - 1; i++) {
|
|
|
|
const currentDate = new Date();
|
|
|
|
const year = currentDate.getFullYear();
|
|
|
|
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
const day = currentDate.getDate().toString().padStart(2, '0');
|
|
|
|
const dateString = `${year}-${month}-${day}`;
|
|
|
|
const start = moment(dateString + ' ' + time[i], 'YYYY-MM-DD HH:mm');
|
|
|
|
const curr = moment(dateString + ' ' + currentTimeStr, 'YYYY-MM-DD HH:mm');
|
|
|
|
const end = moment(dateString + ' ' + time[i + 1], 'YYYY-MM-DD HH:mm');
|
|
|
|
if (curr.isBetween(start, end)) {
|
|
|
|
lowerIndex = i;
|
|
|
|
upperIndex = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lowerIndex === -1 || upperIndex === -1) {
|
2023-05-08 09:46:25 +02:00
|
|
|
console.log("Warning: Current time not found in time array and not between two elements in time array. Fixing it...");
|
2023-05-07 11:31:52 +02:00
|
|
|
lowerIndex = 0;
|
|
|
|
upperIndex = 1;
|
|
|
|
var tmp1 = time[0].split(':');
|
|
|
|
currenttime[0] = tmp1[0];
|
|
|
|
currenttime[1] = tmp1[1];
|
|
|
|
}
|
|
|
|
var lowerTime = time[lowerIndex].split(":");
|
|
|
|
var upperTime = time[upperIndex].split(":");
|
|
|
|
var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[1] - lowerTime[1]) / 60);
|
|
|
|
var indexFloat = lowerIndex + timeDiff / ((upperTime[0] - lowerTime[0]) + ((upperTime[1] - lowerTime[1]) / 60));
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
if (indexFloat > index) {
|
|
|
|
index = indexFloat;
|
|
|
|
}
|
|
|
|
var trace1 = {
|
|
|
|
x: time,
|
|
|
|
y: channel1,
|
|
|
|
name: 'Channel 1',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace2 = {
|
|
|
|
x: time,
|
|
|
|
y: channel2,
|
|
|
|
name: 'Channel 2',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace3 = {
|
|
|
|
x: time,
|
|
|
|
y: channel3,
|
|
|
|
name: 'Channel 3',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var trace4 = {
|
|
|
|
x: time,
|
|
|
|
y: channel4,
|
|
|
|
name: 'Channel 4',
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines+markers',
|
|
|
|
};
|
|
|
|
var layout = {
|
|
|
|
title: 'Timing Control Data Blocks',
|
|
|
|
xaxis: {
|
|
|
|
title: 'Time',
|
|
|
|
tickangle: -45,
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
title: 'Brightness',
|
|
|
|
range: [0, 100],
|
|
|
|
},
|
|
|
|
shapes: [{
|
|
|
|
type: 'line',
|
|
|
|
x0: index,
|
|
|
|
y0: 0,
|
|
|
|
x1: index,
|
|
|
|
y1: 255,
|
|
|
|
line: {
|
|
|
|
color: 'lightgrey',
|
|
|
|
width: 3,
|
|
|
|
dash: 'dot'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
Plotly.newPlot('tc_plot_chart', [trace1, trace2, trace3, trace4], layout);
|
2023-05-10 10:48:50 +02:00
|
|
|
var plot = document.getElementById('tc_plot_chart');
|
|
|
|
}
|
|
|
|
function rgbaToHex(rgba) {
|
|
|
|
var hex = rgba.map(function (value) {
|
|
|
|
var hexValue = Math.round(value * 255).toString(16);
|
|
|
|
return hexValue.length === 1 ? '0' + hexValue : hexValue;
|
|
|
|
});
|
|
|
|
return '#' + hex.join('').toUpperCase();
|
2023-05-07 11:31:52 +02:00
|
|
|
}
|
|
|
|
setInterval(function() {
|
|
|
|
if (document.getElementById('tab-tde').classList.contains('visible')) {
|
|
|
|
loadTCGraphData();
|
|
|
|
}
|
|
|
|
}, 2000);
|
2023-05-05 08:21:09 +02:00
|
|
|
function updateLightState() {
|
|
|
|
console.log('----> setting bri and power switch <----');
|
|
|
|
for (let i = 1; i <= {{LIGHT_COUNT}}; i++) {
|
|
|
|
const lightURL = `http://{{IP_ADDRESS}}/state?light=${i}`;
|
|
|
|
fetch(lightURL).then(response => response.json()).then(data => {
|
|
|
|
const briSlider = document.getElementById(`bri${i - 1}`);
|
|
|
|
const briSliderVal = document.getElementById(`bri${i - 1}_val`);
|
|
|
|
const onLinkOn = document.getElementById(`on${i - 1}_on`);
|
|
|
|
const onLinkOff = document.getElementById(`on${i - 1}_off`);
|
|
|
|
briSlider.value = data.bri;
|
|
|
|
briSliderVal.innerHTML = (Math.round((data.bri * 100.0 / 255.0) * 100) / 100).toFixed(2);
|
|
|
|
if (data.on == true) {
|
|
|
|
onLinkOn.classList.add('pure-button-primary');
|
|
|
|
onLinkOff.classList.remove('pure-button-primary');
|
|
|
|
} else {
|
|
|
|
onLinkOn.classList.remove('pure-button-primary');
|
|
|
|
onLinkOff.classList.add('pure-button-primary');
|
|
|
|
}
|
|
|
|
}).catch(error => console.error(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setInterval(updateLightState, 10000);
|
|
|
|
updateLightState();
|
|
|
|
function updatePWMValues() {
|
|
|
|
console.log('----> setting pwm data <----');
|
|
|
|
for (let i = 0; i < {{LIGHT_COUNT}}; i++) {
|
|
|
|
const lightID = i + 1;
|
|
|
|
const pwmElement = document.getElementById(`light${i}_pwm`);
|
|
|
|
const pwmElementTxt = document.getElementById(`light${i}_pwm_txt`);
|
|
|
|
if (pwmElement) {
|
|
|
|
const url = `http://{{IP_ADDRESS}}/state?light=${lightID}`;
|
|
|
|
fetch(url).then(response => response.json()).then(data => {
|
|
|
|
const pwmValue = ((Math.round((data.curpwm - ((data.curpwm >= {{PWM_MIN}}) ? {{PWM_MIN}} : 0)) / {{PWM_MAX}} * 10000) / 100).toFixed(2));
|
|
|
|
pwmElement.innerText = pwmValue.toString();
|
|
|
|
pwmElement.value = pwmValue;
|
|
|
|
pwmElementTxt.innerText = pwmValue.toString();
|
|
|
|
}).catch(error => console.error(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatePWMValues();
|
|
|
|
setInterval(updatePWMValues, 5000);
|
|
|
|
var links = document.querySelectorAll('[id^="on"][id$="_off"]');
|
|
|
|
links.forEach(function(link) {
|
|
|
|
link.addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
2023-05-07 16:57:24 +02:00
|
|
|
var id_full = this.id;
|
2023-05-05 08:21:09 +02:00
|
|
|
var id = this.id.replace('on', '').replace('_off', '');
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', 'http://{{IP_ADDRESS}}/?on' + id + '=false&transition=' + document.getElementById('transition').value, true);
|
2023-05-07 16:57:24 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
var button = document.getElementById(id_full);
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
|
|
|
button.innerHTML = "Disabled!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "OFF";
|
|
|
|
}, 2000);
|
|
|
|
console.log('Data successfully sent to server!');
|
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "OFF";
|
|
|
|
}, 2000);
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast('Error while sending data to server.', 'error');
|
2023-05-07 16:57:24 +02:00
|
|
|
console.log('Error while sending data to server.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-05 08:21:09 +02:00
|
|
|
xhr.send();
|
|
|
|
updateLightState();
|
|
|
|
this.classList.add('pure-button-primary');
|
|
|
|
document.getElementById('on'+id+'_on').classList.remove('pure-button-primary');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var links = document.querySelectorAll('[id^="on"][id$="_on"]');
|
|
|
|
links.forEach(function(link) {
|
|
|
|
link.addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
2023-05-07 16:57:24 +02:00
|
|
|
var id_full = this.id;
|
2023-05-05 08:21:09 +02:00
|
|
|
var id = this.id.replace('on', '').replace('_on', '');
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', 'http://{{IP_ADDRESS}}/?on' + id + '=true&transition=' + document.getElementById('transition').value, true);
|
2023-05-07 16:57:24 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
var button = document.getElementById(id_full);
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
|
|
|
button.innerHTML = "Enabled!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "ON";
|
|
|
|
}, 2000);
|
|
|
|
console.log('Data successfully sent to server!');
|
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "ON";
|
|
|
|
}, 2000);
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast('Error while sending data to server.', 'error');
|
2023-05-07 16:57:24 +02:00
|
|
|
console.log('Error while sending data to server.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-05 08:21:09 +02:00
|
|
|
xhr.send();
|
|
|
|
updateLightState();
|
|
|
|
this.classList.add('pure-button-primary');
|
|
|
|
document.getElementById('on'+id+'_off').classList.remove('pure-button-primary');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function createTable() {
|
|
|
|
var table = document.createElement("table");
|
2023-05-05 20:40:06 +02:00
|
|
|
table.border = "1";
|
2023-05-05 08:21:09 +02:00
|
|
|
var headerRow = document.createElement("tr");
|
2023-05-05 20:40:06 +02:00
|
|
|
var headers = ["Hour", "Minute", "Ch1", "Ch2", "Ch3", "Ch4"];
|
|
|
|
headers.forEach(header => {
|
|
|
|
var th = document.createElement("th");
|
|
|
|
th.innerHTML = header;
|
|
|
|
headerRow.appendChild(th);
|
|
|
|
});
|
2023-05-05 08:21:09 +02:00
|
|
|
table.appendChild(headerRow);
|
2023-05-05 20:40:06 +02:00
|
|
|
for (var i = 1; i <= 10; i++) {
|
|
|
|
var tr = document.createElement("tr");
|
2023-05-07 11:31:52 +02:00
|
|
|
var tdHour = createSelectCell(23, 0, 1);
|
2023-05-05 20:40:06 +02:00
|
|
|
tdHour.id = "hour" + i;
|
|
|
|
tr.appendChild(tdHour);
|
2023-05-10 08:24:54 +02:00
|
|
|
var tdMinute = createSelectCell(59, 0, 10);
|
2023-05-05 20:40:06 +02:00
|
|
|
tdMinute.id = "minute" + i;
|
|
|
|
tr.appendChild(tdMinute);
|
2023-05-08 10:05:10 +02:00
|
|
|
for (var j = 1; j <=4; j++)
|
|
|
|
{
|
2023-05-08 10:27:35 +02:00
|
|
|
var tdCh = createSlider("ch" + j + "_" + i, 100, 0, 1);
|
2023-05-08 10:05:10 +02:00
|
|
|
tr.appendChild(tdCh);
|
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
table.appendChild(tr);
|
2023-05-05 11:07:56 +02:00
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
var container = document.getElementById("table-container");
|
|
|
|
container.innerHTML = "";
|
|
|
|
container.classList.add("pure-form");
|
|
|
|
container.appendChild(table);
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
2023-05-08 10:05:10 +02:00
|
|
|
function createSlider(id, max, value, step) {
|
|
|
|
var input = document.createElement("input");
|
|
|
|
input.type = "range";
|
|
|
|
input.min = 0;
|
|
|
|
input.max = max;
|
|
|
|
input.step = step;
|
|
|
|
input.value = value;
|
2023-05-10 10:48:50 +02:00
|
|
|
input.style = "1px solod darkgray";
|
2023-05-08 10:05:10 +02:00
|
|
|
input.classList.add("pure-slider-range");
|
2023-05-08 10:27:35 +02:00
|
|
|
if (id.startsWith("ch1_")) {
|
2023-05-10 10:48:50 +02:00
|
|
|
input.style.background = "linear-gradient(to bottom, " + rgb2Hex(31, 119, 180) + ", white)";
|
2023-05-08 10:27:35 +02:00
|
|
|
} else if (id.startsWith("ch2_")) {
|
2023-05-10 10:48:50 +02:00
|
|
|
input.style.background = "linear-gradient(to bottom, " + rgb2Hex(255, 127, 14) + ", white)";
|
2023-05-08 10:27:35 +02:00
|
|
|
} else if (id.startsWith("ch3_")) {
|
2023-05-10 10:48:50 +02:00
|
|
|
input.style.background = "linear-gradient(to bottom, " + rgb2Hex(44, 160, 44) + ", white)";
|
2023-05-08 10:27:35 +02:00
|
|
|
} else if (id.startsWith("ch4_")) {
|
2023-05-10 10:48:50 +02:00
|
|
|
input.style.background = "linear-gradient(to bottom, " + rgb2Hex(214, 39, 40) + ", white)";
|
2023-05-08 10:27:35 +02:00
|
|
|
}
|
|
|
|
div = document.createElement("div");
|
2023-05-08 10:05:10 +02:00
|
|
|
div.appendChild(input);
|
2023-05-08 10:27:35 +02:00
|
|
|
var span = document.createElement("span");
|
|
|
|
span.innerHTML = " " + value + " %";
|
|
|
|
div.appendChild(span);
|
|
|
|
input.oninput = function() {
|
|
|
|
span.innerHTML = " " + this.value + " %";
|
|
|
|
};
|
2023-05-08 10:05:10 +02:00
|
|
|
var td = document.createElement("td");
|
|
|
|
td.id = id;
|
|
|
|
td.appendChild(div);
|
|
|
|
return td;
|
|
|
|
}
|
2023-05-07 11:31:52 +02:00
|
|
|
function createSelectCell(max, value, step) {
|
2023-05-05 20:40:06 +02:00
|
|
|
var select = document.createElement("select");
|
2023-05-07 11:31:52 +02:00
|
|
|
for (var i = 0; i <= max; i += step) {
|
2023-05-05 11:07:56 +02:00
|
|
|
var option = document.createElement("option");
|
|
|
|
option.value = i;
|
2023-05-05 20:40:06 +02:00
|
|
|
option.text = i;
|
|
|
|
select.appendChild(option);
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
select.value = value;
|
|
|
|
var row = document.createElement("td");
|
|
|
|
row.appendChild(select);
|
|
|
|
return row;
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
function fillTableFromJson() {
|
|
|
|
fetch('http://{{IP_ADDRESS}}/tc_data_blocks_read')
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => {
|
|
|
|
var tcdata = data.tcdata;
|
|
|
|
for (var i = 0; i < tcdata.length; i++) {
|
|
|
|
var row = document.getElementById("hour" + (i+1)).parentNode;
|
|
|
|
row.cells[0].childNodes[0].value = tcdata[i].hour;
|
|
|
|
row.cells[1].childNodes[0].value = tcdata[i].min;
|
2023-05-08 10:27:35 +02:00
|
|
|
row.cells[2].childNodes[0].childNodes[0].value = parseInt(Math.round(tcdata[i].ch1 * 100 / 255));
|
|
|
|
row.cells[2].childNodes[0].childNodes[1].innerHTML = " " + row.cells[2].childNodes[0].childNodes[0].value + " %";
|
|
|
|
row.cells[3].childNodes[0].childNodes[0].value = parseInt(Math.round(tcdata[i].ch2 * 100 / 255));
|
|
|
|
row.cells[3].childNodes[0].childNodes[1].innerHTML = " " + row.cells[3].childNodes[0].childNodes[0].value + " %";
|
|
|
|
row.cells[4].childNodes[0].childNodes[0].value = parseInt(Math.round(tcdata[i].ch3 * 100 / 255));
|
|
|
|
row.cells[4].childNodes[0].childNodes[1].innerHTML = " " + row.cells[4].childNodes[0].childNodes[0].value + " %";
|
|
|
|
row.cells[5].childNodes[0].childNodes[0].value = parseInt(Math.round(tcdata[i].ch4 * 100 / 255));
|
|
|
|
row.cells[5].childNodes[0].childNodes[1].innerHTML = " " + row.cells[5].childNodes[0].childNodes[0].value + " %";
|
2023-05-05 08:21:09 +02:00
|
|
|
}
|
2023-05-07 11:31:52 +02:00
|
|
|
loadTCGraphData();
|
2023-05-05 20:40:06 +02:00
|
|
|
});
|
2023-05-05 11:22:09 +02:00
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
function createJsonFromTable() {
|
2023-05-05 11:22:09 +02:00
|
|
|
var tableRows = document.querySelectorAll("table tr");
|
2023-05-05 20:40:06 +02:00
|
|
|
var tcdata = [];
|
2023-05-08 09:46:25 +02:00
|
|
|
var timeArr = [];
|
2023-05-05 20:40:06 +02:00
|
|
|
for (var i = 1; i <= 10; i++) {
|
|
|
|
var row = document.getElementById("hour" + i).parentNode;
|
|
|
|
var hour = parseInt(row.cells[0].childNodes[0].value);
|
|
|
|
var min = parseInt(row.cells[1].childNodes[0].value);
|
2023-05-08 09:46:25 +02:00
|
|
|
timeArr.push(hour * 60 + min);
|
2023-05-08 10:27:35 +02:00
|
|
|
var ch1 = Math.round(parseInt(row.cells[2].childNodes[0].childNodes[0].value) * 2.55);
|
|
|
|
var ch2 = Math.round(parseInt(row.cells[3].childNodes[0].childNodes[0].value) * 2.55);
|
|
|
|
var ch3 = Math.round(parseInt(row.cells[4].childNodes[0].childNodes[0].value) * 2.55);
|
|
|
|
var ch4 = Math.round(parseInt(row.cells[5].childNodes[0].childNodes[0].value) * 2.55);
|
2023-05-05 20:40:06 +02:00
|
|
|
tcdata.push({hour: hour, min: min, ch1: ch1, ch2: ch2, ch3: ch3, ch4: ch4});
|
2023-05-05 11:22:09 +02:00
|
|
|
}
|
2023-05-08 09:46:25 +02:00
|
|
|
for (var i = 0; i < timeArr.length - 1; i++) {
|
|
|
|
if (timeArr[i] >= timeArr[i + 1]) {
|
|
|
|
showToast('Error while verifying timing control data. The timestamps are not incrementing.', 'error');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
var currentTime = {hour: new Date().getHours(), min: new Date().getMinutes()};
|
|
|
|
var jsonData = {tcdata: tcdata, currenttime: currentTime};
|
|
|
|
return JSON.stringify(jsonData);
|
2023-05-05 11:22:09 +02:00
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
function sendDataToServer() {
|
|
|
|
var jsonData = createJsonFromTable();
|
2023-05-08 09:46:25 +02:00
|
|
|
if (!jsonData) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "save";
|
|
|
|
}, 2000);
|
|
|
|
console.log('Error while generating timing control data.');
|
|
|
|
return;
|
|
|
|
}
|
2023-05-05 20:40:06 +02:00
|
|
|
var urlEncodedData = encodeURIComponent(jsonData);
|
|
|
|
var url = 'http://{{IP_ADDRESS}}/tc_data_blocks_store?data=' + urlEncodedData;
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', url, true);
|
2023-05-07 11:31:52 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
var button = document.getElementById("save-button");
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
|
|
|
button.innerHTML = "Saved!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "save";
|
|
|
|
}, 2000);
|
2023-05-05 20:40:06 +02:00
|
|
|
console.log('Data successfully sent to server!');
|
2023-05-07 11:31:52 +02:00
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "save";
|
|
|
|
}, 2000);
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast('Error while sending timing control data to server.', 'error');
|
2023-05-07 11:31:52 +02:00
|
|
|
console.log('Error while sending data to server.');
|
|
|
|
}
|
2023-05-05 11:22:09 +02:00
|
|
|
}
|
|
|
|
};
|
2023-05-06 13:38:56 +02:00
|
|
|
xhr.send();
|
2023-05-07 11:31:52 +02:00
|
|
|
loadTCGraphData();
|
2023-05-05 11:22:09 +02:00
|
|
|
}
|
2023-05-08 09:46:25 +02:00
|
|
|
function showToast(message, type) {
|
|
|
|
const toast = document.querySelector('.toast');
|
|
|
|
if (toast) {
|
|
|
|
toast.textContent = message;
|
|
|
|
toast.classList.remove('success', 'error');
|
|
|
|
if (type === 'success') {
|
|
|
|
toast.classList.add('success');
|
|
|
|
} else if (type === 'error') {
|
|
|
|
toast.classList.add('error');
|
|
|
|
}
|
|
|
|
toast.style.opacity = 1;
|
|
|
|
setTimeout(() => {
|
|
|
|
toast.style.opacity = 0;
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 16:48:59 +02:00
|
|
|
function sendPWMTestRequest(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var button = event.target;
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'http://{{IP_ADDRESS}}/test_pwm', true);
|
|
|
|
xhr.onload = function () {
|
|
|
|
if (xhr.status == 200 && xhr.responseText === "OK") {
|
|
|
|
button.innerHTML = "PWM test started";
|
|
|
|
button.style.backgroundColor = "green";
|
|
|
|
} else {
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
button.style.backgroundColor = "red";
|
|
|
|
}
|
|
|
|
setTimeout(function () {
|
|
|
|
button.innerHTML = "PWM Test";
|
|
|
|
button.style.backgroundColor = "";
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
button.style.backgroundColor = "red";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.innerHTML = "PWM Test";
|
|
|
|
button.style.backgroundColor = "";
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|