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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.responseText);
  7. }
  8. else {
  9. console.log("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. // connect actions to range inputs
  28. function rangeToOutput(range, output) {
  29. document.getElementById(range).oninput = function () {
  30. document.getElementById(output).innerHTML = this.value;
  31. // add to pending range changes
  32. rangeValues[this.id] = this.value;
  33. }
  34. }
  35. rangeToOutput("range_act_pwm", "out_act_pwm");
  36. rangeToOutput("range_idl_pwm", "out_idl_pwm");
  37. rangeToOutput("range_tim_sta", "out_tim_sta");
  38. rangeToOutput("range_tim_ldr", "out_tim_ldr");
  39. rangeToOutput("range_ldr_shw", "out_ldr_shw");
  40. let xhr = new XMLHttpRequest();
  41. xhr.onreadystatechange = function () {
  42. if (xhr.readyState == 4) {
  43. if (xhr.status == 200) {
  44. console.log(xhr.responseText);
  45. terminal = document.getElementById("term");
  46. autoscroll = document.getElementById("scroll");
  47. terminal.innerHTML += xhr.responseText;
  48. if (autoscroll.checked)
  49. terminal.scrollTop = terminal.scrollHeight;
  50. }
  51. else {
  52. console.log("status:", xhr.status);
  53. }
  54. }
  55. }
  56. function reloadTerminal() {
  57. xhr.open("POST", "/terminal", true);
  58. xhr.send();
  59. };
  60. document.addEventListener('DOMContentLoaded', () => {
  61. setInterval(reloadTerminal, 1000);
  62. setInterval(reloadRangeValues, 1000);
  63. });
  64. // use data- attributes for action
  65. document.querySelectorAll('.control').forEach((button) => {
  66. button.onclick = () => {
  67. fetch(`/action`, {
  68. method: 'POST',
  69. headers: {
  70. 'Content-Type': 'application/x-www-form-urlencoded',
  71. },
  72. body: `control=${button.dataset.action}`
  73. })
  74. .then(response => console.log(response))
  75. .catch(error => console.log('Error:', error));
  76. }
  77. });
  78. document.querySelector('#clear_term').onclick = () => {
  79. document.querySelector("#term").innerHTML = '';
  80. };