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.

1656 lines
47 KiB

8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
8 years ago
  1. var websock;
  2. var password = false;
  3. var maxNetworks;
  4. var maxSchedules;
  5. var messages = [];
  6. var free_size = 0;
  7. var urls = {};
  8. var numChanged = 0;
  9. var numReboot = 0;
  10. var numReconnect = 0;
  11. var numReload = 0;
  12. var useWhite = false;
  13. var useCCT = false;
  14. var now = 0;
  15. var ago = 0;
  16. <!-- removeIf(!rfm69)-->
  17. var packets;
  18. var filters = [];
  19. <!-- endRemoveIf(!rfm69)-->
  20. // -----------------------------------------------------------------------------
  21. // Messages
  22. // -----------------------------------------------------------------------------
  23. function initMessages() {
  24. messages[1] = "Remote update started";
  25. messages[2] = "OTA update started";
  26. messages[3] = "Error parsing data!";
  27. messages[4] = "The file does not look like a valid configuration backup or is corrupted";
  28. messages[5] = "Changes saved. You should reboot your board now";
  29. messages[7] = "Passwords do not match!";
  30. messages[8] = "Changes saved";
  31. messages[9] = "No changes detected";
  32. messages[10] = "Session expired, please reload page...";
  33. }
  34. <!-- removeIf(!sensor)-->
  35. function sensorName(id) {
  36. var names = [
  37. "DHT", "Dallas", "Emon Analog", "Emon ADC121", "Emon ADS1X15",
  38. "HLW8012", "V9261F", "ECH1560", "Analog", "Digital",
  39. "Events", "PMSX003", "BMX280", "MHZ19", "SI7021",
  40. "SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD",
  41. "TMP3X", "HC-SR04", "SenseAir", "GeigerTicks", "GeigerCPM"
  42. ];
  43. if (1 <= id && id <= names.length) {
  44. return names[id - 1];
  45. }
  46. return null;
  47. }
  48. function magnitudeType(type) {
  49. var types = [
  50. "Temperature", "Humidity", "Pressure",
  51. "Current", "Voltage", "Active Power", "Apparent Power",
  52. "Reactive Power", "Power Factor", "Energy", "Energy (delta)",
  53. "Analog", "Digital", "Events",
  54. "PM1.0", "PM2.5", "PM10", "CO2", "Lux", "UV", "Distance" , "HCHO",
  55. "Local Dose Rate", "Local Dose Rate"
  56. ];
  57. if (1 <= type && type <= types.length) {
  58. return types[type - 1];
  59. }
  60. return null;
  61. }
  62. function magnitudeError(error) {
  63. var errors = [
  64. "OK", "Out of Range", "Warming Up", "Timeout", "Wrong ID",
  65. "Data Error", "I2C Error", "GPIO Error", "Calibration error"
  66. ];
  67. if (0 <= error && error < errors.length) {
  68. return errors[error];
  69. }
  70. return "Error " + error;
  71. }
  72. <!-- endRemoveIf(!sensor)-->
  73. // -----------------------------------------------------------------------------
  74. // Utils
  75. // -----------------------------------------------------------------------------
  76. $.fn.enterKey = function (fnc) {
  77. return this.each(function () {
  78. $(this).keypress(function (ev) {
  79. var keycode = parseInt(ev.keyCode ? ev.keyCode : ev.which, 10);
  80. if (13 === keycode) {
  81. return fnc.call(this, ev);
  82. }
  83. });
  84. });
  85. };
  86. function keepTime() {
  87. $("span[name='ago']").html(ago);
  88. ago++;
  89. if (0 === now) { return; }
  90. var date = new Date(now * 1000);
  91. var text = date.toISOString().substring(0, 19).replace("T", " ");
  92. $("input[name='now']").val(text);
  93. $("span[name='now']").html(text);
  94. now++;
  95. }
  96. function zeroPad(number, positions) {
  97. var zeros = "";
  98. for (var i = 0; i < positions; i++) {
  99. zeros += "0";
  100. }
  101. return (zeros + number).slice(-positions);
  102. }
  103. function loadTimeZones() {
  104. var time_zones = [
  105. -720, -660, -600, -570, -540,
  106. -480, -420, -360, -300, -240,
  107. -210, -180, -120, -60, 0,
  108. 60, 120, 180, 210, 240,
  109. 270, 300, 330, 345, 360,
  110. 390, 420, 480, 510, 525,
  111. 540, 570, 600, 630, 660,
  112. 720, 765, 780, 840
  113. ];
  114. for (var i in time_zones) {
  115. var value = time_zones[i];
  116. var offset = value >= 0 ? value : -value;
  117. var text = "GMT" + (value >= 0 ? "+" : "-") +
  118. zeroPad(parseInt(offset / 60, 10), 2) + ":" +
  119. zeroPad(offset % 60, 2);
  120. $("select[name='ntpOffset']").append(
  121. $("<option></option>").
  122. attr("value",value).
  123. text(text));
  124. }
  125. }
  126. function validateForm(form) {
  127. // http://www.the-art-of-web.com/javascript/validate-password/
  128. // at least one lowercase and one uppercase letter or number
  129. // at least five characters (letters, numbers or special characters)
  130. var re_password = /^(?=.*[A-Z\d])(?=.*[a-z])[\w~!@#$%^&*\(\)<>,.\?;:{}\[\]\\|]{5,}$/;
  131. // password
  132. var adminPass1 = $("input[name='adminPass']", form).first().val();
  133. if (adminPass1.length > 0 && !re_password.test(adminPass1)) {
  134. alert("The password you have entered is not valid, it must have at least 5 characters, 1 lowercase and 1 uppercase or number!");
  135. return false;
  136. }
  137. var adminPass2 = $("input[name='adminPass']", form).last().val();
  138. if (adminPass1 !== adminPass2) {
  139. alert("Passwords are different!");
  140. return false;
  141. }
  142. // RFCs mandate that a hostname's labels may contain only
  143. // the ASCII letters 'a' through 'z' (case-insensitive),
  144. // the digits '0' through '9', and the hyphen.
  145. // Hostname labels cannot begin or end with a hyphen.
  146. // No other symbols, punctuation characters, or blank spaces are permitted.
  147. // Negative lookbehind does not work in Javascript
  148. // var re_hostname = new RegExp('^(?!-)[A-Za-z0-9-]{1,32}(?<!-)$');
  149. var re_hostname = new RegExp('^(?!-)[A-Za-z0-9-]{0,31}[A-Za-z0-9]$');
  150. var hostname = $("input[name='hostname']", form).val();
  151. if (!re_hostname.test(hostname)) {
  152. alert("Hostname cannot be empty and may only contain the ASCII letters ('A' through 'Z' and 'a' through 'z'), the digits '0' through '9', and the hyphen ('-')! They can neither start or end with an hyphen.");
  153. return false;
  154. }
  155. return true;
  156. }
  157. function getValue(element) {
  158. if ($(element).attr("type") === "checkbox") {
  159. return $(element).prop("checked") ? 1 : 0;
  160. } else if ($(element).attr("type") === "radio") {
  161. if (!$(element).prop("checked")) {
  162. return null;
  163. }
  164. }
  165. return $(element).val();
  166. }
  167. function addValue(data, name, value) {
  168. // These fields will always be a list of values
  169. var is_group = [
  170. "ssid", "pass", "gw", "mask", "ip", "dns",
  171. "schEnabled", "schSwitch","schAction","schType","schHour","schMinute","schWDs","schUTC",
  172. "relayBoot", "relayPulse", "relayTime",
  173. "mqttGroup", "mqttGroupInv", "relayOnDisc",
  174. "dczRelayIdx", "dczMagnitude",
  175. "tspkRelay", "tspkMagnitude",
  176. "ledMode",
  177. "adminPass",
  178. "node", "key", "topic"
  179. ];
  180. if (name in data) {
  181. if (!Array.isArray(data[name])) {
  182. data[name] = [data[name]];
  183. }
  184. data[name].push(value);
  185. } else if (is_group.indexOf(name) >= 0) {
  186. data[name] = [value];
  187. } else {
  188. data[name] = value;
  189. }
  190. }
  191. function getData(form) {
  192. var data = {};
  193. // Populate data
  194. $("input,select", form).each(function() {
  195. var name = $(this).attr("name");
  196. var value = getValue(this);
  197. if (null !== value) {
  198. addValue(data, name, value);
  199. }
  200. });
  201. // Post process
  202. addValue(data, "schSwitch", 0xFF);
  203. delete data["filename"];
  204. delete data["rfbcode"];
  205. return data;
  206. }
  207. function randomString(length, chars) {
  208. var mask = "";
  209. if (chars.indexOf("a") > -1) { mask += "abcdefghijklmnopqrstuvwxyz"; }
  210. if (chars.indexOf("A") > -1) { mask += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  211. if (chars.indexOf("#") > -1) { mask += "0123456789"; }
  212. if (chars.indexOf("@") > -1) { mask += "ABCDEF"; }
  213. if (chars.indexOf("!") > -1) { mask += "~`!@#$%^&*()_+-={}[]:\";'<>?,./|\\"; }
  214. var result = "";
  215. for (var i = length; i > 0; --i) {
  216. result += mask[Math.round(Math.random() * (mask.length - 1))];
  217. }
  218. return result;
  219. }
  220. function generateAPIKey() {
  221. var apikey = randomString(16, "@#");
  222. $("input[name='apiKey']").val(apikey);
  223. return false;
  224. }
  225. function getJson(str) {
  226. try {
  227. return JSON.parse(str);
  228. } catch (e) {
  229. return false;
  230. }
  231. }
  232. // -----------------------------------------------------------------------------
  233. // Actions
  234. // -----------------------------------------------------------------------------
  235. function sendAction(action, data) {
  236. websock.send(JSON.stringify({action: action, data: data}));
  237. }
  238. function sendConfig(data) {
  239. websock.send(JSON.stringify({config: data}));
  240. }
  241. function resetOriginals() {
  242. $("input,select").each(function() {
  243. $(this).attr("original", $(this).val());
  244. });
  245. numReboot = numReconnect = numReload = 0;
  246. }
  247. function doReload(milliseconds) {
  248. setTimeout(function() {
  249. window.location.reload();
  250. }, parseInt(milliseconds, 10));
  251. }
  252. /**
  253. * Check a file object to see if it is a valid firmware image
  254. * The file first byte should be 0xE9
  255. * @param {file} file File object
  256. * @param {Function} callback Function to call back with the result
  257. */
  258. function checkFirmware(file, callback) {
  259. var reader = new FileReader();
  260. reader.onloadend = function(evt) {
  261. if (FileReader.DONE === evt.target.readyState) {
  262. callback(0xE9 === evt.target.result.charCodeAt(0));
  263. }
  264. };
  265. var blob = file.slice(0, 1);
  266. reader.readAsBinaryString(blob);
  267. }
  268. function doUpgrade() {
  269. var file = $("input[name='upgrade']")[0].files[0];
  270. if (typeof file === "undefined") {
  271. alert("First you have to select a file from your computer.");
  272. return false;
  273. }
  274. if (file.size > free_size) {
  275. alert("Image it too large to fit in the available space for OTA. Consider doing a two-step update.");
  276. return false;
  277. }
  278. checkFirmware(file, function(ok) {
  279. if (!ok) {
  280. alert("The file does not seem to be a valid firmware image.");
  281. return;
  282. }
  283. var data = new FormData();
  284. data.append("upgrade", file, file.name);
  285. $.ajax({
  286. // Your server script to process the upload
  287. url: urls.upgrade.href,
  288. type: "POST",
  289. // Form data
  290. data: data,
  291. // Tell jQuery not to process data or worry about content-type
  292. // You *must* include these options!
  293. cache: false,
  294. contentType: false,
  295. processData: false,
  296. success: function(data, text) {
  297. $("#upgrade-progress").hide();
  298. if ("OK" === data) {
  299. alert("Firmware image uploaded, board rebooting. This page will be refreshed in 5 seconds.");
  300. doReload(5000);
  301. } else {
  302. alert("There was an error trying to upload the new image, please try again (" + data + ").");
  303. }
  304. },
  305. // Custom XMLHttpRequest
  306. xhr: function() {
  307. $("#upgrade-progress").show();
  308. var myXhr = $.ajaxSettings.xhr();
  309. if (myXhr.upload) {
  310. // For handling the progress of the upload
  311. myXhr.upload.addEventListener("progress", function(e) {
  312. if (e.lengthComputable) {
  313. $("progress").attr({ value: e.loaded, max: e.total });
  314. }
  315. } , false);
  316. }
  317. return myXhr;
  318. }
  319. });
  320. });
  321. return false;
  322. }
  323. function doUpdatePassword() {
  324. var form = $("#formPassword");
  325. if (validateForm(form)) {
  326. sendConfig(getData(form));
  327. }
  328. return false;
  329. }
  330. function checkChanges() {
  331. if (numChanged > 0) {
  332. var response = window.confirm("Some changes have not been saved yet, do you want to save them first?");
  333. if (response) {
  334. doUpdate();
  335. }
  336. }
  337. }
  338. function doAction(question, action) {
  339. checkChanges();
  340. if (question) {
  341. var response = window.confirm(question);
  342. if (false === response) {
  343. return false;
  344. }
  345. }
  346. sendAction(action, {});
  347. doReload(5000);
  348. return false;
  349. }
  350. function doReboot(ask) {
  351. var question = (typeof ask === "undefined" || false === ask) ?
  352. null :
  353. "Are you sure you want to reboot the device?";
  354. return doAction(question, "reboot");
  355. }
  356. function doReconnect(ask) {
  357. var question = (typeof ask === "undefined" || false === ask) ?
  358. null :
  359. "Are you sure you want to disconnect from the current WIFI network?";
  360. return doAction(question, "reconnect");
  361. }
  362. function doUpdate() {
  363. var form = $("#formSave");
  364. if (validateForm(form)) {
  365. // Get data
  366. sendConfig(getData(form));
  367. // Empty special fields
  368. $(".pwrExpected").val(0);
  369. $("input[name='pwrResetCalibration']").prop("checked", false);
  370. $("input[name='pwrResetE']").prop("checked", false);
  371. // Change handling
  372. numChanged = 0;
  373. setTimeout(function() {
  374. var response;
  375. if (numReboot > 0) {
  376. response = window.confirm("You have to reboot the board for the changes to take effect, do you want to do it now?");
  377. if (response) { doReboot(false); }
  378. } else if (numReconnect > 0) {
  379. response = window.confirm("You have to reconnect to the WiFi for the changes to take effect, do you want to do it now?");
  380. if (response) { doReconnect(false); }
  381. } else if (numReload > 0) {
  382. response = window.confirm("You have to reload the page to see the latest changes, do you want to do it now?");
  383. if (response) { doReload(0); }
  384. }
  385. resetOriginals();
  386. }, 1000);
  387. }
  388. return false;
  389. }
  390. function doBackup() {
  391. document.getElementById("downloader").src = urls.config.href;
  392. return false;
  393. }
  394. function onFileUpload(event) {
  395. var inputFiles = this.files;
  396. if (typeof inputFiles === "undefined" || inputFiles.length === 0) {
  397. return false;
  398. }
  399. var inputFile = inputFiles[0];
  400. this.value = "";
  401. var response = window.confirm("Previous settings will be overwritten. Are you sure you want to restore this settings?");
  402. if (!response) {
  403. return false;
  404. }
  405. var reader = new FileReader();
  406. reader.onload = function(e) {
  407. var data = getJson(e.target.result);
  408. if (data) {
  409. sendAction("restore", data);
  410. } else {
  411. window.alert(messages[4]);
  412. }
  413. };
  414. reader.readAsText(inputFile);
  415. return false;
  416. }
  417. function doRestore() {
  418. if (typeof window.FileReader !== "function") {
  419. alert("The file API isn't supported on this browser yet.");
  420. } else {
  421. $("#uploader").click();
  422. }
  423. return false;
  424. }
  425. function doFactoryReset() {
  426. var response = window.confirm("Are you sure you want to restore to factory settings?");
  427. if (response === false) {
  428. return false;
  429. }
  430. websock.send(JSON.stringify({"action": "factory_reset"}));
  431. doReload(5000);
  432. return false;
  433. }
  434. function doToggle(id, value) {
  435. sendAction("relay", {id: id, status: value ? 1 : 0 });
  436. return false;
  437. }
  438. function doScan() {
  439. $("#scanResult").html("");
  440. $("div.scan.loading").show();
  441. sendAction("scan", {});
  442. return false;
  443. }
  444. function doHAConfig() {
  445. $("#haConfig").html("");
  446. sendAction("haconfig", {});
  447. return false;
  448. }
  449. function doDebugCommand() {
  450. var el = $("input[name='dbgcmd']");
  451. var command = el.val();
  452. el.val("");
  453. sendAction("dbgcmd", {command: command});
  454. return false;
  455. }
  456. function doDebugClear() {
  457. $("#weblog").text("");
  458. return false;
  459. }
  460. <!-- removeIf(!rfm69)-->
  461. function doClearCounts() {
  462. websock.send(JSON.stringify({'action': 'clear-counts'}));
  463. return false;
  464. }
  465. function doFilter(e) {
  466. var index = packets.cell(this).index();
  467. if (index == 'undefined') return;
  468. var c = index.column;
  469. var column = packets.column(c);
  470. if (filters[c]) {
  471. filters[c] = false;
  472. column.search("");
  473. $(column.header()).removeClass("filtered");
  474. } else {
  475. filters[c] = true;
  476. var data = packets.row(this).data();
  477. if (e.which == 1) {
  478. column.search('^' + data[c] + '$', true, false );
  479. } else {
  480. column.search('^((?!(' + data[c] + ')).)*$', true, false );
  481. }
  482. $(column.header()).addClass("filtered");
  483. }
  484. column.draw();
  485. return false;
  486. }
  487. function doClearFilters() {
  488. for (var i = 0; i < packets.columns()[0].length; i++) {
  489. if (filters[i]) {
  490. filters[i] = false;
  491. var column = packets.column(i);
  492. column.search("");
  493. $(column.header()).removeClass("filtered");
  494. column.draw();
  495. }
  496. }
  497. return false;
  498. }
  499. <!-- endRemoveIf(!rfm69)-->
  500. // -----------------------------------------------------------------------------
  501. // Visualization
  502. // -----------------------------------------------------------------------------
  503. function toggleMenu() {
  504. $("#layout").toggleClass("active");
  505. $("#menu").toggleClass("active");
  506. $("#menuLink").toggleClass("active");
  507. }
  508. function showPanel() {
  509. $(".panel").hide();
  510. if ($("#layout").hasClass("active")) { toggleMenu(); }
  511. $("#" + $(this).attr("data")).show();
  512. }
  513. // -----------------------------------------------------------------------------
  514. // Relays & magnitudes mapping
  515. // -----------------------------------------------------------------------------
  516. function createRelayList(data, container, template_name) {
  517. var current = $("#" + container + " > div").length;
  518. if (current > 0) { return; }
  519. var template = $("#" + template_name + " .pure-g")[0];
  520. for (var i in data) {
  521. var line = $(template).clone();
  522. $("label", line).html("Switch #" + i);
  523. $("input", line).attr("tabindex", 40 + i).val(data[i]);
  524. line.appendTo("#" + container);
  525. }
  526. }
  527. <!-- removeIf(!sensor)-->
  528. function createMagnitudeList(data, container, template_name) {
  529. var current = $("#" + container + " > div").length;
  530. if (current > 0) { return; }
  531. var template = $("#" + template_name + " .pure-g")[0];
  532. for (var i in data) {
  533. var magnitude = data[i];
  534. var line = $(template).clone();
  535. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  536. $("div.hint", line).html(magnitude.name);
  537. $("input", line).attr("tabindex", 40 + i).val(magnitude.idx);
  538. line.appendTo("#" + container);
  539. }
  540. }
  541. <!-- endRemoveIf(!sensor)-->
  542. // -----------------------------------------------------------------------------
  543. // RFM69
  544. // -----------------------------------------------------------------------------
  545. <!-- removeIf(!rfm69)-->
  546. function addMapping() {
  547. var template = $("#nodeTemplate .pure-g")[0];
  548. var line = $(template).clone();
  549. var tabindex = $("#mapping > div").length * 3 + 50;
  550. $(line).find("input").each(function() {
  551. $(this).attr("tabindex", tabindex++);
  552. });
  553. $(line).find("button").on('click', delMapping);
  554. line.appendTo("#mapping");
  555. }
  556. function delMapping() {
  557. var parent = $(this).parent().parent();
  558. $(parent).remove();
  559. }
  560. <!-- endRemoveIf(!rfm69)-->
  561. // -----------------------------------------------------------------------------
  562. // Wifi
  563. // -----------------------------------------------------------------------------
  564. function delNetwork() {
  565. var parent = $(this).parents(".pure-g");
  566. $(parent).remove();
  567. }
  568. function moreNetwork() {
  569. var parent = $(this).parents(".pure-g");
  570. $(".more", parent).toggle();
  571. }
  572. function addNetwork() {
  573. var numNetworks = $("#networks > div").length;
  574. if (numNetworks >= maxNetworks) {
  575. alert("Max number of networks reached");
  576. return null;
  577. }
  578. var tabindex = 200 + numNetworks * 10;
  579. var template = $("#networkTemplate").children();
  580. var line = $(template).clone();
  581. $(line).find("input").each(function() {
  582. $(this).attr("tabindex", tabindex);
  583. tabindex++;
  584. });
  585. $(line).find(".button-del-network").on("click", delNetwork);
  586. $(line).find(".button-more-network").on("click", moreNetwork);
  587. line.appendTo("#networks");
  588. return line;
  589. }
  590. // -----------------------------------------------------------------------------
  591. // Relays scheduler
  592. // -----------------------------------------------------------------------------
  593. function delSchedule() {
  594. var parent = $(this).parents(".pure-g");
  595. $(parent).remove();
  596. }
  597. function moreSchedule() {
  598. var parent = $(this).parents(".pure-g");
  599. $("div.more", parent).toggle();
  600. }
  601. function addSchedule(event) {
  602. var numSchedules = $("#schedules > div").length;
  603. if (numSchedules >= maxSchedules) {
  604. alert("Max number of schedules reached");
  605. return null;
  606. }
  607. var tabindex = 200 + numSchedules * 10;
  608. var template = $("#scheduleTemplate").children();
  609. var line = $(template).clone();
  610. var type = (1 === event.data.schType) ? "switch" : "light";
  611. template = $("#" + type + "ActionTemplate").children();
  612. var actionLine = template.clone();
  613. $(line).find("#schActionDiv").append(actionLine);
  614. $(line).find("input").each(function() {
  615. $(this).attr("tabindex", tabindex);
  616. tabindex++;
  617. });
  618. $(line).find(".button-del-schedule").on("click", delSchedule);
  619. $(line).find(".button-more-schedule").on("click", moreSchedule);
  620. line.appendTo("#schedules");
  621. $(line).find("input[type='checkbox']").prop("checked", false);
  622. initCheckboxes();
  623. return line;
  624. }
  625. // -----------------------------------------------------------------------------
  626. // Relays
  627. // -----------------------------------------------------------------------------
  628. function initRelays(data) {
  629. var current = $("#relays > div").length;
  630. if (current > 0) { return; }
  631. var template = $("#relayTemplate .pure-g")[0];
  632. for (var i=0; i<data.length; i++) {
  633. // Add relay fields
  634. var line = $(template).clone();
  635. $(".id", line).html(i);
  636. $(":checkbox", line).prop('checked', data[i]).attr("data", i);
  637. line.appendTo("#relays");
  638. // Populate the relay SELECTs
  639. $("select.isrelay").append(
  640. $("<option></option>").attr("value",i).text("Switch #" + i));
  641. }
  642. }
  643. function initCheckboxes() {
  644. var setCheckbox = function(element, value) {
  645. var container = $(".toggle-container", $(element));
  646. if (value) {
  647. container.css("clipPath", "inset(0 0 0 50%)");
  648. container.css("backgroundColor", "#00c000");
  649. } else {
  650. container.css("clipPath", "inset(0 50% 0 0)");
  651. container.css("backgroundColor", "#c00000");
  652. }
  653. }
  654. $(".checkbox-container")
  655. .each(function() {
  656. var status = $(this).next().prop('checked');
  657. setCheckbox(this, status);
  658. })
  659. .off('click')
  660. .on('click', function() {
  661. var checkbox = $(this).next();
  662. var status = checkbox.prop('checked');
  663. status = !status;
  664. checkbox.prop('checked', status);
  665. setCheckbox(this, status);
  666. if ("relay" == checkbox.attr('name')) {
  667. var id = parseInt(checkbox.attr('data'), 10);
  668. doToggle(id, status);
  669. }
  670. });
  671. }
  672. function createCheckboxes() {
  673. $("input[type='checkbox']").each(function() {
  674. var text_on = $(this).attr("on") || "YES";
  675. var text_off = $(this).attr("off") || "NO";
  676. var toggles = "<div class=\"toggle\"><p>" + text_on + "</p></div><div class=\"toggle\"><p>" + text_off + "</p></div>";
  677. var content = "<div class=\"checkbox-container\"><div class=\"inner-container\">" + toggles
  678. + "</div><div class=\"inner-container toggle-container\">" + toggles + "</div></div>";
  679. $(this).before(content).hide();
  680. });
  681. }
  682. function initRelayConfig(data) {
  683. var current = $("#relayConfig > div").length;
  684. if (current > 0) { return; }
  685. var template = $("#relayConfigTemplate").children();
  686. for (var i in data) {
  687. var relay = data[i];
  688. var line = $(template).clone();
  689. $("span.gpio", line).html(relay.gpio);
  690. $("span.id", line).html(i);
  691. $("select[name='relayBoot']", line).val(relay.boot);
  692. $("select[name='relayPulse']", line).val(relay.pulse);
  693. $("input[name='relayTime']", line).val(relay.pulse_ms);
  694. $("input[name='mqttGroup']", line).val(relay.group);
  695. $("select[name='mqttGroupInv']", line).val(relay.group_inv);
  696. $("select[name='relayOnDisc']", line).val(relay.on_disc);
  697. line.appendTo("#relayConfig");
  698. }
  699. }
  700. // -----------------------------------------------------------------------------
  701. // Sensors & Magnitudes
  702. // -----------------------------------------------------------------------------
  703. <!-- removeIf(!sensor)-->
  704. function initMagnitudes(data) {
  705. // check if already initialized
  706. var done = $("#magnitudes > div").length;
  707. if (done > 0) { return; }
  708. // add templates
  709. var template = $("#magnitudeTemplate").children();
  710. for (var i in data) {
  711. var magnitude = data[i];
  712. var line = $(template).clone();
  713. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  714. $("div.hint", line).html(magnitude.description);
  715. $("input", line).attr("data", i);
  716. line.appendTo("#magnitudes");
  717. }
  718. }
  719. <!-- endRemoveIf(!sensor)-->
  720. // -----------------------------------------------------------------------------
  721. // Lights
  722. // -----------------------------------------------------------------------------
  723. <!-- removeIf(!light)-->
  724. function initColorRGB() {
  725. // check if already initialized
  726. var done = $("#colors > div").length;
  727. if (done > 0) { return; }
  728. // add template
  729. var template = $("#colorRGBTemplate").children();
  730. var line = $(template).clone();
  731. line.appendTo("#colors");
  732. // init color wheel
  733. $("input[name='color']").wheelColorPicker({
  734. sliders: "wrgbp"
  735. }).on("sliderup", function() {
  736. var value = $(this).wheelColorPicker("getValue", "css");
  737. sendAction("color", {rgb: value});
  738. });
  739. // init bright slider
  740. $("#brightness").on("change", function() {
  741. var value = $(this).val();
  742. var parent = $(this).parents(".pure-g");
  743. $("span", parent).html(value);
  744. sendAction("color", {brightness: value});
  745. });
  746. }
  747. function initCCT() {
  748. // check if already initialized
  749. var done = $("#cct > div").length;
  750. if (done > 0) { return; }
  751. $("#miredsTemplate").children().clone().appendTo("#cct");
  752. $("#mireds").on("change", function() {
  753. var value = $(this).val();
  754. var parent = $(this).parents(".pure-g");
  755. $("span", parent).html(value);
  756. sendAction("mireds", {mireds: value});
  757. });
  758. }
  759. function initColorHSV() {
  760. // check if already initialized
  761. var done = $("#colors > div").length;
  762. if (done > 0) { return; }
  763. // add template
  764. var template = $("#colorHSVTemplate").children();
  765. var line = $(template).clone();
  766. line.appendTo("#colors");
  767. // init color wheel
  768. $("input[name='color']").wheelColorPicker({
  769. sliders: "whsvp"
  770. }).on("sliderup", function() {
  771. var color = $(this).wheelColorPicker("getColor");
  772. var value = parseInt(color.h * 360, 10) + "," + parseInt(color.s * 100, 10) + "," + parseInt(color.v * 100, 10);
  773. sendAction("color", {hsv: value});
  774. });
  775. }
  776. function initChannels(num) {
  777. // check if already initialized
  778. var done = $("#channels > div").length > 0;
  779. if (done) { return; }
  780. // does it have color channels?
  781. var colors = $("#colors > div").length > 0;
  782. // calculate channels to create
  783. var max = num;
  784. if (colors) {
  785. max = num % 3;
  786. if ((max > 0) & useWhite) {
  787. max--;
  788. if (useCCT) {
  789. max--;
  790. }
  791. }
  792. }
  793. var start = num - max;
  794. var onChannelSliderChange = function() {
  795. var id = $(this).attr("data");
  796. var value = $(this).val();
  797. var parent = $(this).parents(".pure-g");
  798. $("span", parent).html(value);
  799. sendAction("channel", {id: id, value: value});
  800. };
  801. // add templates
  802. var i = 0;
  803. var template = $("#channelTemplate").children();
  804. for (i=0; i<max; i++) {
  805. var channel_id = start + i;
  806. var line = $(template).clone();
  807. $("span.slider", line).attr("data", channel_id);
  808. $("input.slider", line).attr("data", channel_id).on("change", onChannelSliderChange);
  809. $("label", line).html("Channel #" + channel_id);
  810. line.appendTo("#channels");
  811. }
  812. for (i=0; i<num; i++) {
  813. $("select.islight").append(
  814. $("<option></option>").attr("value",i).text("Channel #" + i));
  815. }
  816. }
  817. <!-- endRemoveIf(!light)-->
  818. // -----------------------------------------------------------------------------
  819. // RFBridge
  820. // -----------------------------------------------------------------------------
  821. <!-- removeIf(!rfbridge)-->
  822. function rfbLearn() {
  823. var parent = $(this).parents(".pure-g");
  824. var input = $("input", parent);
  825. sendAction("rfblearn", {id: input.attr("data-id"), status: input.attr("data-status")});
  826. }
  827. function rfbForget() {
  828. var parent = $(this).parents(".pure-g");
  829. var input = $("input", parent);
  830. sendAction("rfbforget", {id: input.attr("data-id"), status: input.attr("data-status")});
  831. }
  832. function rfbSend() {
  833. var parent = $(this).parents(".pure-g");
  834. var input = $("input", parent);
  835. sendAction("rfbsend", {id: input.attr("data-id"), status: input.attr("data-status"), data: input.val()});
  836. }
  837. function addRfbNode() {
  838. var numNodes = $("#rfbNodes > legend").length;
  839. var template = $("#rfbNodeTemplate").children();
  840. var line = $(template).clone();
  841. var status = true;
  842. $("span", line).html(numNodes);
  843. $(line).find("input").each(function() {
  844. $(this).attr("data-id", numNodes);
  845. $(this).attr("data-status", status ? 1 : 0);
  846. status = !status;
  847. });
  848. $(line).find(".button-rfb-learn").on("click", rfbLearn);
  849. $(line).find(".button-rfb-forget").on("click", rfbForget);
  850. $(line).find(".button-rfb-send").on("click", rfbSend);
  851. line.appendTo("#rfbNodes");
  852. return line;
  853. }
  854. <!-- endRemoveIf(!rfbridge)-->
  855. // -----------------------------------------------------------------------------
  856. // Processing
  857. // -----------------------------------------------------------------------------
  858. function processData(data) {
  859. // title
  860. if ("app_name" in data) {
  861. var title = data.app_name;
  862. if ("app_version" in data) {
  863. title = title + " " + data.app_version;
  864. }
  865. $("span[name=title]").html(title);
  866. if ("hostname" in data) {
  867. title = data.hostname + " - " + title;
  868. }
  869. document.title = title;
  870. }
  871. Object.keys(data).forEach(function(key) {
  872. var i;
  873. var value = data[key];
  874. // ---------------------------------------------------------------------
  875. // Web mode
  876. // ---------------------------------------------------------------------
  877. if ("webMode" === key) {
  878. password = (1 === value);
  879. $("#layout").toggle(!password);
  880. $("#password").toggle(password);
  881. }
  882. // ---------------------------------------------------------------------
  883. // Actions
  884. // ---------------------------------------------------------------------
  885. if ("action" === key) {
  886. if ("reload" === data.action) { doReload(1000); }
  887. return;
  888. }
  889. // ---------------------------------------------------------------------
  890. // RFBridge
  891. // ---------------------------------------------------------------------
  892. <!-- removeIf(!rfbridge)-->
  893. if ("rfbCount" === key) {
  894. for (i=0; i<data.rfbCount; i++) { addRfbNode(); }
  895. return;
  896. }
  897. if ("rfbrawVisible" === key) {
  898. $("input[name='rfbcode']").attr("maxlength", 116);
  899. }
  900. if ("rfb" === key) {
  901. var nodes = data.rfb;
  902. for (i in nodes) {
  903. var node = nodes[i];
  904. $("input[name='rfbcode'][data-id='" + node.id + "'][data-status='" + node.status + "']").val(node.data);
  905. }
  906. return;
  907. }
  908. <!-- endRemoveIf(!rfbridge)-->
  909. // ---------------------------------------------------------------------
  910. // RFM69
  911. // ---------------------------------------------------------------------
  912. <!-- removeIf(!rfm69)-->
  913. if (key == "packet") {
  914. var packet = data.packet;
  915. var d = new Date();
  916. packets.row.add([
  917. d.toLocaleTimeString('en-US', { hour12: false }),
  918. packet.senderID,
  919. packet.packetID,
  920. packet.targetID,
  921. packet.name,
  922. packet.value,
  923. packet.rssi,
  924. packet.duplicates,
  925. packet.missing,
  926. ]).draw(false);
  927. return;
  928. }
  929. if (key == "mapping") {
  930. for (var i in data.mapping) {
  931. // add a new row
  932. addMapping();
  933. // get group
  934. var line = $("#mapping .pure-g")[i];
  935. // fill in the blanks
  936. var mapping = data.mapping[i];
  937. Object.keys(mapping).forEach(function(key) {
  938. var id = "input[name=" + key + "]";
  939. if ($(id, line).length) $(id, line).val(mapping[key]).attr("original", mapping[key]);
  940. });
  941. }
  942. return;
  943. }
  944. <!-- endRemoveIf(!rfm69)-->
  945. // ---------------------------------------------------------------------
  946. // Lights
  947. // ---------------------------------------------------------------------
  948. <!-- removeIf(!light)-->
  949. if ("rgb" === key) {
  950. initColorRGB();
  951. $("input[name='color']").wheelColorPicker("setValue", value, true);
  952. return;
  953. }
  954. if ("hsv" === key) {
  955. initColorHSV();
  956. // wheelColorPicker expects HSV to be between 0 and 1 all of them
  957. var chunks = value.split(",");
  958. var obj = {};
  959. obj.h = chunks[0] / 360;
  960. obj.s = chunks[1] / 100;
  961. obj.v = chunks[2] / 100;
  962. $("input[name='color']").wheelColorPicker("setColor", obj);
  963. return;
  964. }
  965. if ("brightness" === key) {
  966. $("#brightness").val(value);
  967. $("span.brightness").html(value);
  968. return;
  969. }
  970. if ("channels" === key) {
  971. var len = value.length;
  972. initChannels(len);
  973. for (i in value) {
  974. var ch = value[i];
  975. $("input.slider[data=" + i + "]").val(ch);
  976. $("span.slider[data=" + i + "]").html(ch);
  977. }
  978. return;
  979. }
  980. if ("mireds" === key) {
  981. $("#mireds").val(value);
  982. $("span.mireds").html(value);
  983. return;
  984. }
  985. if ("useWhite" === key) {
  986. useWhite = value;
  987. }
  988. if ("useCCT" === key) {
  989. initCCT();
  990. useCCT = value;
  991. }
  992. <!-- endRemoveIf(!light)-->
  993. // ---------------------------------------------------------------------
  994. // Sensors & Magnitudes
  995. // ---------------------------------------------------------------------
  996. <!-- removeIf(!sensor)-->
  997. if ("magnitudes" === key) {
  998. initMagnitudes(value);
  999. for (i in value) {
  1000. var magnitude = value[i];
  1001. var error = magnitude.error || 0;
  1002. var text = (0 === error) ?
  1003. magnitude.value + magnitude.units :
  1004. magnitudeError(error);
  1005. var element = $("input[name='magnitude'][data='" + i + "']");
  1006. element.val(text);
  1007. $("div.hint", element.parent().parent()).html(magnitude.description);
  1008. }
  1009. return;
  1010. }
  1011. <!-- endRemoveIf(!sensor)-->
  1012. // ---------------------------------------------------------------------
  1013. // WiFi
  1014. // ---------------------------------------------------------------------
  1015. if ("maxNetworks" === key) {
  1016. maxNetworks = parseInt(value, 10);
  1017. return;
  1018. }
  1019. if ("wifi" === key) {
  1020. for (i in value) {
  1021. var wifi = value[i];
  1022. var nwk_line = addNetwork();
  1023. Object.keys(wifi).forEach(function(key) {
  1024. $("input[name='" + key + "']", nwk_line).val(wifi[key]);
  1025. });
  1026. }
  1027. return;
  1028. }
  1029. if ("scanResult" === key) {
  1030. $("div.scan.loading").hide();
  1031. $("#scanResult").show();
  1032. }
  1033. // -----------------------------------------------------------------------------
  1034. // Home Assistant
  1035. // -----------------------------------------------------------------------------
  1036. if ("haConfig" === key) {
  1037. $("#haConfig").show();
  1038. }
  1039. // -----------------------------------------------------------------------------
  1040. // Relays scheduler
  1041. // -----------------------------------------------------------------------------
  1042. if ("maxSchedules" === key) {
  1043. maxSchedules = parseInt(value, 10);
  1044. return;
  1045. }
  1046. if ("schedule" === key) {
  1047. for (i in value) {
  1048. var schedule = value[i];
  1049. var sch_line = addSchedule({ data: {schType: schedule["schType"] }});
  1050. Object.keys(schedule).forEach(function(key) {
  1051. var sch_value = schedule[key];
  1052. $("input[name='" + key + "']", sch_line).val(sch_value);
  1053. $("select[name='" + key + "']", sch_line).prop("value", sch_value);
  1054. $("input[type='checkbox'][name='" + key + "']", sch_line).prop("checked", sch_value);
  1055. });
  1056. }
  1057. return;
  1058. }
  1059. // ---------------------------------------------------------------------
  1060. // Relays
  1061. // ---------------------------------------------------------------------
  1062. if ("relayStatus" === key) {
  1063. initRelays(value);
  1064. for (i in value) {
  1065. $("input[name='relay'][data='" + i + "']").prop("checked", value[i]);
  1066. }
  1067. return;
  1068. }
  1069. // Relay configuration
  1070. if ("relayConfig" === key) {
  1071. initRelayConfig(value);
  1072. return;
  1073. }
  1074. // ---------------------------------------------------------------------
  1075. // Domoticz
  1076. // ---------------------------------------------------------------------
  1077. // Domoticz - Relays
  1078. if ("dczRelays" === key) {
  1079. createRelayList(value, "dczRelays", "dczRelayTemplate");
  1080. return;
  1081. }
  1082. // Domoticz - Magnitudes
  1083. <!-- removeIf(!sensor)-->
  1084. if ("dczMagnitudes" === key) {
  1085. createMagnitudeList(value, "dczMagnitudes", "dczMagnitudeTemplate");
  1086. return;
  1087. }
  1088. <!-- endRemoveIf(!sensor)-->
  1089. // ---------------------------------------------------------------------
  1090. // Thingspeak
  1091. // ---------------------------------------------------------------------
  1092. // Thingspeak - Relays
  1093. if ("tspkRelays" === key) {
  1094. createRelayList(value, "tspkRelays", "tspkRelayTemplate");
  1095. return;
  1096. }
  1097. // Thingspeak - Magnitudes
  1098. <!-- removeIf(!sensor)-->
  1099. if ("tspkMagnitudes" === key) {
  1100. createMagnitudeList(value, "tspkMagnitudes", "tspkMagnitudeTemplate");
  1101. return;
  1102. }
  1103. <!-- endRemoveIf(!sensor)-->
  1104. // ---------------------------------------------------------------------
  1105. // General
  1106. // ---------------------------------------------------------------------
  1107. // Messages
  1108. if ("message" === key) {
  1109. window.alert(messages[value]);
  1110. return;
  1111. }
  1112. // Web log
  1113. if ("weblog" === key) {
  1114. $("#weblog").append(new Text(value));
  1115. $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height());
  1116. return;
  1117. }
  1118. // Enable options
  1119. var position = key.indexOf("Visible");
  1120. if (position > 0 && position === key.length - 7) {
  1121. var module = key.slice(0,-7);
  1122. $(".module-" + module).css("display", "inherit");
  1123. return;
  1124. }
  1125. if ("deviceip" === key) {
  1126. var a_href = $("span[name='" + key + "']").parent();
  1127. a_href.attr("href", "//" + value);
  1128. a_href.next().attr("href", "telnet://" + value);
  1129. }
  1130. if ("now" === key) {
  1131. now = value;
  1132. return;
  1133. }
  1134. if ("free_size" === key) {
  1135. free_size = parseInt(value, 10);
  1136. }
  1137. // Pre-process
  1138. if ("mqttStatus" === key) {
  1139. value = value ? "CONNECTED" : "NOT CONNECTED";
  1140. }
  1141. if ("ntpStatus" === key) {
  1142. value = value ? "SYNC'D" : "NOT SYNC'D";
  1143. }
  1144. if ("uptime" === key) {
  1145. ago = 0;
  1146. var uptime = parseInt(value, 10);
  1147. var seconds = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1148. var minutes = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1149. var hours = uptime % 24; uptime = parseInt(uptime / 24, 10);
  1150. var days = uptime;
  1151. value = days + "d " + zeroPad(hours, 2) + "h " + zeroPad(minutes, 2) + "m " + zeroPad(seconds, 2) + "s";
  1152. }
  1153. // ---------------------------------------------------------------------
  1154. // Matching
  1155. // ---------------------------------------------------------------------
  1156. var pre;
  1157. var post;
  1158. // Look for INPUTs
  1159. var input = $("input[name='" + key + "']");
  1160. if (input.length > 0) {
  1161. if (input.attr("type") === "checkbox") {
  1162. input.prop("checked", value);
  1163. } else if (input.attr("type") === "radio") {
  1164. input.val([value]);
  1165. } else {
  1166. pre = input.attr("pre") || "";
  1167. post = input.attr("post") || "";
  1168. input.val(pre + value + post);
  1169. }
  1170. }
  1171. // Look for SPANs
  1172. var span = $("span[name='" + key + "']");
  1173. if (span.length > 0) {
  1174. pre = span.attr("pre") || "";
  1175. post = span.attr("post") || "";
  1176. span.html(pre + value + post);
  1177. }
  1178. // Look for SELECTs
  1179. var select = $("select[name='" + key + "']");
  1180. if (select.length > 0) {
  1181. select.val(value);
  1182. }
  1183. });
  1184. // Auto generate an APIKey if none defined yet
  1185. if ($("input[name='apiKey']").val() === "") {
  1186. generateAPIKey();
  1187. }
  1188. resetOriginals();
  1189. initCheckboxes();
  1190. }
  1191. function hasChanged() {
  1192. var newValue, originalValue;
  1193. if ($(this).attr("type") === "checkbox") {
  1194. newValue = $(this).prop("checked");
  1195. originalValue = ($(this).attr("original") === "true");
  1196. } else {
  1197. newValue = $(this).val();
  1198. originalValue = $(this).attr("original");
  1199. }
  1200. var hasChanged = $(this).attr("hasChanged") || 0;
  1201. var action = $(this).attr("action");
  1202. if (typeof originalValue === "undefined") { return; }
  1203. if ("none" === action) { return; }
  1204. if (newValue !== originalValue) {
  1205. if (0 === hasChanged) {
  1206. ++numChanged;
  1207. if ("reconnect" === action) { ++numReconnect; }
  1208. if ("reboot" === action) { ++numReboot; }
  1209. if ("reload" === action) { ++numReload; }
  1210. $(this).attr("hasChanged", 1);
  1211. }
  1212. } else {
  1213. if (1 === hasChanged) {
  1214. --numChanged;
  1215. if ("reconnect" === action) { --numReconnect; }
  1216. if ("reboot" === action) { --numReboot; }
  1217. if ("reload" === action) { --numReload; }
  1218. $(this).attr("hasChanged", 0);
  1219. }
  1220. }
  1221. }
  1222. // -----------------------------------------------------------------------------
  1223. // Init & connect
  1224. // -----------------------------------------------------------------------------
  1225. function initUrls(root) {
  1226. var paths = ["ws", "upgrade", "config", "auth"];
  1227. urls["root"] = root;
  1228. paths.forEach(function(path) {
  1229. urls[path] = new URL(path, root);
  1230. urls[path].protocol = root.protocol;
  1231. });
  1232. if (root.protocol == "https:") {
  1233. urls.ws.protocol = "wss:";
  1234. } else {
  1235. urls.ws.protocol = "ws:";
  1236. }
  1237. }
  1238. function connectToURL(url) {
  1239. initUrls(url);
  1240. $.ajax({
  1241. 'method': 'GET',
  1242. 'crossDomain': true,
  1243. 'url': urls.auth.href,
  1244. 'xhrFields': { 'withCredentials': true }
  1245. }).done(function(data) {
  1246. if (websock) { websock.close(); }
  1247. websock = new WebSocket(urls.ws.href);
  1248. websock.onmessage = function(evt) {
  1249. var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
  1250. if (data) {
  1251. processData(data);
  1252. }
  1253. };
  1254. }).fail(function() {
  1255. // Nothing to do, reload page and retry
  1256. });
  1257. }
  1258. function connect(host) {
  1259. if (!host.startsWith("http:") && !host.startsWith("https:")) {
  1260. host = "http://" + host;
  1261. }
  1262. connectToURL(new URL(host));
  1263. }
  1264. function connectToCurrentURL() {
  1265. connectToURL(new URL(window.location));
  1266. }
  1267. function getParameterByName(name) {
  1268. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  1269. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  1270. }
  1271. $(function() {
  1272. initMessages();
  1273. loadTimeZones();
  1274. createCheckboxes();
  1275. setInterval(function() { keepTime(); }, 1000);
  1276. $("#menuLink").on("click", toggleMenu);
  1277. $(".pure-menu-link").on("click", showPanel);
  1278. $("progress").attr({ value: 0, max: 100 });
  1279. $(".button-update").on("click", doUpdate);
  1280. $(".button-update-password").on("click", doUpdatePassword);
  1281. $(".button-reboot").on("click", doReboot);
  1282. $(".button-reconnect").on("click", doReconnect);
  1283. $(".button-wifi-scan").on("click", doScan);
  1284. $(".button-ha-config").on("click", doHAConfig);
  1285. $(".button-dbgcmd").on("click", doDebugCommand);
  1286. $("input[name='dbgcmd']").enterKey(doDebugCommand);
  1287. $(".button-dbg-clear").on("click", doDebugClear);
  1288. $(".button-settings-backup").on("click", doBackup);
  1289. $(".button-settings-restore").on("click", doRestore);
  1290. $(".button-settings-factory").on("click", doFactoryReset);
  1291. $("#uploader").on("change", onFileUpload);
  1292. $(".button-upgrade").on("click", doUpgrade);
  1293. $(".button-apikey").on("click", generateAPIKey);
  1294. $(".button-upgrade-browse").on("click", function() {
  1295. $("input[name='upgrade']")[0].click();
  1296. return false;
  1297. });
  1298. $("input[name='upgrade']").change(function (){
  1299. var file = this.files[0];
  1300. $("input[name='filename']").val(file.name);
  1301. });
  1302. $(".button-add-network").on("click", function() {
  1303. $(".more", addNetwork()).toggle();
  1304. });
  1305. $(".button-add-switch-schedule").on("click", { schType: 1 }, addSchedule);
  1306. <!-- removeIf(!light)-->
  1307. $(".button-add-light-schedule").on("click", { schType: 2 }, addSchedule);
  1308. <!-- endRemoveIf(!light)-->
  1309. <!-- removeIf(!rfm69)-->
  1310. $(".button-add-mapping").on('click', addMapping);
  1311. $(".button-del-mapping").on('click', delMapping);
  1312. $(".button-clear-counts").on('click', doClearCounts);
  1313. $(".button-clear-filters").on('click', doClearFilters);
  1314. $('#packets tbody').on('mousedown', 'td', doFilter);
  1315. packets = $('#packets').DataTable({
  1316. "paging": false
  1317. });
  1318. for (var i = 0; i < packets.columns()[0].length; i++) {
  1319. filters[i] = false;
  1320. }
  1321. <!-- endRemoveIf(!rfm69)-->
  1322. $(document).on("change", "input", hasChanged);
  1323. $(document).on("change", "select", hasChanged);
  1324. // don't autoconnect when opening from filesystem
  1325. if (window.location.protocol === "file:") { return; }
  1326. // Check host param in query string
  1327. if (host = getParameterByName('host')) {
  1328. connect(host);
  1329. } else {
  1330. connectToCurrentURL();
  1331. }
  1332. });