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.

1630 lines
47 KiB

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