The esp8266 portion of the project
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.

176 lines
6.7 KiB

  1. function incHeight() {
  2. var el = document.getElementById('mySection');
  3. var height = el.offsetHeight;
  4. console.log("Height = " + height);
  5. var newHeight = height + 250;
  6. el.style.height = newHeight + 'px';
  7. }
  8. var header = document.querySelector('header');
  9. var section = document.querySelector('section');
  10. function handleErrors(response) {
  11. if (!response.ok) {
  12. throw Error(response.statusText);
  13. }
  14. return response;
  15. }
  16. async function getData() {
  17. //await the response of the fetch call
  18. let response = await fetch('combinedSettingsFile.txt');
  19. //proceed once the first promise is resolved.
  20. let data = await response.json()
  21. //proceed only when the second promise is resolved
  22. return data;
  23. }
  24. async function getTemperatures() {
  25. //await the response of the fetch call
  26. let response = await fetch('temperatures.txt');
  27. //proceed once the first promise is resolved.
  28. let data = await response.json()
  29. //proceed only when the second promise is resolved
  30. return data;
  31. }
  32. async function getTemperatures2() {
  33. //await the response of the fetch call
  34. let response = await fetch('api/get/temperatures');
  35. //proceed once the first promise is resolved.
  36. let data = await response.json()
  37. //proceed only when the second promise is resolved
  38. return data;
  39. }
  40. getTemperatures()
  41. .then(data => {
  42. data.temperatures.forEach(function(temperature) {
  43. console.log(temperature);
  44. })
  45. })
  46. // .then(result => {
  47. // //Here body is not ready yet, throw promise
  48. // if (!result.ok) throw result;
  49. // return result.json();
  50. // })
  51. // .then(result => {
  52. // //Successful request processing
  53. // console.log(result);
  54. // }).catch(error => {
  55. // //Here is still promise
  56. // console.log(error);
  57. // error.json().then((body) => {
  58. // //Here is already the payload from API
  59. // console.log(body);
  60. // });
  61. // })
  62. setInterval(function() {
  63. let temperaturesSwitch = document.getElementById('temperatures-switch');
  64. if (temperaturesSwitch.checked) {
  65. // getTemperatures2()
  66. fetch('api/get/temperatures')
  67. // .then(handleErrors)
  68. .then(result => {
  69. //Here body is not ready yet, throw promise
  70. if (!result.ok) throw result;
  71. return result.json();
  72. })
  73. .then(data => {
  74. let p = document.getElementById('temperature');
  75. if (data.status == 'failed') {
  76. let errorMessage = sprintf('status = %s reason = %s', data.status, data.message)
  77. p.textContent = errorMessage;
  78. console.log(errorMessage);
  79. } else {
  80. p.textContent = "";
  81. data.temperatures.forEach(function(temperature) {
  82. console.log("Temperature = " + temperature);
  83. temp = sprintf("%05.2f", temperature);
  84. p.textContent += temp + ' ';
  85. })
  86. // let temp = sprintf("%.2f", 36.72);
  87. // p.textContent += temp + ' ';
  88. }
  89. })
  90. .catch(error => {
  91. //Here is still promise
  92. // console.log(error);
  93. // if (error.status == 404) {
  94. // console.log('error = ' + 404);
  95. // }
  96. // else {
  97. // error.json().then((body) => {
  98. //Here is already the payload from API
  99. // let errorMessage = sprintf('status = %s reason = %s', body.status, body.message)
  100. // console.log(errorMessage);
  101. // });
  102. // }
  103. let p = document.getElementById('temperature');
  104. p.textContent = 'Network Error';
  105. })
  106. }
  107. },
  108. 2000
  109. );
  110. //call getData function
  111. getData()
  112. //.then(data => console.log(data))//log the data
  113. .then(function showSettings(data) {
  114. var ballasts = data['settings'];
  115. console.log(ballasts);
  116. for (let i = 0; i < 3; i++) {
  117. // var myArticle = document.createElement('article');
  118. // var myH2 = document.createElement('h3');
  119. // var myPara1 = document.createElement('p');
  120. // var myPara2 = document.createElement('p');
  121. // var myPara3 = document.createElement('p');
  122. // var myPara4 = document.createElement('p');
  123. // var myPara5 = document.createElement('p');
  124. // // var myList = document.createElement('ul');
  125. // myH2.textContent = 'Ballast' + ' #' + (i + 1);
  126. // myPara1.textContent = 'Start Hour: ' + ballasts[i].shour;
  127. // myPara2.textContent = 'Start Minute: ' + ballasts[i].smin;
  128. // myPara3.textContent = 'Fade Period: ' + ballasts[i].fadePeriod;
  129. // myPara4.textContent = 'End Hour: ' + ballasts[i].ehour;
  130. // myPara5.textContent = 'End Minute: ' + ballasts[i].emin;
  131. // console.log(ballasts[i].shour);
  132. // myArticle.appendChild(myH2);
  133. // myArticle.appendChild(myPara1);
  134. // myArticle.appendChild(myPara2);
  135. // myArticle.appendChild(myPara3);
  136. // myArticle.appendChild(myPara4);
  137. // myArticle.appendChild(myPara5);
  138. // // myArticle.appendChild(myList);
  139. // section.appendChild(myArticle);
  140. let myDiv = document.getElementById('ballast' + (i + 1));
  141. // let myH2 = document.createElement('h3');
  142. let myPara1 = document.createElement('p');
  143. let myPara2 = document.createElement('p');
  144. let myPara3 = document.createElement('p');
  145. let myPara4 = document.createElement('p');
  146. let myPara5 = document.createElement('p');
  147. // myH2.textContent = 'Ballast' + ' #' + (i + 1);
  148. myPara1.textContent = 'Start Hour: ' + ballasts[i].shour;
  149. myPara2.textContent = 'Start Minute: ' + ballasts[i].smin;
  150. myPara3.textContent = 'Fade Period: ' + ballasts[i].fadePeriod;
  151. myPara4.textContent = 'End Hour: ' + ballasts[i].ehour;
  152. myPara5.textContent = 'End Minute: ' + ballasts[i].emin;
  153. // myDiv.appendChild(myH2);
  154. myDiv.appendChild(myPara1);
  155. myDiv.appendChild(myPara2);
  156. myDiv.appendChild(myPara3);
  157. myDiv.appendChild(myPara4);
  158. myDiv.appendChild(myPara5);
  159. }
  160. return data;
  161. })
  162. .then(data => console.log(data))
  163. // .then(incHeight());