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.

1667 lines
48 KiB

8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
8 years ago
  1. var websock;
  2. var password = false;
  3. var maxNetworks;
  4. var maxSchedules;
  5. var messages = [];
  6. var free_size = 0;
  7. var urls = {};
  8. var numChanged = 0;
  9. var numReboot = 0;
  10. var numReconnect = 0;
  11. var numReload = 0;
  12. var useWhite = false;
  13. var useCCT = false;
  14. var now = 0;
  15. var ago = 0;
  16. <!-- removeIf(!rfm69)-->
  17. var packets;
  18. var filters = [];
  19. <!-- endRemoveIf(!rfm69)-->
  20. // -----------------------------------------------------------------------------
  21. // Messages
  22. // -----------------------------------------------------------------------------
  23. function initMessages() {
  24. messages[1] = "Remote update started";
  25. messages[2] = "OTA update started";
  26. messages[3] = "Error parsing data!";
  27. messages[4] = "The file does not look like a valid configuration backup or is corrupted";
  28. messages[5] = "Changes saved. You should reboot your board now";
  29. messages[7] = "Passwords do not match!";
  30. messages[8] = "Changes saved";
  31. messages[9] = "No changes detected";
  32. messages[10] = "Session expired, please reload page...";
  33. }
  34. <!-- removeIf(!sensor)-->
  35. function sensorName(id) {
  36. var names = [
  37. "DHT", "Dallas", "Emon Analog", "Emon ADC121", "Emon ADS1X15",
  38. "HLW8012", "V9261F", "ECH1560", "Analog", "Digital",
  39. "Events", "PMSX003", "BMX280", "MHZ19", "SI7021",
  40. "SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD",
  41. "TMP3X", "HC-SR04", "SenseAir", "GeigerTicks", "GeigerCPM"
  42. ];
  43. if (1 <= id && id <= names.length) {
  44. return names[id - 1];
  45. }
  46. return null;
  47. }
  48. function magnitudeType(type) {
  49. var types = [
  50. "Temperature", "Humidity", "Pressure",
  51. "Current", "Voltage", "Active Power", "Apparent Power",
  52. "Reactive Power", "Power Factor", "Energy", "Energy (delta)",
  53. "Analog", "Digital", "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. sendAction("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. sendAction("clear-counts", {});
  469. return false;
  470. }
  471. function doClearMessages() {
  472. packets.clear().draw(false);
  473. }
  474. function doFilter(e) {
  475. var index = packets.cell(this).index();
  476. if (index == 'undefined') return;
  477. var c = index.column;
  478. var column = packets.column(c);
  479. if (filters[c]) {
  480. filters[c] = false;
  481. column.search("");
  482. $(column.header()).removeClass("filtered");
  483. } else {
  484. filters[c] = true;
  485. var data = packets.row(this).data();
  486. if (e.which == 1) {
  487. column.search('^' + data[c] + '$', true, false );
  488. } else {
  489. column.search('^((?!(' + data[c] + ')).)*$', true, false );
  490. }
  491. $(column.header()).addClass("filtered");
  492. }
  493. column.draw();
  494. return false;
  495. }
  496. function doClearFilters() {
  497. for (var i = 0; i < packets.columns()[0].length; i++) {
  498. if (filters[i]) {
  499. filters[i] = false;
  500. var column = packets.column(i);
  501. column.search("");
  502. $(column.header()).removeClass("filtered");
  503. column.draw();
  504. }
  505. }
  506. return false;
  507. }
  508. <!-- endRemoveIf(!rfm69)-->
  509. // -----------------------------------------------------------------------------
  510. // Visualization
  511. // -----------------------------------------------------------------------------
  512. function toggleMenu() {
  513. $("#layout").toggleClass("active");
  514. $("#menu").toggleClass("active");
  515. $("#menuLink").toggleClass("active");
  516. }
  517. function showPanel() {
  518. $(".panel").hide();
  519. if ($("#layout").hasClass("active")) { toggleMenu(); }
  520. $("#" + $(this).attr("data")).show();
  521. }
  522. // -----------------------------------------------------------------------------
  523. // Relays & magnitudes mapping
  524. // -----------------------------------------------------------------------------
  525. function createRelayList(data, container, template_name) {
  526. var current = $("#" + container + " > div").length;
  527. if (current > 0) { return; }
  528. var template = $("#" + template_name + " .pure-g")[0];
  529. for (var i in data) {
  530. var line = $(template).clone();
  531. $("label", line).html("Switch #" + i);
  532. $("input", line).attr("tabindex", 40 + i).val(data[i]);
  533. line.appendTo("#" + container);
  534. }
  535. }
  536. <!-- removeIf(!sensor)-->
  537. function createMagnitudeList(data, container, template_name) {
  538. var current = $("#" + container + " > div").length;
  539. if (current > 0) { return; }
  540. var template = $("#" + template_name + " .pure-g")[0];
  541. for (var i in data) {
  542. var magnitude = data[i];
  543. var line = $(template).clone();
  544. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  545. $("div.hint", line).html(magnitude.name);
  546. $("input", line).attr("tabindex", 40 + i).val(magnitude.idx);
  547. line.appendTo("#" + container);
  548. }
  549. }
  550. <!-- endRemoveIf(!sensor)-->
  551. // -----------------------------------------------------------------------------
  552. // RFM69
  553. // -----------------------------------------------------------------------------
  554. <!-- removeIf(!rfm69)-->
  555. function addMapping() {
  556. var template = $("#nodeTemplate .pure-g")[0];
  557. var line = $(template).clone();
  558. var tabindex = $("#mapping > div").length * 3 + 50;
  559. $(line).find("input").each(function() {
  560. $(this).attr("tabindex", tabindex++);
  561. });
  562. $(line).find("button").on('click', delMapping);
  563. line.appendTo("#mapping");
  564. }
  565. function delMapping() {
  566. var parent = $(this).parent().parent();
  567. $(parent).remove();
  568. }
  569. <!-- endRemoveIf(!rfm69)-->
  570. // -----------------------------------------------------------------------------
  571. // Wifi
  572. // -----------------------------------------------------------------------------
  573. function delNetwork() {
  574. var parent = $(this).parents(".pure-g");
  575. $(parent).remove();
  576. }
  577. function moreNetwork() {
  578. var parent = $(this).parents(".pure-g");
  579. $(".more", parent).toggle();
  580. }
  581. function addNetwork() {
  582. var numNetworks = $("#networks > div").length;
  583. if (numNetworks >= maxNetworks) {
  584. alert("Max number of networks reached");
  585. return null;
  586. }
  587. var tabindex = 200 + numNetworks * 10;
  588. var template = $("#networkTemplate").children();
  589. var line = $(template).clone();
  590. $(line).find("input").each(function() {
  591. $(this).attr("tabindex", tabindex);
  592. tabindex++;
  593. });
  594. $(line).find(".button-del-network").on("click", delNetwork);
  595. $(line).find(".button-more-network").on("click", moreNetwork);
  596. line.appendTo("#networks");
  597. return line;
  598. }
  599. // -----------------------------------------------------------------------------
  600. // Relays scheduler
  601. // -----------------------------------------------------------------------------
  602. function delSchedule() {
  603. var parent = $(this).parents(".pure-g");
  604. $(parent).remove();
  605. }
  606. function moreSchedule() {
  607. var parent = $(this).parents(".pure-g");
  608. $("div.more", parent).toggle();
  609. }
  610. function addSchedule(event) {
  611. var numSchedules = $("#schedules > div").length;
  612. if (numSchedules >= maxSchedules) {
  613. alert("Max number of schedules reached");
  614. return null;
  615. }
  616. var tabindex = 200 + numSchedules * 10;
  617. var template = $("#scheduleTemplate").children();
  618. var line = $(template).clone();
  619. var type = (1 === event.data.schType) ? "switch" : "light";
  620. template = $("#" + type + "ActionTemplate").children();
  621. var actionLine = template.clone();
  622. $(line).find("#schActionDiv").append(actionLine);
  623. $(line).find("input").each(function() {
  624. $(this).attr("tabindex", tabindex);
  625. tabindex++;
  626. });
  627. $(line).find(".button-del-schedule").on("click", delSchedule);
  628. $(line).find(".button-more-schedule").on("click", moreSchedule);
  629. line.appendTo("#schedules");
  630. $(line).find("input[type='checkbox']").prop("checked", false);
  631. initCheckboxes();
  632. return line;
  633. }
  634. // -----------------------------------------------------------------------------
  635. // Relays
  636. // -----------------------------------------------------------------------------
  637. function initRelays(data) {
  638. var current = $("#relays > div").length;
  639. if (current > 0) { return; }
  640. var template = $("#relayTemplate .pure-g")[0];
  641. for (var i=0; i<data.length; i++) {
  642. // Add relay fields
  643. var line = $(template).clone();
  644. $(".id", line).html(i);
  645. $(":checkbox", line).prop('checked', data[i]).attr("data", i);
  646. line.appendTo("#relays");
  647. // Populate the relay SELECTs
  648. $("select.isrelay").append(
  649. $("<option></option>").attr("value",i).text("Switch #" + i));
  650. }
  651. }
  652. function initCheckboxes() {
  653. var setCheckbox = function(element, value) {
  654. var container = $(".toggle-container", $(element));
  655. if (value) {
  656. container.css("clipPath", "inset(0 0 0 50%)");
  657. container.css("backgroundColor", "#00c000");
  658. } else {
  659. container.css("clipPath", "inset(0 50% 0 0)");
  660. container.css("backgroundColor", "#c00000");
  661. }
  662. }
  663. $(".checkbox-container")
  664. .each(function() {
  665. var status = $(this).next().prop('checked');
  666. setCheckbox(this, status);
  667. })
  668. .off('click')
  669. .on('click', function() {
  670. var checkbox = $(this).next();
  671. var status = checkbox.prop('checked');
  672. status = !status;
  673. checkbox.prop('checked', status);
  674. setCheckbox(this, status);
  675. if ("relay" == checkbox.attr('name')) {
  676. var id = parseInt(checkbox.attr('data'), 10);
  677. doToggle(id, status);
  678. }
  679. });
  680. }
  681. function createCheckboxes() {
  682. $("input[type='checkbox']").each(function() {
  683. var text_on = $(this).attr("on") || "YES";
  684. var text_off = $(this).attr("off") || "NO";
  685. var toggles = "<div class=\"toggle\"><p>" + text_on + "</p></div><div class=\"toggle\"><p>" + text_off + "</p></div>";
  686. var content = "<div class=\"checkbox-container\"><div class=\"inner-container\">" + toggles
  687. + "</div><div class=\"inner-container toggle-container\">" + toggles + "</div></div>";
  688. $(this).before(content).hide();
  689. });
  690. }
  691. function initRelayConfig(data) {
  692. var current = $("#relayConfig > div").length;
  693. if (current > 0) { return; }
  694. var template = $("#relayConfigTemplate").children();
  695. for (var i in data) {
  696. var relay = data[i];
  697. var line = $(template).clone();
  698. $("span.gpio", line).html(relay.gpio);
  699. $("span.id", line).html(i);
  700. $("select[name='relayBoot']", line).val(relay.boot);
  701. $("select[name='relayPulse']", line).val(relay.pulse);
  702. $("input[name='relayTime']", line).val(relay.pulse_ms);
  703. $("input[name='mqttGroup']", line).val(relay.group);
  704. $("select[name='mqttGroupInv']", line).val(relay.group_inv);
  705. $("select[name='relayOnDisc']", line).val(relay.on_disc);
  706. line.appendTo("#relayConfig");
  707. }
  708. }
  709. // -----------------------------------------------------------------------------
  710. // Sensors & Magnitudes
  711. // -----------------------------------------------------------------------------
  712. <!-- removeIf(!sensor)-->
  713. function initMagnitudes(data) {
  714. // check if already initialized
  715. var done = $("#magnitudes > div").length;
  716. if (done > 0) { return; }
  717. // add templates
  718. var template = $("#magnitudeTemplate").children();
  719. for (var i in data) {
  720. var magnitude = data[i];
  721. var line = $(template).clone();
  722. $("label", line).html(magnitudeType(magnitude.type) + " #" + parseInt(magnitude.index, 10));
  723. $("div.hint", line).html(magnitude.description);
  724. $("input", line).attr("data", i);
  725. line.appendTo("#magnitudes");
  726. }
  727. }
  728. <!-- endRemoveIf(!sensor)-->
  729. // -----------------------------------------------------------------------------
  730. // Lights
  731. // -----------------------------------------------------------------------------
  732. <!-- removeIf(!light)-->
  733. function initColorRGB() {
  734. // check if already initialized
  735. var done = $("#colors > div").length;
  736. if (done > 0) { return; }
  737. // add template
  738. var template = $("#colorRGBTemplate").children();
  739. var line = $(template).clone();
  740. line.appendTo("#colors");
  741. // init color wheel
  742. $("input[name='color']").wheelColorPicker({
  743. sliders: "wrgbp"
  744. }).on("sliderup", function() {
  745. var value = $(this).wheelColorPicker("getValue", "css");
  746. sendAction("color", {rgb: value});
  747. });
  748. // init bright slider
  749. $("#brightness").on("change", function() {
  750. var value = $(this).val();
  751. var parent = $(this).parents(".pure-g");
  752. $("span", parent).html(value);
  753. sendAction("color", {brightness: value});
  754. });
  755. }
  756. function initCCT() {
  757. // check if already initialized
  758. var done = $("#cct > div").length;
  759. if (done > 0) { return; }
  760. $("#miredsTemplate").children().clone().appendTo("#cct");
  761. $("#mireds").on("change", function() {
  762. var value = $(this).val();
  763. var parent = $(this).parents(".pure-g");
  764. $("span", parent).html(value);
  765. sendAction("mireds", {mireds: value});
  766. });
  767. }
  768. function initColorHSV() {
  769. // check if already initialized
  770. var done = $("#colors > div").length;
  771. if (done > 0) { return; }
  772. // add template
  773. var template = $("#colorHSVTemplate").children();
  774. var line = $(template).clone();
  775. line.appendTo("#colors");
  776. // init color wheel
  777. $("input[name='color']").wheelColorPicker({
  778. sliders: "whsvp"
  779. }).on("sliderup", function() {
  780. var color = $(this).wheelColorPicker("getColor");
  781. var value = parseInt(color.h * 360, 10) + "," + parseInt(color.s * 100, 10) + "," + parseInt(color.v * 100, 10);
  782. sendAction("color", {hsv: value});
  783. });
  784. }
  785. function initChannels(num) {
  786. // check if already initialized
  787. var done = $("#channels > div").length > 0;
  788. if (done) { return; }
  789. // does it have color channels?
  790. var colors = $("#colors > div").length > 0;
  791. // calculate channels to create
  792. var max = num;
  793. if (colors) {
  794. max = num % 3;
  795. if ((max > 0) & useWhite) {
  796. max--;
  797. if (useCCT) {
  798. max--;
  799. }
  800. }
  801. }
  802. var start = num - max;
  803. var onChannelSliderChange = function() {
  804. var id = $(this).attr("data");
  805. var value = $(this).val();
  806. var parent = $(this).parents(".pure-g");
  807. $("span", parent).html(value);
  808. sendAction("channel", {id: id, value: value});
  809. };
  810. // add templates
  811. var i = 0;
  812. var template = $("#channelTemplate").children();
  813. for (i=0; i<max; i++) {
  814. var channel_id = start + i;
  815. var line = $(template).clone();
  816. $("span.slider", line).attr("data", channel_id);
  817. $("input.slider", line).attr("data", channel_id).on("change", onChannelSliderChange);
  818. $("label", line).html("Channel #" + channel_id);
  819. line.appendTo("#channels");
  820. }
  821. for (i=0; i<num; i++) {
  822. $("select.islight").append(
  823. $("<option></option>").attr("value",i).text("Channel #" + i));
  824. }
  825. }
  826. <!-- endRemoveIf(!light)-->
  827. // -----------------------------------------------------------------------------
  828. // RFBridge
  829. // -----------------------------------------------------------------------------
  830. <!-- removeIf(!rfbridge)-->
  831. function rfbLearn() {
  832. var parent = $(this).parents(".pure-g");
  833. var input = $("input", parent);
  834. sendAction("rfblearn", {id: input.attr("data-id"), status: input.attr("data-status")});
  835. }
  836. function rfbForget() {
  837. var parent = $(this).parents(".pure-g");
  838. var input = $("input", parent);
  839. sendAction("rfbforget", {id: input.attr("data-id"), status: input.attr("data-status")});
  840. }
  841. function rfbSend() {
  842. var parent = $(this).parents(".pure-g");
  843. var input = $("input", parent);
  844. sendAction("rfbsend", {id: input.attr("data-id"), status: input.attr("data-status"), data: input.val()});
  845. }
  846. function addRfbNode() {
  847. var numNodes = $("#rfbNodes > legend").length;
  848. var template = $("#rfbNodeTemplate").children();
  849. var line = $(template).clone();
  850. var status = true;
  851. $("span", line).html(numNodes);
  852. $(line).find("input").each(function() {
  853. $(this).attr("data-id", numNodes);
  854. $(this).attr("data-status", status ? 1 : 0);
  855. status = !status;
  856. });
  857. $(line).find(".button-rfb-learn").on("click", rfbLearn);
  858. $(line).find(".button-rfb-forget").on("click", rfbForget);
  859. $(line).find(".button-rfb-send").on("click", rfbSend);
  860. line.appendTo("#rfbNodes");
  861. return line;
  862. }
  863. <!-- endRemoveIf(!rfbridge)-->
  864. // -----------------------------------------------------------------------------
  865. // Processing
  866. // -----------------------------------------------------------------------------
  867. function processData(data) {
  868. // title
  869. if ("app_name" in data) {
  870. var title = data.app_name;
  871. if ("app_version" in data) {
  872. title = title + " " + data.app_version;
  873. }
  874. $("span[name=title]").html(title);
  875. if ("hostname" in data) {
  876. title = data.hostname + " - " + title;
  877. }
  878. document.title = title;
  879. }
  880. Object.keys(data).forEach(function(key) {
  881. var i;
  882. var value = data[key];
  883. // ---------------------------------------------------------------------
  884. // Web mode
  885. // ---------------------------------------------------------------------
  886. if ("webMode" === key) {
  887. password = (1 === value);
  888. $("#layout").toggle(!password);
  889. $("#password").toggle(password);
  890. }
  891. // ---------------------------------------------------------------------
  892. // Actions
  893. // ---------------------------------------------------------------------
  894. if ("action" === key) {
  895. if ("reload" === data.action) { doReload(1000); }
  896. return;
  897. }
  898. // ---------------------------------------------------------------------
  899. // RFBridge
  900. // ---------------------------------------------------------------------
  901. <!-- removeIf(!rfbridge)-->
  902. if ("rfbCount" === key) {
  903. for (i=0; i<data.rfbCount; i++) { addRfbNode(); }
  904. return;
  905. }
  906. if ("rfbrawVisible" === key) {
  907. $("input[name='rfbcode']").attr("maxlength", 116);
  908. }
  909. if ("rfb" === key) {
  910. var nodes = data.rfb;
  911. for (i in nodes) {
  912. var node = nodes[i];
  913. $("input[name='rfbcode'][data-id='" + node.id + "'][data-status='" + node.status + "']").val(node.data);
  914. }
  915. return;
  916. }
  917. <!-- endRemoveIf(!rfbridge)-->
  918. // ---------------------------------------------------------------------
  919. // RFM69
  920. // ---------------------------------------------------------------------
  921. <!-- removeIf(!rfm69)-->
  922. if (key == "packet") {
  923. var packet = data.packet;
  924. var d = new Date();
  925. packets.row.add([
  926. d.toLocaleTimeString('en-US', { hour12: false }),
  927. packet.senderID,
  928. packet.packetID,
  929. packet.targetID,
  930. packet.key,
  931. packet.value,
  932. packet.rssi,
  933. packet.duplicates,
  934. packet.missing,
  935. ]).draw(false);
  936. return;
  937. }
  938. if (key == "mapping") {
  939. for (var i in data.mapping) {
  940. // add a new row
  941. addMapping();
  942. // get group
  943. var line = $("#mapping .pure-g")[i];
  944. // fill in the blanks
  945. var mapping = data.mapping[i];
  946. Object.keys(mapping).forEach(function(key) {
  947. var id = "input[name=" + key + "]";
  948. if ($(id, line).length) $(id, line).val(mapping[key]).attr("original", mapping[key]);
  949. });
  950. }
  951. return;
  952. }
  953. <!-- endRemoveIf(!rfm69)-->
  954. // ---------------------------------------------------------------------
  955. // Lights
  956. // ---------------------------------------------------------------------
  957. <!-- removeIf(!light)-->
  958. if ("rgb" === key) {
  959. initColorRGB();
  960. $("input[name='color']").wheelColorPicker("setValue", value, true);
  961. return;
  962. }
  963. if ("hsv" === key) {
  964. initColorHSV();
  965. // wheelColorPicker expects HSV to be between 0 and 1 all of them
  966. var chunks = value.split(",");
  967. var obj = {};
  968. obj.h = chunks[0] / 360;
  969. obj.s = chunks[1] / 100;
  970. obj.v = chunks[2] / 100;
  971. $("input[name='color']").wheelColorPicker("setColor", obj);
  972. return;
  973. }
  974. if ("brightness" === key) {
  975. $("#brightness").val(value);
  976. $("span.brightness").html(value);
  977. return;
  978. }
  979. if ("channels" === key) {
  980. var len = value.length;
  981. initChannels(len);
  982. for (i in value) {
  983. var ch = value[i];
  984. $("input.slider[data=" + i + "]").val(ch);
  985. $("span.slider[data=" + i + "]").html(ch);
  986. }
  987. return;
  988. }
  989. if ("mireds" === key) {
  990. $("#mireds").val(value);
  991. $("span.mireds").html(value);
  992. return;
  993. }
  994. if ("useWhite" === key) {
  995. useWhite = value;
  996. }
  997. if ("useCCT" === key) {
  998. initCCT();
  999. useCCT = value;
  1000. }
  1001. <!-- endRemoveIf(!light)-->
  1002. // ---------------------------------------------------------------------
  1003. // Sensors & Magnitudes
  1004. // ---------------------------------------------------------------------
  1005. <!-- removeIf(!sensor)-->
  1006. if ("magnitudes" === key) {
  1007. initMagnitudes(value);
  1008. for (i in value) {
  1009. var magnitude = value[i];
  1010. var error = magnitude.error || 0;
  1011. var text = (0 === error) ?
  1012. magnitude.value + magnitude.units :
  1013. magnitudeError(error);
  1014. var element = $("input[name='magnitude'][data='" + i + "']");
  1015. element.val(text);
  1016. $("div.hint", element.parent().parent()).html(magnitude.description);
  1017. }
  1018. return;
  1019. }
  1020. <!-- endRemoveIf(!sensor)-->
  1021. // ---------------------------------------------------------------------
  1022. // WiFi
  1023. // ---------------------------------------------------------------------
  1024. if ("maxNetworks" === key) {
  1025. maxNetworks = parseInt(value, 10);
  1026. return;
  1027. }
  1028. if ("wifi" === key) {
  1029. for (i in value) {
  1030. var wifi = value[i];
  1031. var nwk_line = addNetwork();
  1032. Object.keys(wifi).forEach(function(key) {
  1033. $("input[name='" + key + "']", nwk_line).val(wifi[key]);
  1034. });
  1035. }
  1036. return;
  1037. }
  1038. if ("scanResult" === key) {
  1039. $("div.scan.loading").hide();
  1040. $("#scanResult").show();
  1041. }
  1042. // -----------------------------------------------------------------------------
  1043. // Home Assistant
  1044. // -----------------------------------------------------------------------------
  1045. if ("haConfig" === key) {
  1046. $("#haConfig").show();
  1047. }
  1048. // -----------------------------------------------------------------------------
  1049. // Relays scheduler
  1050. // -----------------------------------------------------------------------------
  1051. if ("maxSchedules" === key) {
  1052. maxSchedules = parseInt(value, 10);
  1053. return;
  1054. }
  1055. if ("schedule" === key) {
  1056. for (i in value) {
  1057. var schedule = value[i];
  1058. var sch_line = addSchedule({ data: {schType: schedule["schType"] }});
  1059. Object.keys(schedule).forEach(function(key) {
  1060. var sch_value = schedule[key];
  1061. $("input[name='" + key + "']", sch_line).val(sch_value);
  1062. $("select[name='" + key + "']", sch_line).prop("value", sch_value);
  1063. $("input[type='checkbox'][name='" + key + "']", sch_line).prop("checked", sch_value);
  1064. });
  1065. }
  1066. return;
  1067. }
  1068. // ---------------------------------------------------------------------
  1069. // Relays
  1070. // ---------------------------------------------------------------------
  1071. if ("relayStatus" === key) {
  1072. initRelays(value);
  1073. for (i in value) {
  1074. $("input[name='relay'][data='" + i + "']").prop("checked", value[i]);
  1075. }
  1076. return;
  1077. }
  1078. // Relay configuration
  1079. if ("relayConfig" === key) {
  1080. initRelayConfig(value);
  1081. return;
  1082. }
  1083. // ---------------------------------------------------------------------
  1084. // Domoticz
  1085. // ---------------------------------------------------------------------
  1086. // Domoticz - Relays
  1087. if ("dczRelays" === key) {
  1088. createRelayList(value, "dczRelays", "dczRelayTemplate");
  1089. return;
  1090. }
  1091. // Domoticz - Magnitudes
  1092. <!-- removeIf(!sensor)-->
  1093. if ("dczMagnitudes" === key) {
  1094. createMagnitudeList(value, "dczMagnitudes", "dczMagnitudeTemplate");
  1095. return;
  1096. }
  1097. <!-- endRemoveIf(!sensor)-->
  1098. // ---------------------------------------------------------------------
  1099. // Thingspeak
  1100. // ---------------------------------------------------------------------
  1101. // Thingspeak - Relays
  1102. if ("tspkRelays" === key) {
  1103. createRelayList(value, "tspkRelays", "tspkRelayTemplate");
  1104. return;
  1105. }
  1106. // Thingspeak - Magnitudes
  1107. <!-- removeIf(!sensor)-->
  1108. if ("tspkMagnitudes" === key) {
  1109. createMagnitudeList(value, "tspkMagnitudes", "tspkMagnitudeTemplate");
  1110. return;
  1111. }
  1112. <!-- endRemoveIf(!sensor)-->
  1113. // ---------------------------------------------------------------------
  1114. // General
  1115. // ---------------------------------------------------------------------
  1116. // Messages
  1117. if ("message" === key) {
  1118. window.alert(messages[value]);
  1119. return;
  1120. }
  1121. // Web log
  1122. if ("weblog" === key) {
  1123. $("#weblog").append(new Text(value));
  1124. $("#weblog").scrollTop($("#weblog")[0].scrollHeight - $("#weblog").height());
  1125. return;
  1126. }
  1127. // Enable options
  1128. var position = key.indexOf("Visible");
  1129. if (position > 0 && position === key.length - 7) {
  1130. var module = key.slice(0,-7);
  1131. $(".module-" + module).css("display", "inherit");
  1132. return;
  1133. }
  1134. if ("deviceip" === key) {
  1135. var a_href = $("span[name='" + key + "']").parent();
  1136. a_href.attr("href", "//" + value);
  1137. a_href.next().attr("href", "telnet://" + value);
  1138. }
  1139. if ("now" === key) {
  1140. now = value;
  1141. return;
  1142. }
  1143. if ("free_size" === key) {
  1144. free_size = parseInt(value, 10);
  1145. }
  1146. // Pre-process
  1147. if ("mqttStatus" === key) {
  1148. value = value ? "CONNECTED" : "NOT CONNECTED";
  1149. }
  1150. if ("ntpStatus" === key) {
  1151. value = value ? "SYNC'D" : "NOT SYNC'D";
  1152. }
  1153. if ("uptime" === key) {
  1154. ago = 0;
  1155. var uptime = parseInt(value, 10);
  1156. var seconds = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1157. var minutes = uptime % 60; uptime = parseInt(uptime / 60, 10);
  1158. var hours = uptime % 24; uptime = parseInt(uptime / 24, 10);
  1159. var days = uptime;
  1160. value = days + "d " + zeroPad(hours, 2) + "h " + zeroPad(minutes, 2) + "m " + zeroPad(seconds, 2) + "s";
  1161. }
  1162. // ---------------------------------------------------------------------
  1163. // Matching
  1164. // ---------------------------------------------------------------------
  1165. var pre;
  1166. var post;
  1167. // Look for INPUTs
  1168. var input = $("input[name='" + key + "']");
  1169. if (input.length > 0) {
  1170. if (input.attr("type") === "checkbox") {
  1171. input.prop("checked", value);
  1172. } else if (input.attr("type") === "radio") {
  1173. input.val([value]);
  1174. } else {
  1175. pre = input.attr("pre") || "";
  1176. post = input.attr("post") || "";
  1177. input.val(pre + value + post);
  1178. }
  1179. }
  1180. // Look for SPANs
  1181. var span = $("span[name='" + key + "']");
  1182. if (span.length > 0) {
  1183. pre = span.attr("pre") || "";
  1184. post = span.attr("post") || "";
  1185. span.html(pre + value + post);
  1186. }
  1187. // Look for SELECTs
  1188. var select = $("select[name='" + key + "']");
  1189. if (select.length > 0) {
  1190. select.val(value);
  1191. }
  1192. });
  1193. // Auto generate an APIKey if none defined yet
  1194. if ($("input[name='apiKey']").val() === "") {
  1195. generateAPIKey();
  1196. }
  1197. resetOriginals();
  1198. initCheckboxes();
  1199. }
  1200. function hasChanged() {
  1201. var newValue, originalValue;
  1202. if ($(this).attr("type") === "checkbox") {
  1203. newValue = $(this).prop("checked");
  1204. originalValue = ($(this).attr("original") === "true");
  1205. } else {
  1206. newValue = $(this).val();
  1207. originalValue = $(this).attr("original");
  1208. }
  1209. var hasChanged = $(this).attr("hasChanged") || 0;
  1210. var action = $(this).attr("action");
  1211. if (typeof originalValue === "undefined") { return; }
  1212. if ("none" === action) { return; }
  1213. if (newValue !== originalValue) {
  1214. if (0 === hasChanged) {
  1215. ++numChanged;
  1216. if ("reconnect" === action) { ++numReconnect; }
  1217. if ("reboot" === action) { ++numReboot; }
  1218. if ("reload" === action) { ++numReload; }
  1219. $(this).attr("hasChanged", 1);
  1220. }
  1221. } else {
  1222. if (1 === hasChanged) {
  1223. --numChanged;
  1224. if ("reconnect" === action) { --numReconnect; }
  1225. if ("reboot" === action) { --numReboot; }
  1226. if ("reload" === action) { --numReload; }
  1227. $(this).attr("hasChanged", 0);
  1228. }
  1229. }
  1230. }
  1231. // -----------------------------------------------------------------------------
  1232. // Init & connect
  1233. // -----------------------------------------------------------------------------
  1234. function initUrls(root) {
  1235. var paths = ["ws", "upgrade", "config", "auth"];
  1236. urls["root"] = root;
  1237. paths.forEach(function(path) {
  1238. urls[path] = new URL(path, root);
  1239. urls[path].protocol = root.protocol;
  1240. });
  1241. if (root.protocol == "https:") {
  1242. urls.ws.protocol = "wss:";
  1243. } else {
  1244. urls.ws.protocol = "ws:";
  1245. }
  1246. }
  1247. function connectToURL(url) {
  1248. initUrls(url);
  1249. $.ajax({
  1250. 'method': 'GET',
  1251. 'crossDomain': true,
  1252. 'url': urls.auth.href,
  1253. 'xhrFields': { 'withCredentials': true }
  1254. }).done(function(data) {
  1255. if (websock) { websock.close(); }
  1256. websock = new WebSocket(urls.ws.href);
  1257. websock.onmessage = function(evt) {
  1258. var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
  1259. if (data) {
  1260. processData(data);
  1261. }
  1262. };
  1263. }).fail(function() {
  1264. // Nothing to do, reload page and retry
  1265. });
  1266. }
  1267. function connect(host) {
  1268. if (!host.startsWith("http:") && !host.startsWith("https:")) {
  1269. host = "http://" + host;
  1270. }
  1271. connectToURL(new URL(host));
  1272. }
  1273. function connectToCurrentURL() {
  1274. connectToURL(new URL(window.location));
  1275. }
  1276. function getParameterByName(name) {
  1277. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  1278. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  1279. }
  1280. $(function() {
  1281. initMessages();
  1282. loadTimeZones();
  1283. createCheckboxes();
  1284. setInterval(function() { keepTime(); }, 1000);
  1285. $("#menuLink").on("click", toggleMenu);
  1286. $(".pure-menu-link").on("click", showPanel);
  1287. $("progress").attr({ value: 0, max: 100 });
  1288. $(".button-update").on("click", doUpdate);
  1289. $(".button-update-password").on("click", doUpdatePassword);
  1290. $(".button-reboot").on("click", doReboot);
  1291. $(".button-reconnect").on("click", doReconnect);
  1292. $(".button-wifi-scan").on("click", doScan);
  1293. $(".button-ha-config").on("click", doHAConfig);
  1294. $(".button-dbgcmd").on("click", doDebugCommand);
  1295. $("input[name='dbgcmd']").enterKey(doDebugCommand);
  1296. $(".button-dbg-clear").on("click", doDebugClear);
  1297. $(".button-settings-backup").on("click", doBackup);
  1298. $(".button-settings-restore").on("click", doRestore);
  1299. $(".button-settings-factory").on("click", doFactoryReset);
  1300. $("#uploader").on("change", onFileUpload);
  1301. $(".button-upgrade").on("click", doUpgrade);
  1302. $(".button-apikey").on("click", generateAPIKey);
  1303. $(".button-upgrade-browse").on("click", function() {
  1304. $("input[name='upgrade']")[0].click();
  1305. return false;
  1306. });
  1307. $("input[name='upgrade']").change(function (){
  1308. var file = this.files[0];
  1309. $("input[name='filename']").val(file.name);
  1310. });
  1311. $(".button-add-network").on("click", function() {
  1312. $(".more", addNetwork()).toggle();
  1313. });
  1314. $(".button-add-switch-schedule").on("click", { schType: 1 }, addSchedule);
  1315. <!-- removeIf(!light)-->
  1316. $(".button-add-light-schedule").on("click", { schType: 2 }, addSchedule);
  1317. <!-- endRemoveIf(!light)-->
  1318. <!-- removeIf(!rfm69)-->
  1319. $(".button-add-mapping").on('click', addMapping);
  1320. $(".button-del-mapping").on('click', delMapping);
  1321. $(".button-clear-counts").on('click', doClearCounts);
  1322. $(".button-clear-messages").on('click', doClearMessages);
  1323. $(".button-clear-filters").on('click', doClearFilters);
  1324. $('#packets tbody').on('mousedown', 'td', doFilter);
  1325. packets = $('#packets').DataTable({
  1326. "paging": false
  1327. });
  1328. for (var i = 0; i < packets.columns()[0].length; i++) {
  1329. filters[i] = false;
  1330. }
  1331. <!-- endRemoveIf(!rfm69)-->
  1332. $(document).on("change", "input", hasChanged);
  1333. $(document).on("change", "select", hasChanged);
  1334. // don't autoconnect when opening from filesystem
  1335. if (window.location.protocol === "file:") { return; }
  1336. // Check host param in query string
  1337. if (host = getParameterByName('host')) {
  1338. connect(host);
  1339. } else {
  1340. connectToCurrentURL();
  1341. }
  1342. });