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.

158 lines
4.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
  1. var websock;
  2. function doUpdate() {
  3. var self = $(this);
  4. self.addClass("loading");
  5. $.ajax({
  6. 'method': 'POST',
  7. 'url': '/save',
  8. 'dataType': 'json',
  9. 'data': $("#formSave").serializeArray()
  10. }).done(function(data) {
  11. self.removeClass("loading");
  12. }).fail(function() {
  13. self.removeClass("loading");
  14. });
  15. }
  16. function doReset() {
  17. var response = window.confirm("Are you sure you want to reset the device?");
  18. if (response == false) return;
  19. var self = $(this);
  20. self.addClass("loading");
  21. $.ajax({
  22. 'method': 'GET',
  23. 'url': '/reset'
  24. });
  25. }
  26. function doReconnect() {
  27. var response = window.confirm("Are you sure you want to disconnect from the current WIFI network?");
  28. if (response == false) return;
  29. var self = $(this);
  30. self.addClass("loading");
  31. $.ajax({
  32. 'method': 'GET',
  33. 'url': '/reconnect'
  34. });
  35. }
  36. function showPanel() {
  37. $(".panel").hide();
  38. $("#" + $(this).attr("data")).show();
  39. if ($("#layout").hasClass('active')) toggleMenu();
  40. };
  41. function toggleMenu() {
  42. $("#layout").toggleClass('active');
  43. $("#menu").toggleClass('active');
  44. $("#menuLink").toggleClass('active');
  45. }
  46. function processData(data) {
  47. // pre-process
  48. if ("network" in data) {
  49. data.network = data.network.toUpperCase();
  50. }
  51. if ("mqttStatus" in data) {
  52. data.mqttStatus = data.mqttStatus ? "CONNECTED" : "NOT CONNECTED";
  53. }
  54. // relay
  55. if ("relayStatus" in data) {
  56. $("input[name='relayStatus']")
  57. .prop("checked", data.relayStatus)
  58. .iphoneStyle({
  59. checkedLabel: 'ON',
  60. uncheckedLabel: 'OFF',
  61. onChange: function(elem, value) {
  62. $.ajax({
  63. 'method': 'GET',
  64. 'url': value ? '/relay/on' : '/relay/off',
  65. 'dataType': 'json'
  66. });
  67. }
  68. })
  69. .iphoneStyle("refresh");
  70. }
  71. // title
  72. if ("app" in data) {
  73. $(".pure-menu-heading").html(data.app);
  74. var title = data.app;
  75. if ("hostname" in data) {
  76. title = data.hostname + " - " + title;
  77. }
  78. document.title = title;
  79. }
  80. // automatic assign
  81. Object.keys(data).forEach(function(key) {
  82. // Look for INPUTs
  83. var element = $("input[name=" + key + "]");
  84. if (element.length > 0) {
  85. if (element.attr('type') == 'checkbox') {
  86. element.prop("checked", data[key] == 1)
  87. .iphoneStyle({
  88. resizeContainer: false,
  89. resizeHandle: false,
  90. checkedLabel: 'ON',
  91. uncheckedLabel: 'OFF'
  92. })
  93. .iphoneStyle("refresh");
  94. } else {
  95. element.val(data[key]);
  96. }
  97. }
  98. // Look for SELECTs
  99. var element = $("select[name=" + key + "]");
  100. if (element.length > 0) {
  101. element.val(data[key]);
  102. }
  103. });
  104. // WIFI
  105. var groups = $("#panel-wifi .pure-g");
  106. for (var i in data.wifi) {
  107. var wifi = data.wifi[i];
  108. Object.keys(wifi).forEach(function(key) {
  109. var id = "input[name=" + key + "]";
  110. if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]);
  111. });
  112. };
  113. }
  114. function getJson(str) {
  115. try {
  116. return JSON.parse(str);
  117. } catch (e) {
  118. return false;
  119. }
  120. }
  121. function init() {
  122. $("#menuLink").on('click', toggleMenu);
  123. $(".button-update").on('click', doUpdate);
  124. $(".button-reset").on('click', doReset);
  125. $(".button-reconnect").on('click', doReconnect);
  126. $(".pure-menu-link").on('click', showPanel);
  127. var host = window.location.hostname;
  128. websock = new WebSocket('ws://' + host + ':81/');
  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);