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.

1647 lines
48 KiB

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