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.

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