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.

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