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.

108 lines
2.7 KiB

  1. var update_timer = null;
  2. var relaySlider;
  3. function doUpdate() {
  4. var self = $(this);
  5. self.addClass("loading");
  6. $.ajax({
  7. 'method': 'POST',
  8. 'url': '/post',
  9. 'dataType': 'json',
  10. 'data': $("#formSave").serializeArray()
  11. }).done(function(data) {
  12. self.removeClass("loading");
  13. }).fail(function() {
  14. self.removeClass("loading");
  15. });
  16. }
  17. function showPanel() {
  18. $(".panel").hide();
  19. $("#" + $(this).attr("data")).show();
  20. if ($("#layout").hasClass('active')) toggleMenu();
  21. };
  22. function toggleMenu() {
  23. $("#layout").toggleClass('active');
  24. $("#menu").toggleClass('active');
  25. $("#menuLink").toggleClass('active');
  26. }
  27. function parseResponse(data) {
  28. // pre-process
  29. if ("network" in data) data.network = data.network.toUpperCase();
  30. if ("mqttStatus" in data) data.mqttStatus = data.mqttStatus ? "CONNECTED" : "NOT CONNECTED";
  31. // relay
  32. if ("relayStatus" in data) {
  33. $("input[name='relayStatus']")
  34. .prop("checked", data.relayStatus)
  35. .iphoneStyle("refresh");
  36. }
  37. // title
  38. if ("app" in data) {
  39. document.title = data.app;
  40. $(".pure-menu-heading").html(data.app);
  41. }
  42. // automatic assign
  43. Object.keys(data).forEach(function(key) {
  44. var id = "input[name=" + key + "]";
  45. if ($(id).length) $(id).val(data[key]);
  46. });
  47. // WIFI
  48. var groups = $("#panel-wifi .pure-g");
  49. for (var i in data.wifi) {
  50. var wifi = data.wifi[i];
  51. Object.keys(wifi).forEach(function(key) {
  52. var id = "input[name=" + key + "]";
  53. if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]);
  54. });
  55. };
  56. if ("updateInterval" in data) {
  57. if (update_timer) clearInterval(update_timer);
  58. if (data.updateInterval > 0) {
  59. update_timer = setInterval(update, data.updateInterval);
  60. }
  61. }
  62. }
  63. function update() {
  64. $.ajax({
  65. 'method': 'GET',
  66. 'url': '/status',
  67. 'dataType': 'json'
  68. }).done(parseResponse);
  69. }
  70. function init() {
  71. $.ajax({
  72. 'method': 'GET',
  73. 'url': '/get',
  74. 'dataType': 'json'
  75. }).done(parseResponse);
  76. }
  77. $(function() {
  78. $("#menuLink").on('click', toggleMenu);
  79. $(".button-update").on('click', doUpdate);
  80. $(".pure-menu-link").on('click', showPanel);
  81. relaySlider = $('#relayStatus').iphoneStyle({
  82. checkedLabel: 'ON',
  83. uncheckedLabel: 'OFF',
  84. onChange: function(elem, value) {
  85. $.ajax({
  86. 'method': 'GET',
  87. 'url': value ? '/relay/on' : '/relay/off',
  88. 'dataType': 'json'
  89. });
  90. setTimeout(update, 200);
  91. }
  92. });
  93. init();
  94. });