Fork of the espurna firmware for `mhsw` switches
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.

224 lines
6.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. var websock;
  2. function doUpdate() {
  3. var data = $("#formSave").serializeArray();
  4. websock.send(JSON.stringify({'config': data}));
  5. $(".powExpected").val(0);
  6. return false;
  7. }
  8. function doReset() {
  9. var response = window.confirm("Are you sure you want to reset the device?");
  10. if (response == false) return false;
  11. websock.send(JSON.stringify({'action': 'reset'}));
  12. return false;
  13. }
  14. function doReconnect() {
  15. var response = window.confirm("Are you sure you want to disconnect from the current WIFI network?");
  16. if (response == false) return false;
  17. websock.send(JSON.stringify({'action': 'reconnect'}));
  18. return false;
  19. }
  20. function doToggle(element, value) {
  21. var relayID = parseInt(element.attr("data"));
  22. websock.send(JSON.stringify({'action': value ? 'on' : 'off', 'relayID': relayID}));
  23. return false;
  24. }
  25. function randomString(length, chars) {
  26. var mask = '';
  27. if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
  28. if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  29. if (chars.indexOf('#') > -1) mask += '0123456789';
  30. if (chars.indexOf('@') > -1) mask += 'ABCDEF';
  31. if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
  32. var result = '';
  33. for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
  34. return result;
  35. }
  36. function doGenerateAPIKey() {
  37. var apikey = randomString(16, '@#');
  38. $("input[name=\"apiKey\"]").val(apikey);
  39. return false;
  40. }
  41. function showPanel() {
  42. $(".panel").hide();
  43. $("#" + $(this).attr("data")).show();
  44. if ($("#layout").hasClass('active')) toggleMenu();
  45. $("input[type='checkbox']").iphoneStyle("calculateDimensions").iphoneStyle("refresh");
  46. };
  47. function toggleMenu() {
  48. $("#layout").toggleClass('active');
  49. $("#menu").toggleClass('active');
  50. $("#menuLink").toggleClass('active');
  51. }
  52. function createRelays(count) {
  53. var current = $("#relays > div").length;
  54. if (current > 0) return;
  55. var template = $("#relayTemplate .pure-g")[0];
  56. for (var relayID=0; relayID<count; relayID++) {
  57. var line = $(template).clone();
  58. $(line).find("input").each(function() {
  59. $(this).attr("data", relayID);
  60. });
  61. if (count > 1) $(".relay_id", line).html(" " + relayID);
  62. line.appendTo("#relays");
  63. $(":checkbox", line).iphoneStyle({
  64. onChange: doToggle,
  65. resizeContainer: true,
  66. resizeHandle: true,
  67. checkedLabel: 'ON',
  68. uncheckedLabel: 'OFF'
  69. });
  70. }
  71. }
  72. function processData(data) {
  73. // title
  74. if ("app" in data) {
  75. $(".pure-menu-heading").html(data.app);
  76. var title = data.app;
  77. if ("hostname" in data) {
  78. title = data.hostname + " - " + title;
  79. }
  80. document.title = title;
  81. }
  82. Object.keys(data).forEach(function(key) {
  83. // Wifi
  84. if (key == "wifi") {
  85. var groups = $("#panel-wifi .pure-g");
  86. for (var i in data.wifi) {
  87. var wifi = data.wifi[i];
  88. Object.keys(wifi).forEach(function(key) {
  89. var id = "input[name=" + key + "]";
  90. if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]);
  91. });
  92. };
  93. return;
  94. }
  95. // Relay status
  96. if (key == "relayStatus") {
  97. var relays = data.relayStatus;
  98. createRelays(relays.length);
  99. for (var relayID in relays) {
  100. var element = $(".relayStatus[data=" + relayID + "]");
  101. if (element.length > 0) {
  102. element
  103. .prop("checked", relays[relayID])
  104. .iphoneStyle("refresh");
  105. }
  106. }
  107. return;
  108. }
  109. // Messages
  110. if (key == "message") {
  111. window.alert(data.message);
  112. return;
  113. }
  114. // Enable options
  115. if (key.endsWith("Visible")) {
  116. var module = key.slice(0,-7);
  117. console.log(module);
  118. $(".module-" + module).show();
  119. return;
  120. }
  121. // Pre-process
  122. if (key == "network") {
  123. data.network = data.network.toUpperCase();
  124. }
  125. if (key == "mqttStatus") {
  126. data.mqttStatus = data.mqttStatus ? "CONNECTED" : "NOT CONNECTED";
  127. }
  128. // Look for INPUTs
  129. var element = $("input[name=" + key + "]");
  130. if (element.length > 0) {
  131. if (element.attr('type') == 'checkbox') {
  132. element
  133. .prop("checked", data[key])
  134. .iphoneStyle("refresh");
  135. } else {
  136. element.val(data[key]);
  137. }
  138. return;
  139. }
  140. // Look for SELECTs
  141. var element = $("select[name=" + key + "]");
  142. if (element.length > 0) {
  143. element.val(data[key]);
  144. return;
  145. }
  146. });
  147. // Auto generate an APIKey if none defined yet
  148. if ($("input[name='apiKey']").val() == "") {
  149. doGenerateAPIKey();
  150. }
  151. }
  152. function getJson(str) {
  153. try {
  154. return JSON.parse(str);
  155. } catch (e) {
  156. return false;
  157. }
  158. }
  159. function initWebSocket(host) {
  160. if (host === undefined) {
  161. host = window.location.hostname;
  162. }
  163. websock = new WebSocket('ws://' + host + '/ws');
  164. websock.onopen = function(evt) {};
  165. websock.onclose = function(evt) {};
  166. websock.onerror = function(evt) {};
  167. websock.onmessage = function(evt) {
  168. var data = getJson(evt.data);
  169. if (data) processData(data);
  170. };
  171. }
  172. function init() {
  173. $("#menuLink").on('click', toggleMenu);
  174. $(".button-update").on('click', doUpdate);
  175. $(".button-reset").on('click', doReset);
  176. $(".button-reconnect").on('click', doReconnect);
  177. $(".button-apikey").on('click', doGenerateAPIKey);
  178. $(".pure-menu-link").on('click', showPanel);
  179. $.ajax({
  180. 'method': 'GET',
  181. 'url': '/auth'
  182. }).done(function(data) {
  183. initWebSocket();
  184. });
  185. }
  186. $(init);