readconfigcommon.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. async function SaveConfigToServer(_domainname) {
  2. while (config_split.length > 0 && config_split[config_split.length - 1] === "") {
  3. config_split.pop();
  4. }
  5. config_gesamt = config_split.join("\n") + "\n";
  6. await FileDeleteOnServer("/config/config.ini", _domainname);
  7. return await FileSendContent(config_gesamt, "/config/config.ini", _domainname);
  8. }
  9. async function UpdateConfig(zw, _index, _enhance, _domainname) {
  10. var namezw = zw["name"];
  11. await FileCopyOnServer("/img_tmp/ref_zw.jpg", namezw, _domainname);
  12. namezw = zw["name"].replace(".jpg", "_org.jpg");
  13. await FileCopyOnServer("/img_tmp/ref_zw_org.jpg", namezw, _domainname);
  14. }
  15. function createReader(file) {
  16. var image = new Image();
  17. reader.onload = function(evt) {
  18. var image = new Image();
  19. image.onload = function(evt) {
  20. var width = this.width;
  21. var height = this.height;
  22. };
  23. image.src = evt.target.result;
  24. };
  25. reader.readAsDataURL(file);
  26. }
  27. function ZerlegeZeile(input, delimiter = " =\t\r") {
  28. var Output = Array(0);
  29. // delimiter = " =,\t";
  30. /* The input can have multiple formats:
  31. * - key = value
  32. * - key = value1 value2 value3 ...
  33. * - key value1 value2 value3 ...
  34. *
  35. * Examples:
  36. * - ImageSize = VGA
  37. * - IO0 = input disabled 10 false false
  38. * - main.dig1 28 144 55 100 false
  39. *
  40. * This causes issues eg. if a password key has a whitespace or equal sign in its value.
  41. * As a workaround and to not break any legacy usage, we enforce to only use the
  42. * equal sign, if the key is "password"
  43. */
  44. if (input.includes("password") || input.includes("Token")) { // Line contains a password, use the equal sign as the only delimiter and only split on first occurrence
  45. var pos = input.indexOf("=");
  46. delimiter = " \t\r"
  47. Output.push(trim(input.substr(0, pos), delimiter));
  48. Output.push(trim(input.substr(pos +1, input.length), delimiter));
  49. }
  50. else { // Legacy Mode
  51. input = trim(input, delimiter);
  52. var pos = findDelimiterPos(input, delimiter);
  53. var token;
  54. while (pos > -1) {
  55. token = input.substr(0, pos);
  56. token = trim(token, delimiter);
  57. Output.push(token);
  58. input = input.substr(pos+1, input.length);
  59. input = trim(input, delimiter);
  60. pos = findDelimiterPos(input, delimiter);
  61. }
  62. Output.push(input);
  63. }
  64. return Output;
  65. }
  66. function findDelimiterPos(input, delimiter) {
  67. var pos = -1;
  68. var zw;
  69. var akt_del;
  70. for (var anz = 0; anz < delimiter.length; ++anz) {
  71. akt_del = delimiter[anz];
  72. zw = input.indexOf(akt_del);
  73. if (zw > -1) {
  74. if (pos > -1) {
  75. if (zw < pos) {
  76. pos = zw;
  77. }
  78. }
  79. else {
  80. pos = zw;
  81. }
  82. }
  83. }
  84. return pos;
  85. }
  86. function trim(istring, adddelimiter) {
  87. while ((istring.length > 0) && (adddelimiter.indexOf(istring[0]) >= 0)) {
  88. istring = istring.substr(1, istring.length-1);
  89. }
  90. while ((istring.length > 0) && (adddelimiter.indexOf(istring[istring.length-1]) >= 0)) {
  91. istring = istring.substr(0, istring.length-1);
  92. }
  93. return istring;
  94. }
  95. function getConfig() {
  96. return config_gesamt;
  97. }
  98. async function loadConfig(_domainname) {
  99. try {
  100. const response = await fetch(_domainname + "/fileserver/config/config.ini");
  101. if (!response.ok) {
  102. console.warn("Could not load configuration:", response.status, response.statusText);
  103. return false;
  104. }
  105. config_gesamt = (await response.text()).replace("InitalRotate", "InitialRotate");
  106. return true;
  107. } catch (error) {
  108. console.warn("Could not load configuration:", error);
  109. return false;
  110. }
  111. }
  112. function dataURLtoBlob(dataurl) {
  113. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  114. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  115. while(n--){
  116. u8arr[n] = bstr.charCodeAt(n);
  117. }
  118. return new Blob([u8arr], {type:mime});
  119. }
  120. async function FileCopyOnServer(_source, _target, _domainname = "") {
  121. try {
  122. const url = _domainname + "/editflow?task=copy&in=" + _source + "&out=" + _target;
  123. const response = await fetch(url);
  124. if (!response.ok) {
  125. console.warn("File copy failed:", response.status, response.statusText);
  126. }
  127. return response.ok;
  128. } catch (error) {
  129. console.warn("File copy failed:", error);
  130. return false;
  131. }
  132. }
  133. async function FileDeleteOnServer(_filename, _domainname = "") {
  134. try {
  135. const response = await fetch(_domainname + "/delete" + _filename, { method: "POST" });
  136. if (!response.ok) {
  137. console.warn("File delete failed:", response.status, response.statusText);
  138. }
  139. return response.ok;
  140. } catch (error) {
  141. console.warn("File delete failed:", error);
  142. return false;
  143. }
  144. }
  145. async function FileSendContent(_content, _filename, _domainname = "") {
  146. try {
  147. const response = await fetch(_domainname + "/upload" + _filename, {
  148. method: "POST",
  149. body: _content
  150. });
  151. if (!response.ok) {
  152. firework.launch("An error occurred while saving the file: " + response.statusText, "danger", 30000);
  153. }
  154. return response.ok;
  155. } catch (error) {
  156. firework.launch("Failed to send file to device: " + error, "danger", 30000);
  157. return false;
  158. }
  159. }
  160. async function MakeRefImageZW(zw, _enhance, _domainname) {
  161. var _filename = zw["name"].replace("/config/", "/img_tmp/");
  162. var url = _domainname + "/editflow?task=cutref&in=/config/reference.jpg&out=" + _filename +
  163. "&x=" + zw["x"] + "&y=" + zw["y"] + "&dx=" + zw["dx"] + "&dy=" + zw["dy"];
  164. if (_enhance === true) {
  165. url += "&enhance=true";
  166. }
  167. try {
  168. const response = await fetch(url);
  169. if (!response.ok) {
  170. console.warn("Reference image update failed:", response.status, response.statusText);
  171. return false;
  172. }
  173. if ((await response.text()) === "CutImage Done") {
  174. firework.launch(
  175. _enhance === true ? "Image Contrast got enhanced" : "Alignment Marker have been updated",
  176. "success",
  177. 5000
  178. );
  179. return true;
  180. }
  181. } catch (error) {
  182. console.warn("Reference image update failed:", error);
  183. }
  184. return false;
  185. }