readconfigcommon.js 9.2 KB

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