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.

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