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.

1668 lines
48 KiB

8 years ago
7 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
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 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
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
7 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
6 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. if (0xE9 !== evt.target.result.charCodeAt(0)) callback(false);
  263. if (0x03 !== evt.target.result.charCodeAt(2)) {
  264. var response = window.confirm("Binary image is not using DOUT flash mode. This might cause resets in some devices. Press OK to continue.");
  265. callback(response);
  266. } else {
  267. callback(true);
  268. }
  269. }
  270. };
  271. var blob = file.slice(0, 3);
  272. reader.readAsBinaryString(blob);
  273. }
  274. function doUpgrade() {
  275. var file = $("input[name='upgrade']")[0].files[0];
  276. if (typeof file === "undefined") {
  277. alert("First you have to select a file from your computer.");
  278. return false;
  279. }
  280. if (file.size > free_size) {
  281. alert("Image it too large to fit in the available space for OTA. Consider doing a two-step update.");
  282. return false;
  283. }
  284. checkFirmware(file, function(ok) {
  285. if (!ok) {
  286. alert("The file does not seem to be a valid firmware image.");
  287. return;
  288. }
  289. var data = new FormData();
  290. data.append("upgrade", file, file.name);
  291. $.ajax({
  292. // Your server script to process the upload
  293. url: urls.upgrade.href,
  294. type: "POST",
  295. // Form data
  296. data: data,
  297. // Tell jQuery not to process data or worry about content-type
  298. // You *must* include these options!
  299. cache: false,
  300. contentType: false,
  301. processData: false,
  302. success: function(data, text) {
  303. $("#upgrade-progress").hide();
  304. if ("OK" === data) {
  305. alert("Firmware image uploaded, board rebooting. This page will be refreshed in 5 seconds.");
  306. doReload(5000);
  307. } else {
  308. alert("There was an error trying to upload the new image, please try again (" + data + ").");
  309. }
  310. },
  311. // Custom XMLHttpRequest
  312. xhr: function() {
  313. $("#upgrade-progress").show();
  314. var myXhr = $.ajaxSettings.xhr();
  315. if (myXhr.upload) {
  316. // For handling the progress of the upload
  317. myXhr.upload.addEventListener("progress", function(e) {
  318. if (e.lengthComputable) {
  319. $("progress").attr({ value: e.loaded, max: e.total });
  320. }
  321. } , false);
  322. }
  323. return myXhr;
  324. }
  325. });
  326. });
  327. return false;
  328. }
  329. function doUpdatePassword() {
  330. var form = $("#formPassword");
  331. if (validateForm(form)) {
  332. sendConfig(getData(form));
  333. }
  334. return false;
  335. }
  336. function checkChanges() {
  337. if (numChanged > 0) {
  338. var response = window.confirm("Some changes have not been saved yet, do you want to save them first?");
  339. if (response) {
  340. doUpdate();
  341. }
  342. }
  343. }
  344. function doAction(question, action) {
  345. checkChanges();
  346. if (question) {
  347. var response = window.confirm(question);
  348. if (false === response) {
  349. return false;
  350. }
  351. }
  352. sendAction(action, {});
  353. doReload(5000);
  354. return false;
  355. }
  356. function doReboot(ask) {
  357. var question = (typeof ask === "undefined" || false === ask) ?
  358. null :
  359. "Are you sure you want to reboot the device?";
  360. return doAction(question, "reboot");
  361. }
  362. function doReconnect(ask) {
  363. var question = (typeof ask === "undefined" || false === ask) ?
  364. null :
  365. "Are you sure you want to disconnect from the current WIFI network?";
  366. return doAction(question, "reconnect");
  367. }
  368. function doUpdate() {
  369. var form = $("#formSave");
  370. if (validateForm(form)) {
  371. // Get data
  372. sendConfig(getData(form));
  373. // Empty special fields
  374. $(".pwrExpected").val(0);
  375. $("input[name='pwrResetCalibration']").prop("checked", false);
  376. $("input[name='pwrResetE']").prop("checked", false);
  377. // Change handling
  378. numChanged = 0;
  379. setTimeout(function() {
  380. var response;
  381. if (numReboot > 0) {
  382. response = window.confirm("You have to reboot the board for the changes to take effect, do you want to do it now?");
  383. if (response) { doReboot(false); }
  384. } else if (numReconnect > 0) {
  385. response = window.confirm("You have to reconnect to the WiFi for the changes to take effect, do you want to do it now?");
  386. if (response) { doReconnect(false); }
  387. } else if (numReload > 0) {
  388. response = window.confirm("You have to reload the page to see the latest changes, do you want to do it now?");
  389. if (response) { doReload(0); }
  390. }
  391. resetOriginals();
  392. }, 1000);
  393. }
  394. return false;
  395. }
  396. function doBackup() {
  397. document.getElementById("downloader").src = urls.config.href;
  398. return false;
  399. }
  400. function onFileUpload(event) {
  401. var inputFiles = this.files;
  402. if (typeof inputFiles === "undefined" || inputFiles.length === 0) {
  403. return false;
  404. }
  405. var inputFile = inputFiles[0];
  406. this.value = "";
  407. var response = window.confirm("Previous settings will be overwritten. Are you sure you want to restore this settings?");
  408. if (!response) {
  409. return false;
  410. }
  411. var reader = new FileReader();
  412. reader.onload = function(e) {
  413. var data = getJson(e.target.result);
  414. if (data) {
  415. sendAction("restore", data);
  416. } else {
  417. window.alert(messages[4]);
  418. }
  419. };
  420. reader.readAsText(inputFile);
  421. return false;
  422. }
  423. function doRestore() {
  424. if (typeof window.FileReader !== "function") {
  425. alert("The file API isn't supported on this browser yet.");
  426. } else {
  427. $("#uploader").click();
  428. }
  429. return false;
  430. }
  431. function doFactoryReset() {
  432. var response = window.confirm("Are you sure you want to restore to factory settings?");
  433. if (response === false) {
  434. return false;
  435. }
  436. sendAction("factory_reset", {});
  437. doReload(5000);
  438. return false;
  439. }
  440. function doToggle(id, value) {
  441. sendAction("relay", {id: id, status: value ? 1 : 0 });
  442. return false;
  443. }
  444. function doScan() {
  445. $("#scanResult").html("");
  446. $("div.scan.loading").show();
  447. sendAction("scan", {});
  448. return false;
  449. }
  450. function doHAConfig() {
  451. $("#haConfig").html("");
  452. sendAction("haconfig", {});
  453. return false;
  454. }
  455. function doDebugCommand() {
  456. var el = $("input[name='dbgcmd']");
  457. var command = el.val();
  458. el.val("");
  459. sendAction("dbgcmd", {command: command});
  460. return false;
  461. }
  462. function doDebugClear() {
  463. $("#weblog").text("");
  464. return false;
  465. }
  466. <!-- removeIf(!rfm69)-->
  467. function doClearCounts() {
  468. sendAction("clear-counts", {});
  469. return false;
  470. }
  471. function doClearMessages() {
  472. packets.clear().draw(false);
  473. return false;
  474. }
  475. function doFilter(e) {
  476. var index = packets.cell(this).index();
  477. if (index == 'undefined') return;
  478. var c = index.column;
  479. var column = packets.column(c);
  480. if (filters[c]) {
  481. filters[c] = false;
  482. column.search("");
  483. $(column.header()).removeClass("filtered");
  484. } else {
  485. filters[c] = true;
  486. var data = packets.row(this).data();
  487. if (e.which == 1) {
  488. column.search('^' + data[c] + '$', true, false );
  489. } else {
  490. column.search('^((?!(' + data[c] + ')).)*$', true, false );
  491. }
  492. $(column.header()).addClass("filtered");
  493. }
  494. column.draw();
  495. return false;
  496. }
  497. function doClearFilters() {
  498. for (var i = 0; i < packets.columns()[0].length; i++) {
  499. if (filters[i]) {
  500. filters[i] = false;
  501. var column = packets.column(i);
  502. column.search("");
  503. $(column.header()).removeClass("filtered");
  504. column.draw();
  505. }
  506. }
  507. return false;
  508. }
  509. <!-- endRemoveIf(!rfm69)-->
  510. // -----------------------------------------------------------------------------
  511. // Visualization
  512. // -----------------------------------------------------------------------------
  513. function toggleMenu() {
  514. $("#layout").toggleClass("active");
  515. $("#menu").toggleClass("active");
  516. $("#menuLink").toggleClass("active");
  517. }
  518. function showPanel() {
  519. $(".panel").hide();
  520. if ($("#layout").hasClass("active")) { toggleMenu(); }
  521. $("#" + $(this).attr("data")).show();
  522. }
  523. // -----------------------------------------------------------------------------
  524. // Relays & magnitudes mapping
  525. // -----------------------------------------------------------------------------
  526. function createRelayList(data, container, template_name) {
  527. var current = $("#" + container + " > div").length;
  528. if (current > 0) { return; }
  529. var template = $("#" + template_name + " .pure-g")[0];
  530. for (var i in data) {
  531. var line = $(template).clone();
  532. $("label", line).html("Switch #" + i);
  533. $("input", line).attr("tabindex", 40 + i).val(data[i]);
  534. line.appendTo("#" + container);
  535. }
  536. }
  537. <!-- removeIf(!sensor)-->
  538. function createMagnitudeList(data, container, template_name) {
  539. var current = $("#" + container + " > div").length;
  540. if (current > 0) { return; }
  541. var template = $("#" + template_name + " .pure-g")[0];
  542. for (var i in data) {
  543. var magnitude = data[i];
  544. var line = $(template).clone();
  545. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  546. $("div.hint", line).html(magnitude.name);
  547. $("input", line).attr("tabindex", 40 + i).val(magnitude.idx);
  548. line.appendTo("#" + container);
  549. }
  550. }
  551. <!-- endRemoveIf(!sensor)-->
  552. // -----------------------------------------------------------------------------
  553. // RFM69
  554. // -----------------------------------------------------------------------------
  555. <!-- removeIf(!rfm69)-->
  556. function addMapping() {
  557. var template = $("#nodeTemplate .pure-g")[0];
  558. var line = $(template).clone();
  559. var tabindex = $("#mapping > div").length * 3 + 50;
  560. $(line).find("input").each(function() {
  561. $(this).attr("tabindex", tabindex++);
  562. });
  563. $(line).find("button").on('click', delMapping);
  564. line.appendTo("#mapping");
  565. }
  566. function delMapping() {
  567. var parent = $(this).parent().parent();
  568. $(parent).remove();
  569. }
  570. <!-- endRemoveIf(!rfm69)-->
  571. // -----------------------------------------------------------------------------
  572. // Wifi
  573. // -----------------------------------------------------------------------------
  574. function delNetwork() {
  575. var parent = $(this).parents(".pure-g");
  576. $(parent).remove();
  577. }
  578. function moreNetwork() {
  579. var parent = $(this).parents(".pure-g");
  580. $(".more", parent).toggle();
  581. }
  582. function addNetwork() {
  583. var numNetworks = $("#networks > div").length;
  584. if (numNetworks >= maxNetworks) {
  585. alert("Max number of networks reached");
  586. return null;
  587. }
  588. var tabindex = 200 + numNetworks * 10;
  589. var template = $("#networkTemplate").children();
  590. var line = $(template).clone();
  591. $(line).find("input").each(function() {
  592. $(this).attr("tabindex", tabindex);
  593. tabindex++;
  594. });
  595. $(line).find(".button-del-network").on("click", delNetwork);
  596. $(line).find(".button-more-network").on("click", moreNetwork);
  597. line.appendTo("#networks");
  598. return line;
  599. }
  600. // -----------------------------------------------------------------------------
  601. // Relays scheduler
  602. // -----------------------------------------------------------------------------
  603. function delSchedule() {
  604. var parent = $(this).parents(".pure-g");
  605. $(parent).remove();
  606. }
  607. function moreSchedule() {
  608. var parent = $(this).parents(".pure-g");
  609. $("div.more", parent).toggle();
  610. }
  611. function addSchedule(event) {
  612. var numSchedules = $("#schedules > div").length;
  613. if (numSchedules >= maxSchedules) {
  614. alert("Max number of schedules reached");
  615. return null;
  616. }
  617. var tabindex = 200 + numSchedules * 10;
  618. var template = $("#scheduleTemplate").children();
  619. var line = $(template).clone();
  620. var type = (1 === event.data.schType) ? "switch" : "light";
  621. template = $("#" + type + "ActionTemplate").children();
  622. var actionLine = template.clone();
  623. $(line).find("#schActionDiv").append(actionLine);
  624. $(line).find("input").each(function() {
  625. $(this).attr("tabindex", tabindex);
  626. tabindex++;
  627. });
  628. $(line).find(".button-del-schedule").on("click", delSchedule);
  629. $(line).find(".button-more-schedule").on("click", moreSchedule);
  630. line.appendTo("#schedules");
  631. $(line).find("input[type='checkbox']").prop("checked", false);
  632. initCheckboxes();
  633. return line;
  634. }
  635. // -----------------------------------------------------------------------------
  636. // Relays
  637. // -----------------------------------------------------------------------------
  638. function initRelays(data) {
  639. var current = $("#relays > div").length;
  640. if (current > 0) { return; }
  641. var template = $("#relayTemplate .pure-g")[0];
  642. for (var i=0; i<data.length; i++) {
  643. // Add relay fields
  644. var line = $(template).clone();
  645. $(".id", line).html(i);
  646. $(":checkbox", line).prop('checked', data[i]).attr("data", i);
  647. line.appendTo("#relays");
  648. // Populate the relay SELECTs
  649. $("select.isrelay").append(
  650. $("<option></option>").attr("value",i).text("Switch #" + i));
  651. }
  652. }
  653. function initCheckboxes() {
  654. var setCheckbox = function(element, value) {
  655. var container = $(".toggle-container", $(element));
  656. if (value) {
  657. container.css("clipPath", "inset(0 0 0 50%)");
  658. container.css("backgroundColor", "#00c000");
  659. } else {
  660. container.css("clipPath", "inset(0 50% 0 0)");
  661. container.css("backgroundColor", "#c00000");
  662. }
  663. }
  664. $(".checkbox-container")
  665. .each(function() {
  666. var status = $(this).next().prop('checked');
  667. setCheckbox(this, status);
  668. })
  669. .off('click')
  670. .on('click', function() {
  671. var checkbox = $(this).next();
  672. var status = checkbox.prop('checked');
  673. status = !status;
  674. checkbox.prop('checked', status);
  675. setCheckbox(this, status);
  676. if ("relay" == checkbox.attr('name')) {
  677. var id = parseInt(checkbox.attr('data'), 10);
  678. doToggle(id, status);
  679. }
  680. });
  681. }
  682. function createCheckboxes() {
  683. $("input[type='checkbox']").each(function() {
  684. var text_on = $(this).attr("on") || "YES";
  685. var text_off = $(this).attr("off") || "NO";
  686. var toggles = "<div class=\"toggle\"><p>" + text_on + "</p></div><div class=\"toggle\"><p>" + text_off + "</p></div>";
  687. var content = "<div class=\"checkbox-container\"><div class=\"inner-container\">" + toggles
  688. + "</div><div class=\"inner-container toggle-container\">" + toggles + "</div></div>";
  689. $(this).before(content).hide();
  690. });
  691. }
  692. function initRelayConfig(data) {
  693. var current = $("#relayConfig > div").length;
  694. if (current > 0) { return; }
  695. var template = $("#relayConfigTemplate").children();
  696. for (var i in data) {
  697. var relay = data[i];
  698. var line = $(template).clone();
  699. $("span.gpio", line).html(relay.gpio);
  700. $("span.id", line).html(i);
  701. $("select[name='relayBoot']", line).val(relay.boot);
  702. $("select[name='relayPulse']", line).val(relay.pulse);
  703. $("input[name='relayTime']", line).val(relay.pulse_ms);
  704. $("input[name='mqttGroup']", line).val(relay.group);
  705. $("select[name='mqttGroupInv']", line).val(relay.group_inv);
  706. $("select[name='relayOnDisc']", line).val(relay.on_disc);
  707. line.appendTo("#relayConfig");
  708. }
  709. }
  710. // -----------------------------------------------------------------------------
  711. // Sensors & Magnitudes
  712. // -----------------------------------------------------------------------------
  713. <!-- removeIf(!sensor)-->
  714. function initMagnitudes(data) {
  715. // check if already initialized
  716. var done = $("#magnitudes > div").length;
  717. if (done > 0) { return; }
  718. // add templates
  719. var template = $("#magnitudeTemplate").children();
  720. for (var i in data) {
  721. var magnitude = data[i];
  722. var line = $(template).clone();
  723. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  724. $("div.hint", line).html(magnitude.description);
  725. $("input", line).attr("data", i);
  726. line.appendTo("#magnitudes");
  727. }
  728. }
  729. <!-- endRemoveIf(!sensor)-->
  730. // -----------------------------------------------------------------------------
  731. // Lights
  732. // -----------------------------------------------------------------------------
  733. <!-- removeIf(!light)-->
  734. function initColorRGB() {
  735. // check if already initialized
  736. var done = $("#colors > div").length;
  737. if (done > 0) { return; }
  738. // add template
  739. var template = $("#colorRGBTemplate").children();
  740. var line = $(template).clone();
  741. line.appendTo("#colors");
  742. // init color wheel
  743. $("input[name='color']").wheelColorPicker({
  744. sliders: "wrgbp"
  745. }).on("sliderup", function() {
  746. var value = $(this).wheelColorPicker("getValue", "css");
  747. sendAction("color", {rgb: value});
  748. });
  749. // init bright slider
  750. $("#brightness").on("change", function() {
  751. var value = $(this).val();
  752. var parent = $(this).parents(".pure-g");
  753. $("span", parent).html(value);
  754. sendAction("color", {brightness: value});
  755. });
  756. }
  757. function initCCT() {
  758. // check if already initialized
  759. var done = $("#cct > div").length;
  760. if (done > 0) { return; }
  761. $("#miredsTemplate").children().clone().appendTo("#cct");
  762. $("#mireds").on("change", function() {
  763. var value = $(this).val();
  764. var parent = $(this).parents(".pure-g");
  765. $("span", parent).html(value);
  766. sendAction("mireds", {mireds: value});
  767. });
  768. }
  769. function initColorHSV() {
  770. // check if already initialized
  771. var done = $("#colors > div").length;
  772. if (done > 0) { return; }
  773. // add template
  774. var template = $("#colorHSVTemplate").children();
  775. var line = $(template).clone();
  776. line.appendTo("#colors");
  777. // init color wheel
  778. $("input[name='color']").wheelColorPicker({
  779. sliders: "whsvp"
  780. }).on("sliderup", function() {
  781. var color = $(this).wheelColorPicker("getColor");
  782. var value = parseInt(color.h * 360, 10) + "," + parseInt(color.s * 100, 10) + "," + parseInt(color.v * 100, 10);
  783. sendAction("color", {hsv: value});
  784. });
  785. }
  786. function initChannels(num) {
  787. // check if already initialized
  788. var done = $("#channels > div").length > 0;
  789. if (done) { return; }
  790. // does it have color channels?
  791. var colors = $("#colors > div").length > 0;
  792. // calculate channels to create
  793. var max = num;
  794. if (colors) {
  795. max = num % 3;
  796. if ((max > 0) & useWhite) {
  797. max--;
  798. if (useCCT) {
  799. max--;
  800. }
  801. }
  802. }
  803. var start = num - max;
  804. var onChannelSliderChange = function() {
  805. var id = $(this).attr("data");
  806. var value = $(this).val();
  807. var parent = $(this).parents(".pure-g");
  808. $("span", parent).html(value);
  809. sendAction("channel", {id: id, value: value});
  810. };
  811. // add templates
  812. var i = 0;
  813. var template = $("#channelTemplate").children();
  814. for (i=0; i<max; i++) {
  815. var channel_id = start + i;
  816. var line = $(template).clone();
  817. $("span.slider", line).attr("data", channel_id);
  818. $("input.slider", line).attr("data", channel_id).on("change", onChannelSliderChange);
  819. $("label", line).html("Channel #" + channel_id);
  820. line.appendTo("#channels");
  821. }
  822. for (i=0; i<num; i++) {
  823. $("select.islight").append(
  824. $("<option></option>").attr("value",i).text("Channel #" + i));
  825. }
  826. }
  827. <!-- endRemoveIf(!light)-->
  828. // -----------------------------------------------------------------------------
  829. // RFBridge
  830. // -----------------------------------------------------------------------------
  831. <!-- removeIf(!rfbridge)-->
  832. function rfbLearn() {
  833. var parent = $(this).parents(".pure-g");
  834. var input = $("input", parent);
  835. sendAction("rfblearn", {id: input.attr("data-id"), status: input.attr("data-status")});
  836. }
  837. function rfbForget() {
  838. var parent = $(this).parents(".pure-g");
  839. var input = $("input", parent);
  840. sendAction("rfbforget", {id: input.attr("data-id"), status: input.attr("data-status")});
  841. }
  842. function rfbSend() {
  843. var parent = $(this).parents(".pure-g");
  844. var input = $("input", parent);
  845. sendAction("rfbsend", {id: input.attr("data-id"), status: input.attr("data-status"), data: input.val()});
  846. }
  847. function addRfbNode() {
  848. var numNodes = $("#rfbNodes > legend").length;
  849. var template = $("#rfbNodeTemplate").children();
  850. var line = $(template).clone();
  851. var status = true;
  852. $("span", line).html(numNodes);
  853. $(line).find("input").each(function() {
  854. $(this).attr("data-id", numNodes);
  855. $(this).attr("data-status", status ? 1 : 0);
  856. status = !status;
  857. });
  858. $(line).find(".button-rfb-learn").on("click", rfbLearn);
  859. $(line).find(".button-rfb-forget").on("click", rfbForget);
  860. $(line).find(".button-rfb-send").on("click", rfbSend);
  861. line.appendTo("#rfbNodes");
  862. return line;
  863. }
  864. <!-- endRemoveIf(!rfbridge)-->
  865. // -----------------------------------------------------------------------------
  866. // Processing
  867. // -----------------------------------------------------------------------------
  868. function processData(data) {
  869. // title
  870. if ("app_name" in data) {
  871. var title = data.app_name;
  872. if ("app_version" in data) {
  873. title = title + " " + data.app_version;
  874. }
  875. $("span[name=title]").html(title);
  876. if ("hostname" in data) {
  877. title = data.hostname + " - " + title;
  878. }
  879. document.title = title;
  880. }
  881. Object.keys(data).forEach(function(key) {
  882. var i;
  883. var value = data[key];
  884. // ---------------------------------------------------------------------
  885. // Web mode
  886. // ---------------------------------------------------------------------
  887. if ("webMode" === key) {
  888. password = (1 === value);
  889. $("#layout").toggle(!password);
  890. $("#password").toggle(password);
  891. }
  892. // ---------------------------------------------------------------------
  893. // Actions
  894. // ---------------------------------------------------------------------
  895. if ("action" === key) {
  896. if ("reload" === data.action) { doReload(1000); }
  897. return;
  898. }
  899. // ---------------------------------------------------------------------
  900. // RFBridge
  901. // ---------------------------------------------------------------------
  902. <!-- removeIf(!rfbridge)-->
  903. if ("rfbCount" === key) {
  904. for (i=0; i<data.rfbCount; i++) { addRfbNode(); }
  905. return;
  906. }
  907. if ("rfbrawVisible" === key) {
  908. $("input[name='rfbcode']").attr("maxlength", 116);
  909. }
  910. if ("rfb" === key) {
  911. var nodes = data.rfb;
  912. for (i in nodes) {
  913. var node = nodes[i];
  914. $("input[name='rfbcode'][data-id='" + node.id + "'][data-status='" + node.status + "']").val(node.data);
  915. }
  916. return;
  917. }
  918. <!-- endRemoveIf(!rfbridge)-->
  919. // ---------------------------------------------------------------------
  920. // RFM69
  921. // ---------------------------------------------------------------------
  922. <!-- removeIf(!rfm69)-->
  923. if (key == "packet") {
  924. var packet = data.packet;
  925. var d = new Date();
  926. packets.row.add([
  927. d.toLocaleTimeString('en-US', { hour12: false }),
  928. packet.senderID,
  929. packet.packetID,
  930. packet.targetID,
  931. packet.key,
  932. packet.value,
  933. packet.rssi,
  934. packet.duplicates,
  935. packet.missing,
  936. ]).draw(false);
  937. return;
  938. }
  939. if (key == "mapping") {
  940. for (var i in data.mapping) {
  941. // add a new row
  942. addMapping();
  943. // get group
  944. var line = $("#mapping .pure-g")[i];
  945. // fill in the blanks
  946. var mapping = data.mapping[i];
  947. Object.keys(mapping).forEach(function(key) {
  948. var id = "input[name=" + key + "]";
  949. if ($(id, line).length) $(id, line).val(mapping[key]).attr("original", mapping[key]);
  950. });
  951. }
  952. return;
  953. }
  954. <!-- endRemoveIf(!rfm69)-->
  955. // ---------------------------------------------------------------------
  956. // Lights
  957. // ---------------------------------------------------------------------
  958. <!-- removeIf(!light)-->
  959. if ("rgb" === key) {
  960. initColorRGB();
  961. $("input[name='color']").wheelColorPicker("setValue", value, true);
  962. return;
  963. }
  964. if ("hsv" === key) {
  965. initColorHSV();
  966. // wheelColorPicker expects HSV to be between 0 and 1 all of them
  967. var chunks = value.split(",");
  968. var obj = {};
  969. obj.h = chunks[0] / 360;
  970. obj.s = chunks[1] / 100;
  971. obj.v = chunks[2] / 100;
  972. $("input[name='color']").wheelColorPicker("setColor", obj);
  973. return;
  974. }
  975. if ("brightness" === key) {
  976. $("#brightness").val(value);
  977. $("span.brightness").html(value);
  978. return;
  979. }
  980. if ("channels" === key) {
  981. var len = value.length;
  982. initChannels(len);
  983. for (i in value) {
  984. var ch = value[i];
  985. $("input.slider[data=" + i + "]").val(ch);
  986. $("span.slider[data=" + i + "]").html(ch);
  987. }
  988. return;
  989. }
  990. if ("mireds" === key) {
  991. $("#mireds").val(value);
  992. $("span.mireds").html(value);
  993. return;
  994. }
  995. if ("useWhite" === key) {
  996. useWhite = value;
  997. }
  998. if ("useCCT" === key) {
  999. initCCT();
  1000. useCCT = value;
  1001. }
  1002. <!-- endRemoveIf(!light)-->
  1003. // ---------------------------------------------------------------------
  1004. // Sensors & Magnitudes
  1005. // ---------------------------------------------------------------------
  1006. <!-- removeIf(!sensor)-->
  1007. if ("magnitudes" === key) {
  1008. initMagnitudes(value);
  1009. for (i in value) {
  1010. var magnitude = value[i];
  1011. var error = magnitude.error || 0;
  1012. var text = (0 === error) ?
  1013. magnitude.value + magnitude.units :
  1014. magnitudeError(error);
  1015. var element = $("input[name='magnitude'][data='" + i + "']");
  1016. element.val(text);
  1017. $("div.hint", element.parent().parent()).html(magnitude.description);
  1018. }
  1019. return;
  1020. }
  1021. <!-- endRemoveIf(!sensor)-->
  1022. // ---------------------------------------------------------------------
  1023. // WiFi
  1024. // ---------------------------------------------------------------------
  1025. if ("maxNetworks" === key) {
  1026. maxNetworks = parseInt(value, 10);
  1027. return;
  1028. }
  1029. if ("wifi" === key) {
  1030. for (i in value) {
  1031. var wifi = value[i];
  1032. var nwk_line = addNetwork();
  1033. Object.keys(wifi).forEach(function(key) {
  1034. $("input[name='" + key + "']", nwk_line).val(wifi[key]);
  1035. });
  1036. }
  1037. return;
  1038. }
  1039. if ("scanResult" === key) {
  1040. $("div.scan.loading").hide();
  1041. $("#scanResult").show();
  1042. }
  1043. // -----------------------------------------------------------------------------
  1044. // Home Assistant
  1045. // -----------------------------------------------------------------------------
  1046. if ("haConfig" === key) {
  1047. $("#haConfig").show();
  1048. }
  1049. // -----------------------------------------------------------------------------
  1050. // Relays scheduler
  1051. // -----------------------------------------------------------------------------
  1052. if ("maxSchedules" === key) {
  1053. maxSchedules = parseInt(value, 10);
  1054. return;
  1055. }
  1056. if ("schedule" === key) {
  1057. for (i in value) {
  1058. var schedule = value[i];
  1059. var sch_line = addSchedule({ data: {schType: schedule["schType"] }});
  1060. Object.keys(schedule).forEach(function(key) {
  1061. var sch_value = schedule[key];
  1062. $("input[name='" + key + "']", sch_line).val(sch_value);
  1063. $("select[name='" + key + "']", sch_line).prop("value", sch_value);
  1064. $("input[type='checkbox'][name='" + key + "']", sch_line).prop("checked", sch_value);
  1065. });
  1066. }
  1067. return;
  1068. }
  1069. // ---------------------------------------------------------------------
  1070. // Relays
  1071. // ---------------------------------------------------------------------
  1072. if ("relayStatus" === key) {
  1073. initRelays(value);
  1074. for (i in value) {
  1075. $("input[name='relay'][data='" + i + "']").prop("checked", value[i]);
  1076. }
  1077. return;
  1078. }
  1079. // Relay configuration
  1080. if ("relayConfig" === key) {
  1081. initRelayConfig(value);
  1082. return;
  1083. }
  1084. // ---------------------------------------------------------------------
  1085. // Domoticz
  1086. // ---------------------------------------------------------------------
  1087. // Domoticz - Relays
  1088. if ("dczRelays" === key) {
  1089. createRelayList(value, "dczRelays", "dczRelayTemplate");
  1090. return;
  1091. }
  1092. // Domoticz - Magnitudes
  1093. <!-- removeIf(!sensor)-->
  1094. if ("dczMagnitudes" === key) {
  1095. createMagnitudeList(value, "dczMagnitudes", "dczMagnitudeTemplate");
  1096. return;
  1097. }
  1098. <!-- endRemoveIf(!sensor)-->
  1099. // ---------------------------------------------------------------------
  1100. // Thingspeak
  1101. // ---------------------------------------------------------------------
  1102. // Thingspeak - Relays
  1103. if ("tspkRelays" === key) {
  1104. createRelayList(value, "tspkRelays", "tspkRelayTemplate");
  1105. return;
  1106. }
  1107. // Thingspeak - Magnitudes
  1108. <!-- removeIf(!sensor)-->
  1109. if ("tspkMagnitudes" === key) {
  1110. createMagnitudeList(value, "tspkMagnitudes", "tspkMagnitudeTemplate");
  1111. return;
  1112. }
  1113. <!-- endRemoveIf(!sensor)-->
  1114. // ---------------------------------------------------------------------
  1115. // General
  1116. // ---------------------------------------------------------------------
  1117. // Messages
  1118. if ("message" === key) {
  1119. window.alert(messages[value]);
  1120. return;
  1121. }
  1122. // Web log
  1123. if ("weblog" === key) {
  1124. $("#weblog").append(new Text(value));
  1125. $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height());
  1126. return;
  1127. }
  1128. // Enable options
  1129. var position = key.indexOf("Visible");
  1130. if (position > 0 && position === key.length - 7) {
  1131. var module = key.slice(0,-7);
  1132. $(".module-" + module).css("display", "inherit");
  1133. return;
  1134. }
  1135. if ("deviceip" === key) {
  1136. var a_href = $("span[name='" + key + "']").parent();
  1137. a_href.attr("href", "//" + value);
  1138. a_href.next().attr("href", "telnet://" + value);
  1139. }
  1140. if ("now" === key) {
  1141. now = value;
  1142. return;
  1143. }
  1144. if ("free_size" === key) {
  1145. free_size = parseInt(value, 10);
  1146. }
  1147. // Pre-process
  1148. if ("mqttStatus" === key) {
  1149. value = value ? "CONNECTED" : "NOT CONNECTED";
  1150. }
  1151. if ("ntpStatus" === key) {
  1152. value = value ? "SYNC'D" : "NOT SYNC'D";
  1153. }
  1154. if ("uptime" === key) {
  1155. ago = 0;
  1156. var uptime = parseInt(value, 10);
  1157. var seconds = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1158. var minutes = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1159. var hours = uptime % 24; uptime = parseInt(uptime / 24, 10);
  1160. var days = uptime;
  1161. value = days + "d " + zeroPad(hours, 2) + "h " + zeroPad(minutes, 2) + "m " + zeroPad(seconds, 2) + "s";
  1162. }
  1163. // ---------------------------------------------------------------------
  1164. // Matching
  1165. // ---------------------------------------------------------------------
  1166. var pre;
  1167. var post;
  1168. // Look for INPUTs
  1169. var input = $("input[name='" + key + "']");
  1170. if (input.length > 0) {
  1171. if (input.attr("type") === "checkbox") {
  1172. input.prop("checked", value);
  1173. } else if (input.attr("type") === "radio") {
  1174. input.val([value]);
  1175. } else {
  1176. pre = input.attr("pre") || "";
  1177. post = input.attr("post") || "";
  1178. input.val(pre + value + post);
  1179. }
  1180. }
  1181. // Look for SPANs
  1182. var span = $("span[name='" + key + "']");
  1183. if (span.length > 0) {
  1184. pre = span.attr("pre") || "";
  1185. post = span.attr("post") || "";
  1186. span.html(pre + value + post);
  1187. }
  1188. // Look for SELECTs
  1189. var select = $("select[name='" + key + "']");
  1190. if (select.length > 0) {
  1191. select.val(value);
  1192. }
  1193. });
  1194. // Auto generate an APIKey if none defined yet
  1195. if ($("input[name='apiKey']").val() === "") {
  1196. generateAPIKey();
  1197. }
  1198. resetOriginals();
  1199. initCheckboxes();
  1200. }
  1201. function hasChanged() {
  1202. var newValue, originalValue;
  1203. if ($(this).attr("type") === "checkbox") {
  1204. newValue = $(this).prop("checked");
  1205. originalValue = ($(this).attr("original") === "true");
  1206. } else {
  1207. newValue = $(this).val();
  1208. originalValue = $(this).attr("original");
  1209. }
  1210. var hasChanged = $(this).attr("hasChanged") || 0;
  1211. var action = $(this).attr("action");
  1212. if (typeof originalValue === "undefined") { return; }
  1213. if ("none" === action) { return; }
  1214. if (newValue !== originalValue) {
  1215. if (0 === hasChanged) {
  1216. ++numChanged;
  1217. if ("reconnect" === action) { ++numReconnect; }
  1218. if ("reboot" === action) { ++numReboot; }
  1219. if ("reload" === action) { ++numReload; }
  1220. $(this).attr("hasChanged", 1);
  1221. }
  1222. } else {
  1223. if (1 === hasChanged) {
  1224. --numChanged;
  1225. if ("reconnect" === action) { --numReconnect; }
  1226. if ("reboot" === action) { --numReboot; }
  1227. if ("reload" === action) { --numReload; }
  1228. $(this).attr("hasChanged", 0);
  1229. }
  1230. }
  1231. }
  1232. // -----------------------------------------------------------------------------
  1233. // Init & connect
  1234. // -----------------------------------------------------------------------------
  1235. function initUrls(root) {
  1236. var paths = ["ws", "upgrade", "config", "auth"];
  1237. urls["root"] = root;
  1238. paths.forEach(function(path) {
  1239. urls[path] = new URL(path, root);
  1240. urls[path].protocol = root.protocol;
  1241. });
  1242. if (root.protocol == "https:") {
  1243. urls.ws.protocol = "wss:";
  1244. } else {
  1245. urls.ws.protocol = "ws:";
  1246. }
  1247. }
  1248. function connectToURL(url) {
  1249. initUrls(url);
  1250. $.ajax({
  1251. 'method': 'GET',
  1252. 'crossDomain': true,
  1253. 'url': urls.auth.href,
  1254. 'xhrFields': { 'withCredentials': true }
  1255. }).done(function(data) {
  1256. if (websock) { websock.close(); }
  1257. websock = new WebSocket(urls.ws.href);
  1258. websock.onmessage = function(evt) {
  1259. var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
  1260. if (data) {
  1261. processData(data);
  1262. }
  1263. };
  1264. }).fail(function() {
  1265. // Nothing to do, reload page and retry
  1266. });
  1267. }
  1268. function connect(host) {
  1269. if (!host.startsWith("http:") && !host.startsWith("https:")) {
  1270. host = "http://" + host;
  1271. }
  1272. connectToURL(new URL(host));
  1273. }
  1274. function connectToCurrentURL() {
  1275. connectToURL(new URL(window.location));
  1276. }
  1277. function getParameterByName(name) {
  1278. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  1279. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  1280. }
  1281. $(function() {
  1282. initMessages();
  1283. loadTimeZones();
  1284. createCheckboxes();
  1285. setInterval(function() { keepTime(); }, 1000);
  1286. $("#menuLink").on("click", toggleMenu);
  1287. $(".pure-menu-link").on("click", showPanel);
  1288. $("progress").attr({ value: 0, max: 100 });
  1289. $(".button-update").on("click", doUpdate);
  1290. $(".button-update-password").on("click", doUpdatePassword);
  1291. $(".button-reboot").on("click", doReboot);
  1292. $(".button-reconnect").on("click", doReconnect);
  1293. $(".button-wifi-scan").on("click", doScan);
  1294. $(".button-ha-config").on("click", doHAConfig);
  1295. $(".button-dbgcmd").on("click", doDebugCommand);
  1296. $("input[name='dbgcmd']").enterKey(doDebugCommand);
  1297. $(".button-dbg-clear").on("click", doDebugClear);
  1298. $(".button-settings-backup").on("click", doBackup);
  1299. $(".button-settings-restore").on("click", doRestore);
  1300. $(".button-settings-factory").on("click", doFactoryReset);
  1301. $("#uploader").on("change", onFileUpload);
  1302. $(".button-upgrade").on("click", doUpgrade);
  1303. $(".button-apikey").on("click", generateAPIKey);
  1304. $(".button-upgrade-browse").on("click", function() {
  1305. $("input[name='upgrade']")[0].click();
  1306. return false;
  1307. });
  1308. $("input[name='upgrade']").change(function (){
  1309. var file = this.files[0];
  1310. $("input[name='filename']").val(file.name);
  1311. });
  1312. $(".button-add-network").on("click", function() {
  1313. $(".more", addNetwork()).toggle();
  1314. });
  1315. $(".button-add-switch-schedule").on("click", { schType: 1 }, addSchedule);
  1316. <!-- removeIf(!light)-->
  1317. $(".button-add-light-schedule").on("click", { schType: 2 }, addSchedule);
  1318. <!-- endRemoveIf(!light)-->
  1319. <!-- removeIf(!rfm69)-->
  1320. $(".button-add-mapping").on('click', addMapping);
  1321. $(".button-del-mapping").on('click', delMapping);
  1322. $(".button-clear-counts").on('click', doClearCounts);
  1323. $(".button-clear-messages").on('click', doClearMessages);
  1324. $(".button-clear-filters").on('click', doClearFilters);
  1325. $('#packets tbody').on('mousedown', 'td', doFilter);
  1326. packets = $('#packets').DataTable({
  1327. "paging": false
  1328. });
  1329. for (var i = 0; i < packets.columns()[0].length; i++) {
  1330. filters[i] = false;
  1331. }
  1332. <!-- endRemoveIf(!rfm69)-->
  1333. $(document).on("change", "input", hasChanged);
  1334. $(document).on("change", "select", hasChanged);
  1335. // don't autoconnect when opening from filesystem
  1336. if (window.location.protocol === "file:") { return; }
  1337. // Check host param in query string
  1338. if (host = getParameterByName('host')) {
  1339. connect(host);
  1340. } else {
  1341. connectToCurrentURL();
  1342. }
  1343. });