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.

162 lines
4.2 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
8 years ago
  1. var websock;
  2. var csrf;
  3. function doUpdate() {
  4. var data = $("#formSave").serializeArray();
  5. websock.send(JSON.stringify({'csrf': csrf, '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({'csrf': csrf, '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({'csrf': csrf, 'action': 'reconnect'}));
  19. return false;
  20. }
  21. function doToggle(element, value) {
  22. websock.send(JSON.stringify({'csrf': csrf, 'action': value ? 'on' : 'off'}));
  23. return false;
  24. }
  25. function showPanel() {
  26. $(".panel").hide();
  27. $("#" + $(this).attr("data")).show();
  28. if ($("#layout").hasClass('active')) toggleMenu();
  29. };
  30. function toggleMenu() {
  31. $("#layout").toggleClass('active');
  32. $("#menu").toggleClass('active');
  33. $("#menuLink").toggleClass('active');
  34. }
  35. function processData(data) {
  36. // CSRF
  37. if ("csrf" in data) {
  38. csrf = data.csrf;
  39. }
  40. // messages
  41. if ("message" in data) {
  42. window.alert(data.message);
  43. }
  44. // pre-process
  45. if ("network" in data) {
  46. data.network = data.network.toUpperCase();
  47. }
  48. if ("mqttStatus" in data) {
  49. data.mqttStatus = data.mqttStatus ? "CONNECTED" : "NOT CONNECTED";
  50. }
  51. // relay
  52. if ("relayStatus" in data) {
  53. $("input[name='relayStatus']")
  54. .prop("checked", data.relayStatus)
  55. .iphoneStyle({
  56. checkedLabel: 'ON',
  57. uncheckedLabel: 'OFF',
  58. onChange: doToggle
  59. })
  60. .iphoneStyle("refresh");
  61. }
  62. // title
  63. if ("app" in data) {
  64. $(".pure-menu-heading").html(data.app);
  65. var title = data.app;
  66. if ("hostname" in data) {
  67. title = data.hostname + " - " + title;
  68. }
  69. document.title = title;
  70. }
  71. // automatic assign
  72. Object.keys(data).forEach(function(key) {
  73. // Enable options
  74. if (key.endsWith("Visible")) {
  75. var module = key.slice(0,-7);
  76. console.log(module);
  77. $(".module-" + module).show();
  78. return;
  79. }
  80. // Look for INPUTs
  81. var element = $("input[name=" + key + "]");
  82. if (element.length > 0) {
  83. if (element.attr('type') == 'checkbox') {
  84. element
  85. .prop("checked", data[key] == 1)
  86. .iphoneStyle({
  87. resizeContainer: false,
  88. resizeHandle: false,
  89. checkedLabel: 'ON',
  90. uncheckedLabel: 'OFF'
  91. })
  92. .iphoneStyle("refresh");
  93. } else {
  94. element.val(data[key]);
  95. }
  96. }
  97. // Look for SELECTs
  98. var element = $("select[name=" + key + "]");
  99. if (element.length > 0) {
  100. element.val(data[key]);
  101. }
  102. });
  103. // WIFI
  104. var groups = $("#panel-wifi .pure-g");
  105. for (var i in data.wifi) {
  106. var wifi = data.wifi[i];
  107. Object.keys(wifi).forEach(function(key) {
  108. var id = "input[name=" + key + "]";
  109. if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]);
  110. });
  111. };
  112. }
  113. function getJson(str) {
  114. try {
  115. return JSON.parse(str);
  116. } catch (e) {
  117. return false;
  118. }
  119. }
  120. function init() {
  121. $("#menuLink").on('click', toggleMenu);
  122. $(".button-update").on('click', doUpdate);
  123. $(".button-reset").on('click', doReset);
  124. $(".button-reconnect").on('click', doReconnect);
  125. $(".pure-menu-link").on('click', showPanel);
  126. var host = window.location.hostname;
  127. //host = '192.168.1.115';
  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. $(init);