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.

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