ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

input.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. let rangeValues = {};
  2. let xhrUpd = new XMLHttpRequest();
  3. xhrUpd.onreadystatechange = function () {
  4. if (xhrUpd.readyState == 4) {
  5. if (xhrUpd.status == 200) {
  6. console.log("xhrUpd: ", xhrUpd.responseText);
  7. }
  8. else {
  9. console.log("xhrUpd: status=", xhrUpd.status);
  10. }
  11. }
  12. }
  13. function reloadRangeValues() {
  14. let url = "/update";
  15. // if there are scheduled updates, send them
  16. if (Object.keys(rangeValues).length > 0) {
  17. let params = [];
  18. for (let p in rangeValues)
  19. params.push(encodeURIComponent(p) + "=" + encodeURIComponent(rangeValues[p]));
  20. params = params.join("&");
  21. xhrUpd.open("POST", url, true);
  22. xhrUpd.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  23. xhrUpd.send(params);
  24. rangeValues = {};
  25. }
  26. };
  27. function reloadTerminal() {
  28. const terminal = document.querySelector("#term");
  29. const autoscroll = document.querySelector("#scroll");
  30. fetch(`/terminal`, {
  31. method: 'POST',
  32. headers: {
  33. 'Content-Type': 'application/x-www-form-urlencoded',
  34. },
  35. body: ''
  36. })
  37. .then(response => response.text())
  38. .then(data => {
  39. if(data.length > 0) {
  40. terminal.innerHTML += data;
  41. if (autoscroll.checked)
  42. terminal.scrollTop = terminal.scrollHeight;
  43. }
  44. })
  45. .catch(error => console.log('Error:', error));
  46. };
  47. document.addEventListener('DOMContentLoaded', () => {
  48. setInterval(reloadTerminal, 1000);
  49. setInterval(reloadRangeValues, 1000);
  50. // use data- attributes for action
  51. document.querySelectorAll('.control').forEach((button) => {
  52. button.onclick = () => {
  53. fetch(`/action`, {
  54. method: 'POST',
  55. headers: {
  56. 'Content-Type': 'application/x-www-form-urlencoded',
  57. },
  58. body: `control=${button.dataset.action}`
  59. })
  60. .then(response => console.log(response))
  61. .catch(error => console.log('Error:', error));
  62. }
  63. });
  64. document.querySelectorAll('.regler').forEach((regler) => {
  65. regler.oninput = () => {
  66. document.querySelector(`#${regler.dataset.output}`).innerHTML = regler.value;
  67. rangeValues[regler.id] = regler.value;
  68. }
  69. });
  70. document.querySelector('#clear_term').onclick = () => {
  71. document.querySelector("#term").innerHTML = '';
  72. };
  73. });