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.

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