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.

1634 lines
47 KiB

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