readconfig.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. function readconfig_Version(){
  2. return "1.0.0 - 20200910";
  3. }
  4. var config_gesamt;
  5. var config_split;
  6. var ref = new Array(2);
  7. var digit = new Array(0);
  8. var analog = new Array(0);
  9. var initalrotate = new Object();
  10. var analogEnabled = false;
  11. var posAnalogHeader;
  12. var digitsEnabled = false;
  13. var posDigitsHeader;
  14. function MakeRefZW(zw, _basepath){
  15. _filetarget = zw["name"].replace("/config/", "/img_tmp/");
  16. _filetarget = _filetarget.replace(".jpg", "_org.jpg");
  17. url = _basepath + "/editflow.html?task=cutref&in=/config/reference.jpg&out="+_filetarget+"&x=" + zw["x"] + "&y=" + zw["y"] + "&dx=" + zw["dx"] + "&dy=" + zw["dy"];
  18. var xhttp = new XMLHttpRequest();
  19. try {
  20. xhttp.open("GET", url, false);
  21. xhttp.send(); }
  22. catch (error)
  23. {
  24. // alert("Deleting Config.ini failed");
  25. }
  26. _filetarget2 = zw["name"].replace("/config/", "/img_tmp/");
  27. // _filetarget2 = _filetarget2.replace(".jpg", "_org.jpg");
  28. FileCopyOnServer(_filetarget, _filetarget2, _basepath);
  29. }
  30. function CopyReferenceToImgTmp(_basepath)
  31. {
  32. for (index = 0; index < 2; ++index)
  33. {
  34. _filenamevon = ref[index]["name"];
  35. _filenamenach = _filenamevon.replace("/config/", "/img_tmp/");
  36. FileDeleteOnServer(_filenamenach, _basepath);
  37. FileCopyOnServer(_filenamevon, _filenamenach, _basepath);
  38. _filenamevon = _filenamevon.replace(".jpg", "_org.jpg");
  39. _filenamenach = _filenamenach.replace(".jpg", "_org.jpg");
  40. FileDeleteOnServer(_filenamenach, _basepath);
  41. FileCopyOnServer(_filenamevon, _filenamenach, _basepath);
  42. }
  43. }
  44. function GetReferencesInfo(){
  45. return ref;
  46. }
  47. function ParseConfigAlignment(_aktline){
  48. var akt_ref = 0;
  49. ++_aktline;
  50. while ((_aktline < config_split.length)
  51. && !(config_split[_aktline][0] == "[")
  52. && !((config_split[_aktline][0] == ";") && (config_split[_aktline][1] == "["))) {
  53. var linesplit = ZerlegeZeile(config_split[_aktline]);
  54. if ((linesplit[0].toUpperCase() == "INITIALMIRROR") && (linesplit.length > 1))
  55. {
  56. initalrotate["mirror"] = linesplit[1].toUpperCase().localeCompare("TRUE") == 0;
  57. initalrotate["pos_config_mirror"] = _aktline;
  58. }
  59. if (((linesplit[0].toUpperCase() == "INITALROTATE") || (linesplit[0].toUpperCase() == "INITIALROTATE")) && (linesplit.length > 1))
  60. {
  61. initalrotate["angle"] = parseInt(linesplit[1]);
  62. initalrotate["pos_config"] = _aktline;
  63. }
  64. if (linesplit.length == 3)
  65. {
  66. ref[akt_ref] = new Object();
  67. ref[akt_ref]["pos_ref"] = _aktline;
  68. ref[akt_ref]["name"] = linesplit[0];
  69. ref[akt_ref]["x"] = linesplit[1];
  70. ref[akt_ref]["y"] = linesplit[2];
  71. akt_ref++;
  72. }
  73. ++_aktline;
  74. }
  75. return _aktline;
  76. }
  77. function ParseConfigDigit(_aktline){
  78. ++_aktline;
  79. digit.length = 0;
  80. while ((_aktline < config_split.length)
  81. && !(config_split[_aktline][0] == "[")
  82. && !((config_split[_aktline][0] == ";") && (config_split[_aktline][1] == "["))) {
  83. var linesplit = ZerlegeZeile(config_split[_aktline]);
  84. if (linesplit.length >= 5)
  85. {
  86. zw = new Object();
  87. zw["pos_ref"] = _aktline;
  88. zw["name"] = linesplit[0];
  89. zw["x"] = linesplit[1];
  90. zw["y"] = linesplit[2];
  91. zw["dx"] = linesplit[3];
  92. zw["dy"] = linesplit[4];
  93. zw["ar"] = parseFloat(linesplit[3]) / parseFloat(linesplit[4]);
  94. digit.push(zw);
  95. }
  96. ++_aktline;
  97. }
  98. return _aktline;
  99. }
  100. function GetAnalogEnabled() {
  101. return analogEnabled;
  102. }
  103. function GetDigitsEnabled() {
  104. return digitsEnabled;
  105. }
  106. function ParseConfigAnalog(_aktline){
  107. ++_aktline;
  108. analog.length = 0;
  109. while ((_aktline < config_split.length)
  110. && !(config_split[_aktline][0] == "[")
  111. && !((config_split[_aktline][0] == ";") && (config_split[_aktline][1] == "["))) {
  112. var linesplit = ZerlegeZeile(config_split[_aktline]);
  113. if (linesplit.length >= 5)
  114. {
  115. zw = new Object();
  116. zw["pos_ref"] = _aktline;
  117. zw["name"] = linesplit[0];
  118. zw["x"] = linesplit[1];
  119. zw["y"] = linesplit[2];
  120. zw["dx"] = linesplit[3];
  121. zw["dy"] = linesplit[4];
  122. zw["ar"] = parseFloat(linesplit[3]) / parseFloat(linesplit[4]);
  123. analog.push(zw);
  124. }
  125. ++_aktline;
  126. }
  127. return _aktline;
  128. }
  129. function getROIInfo(_typeROI){
  130. if (_typeROI == "[Digits]"){
  131. targetROI = digit;
  132. }
  133. if (_typeROI == "[Analog]"){
  134. targetROI = analog;
  135. }
  136. return targetROI.slice(); // Kopie senden, nicht orginal!!!
  137. }
  138. function SaveROIToConfig(_ROIInfo, _typeROI, _basepath, _enabled){
  139. if (_enabled) {
  140. text = _typeROI;
  141. }
  142. else {
  143. text = ";" + _typeROI;
  144. }
  145. if (_typeROI == "[Digits]"){
  146. config_split[posDigitsHeader] = text;
  147. targetROI = digit;
  148. }
  149. if (_typeROI == "[Analog]"){
  150. config_split[posAnalogHeader] = text;
  151. targetROI = analog;
  152. }
  153. // Abstimmen Anzahl ROIs:
  154. var _pos = targetROI[targetROI.length-1]["pos_ref"];
  155. for (var i = targetROI.length; i < _ROIInfo.length; ++i){
  156. var zw = config_split[config_split.length-1];
  157. config_split.push(zw);
  158. for (var j = config_split.length-2; j > _pos + 1; --j){
  159. config_split[j] = config_split[j-1];
  160. }
  161. }
  162. for (i = targetROI.length-1; i > _ROIInfo.length-1; --i){
  163. var _zwpos = targetROI[i]["pos_ref"];
  164. config_split.splice(_zwpos, 1);
  165. }
  166. var linewrite = 0;
  167. for (i = 0; i < _ROIInfo.length; ++i){
  168. if (i < targetROI.length){
  169. linewrite = targetROI[i]["pos_ref"];
  170. }
  171. else {
  172. linewrite++;
  173. }
  174. config_split[linewrite] = _ROIInfo[i]["name"] + " " + _ROIInfo[i]["x"] + " " + _ROIInfo[i]["y"] + " " + _ROIInfo[i]["dx"] + " " + _ROIInfo[i]["dy"];
  175. }
  176. SaveConfigToServer(_basepath);
  177. }
  178. function ParseConfig() {
  179. config_split = config_gesamt.split("\n");
  180. var aktline = 0;
  181. while (aktline < config_split.length){
  182. if ((config_split[aktline].trim().toUpperCase() == "[ALIGNMENT]") || (config_split[aktline].trim().toUpperCase() == ";[ALIGNMENT]")){
  183. aktline = ParseConfigAlignment(aktline);
  184. continue;
  185. }
  186. if ((config_split[aktline].trim().toUpperCase() == "[DIGITS]") || (config_split[aktline].trim().toUpperCase() == ";[DIGITS]")){
  187. posDigitsHeader = aktline;
  188. if (config_split[aktline][0] == "[") {
  189. digitsEnabled = true;
  190. }
  191. aktline = ParseConfigDigit(aktline);
  192. continue;
  193. }
  194. if ((config_split[aktline].trim().toUpperCase() == "[ANALOG]") || (config_split[aktline].trim().toUpperCase() == ";[ANALOG]")) {
  195. posAnalogHeader = aktline;
  196. if (config_split[aktline][0] == "[") {
  197. analogEnabled = true;
  198. }
  199. aktline = ParseConfigAnalog(aktline);
  200. continue;
  201. }
  202. aktline++;
  203. }
  204. }
  205. function getPreRotate(){
  206. return initalrotate["angle"];
  207. }
  208. function setPreRotate(_prerotate){
  209. initalrotate["angle"] = _prerotate;
  210. }
  211. function getMirror(){
  212. if (initalrotate.hasOwnProperty("mirror")) {
  213. return initalrotate["mirror"];
  214. }
  215. return false;
  216. }
  217. function setMirror(_mirror){
  218. initalrotate["mirror"] = _mirror;
  219. }
  220. function SaveCanvasToImage(_canvas, _filename, _delete = true, _basepath = ""){
  221. var JPEG_QUALITY=0.8;
  222. var dataUrl = _canvas.toDataURL('image/jpeg', JPEG_QUALITY);
  223. var rtn = dataURLtoBlob(dataUrl);
  224. if (_delete) {
  225. FileDeleteOnServer(_filename, _basepath);
  226. }
  227. FileSendContent(rtn, _filename, _basepath);
  228. }
  229. function SaveConfigToServer(_basepath){
  230. // leere Zeilen am Ende löschen
  231. var zw = config_split.length - 1;
  232. while (config_split[zw] == "") {
  233. config_split.pop();
  234. }
  235. var config_gesamt = "";
  236. for (var i = 0; i < config_split.length; ++i)
  237. {
  238. config_gesamt = config_gesamt + config_split[i] + "\n";
  239. }
  240. FileDeleteOnServer("/config/config.ini", _basepath);
  241. FileSendContent(config_gesamt, "/config/config.ini", _basepath);
  242. }
  243. function UpdateConfigFileReferenceChange(_basepath){
  244. for (var _index = 0; _index < ref.length; ++_index){
  245. var zeile = ref[_index]["name"] + " " + ref[_index]["x"] + " " + ref[_index]["y"];
  246. var _pos = ref[_index]["pos_ref"];
  247. config_split[_pos] = zeile;
  248. }
  249. zeile = "InitialRotate = " + initalrotate["angle"];
  250. var _pos = initalrotate["pos_config"];
  251. config_split[_pos] = zeile;
  252. var mirror = false;
  253. if (initalrotate.hasOwnProperty("mirror")) {
  254. mirror = initalrotate["mirror"];
  255. }
  256. var mirror_pos = -1;
  257. if (initalrotate.hasOwnProperty("pos_config_mirror")) {
  258. mirror_pos = initalrotate["pos_config_mirror"];
  259. }
  260. if (mirror_pos > -1) {
  261. if (mirror) {
  262. config_split[mirror_pos] = "InitialMirror = true";
  263. }
  264. else {
  265. config_split[mirror_pos] = "InitialMirror = false";
  266. }
  267. }
  268. else {
  269. if (mirror) { // neue Zeile muss an der richtigen Stelle eingefügt werden - hier direct nach [Alignment]
  270. var aktline = 0;
  271. while (aktline < config_split.length){
  272. if (config_split[aktline].trim() == "[Alignment]") {
  273. break;
  274. }
  275. aktline++
  276. }
  277. // fuege neue Zeile in config_split ein
  278. var zw = config_split[config_split.length-1];
  279. config_split.push(zw);
  280. for (var j = config_split.length-2; j > aktline + 1; --j){
  281. config_split[j] = config_split[j-1];
  282. }
  283. config_split[aktline + 1] = "InitialMirror = True"
  284. }
  285. }
  286. SaveConfigToServer(_basepath);
  287. }
  288. function UpdateConfigReference(zw, _basepath){
  289. for (var index = 0; index < 2; ++index)
  290. {
  291. var zeile = zw[index]["name"] + " " + zw[index]["x"] + " " + zw[index]["y"];
  292. var _pos = zw[index]["pos_ref"];
  293. config_split[_pos] = zeile;
  294. _filenamenach = ref[index]["name"];
  295. _filenamevon = _filenamenach.replace("/config/", "/img_tmp/");
  296. FileDeleteOnServer(_filenamenach, _basepath);
  297. FileCopyOnServer(_filenamevon, _filenamenach, _basepath);
  298. _filenamenach = _filenamenach.replace(".jpg", "_org.jpg");
  299. _filenamevon = _filenamevon.replace(".jpg", "_org.jpg");
  300. FileDeleteOnServer(_filenamenach, _basepath);
  301. FileCopyOnServer(_filenamevon, _filenamenach, _basepath);
  302. }
  303. SaveConfigToServer(_basepath);
  304. }
  305. function MakeContrastImageZW(zw, _enhance, _basepath){
  306. _filename = zw["name"].replace("/config/", "/img_tmp/");
  307. url = _basepath + "/editflow.html?task=cutref&in=/config/reference.jpg&out=" + _filename + "&x=" + zw["x"] + "&y=" + zw["y"] + "&dx=" + zw["dx"] + "&dy=" + zw["dy"];
  308. if (_enhance == true){
  309. url = url + "&enhance=true";
  310. }
  311. var xhttp = new XMLHttpRequest();
  312. try {
  313. xhttp.open("GET", url, false);
  314. xhttp.send(); }
  315. catch (error)
  316. {
  317. // alert("Deleting Config.ini failed");
  318. }
  319. }
  320. function GetReferenceSize(name){
  321. img = new Image();
  322. var xhttp = new XMLHttpRequest();
  323. url = "http://192.168.178.22/fileserver" + name;
  324. xhttp.open("GET", url, false);
  325. xhttp.send();
  326. var response = xhttp.responseText;
  327. var binary = ""
  328. for (var responseText = xhttp.responseText, responseTextLen = responseText.length, binary = "", i = 0; i < responseTextLen; ++i) {
  329. binary += String.fromCharCode(responseText.charCodeAt(i) & 255)
  330. }
  331. img.src = 'data:image/jpeg;base64,'+ window.btoa(binary);
  332. return [img.width, img.height];
  333. }
  334. function getConfig() {
  335. return config_gesamt;
  336. }
  337. function dataURLtoBlob(dataurl) {
  338. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  339. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  340. while(n--){
  341. u8arr[n] = bstr.charCodeAt(n);
  342. }
  343. return new Blob([u8arr], {type:mime});
  344. }
  345. function SaveReferenceImage(_id_canvas, _filename, _doDelete, _basepath = ""){
  346. if (_doDelete){
  347. FileDeleteOnServer(_filename, _basepath);
  348. }
  349. var canvas = document.getElementById(_id_canvas);
  350. var JPEG_QUALITY=0.8;
  351. var dataUrl = canvas.toDataURL('image/jpeg', JPEG_QUALITY);
  352. var rtn = dataURLtoBlob(dataUrl);
  353. if (!FileSendContent(rtn, _filename, _basepath)){
  354. alert("Error on saving reference image (" + _filename + ")!\nPlease retry.");
  355. location.reload();
  356. };
  357. }