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.

154 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
  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. document.title = data.app;
  74. $(".pure-menu-heading").html(data.app);
  75. }
  76. // automatic assign
  77. Object.keys(data).forEach(function(key) {
  78. // Look for INPUTs
  79. var element = $("input[name=" + key + "]");
  80. if (element.length > 0) {
  81. if (element.attr('type') == 'checkbox') {
  82. element.prop("checked", data[key] == 1)
  83. .iphoneStyle({
  84. resizeContainer: false,
  85. resizeHandle: false,
  86. checkedLabel: 'ON',
  87. uncheckedLabel: 'OFF'
  88. })
  89. .iphoneStyle("refresh");
  90. } else {
  91. element.val(data[key]);
  92. }
  93. }
  94. // Look for SELECTs
  95. var element = $("select[name=" + key + "]");
  96. if (element.length > 0) {
  97. element.val(data[key]);
  98. }
  99. });
  100. // WIFI
  101. var groups = $("#panel-wifi .pure-g");
  102. for (var i in data.wifi) {
  103. var wifi = data.wifi[i];
  104. Object.keys(wifi).forEach(function(key) {
  105. var id = "input[name=" + key + "]";
  106. if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]);
  107. });
  108. };
  109. }
  110. function getJson(str) {
  111. try {
  112. return JSON.parse(str);
  113. } catch (e) {
  114. return false;
  115. }
  116. }
  117. function init() {
  118. $("#menuLink").on('click', toggleMenu);
  119. $(".button-update").on('click', doUpdate);
  120. $(".button-reset").on('click', doReset);
  121. $(".button-reconnect").on('click', doReconnect);
  122. $(".pure-menu-link").on('click', showPanel);
  123. var host = window.location.hostname;
  124. websock = new WebSocket('ws://' + host + ':81/');
  125. websock.onopen = function(evt) {};
  126. websock.onclose = function(evt) {};
  127. websock.onerror = function(evt) {};
  128. websock.onmessage = function(evt) {
  129. var data = getJson(evt.data);
  130. if (data) processData(data);
  131. };
  132. }
  133. $(init);