script.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. var gateway = `ws://${window.location.hostname}/ws`;
  2. var websocket;
  3. window.addEventListener('load', onLoad);
  4. var command = { "cmd": "", "id": "" };
  5. function initWebSocket() {
  6. console.log('Trying to open a WebSocket connection...');
  7. websocket = new WebSocket(gateway);
  8. websocket.onopen = onOpen;
  9. websocket.onclose = onClose;
  10. websocket.onmessage = onMessage;
  11. }
  12. function onOpen(event) {
  13. console.log('Connection opened');
  14. }
  15. function onClose(event) {
  16. console.log('Connection closed');
  17. setTimeout(initWebSocket, 2000);
  18. }
  19. function onMessage(event) {
  20. let data = JSON.parse(event.data);
  21. let led = document.getElementById("led_" + data.id);
  22. led.className = data.state ? 'led-green' : 'led-red';
  23. let button = document.getElementById("toggle_button_" + data.id);
  24. button.innerHTML = 'toggle ' + (data.state ? 'off' : 'on');
  25. }
  26. function onLoad(event) {
  27. initWebSocket();
  28. }
  29. function toggleState(id) {
  30. command.cmd = "toggle";
  31. command.id = id;
  32. websocket.send(JSON.stringify(command));
  33. }
  34. function turnOffAll() {
  35. command.cmd = "turn_off_all";
  36. websocket.send(JSON.stringify(command));
  37. }
  38. function initialLoad() {
  39. fetch('/power_outlets')
  40. .then(function (response) {
  41. data = response.json();
  42. return data;
  43. })
  44. .then(function (data) {
  45. const content = document.getElementById('content');
  46. for (let i = 0; i < data.length; i++) {
  47. let card = document.createElement("div");
  48. card.id = "card_" + i;
  49. card.className = 'card'
  50. let name = document.createElement("h2");
  51. name.innerHTML = data[i].name;
  52. card.appendChild(name);
  53. let state = document.createElement("p");
  54. let led = document.createElement("div");
  55. led.id = 'led_' + i;
  56. led.className = data[i].state ? 'led-green' : 'led-red';
  57. state.appendChild(led)
  58. card.appendChild(state);
  59. let button = document.createElement("button");
  60. button.className = 'toggle_button';
  61. button.id = 'toggle_button_' + i;
  62. button.innerHTML = 'toggle ' + (data[i].state ? 'off' : 'on');
  63. button.name = data[i].name;
  64. button.onclick = function () { toggleState(i) };
  65. card.appendChild(button);
  66. let div = document.createElement("div");
  67. div.className = 'options_button_div';
  68. let optionsButton = document.createElement("button");
  69. optionsButton.id = "options_button_" + i;
  70. optionsButton.className = 'options_button';
  71. optionsButton.onclick = function () { editProperties(i) };
  72. let img = document.createElement("img");
  73. img.setAttribute("src", "options.svg");
  74. optionsButton.appendChild(img);
  75. div.appendChild(optionsButton);
  76. card.appendChild(div);
  77. content.appendChild(card);
  78. }
  79. })
  80. .catch(function (error) {
  81. let card = document.createElement("div");
  82. card.className = 'card'
  83. let name = document.createElement("h2");
  84. name.innerHTML = error;
  85. card.appendChild(name);
  86. document.body.appendChild(card);
  87. });
  88. }
  89. function editProperties(id) {
  90. fetch('/power_outlets/' + id)
  91. .then(function (response) {
  92. data = response.json();
  93. return data;
  94. })
  95. .then(function (data) {
  96. // Get the modal
  97. var modal = document.getElementById("propertiesWindow");
  98. modal.style.display = "block";
  99. // When the user clicks anywhere outside of the modal, close it
  100. window.onclick = function (event) {
  101. if (event.target == modal) {
  102. closePropertiesWindow();
  103. }
  104. }
  105. let p = document.getElementById("propertiesContent");
  106. let divName = document.createElement("div");
  107. divName.id = "div_name"
  108. let labelName = document.createElement("label");
  109. labelName.setAttribute('for', 'powerOutletName');
  110. labelName.innerHTML = 'Name';
  111. divName.appendChild(labelName)
  112. let inputName = document.createElement("input", 'type="text"');
  113. inputName.id = "powerOutletName";
  114. inputName.setAttribute("value", data.name);
  115. divName.appendChild(inputName);
  116. p.appendChild(divName);
  117. let divPin = document.createElement("div");
  118. divPin.id = 'div_pin'
  119. let labelPin = document.createElement("label");
  120. labelPin.setAttribute('for', 'powerOutletPin');
  121. labelPin.innerHTML = 'Pin';
  122. divPin.appendChild(labelPin)
  123. let inputPin = document.createElement("input", 'type="text" id="powerOutletPin"');
  124. inputPin.setAttribute("value", data.pin);
  125. divPin.appendChild(inputPin);
  126. p.appendChild(divPin);
  127. let divState = document.createElement("div");
  128. divState.id = 'div_state';
  129. let labelState = document.createElement("label");
  130. labelState.setAttribute('for', 'powerOutletState');
  131. labelState.innerHTML = 'State';
  132. divState.appendChild(labelState)
  133. let inputState = document.createElement("input", 'type="text" id="powerOutletState"');
  134. inputState.setAttribute("value", data.state);
  135. divState.appendChild(inputState);
  136. p.appendChild(divState);
  137. let divVoltage = document.createElement("div");
  138. divVoltage.id = 'div_voltage';
  139. let labelVoltage = document.createElement("label");
  140. labelVoltage.setAttribute('for', 'powerOutletVoltage');
  141. labelVoltage.innerHTML = 'Voltage';
  142. divVoltage.appendChild(labelVoltage)
  143. let inputVoltage = document.createElement("input", 'type="text"');
  144. inputVoltage.id = "powerOutletVoltage"
  145. inputVoltage.setAttribute("value", data.voltage);
  146. divVoltage.appendChild(inputVoltage);
  147. p.appendChild(divVoltage);
  148. let buttonSave = document.createElement("button");
  149. buttonSave.id = "button_save";
  150. buttonSave.className = 'toggle_button';
  151. buttonSave.innerHTML = "Save";
  152. buttonSave.onclick = function () { saveProperties(id) };
  153. p.appendChild(buttonSave);
  154. }).catch(function (error) {
  155. let card = document.createElement("div");
  156. card.className = 'card'
  157. let name = document.createElement("h2");
  158. name.innerHTML = error;
  159. card.appendChild(name);
  160. document.body.appendChild(card);
  161. });
  162. }
  163. function saveProperties(id) {
  164. const requestOptions = {
  165. method: 'PUT',
  166. headers: { 'Content-Type': 'application/json' },
  167. };
  168. let response = {};
  169. response.id = id;
  170. response.name = document.getElementById('powerOutletName').value;
  171. requestOptions.body = JSON.stringify(response);
  172. console.log(response);
  173. fetch('/power_outlets/' + id, requestOptions)
  174. .then(response => response.json())
  175. .then(function (data) {
  176. console.log(data);
  177. console.log(data.name)
  178. });
  179. closePropertiesWindow();
  180. }
  181. function closePropertiesWindow() {
  182. document.getElementById('propertiesWindow').style.display = 'none';
  183. document.getElementById('div_name').remove();
  184. document.getElementById('div_pin').remove();
  185. document.getElementById('div_state').remove();
  186. document.getElementById('div_voltage').remove();
  187. document.getElementById('button_save').remove();
  188. }
  189. function saveWifiConfig() {
  190. const requestOptions = {
  191. method: 'PUT',
  192. headers: { 'Content-Type': 'application/json' },
  193. };
  194. let response = {};
  195. response.wifi_network_ssid = document.getElementById('wifi_ssid').value;
  196. response.wifi_network_password = document.getElementById('wifi_password').value;
  197. response.soft_ap_ssid = document.getElementById('ap_ssid').value;
  198. response.soft_ap_password = document.getElementById('ap_password').value;
  199. response.soft_ap_ip_address = document.getElementById('ap_ipaddress').value;
  200. response.soft_ap_gateway = document.getElementById('ap_gateway').value;
  201. response.soft_ap_subnet = document.getElementById('ap_subnet').value;
  202. requestOptions.body = JSON.stringify(response);
  203. fetch('/wifi_config', requestOptions)
  204. .then(response => response.json())
  205. .then(function (data) {
  206. console.log(data);
  207. history.back();
  208. })
  209. .catch(function (error) {
  210. console.log("Fetch wifi_config failed");
  211. });
  212. }
  213. //Page load function foor wifi configuration
  214. function initialLoadWifi() {
  215. fetch('/wifi_config')
  216. .then(function (response) {
  217. data = response.json();
  218. return data;
  219. })
  220. .then(function (data) {
  221. console.log(data);
  222. let inputField = document.getElementById('wifi_ssid');
  223. inputField.value = data.wifi_network_ssid;
  224. inputField = document.getElementById('wifi_password');
  225. inputField.value = data.wifi_network_password;
  226. inputField = document.getElementById('ap_ssid');
  227. inputField.value = data.soft_ap_ssid;
  228. inputField = document.getElementById('ap_password');
  229. inputField.value = data.soft_ap_password;
  230. inputField = document.getElementById('ap_ipaddress');
  231. inputField.value = data.soft_ap_ip_address;
  232. inputField = document.getElementById('ap_gateway');
  233. inputField.value = data.soft_ap_gateway;
  234. inputField = document.getElementById('ap_subnet');
  235. inputField.value = data.soft_ap_subnet;
  236. })
  237. .catch(function (error) {
  238. console.log("Fetch wifi_config failed");
  239. })
  240. }