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.

1645 lines
48 KiB

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