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.

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