readconfigcommon.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. function readconfig_Version(){
  2. return "1.0.0 - 20200910";
  3. }
  4. function SaveConfigToServer(_basepath){
  5. // leere Zeilen am Ende löschen
  6. var zw = config_split.length - 1;
  7. while (config_split[zw] == "") {
  8. config_split.pop();
  9. }
  10. var config_gesamt = "";
  11. for (var i = 0; i < config_split.length; ++i)
  12. {
  13. config_gesamt = config_gesamt + config_split[i] + "\n";
  14. }
  15. FileDeleteOnServer("/config/config.ini", _basepath);
  16. FileSendContent(config_gesamt, "/config/config.ini", _basepath);
  17. }
  18. function UpdateConfig(zw, _index, _enhance, _basepath){
  19. var namezw = zw["name"];
  20. FileCopyOnServer("/img_tmp/ref_zw.jpg", namezw, _basepath);
  21. var namezw = zw["name"].replace(".jpg", "_org.jpg");
  22. FileCopyOnServer("/img_tmp/ref_zw_org.jpg", namezw, _basepath);
  23. }
  24. function createReader(file) {
  25. var image = new Image();
  26. reader.onload = function(evt) {
  27. var image = new Image();
  28. image.onload = function(evt) {
  29. var width = this.width;
  30. var height = this.height;
  31. alert (width); // will produce something like 198
  32. };
  33. image.src = evt.target.result;
  34. };
  35. reader.readAsDataURL(file);
  36. }
  37. function ZerlegeZeile(input, delimiter = " =\t\r")
  38. {
  39. var Output = Array(0);
  40. // delimiter = " =,\t";
  41. input = trim(input, delimiter);
  42. /* The input can have multiple formats:
  43. * - key = value
  44. * - key = value1 value2 value3 ...
  45. * - key value1 value2 value3 ...
  46. *
  47. * Examples:
  48. * - ImageSize = VGA
  49. * - IO0 = input disabled 10 false false
  50. * - main.dig1 28 144 55 100 false
  51. *
  52. * This causes issues eg. if a password key has a whitespace or equal sign in its value.
  53. * As a workaround and to not break any legacy usage, we enforce to only use the
  54. * equal sign, if the key is "password"
  55. */
  56. if (input.includes("password")) { // Line contains a password, use the equal sign as the only delimiter and only split on first occurrence
  57. var pos = input.indexOf("=");
  58. Output.push(trim(input.substr(0, pos), delimiter));
  59. Output.push(trim(input.substr(pos +1, input.length), delimiter));
  60. }
  61. else { // Legacy Mode
  62. var pos = findDelimiterPos(input, delimiter);
  63. var token;
  64. while (pos > -1) {
  65. token = input.substr(0, pos);
  66. token = trim(token, delimiter);
  67. Output.push(token);
  68. input = input.substr(pos+1, input.length);
  69. input = trim(input, delimiter);
  70. pos = findDelimiterPos(input, delimiter);
  71. }
  72. Output.push(input);
  73. }
  74. return Output;
  75. }
  76. function findDelimiterPos(input, delimiter)
  77. {
  78. var pos = -1;
  79. var zw;
  80. var akt_del;
  81. for (var anz = 0; anz < delimiter.length; ++anz)
  82. {
  83. akt_del = delimiter[anz];
  84. zw = input.indexOf(akt_del);
  85. if (zw > -1)
  86. {
  87. if (pos > -1)
  88. {
  89. if (zw < pos)
  90. pos = zw;
  91. }
  92. else
  93. pos = zw;
  94. }
  95. }
  96. return pos;
  97. }
  98. function trim(istring, adddelimiter)
  99. {
  100. while ((istring.length > 0) && (adddelimiter.indexOf(istring[0]) >= 0)){
  101. istring = istring.substr(1, istring.length-1);
  102. }
  103. while ((istring.length > 0) && (adddelimiter.indexOf(istring[istring.length-1]) >= 0)){
  104. istring = istring.substr(0, istring.length-1);
  105. }
  106. return istring;
  107. }
  108. function getConfig()
  109. {
  110. return config_gesamt;
  111. }
  112. function loadConfig(_basepath) {
  113. var xhttp = new XMLHttpRequest();
  114. try {
  115. url = _basepath + '/fileserver/config/config.ini';
  116. xhttp.open("GET", url, false);
  117. xhttp.send();
  118. config_gesamt = xhttp.responseText;
  119. config_gesamt = config_gesamt.replace("InitalRotate", "InitialRotate"); // Korrigiere Schreibfehler in config.ini !!!!!
  120. }
  121. catch (error)
  122. {
  123. // alert("Deleting Config.ini failed");
  124. }
  125. return true;
  126. }
  127. function dataURLtoBlob(dataurl) {
  128. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  129. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  130. while(n--){
  131. u8arr[n] = bstr.charCodeAt(n);
  132. }
  133. return new Blob([u8arr], {type:mime});
  134. }
  135. function FileCopyOnServer(_source, _target, _basepath = ""){
  136. url = _basepath + "/editflow?task=copy&in=" + _source + "&out=" + _target;
  137. var xhttp = new XMLHttpRequest();
  138. try {
  139. xhttp.open("GET", url, false);
  140. xhttp.send(); }
  141. catch (error)
  142. {
  143. // alert("Deleting Config.ini failed");
  144. }
  145. }
  146. function FileDeleteOnServer(_filename, _basepath = ""){
  147. var xhttp = new XMLHttpRequest();
  148. var okay = false;
  149. xhttp.onreadystatechange = function() {
  150. if (xhttp.readyState == 4) {
  151. if (xhttp.status == 200) {
  152. okay = true;
  153. } else if (xhttp.status == 0) {
  154. // alert("Server closed the connection on delete abruptly!");
  155. // location.reload()
  156. } else {
  157. // alert(xhttp.status + " Error!\n" + xhttp.responseText);
  158. // location.reload()
  159. }
  160. }
  161. };
  162. try {
  163. var url = _basepath + "/delete" + _filename;
  164. xhttp.open("POST", url, false);
  165. xhttp.send();
  166. }
  167. catch (error)
  168. {
  169. // alert("Deleting Config.ini failed");
  170. }
  171. return okay;
  172. }
  173. function FileSendContent(_content, _filename, _basepath = ""){
  174. var xhttp = new XMLHttpRequest();
  175. var okay = false;
  176. xhttp.onreadystatechange = function() {
  177. if (xhttp.readyState == 4) {
  178. if (xhttp.status == 200) {
  179. okay = true;
  180. } else if (xhttp.status == 0) {
  181. alert("Server closed the connection abruptly!");
  182. } else {
  183. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  184. }
  185. }
  186. };
  187. try {
  188. upload_path = _basepath + "/upload" + _filename;
  189. xhttp.open("POST", upload_path, false);
  190. xhttp.send(_content);
  191. }
  192. catch (error)
  193. {
  194. // alert("Deleting Config.ini failed");
  195. }
  196. return okay;
  197. }
  198. function SaveCanvasToImage(_canvas, _filename, _delete = true, _basepath = ""){
  199. var JPEG_QUALITY=0.8;
  200. var dataUrl = _canvas.toDataURL('image/jpeg', JPEG_QUALITY);
  201. var rtn = dataURLtoBlob(dataUrl);
  202. if (_delete) {
  203. FileDeleteOnServer(_filename, _basepath);
  204. }
  205. FileSendContent(rtn, _filename, _basepath);
  206. }
  207. function MakeContrastImageZW(zw, _enhance, _basepath){
  208. _filename = zw["name"].replace("/config/", "/img_tmp/");
  209. url = _basepath + "/editflow?task=cutref&in=/config/reference.jpg&out=" + _filename + "&x=" + zw["x"] + "&y=" + zw["y"] + "&dx=" + zw["dx"] + "&dy=" + zw["dy"];
  210. if (_enhance == true){
  211. url = url + "&enhance=true";
  212. }
  213. var xhttp = new XMLHttpRequest();
  214. try {
  215. xhttp.open("GET", url, false);
  216. xhttp.send(); }
  217. catch (error)
  218. {
  219. // alert("Deleting Config.ini failed");
  220. }
  221. }
  222. function MakeRefZW(zw, _basepath){
  223. _filetarget = zw["name"].replace("/config/", "/img_tmp/");
  224. _filetarget = _filetarget.replace(".jpg", "_org.jpg");
  225. url = _basepath + "/editflow?task=cutref&in=/config/reference.jpg&out="+_filetarget+"&x=" + zw["x"] + "&y=" + zw["y"] + "&dx=" + zw["dx"] + "&dy=" + zw["dy"];
  226. var xhttp = new XMLHttpRequest();
  227. try {
  228. xhttp.open("GET", url, false);
  229. xhttp.send(); }
  230. catch (error)
  231. {
  232. // alert("Deleting Config.ini failed");
  233. }
  234. _filetarget2 = zw["name"].replace("/config/", "/img_tmp/");
  235. // _filetarget2 = _filetarget2.replace(".jpg", "_org.jpg");
  236. FileCopyOnServer(_filetarget, _filetarget2, _basepath);
  237. }