package it.acxent.pg; import it.acxent.common.StatusMsg; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.db.ResParm; import it.acxent.db.WcString; import it.acxent.util.SimpleDateFormat; import it.acxent.util.Timer; import it.acxent.util.Vectumerator; import java.io.File; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.HashSet; import java.util.stream.Stream; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecuteResultHandler; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteResultHandler; import org.apache.commons.exec.ExecuteStreamHandler; import org.apache.commons.exec.ExecuteWatchdog; import org.apache.commons.exec.PumpStreamHandler; import org.apache.commons.io.output.ByteArrayOutputStream; public class PuntoFoto extends _PgAdapter implements Serializable { private static HashSet hsOcrThread = new HashSet<>(); private static final long serialVersionUID = 1511425419487L; public static final long INDEX_TYPE_NONE = 0L; public static final long INDEX_TYPE_TEMPI = 1L; public static final long INDEX_TYPE_MANUALE = 2L; private long id_puntoFoto; private long id_gara; private String descrizionePuntoFoto; private String pathRelativoFoto; private String tipoPuntoFoto; private Gara gara; private String tipoPuntoFotoNew; private long flgIndexOk; private long id_puntoFotoExport; class ThreadIndicizzaPuntofoto extends Thread { private PuntoFoto puntoFoto; public ThreadIndicizzaPuntofoto(PuntoFoto puntoFoto) { this.puntoFoto = puntoFoto; if (!PuntoFoto.isThreadAttivo()) { PuntoFoto.threadPuntoFoto = true; start(); } } public void run() { boolean debug = false; String TAG_THREAD_MSG = "INDICIZZA PUNTOFOTO " + this.puntoFoto.getDescrizione(); Timestamp start = new Timestamp(Calendar.getInstance().getTimeInMillis()); ResParm rp = new ResParm(true); StringBuffer err = new StringBuffer(); int i = 0; int se1 = 10; int se2 = 100; int fotoInviate = 0, fotoErrate = 0; try { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "...inizio ..."); if (this.puntoFoto.getId_puntoFoto() > 0L) { Foto foto = new Foto(this.puntoFoto.getApFull()); FotoCR fotoCR = new FotoCR(); fotoCR.setId_puntoFoto(this.puntoFoto.getId_puntoFoto()); Vectumerator vec = foto.findByCR(fotoCR, 0, 0); while (vec.hasMoreElements()) { Foto row = (Foto)vec.nextElement(); Foto.creaRiduzioniPerWeb(this.puntoFoto.getGara(), row.getFileNamePercorsoCompleto(), null, false); i++; StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "create " + i + " riduzioni su " + vec.getTotNumberOfRecords()); if (se1 > 0 && i % se1 == 0) System.out.print("."); if (se2 > 0 && i % se2 == 0) System.out.println("" + i + " su " + i); } } else { err.append("ERRORE!! punto foto INESISTENTE"); err.append("\n"); } } catch (Exception e) { err.append("ERRORE!! " + e.getMessage()); err.append("\n"); } rp.setMsg("craete " + i + " preview"); Timestamp stop = new Timestamp(Calendar.getInstance().getTimeInMillis()); SimpleDateFormat dfMS = new SimpleDateFormat("HH:mm:ss "); System.out.println(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + "Risultato import:\n" + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n\nCREAZIONE PREVIEW WWW concluso\n****************"); System.out.println(TAG_THREAD_MSG + " **************************************** ERRORI EXPORT FOTO SU WWW ****************************************"); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " concluso: DURATA: " + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "Risultato invio:\n" + rp.getMsg()); try { sleep(10000L); } catch (Exception e) {} StatusMsg.deleteMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG); PuntoFoto.threadPuntoFoto = false; } } class ThreadOcrPuntofoto extends Thread { private PuntoFoto puntoFoto; public ThreadOcrPuntofoto(PuntoFoto puntoFoto) { this.puntoFoto = puntoFoto; if (PuntoFoto.setThreadOcrAttivo(puntoFoto.getId_puntoFoto())) start(); } public void runPyton() { String TAG_THREAD_MSG = "OCR GARA " + this.puntoFoto.getId_gara() + " " + this.puntoFoto.getGara().getDescrizione() + " PUNTOFOTO " + this.puntoFoto.getId_puntoFoto() + " " + this.puntoFoto.getDescrizionePuntoFoto(); Timestamp start = new Timestamp(Calendar.getInstance().getTimeInMillis()); ResParm rp = new ResParm(true); StringBuffer err = new StringBuffer(); String COMANDOSH = "/bin/sh "; String COMANDO_PRODUZIONE = "cd /home/sites/bin/ && /usr/local/bin/python3 /home/sites/bin/det.py -d $1 -c $2 && echo '******* fine indicizzazione OCR'"; String COMANDO_TEST = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_TEST").getTesto() + " "; String END_COMMAND_TAG = "******* fine indicizzazione OCR"; String COMANDO = "cd /home/sites/bin/ && /usr/local/bin/python3 /home/sites/bin/det.py -d $1 -c $2 && echo '******* fine indicizzazione OCR'"; String dirDaIndicizzare = PuntoFoto.this.getPathCompletoFoto(); String pathRisultato = PuntoFoto.this.getDocBase() + "_csv/"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); String dataFile = df.format(DBAdapter.getToday()); String nomeFileCsv = "" + PuntoFoto.this.getId_gara() + "-" + PuntoFoto.this.getId_gara() + "_" + PuntoFoto.this.getId_puntoFoto() + ".csv"; String TMP_PRODUZIONE_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto(); String TMP_TEST_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto(); String LINEE_FILE = TMP_PRODUZIONE_LINEE_FILE; String fineNameFinale = pathRisultato + pathRisultato; String comandoParm = nomeFileCsv + " " + nomeFileCsv + " " + dirDaIndicizzare; String comandoCompleto = COMANDO + COMANDO; System.out.println(comandoCompleto); PuntoFoto.this.getApFull().writeLog(comandoCompleto); long lineCount = 0L; try { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "eseguo " + comandoCompleto); DefaultExecutor defaultExecutor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(300000L); defaultExecutor.setWatchdog(watchdog); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); defaultExecutor.setStreamHandler((ExecuteStreamHandler)psh); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); CommandLine cl = null; COMANDO = COMANDO.replace("$1", dirDaIndicizzare); LINEE_FILE = pathRisultato + pathRisultato; COMANDO = COMANDO.replace("$2", LINEE_FILE); cl = new CommandLine(COMANDO); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv); System.out.println("chiamata ocr: " + COMANDO); defaultExecutor.execute(cl, (ExecuteResultHandler)resultHandler); String temp = ""; int numeroCicli = 0; File tmpFile = new File(LINEE_FILE); int numTentativi = 0; while (!tmpFile.exists() && numTentativi < 2) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv + DBAdapter.charRight("", numTentativi, ".")); sleep(2000L); numTentativi++; } Stream stream = null; if (tmpFile.exists()) while (temp.indexOf("******* fine indicizzazione OCR") < 0) { String[] arr; numeroCicli++; temp = stdout.toString().trim(); if (temp.indexOf("\n") > 0) { arr = temp.split("\n"); } else { arr = temp.split("\r"); } System.out.println(arr.length); if (arr != null) temp = arr[arr.length - 1]; System.out.println("ultima riga: " + temp); if (tmpFile.exists()) { try { stream = Files.lines(Paths.get(LINEE_FILE), StandardCharsets.UTF_8); lineCount = stream.count() - 1L; } catch (Exception e) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Eccezione file " + nomeFileCsv + ": " + e.getMessage()); } finally { if (stream != null) { stream.close(); stream = null; } } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " waiting.... " + numeroCicli + " Trovate " + lineCount + " Foto con numeri - " + temp); System.out.println(TAG_THREAD_MSG + " waiting.... " + TAG_THREAD_MSG + " Trovate " + numeroCicli + " Foto con numeri: " + lineCount); } sleep(2000L); } temp = stdout.toString().trim(); System.out.println("stout: " + temp); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); System.out.println(" fine waitfor: exit val= " + exitVal); if (exitVal == 0); } catch (Exception e) { err.append("ERRORE!! " + e.getMessage()); err.append("\n"); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "fine OCR. Trovate " + lineCount + " foto con numeri. Importazione indici in corso..."); PuntoFoto.this.getApFull().writeLog("\nfine OCR. Importazione indici in corso..."); if (new File(fineNameFinale).exists()) { rp = this.puntoFoto.getGara().indexFotoPisa(fineNameFinale, TAG_THREAD_MSG); } else { err.append("ERRORE!! file " + nomeFileCsv + " NON trovato."); err.append("\n"); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "ERRORE!! file " + nomeFileCsv + " NON trovato."); PuntoFoto.this.getApFull().writeLog("\nERRORE!! file " + nomeFileCsv + " NON trovato."); } Timestamp stop = new Timestamp(Calendar.getInstance().getTimeInMillis()); SimpleDateFormat dfMS = new SimpleDateFormat("HH:mm:ss "); PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + "Risultato import:\n" + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n\n ocr concluso\n****************"); if (err.length() > 0) { PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " **************************************** ERRORI ocr ****************************************\n" + TAG_THREAD_MSG); rp.appendMsg(" " + err.toString()); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " concluso: DURATA: " + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n" + rp.getMsg()); try { sleep(4000L); } catch (Exception e) {} StatusMsg.deleteMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG); PuntoFoto.removeThreadOcrAttivo(this.puntoFoto.getId_puntoFoto()); } public void runProcessBuilder() { String TAG_THREAD_MSG = "OCR GARA " + this.puntoFoto.getId_gara() + " " + this.puntoFoto.getGara().getDescrizione() + " PUNTOFOTO " + this.puntoFoto.getId_puntoFoto() + " " + this.puntoFoto.getDescrizionePuntoFoto(); Timestamp start = new Timestamp(Calendar.getInstance().getTimeInMillis()); ResParm rp = new ResParm(true); StringBuffer err = new StringBuffer(); String COMANDOSH = "/bin/sh "; String COMANDO_PRODUZIONE = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_PRODUZIONE").getTesto() + " "; String COMANDO_TEST = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_TEST").getTesto() + " "; String COMANDO = COMANDO_PRODUZIONE; String dirDaIndicizzare = PuntoFoto.this.getPathCompletoFoto(); String pathRisultato = PuntoFoto.this.getDocBase() + "_csv/"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); String dataFile = df.format(DBAdapter.getToday()); String nomeFileCsv = "" + PuntoFoto.this.getId_gara() + "-" + PuntoFoto.this.getId_gara() + "_" + PuntoFoto.this.getId_puntoFoto() + ".csv"; String TMP_PRODUZIONE_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto(); String TMP_TEST_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto(); String LINEE_FILE = TMP_PRODUZIONE_LINEE_FILE; String fineNameFinale = pathRisultato + pathRisultato; String comandoParm = nomeFileCsv + " " + nomeFileCsv + " " + dirDaIndicizzare; String comandoCompleto = COMANDO + COMANDO; System.out.println(comandoCompleto); PuntoFoto.this.getApFull().writeLog(comandoCompleto); long lineCount = 0L; try { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "eseguo " + comandoCompleto); ProcessBuilder processBuilder = new ProcessBuilder(new String[0]); System.out.println("Processbuilder.command: " + COMANDO.trim() + " - " + nomeFileCsv + " - " + dirDaIndicizzare + " - " + pathRisultato); processBuilder.command(COMANDO.trim(), nomeFileCsv, dirDaIndicizzare, pathRisultato); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv); Process process = processBuilder.start(); sleep(2000L); File tmpFile = new File(LINEE_FILE); int numTentativi = 0; while (!tmpFile.exists() && numTentativi < 20) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv + DBAdapter.charRight("", numTentativi, ".")); sleep(2000L); numTentativi++; } int numeroCicli = 0; if (tmpFile.exists()) { while (tmpFile.exists()) { numeroCicli++; Stream stream = null; StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "...... " + numeroCicli); System.out.println(TAG_THREAD_MSG + " waiting...... " + TAG_THREAD_MSG + " file tmp: " + numeroCicli); sleep(2000L); } } else { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "File " + nomeFileCsv + " non trovato!!!"); sleep(4000L); } System.out.println("waitfor"); int exitVal = process.waitFor(); System.out.println(" fine waitfor: exit val= " + exitVal); if (exitVal == 0); } catch (Exception e) { err.append("ERRORE!! " + e.getMessage()); err.append("\n"); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "fine OCR. Trovate " + lineCount + " foto con numeri. Importazione indici in corso..."); PuntoFoto.this.getApFull().writeLog("\nfine OCR. Importazione indici in corso..."); if (new File(fineNameFinale).exists()) { rp = this.puntoFoto.getGara().indexFotoPisa(fineNameFinale, TAG_THREAD_MSG); } else { err.append("ERRORE!! file " + nomeFileCsv + " NON trovato."); err.append("\n"); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "ERRORE!! file " + nomeFileCsv + " NON trovato."); PuntoFoto.this.getApFull().writeLog("\nERRORE!! file " + nomeFileCsv + " NON trovato."); } Timestamp stop = new Timestamp(Calendar.getInstance().getTimeInMillis()); SimpleDateFormat dfMS = new SimpleDateFormat("HH:mm:ss "); PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + "Risultato import:\n" + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n\n ocr concluso\n****************"); if (err.length() > 0) { PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " **************************************** ERRORI ocr ****************************************\n" + TAG_THREAD_MSG); rp.appendMsg(" " + err.toString()); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " concluso: DURATA: " + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n" + rp.getMsg()); try { sleep(4000L); } catch (Exception e) {} StatusMsg.deleteMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG); PuntoFoto.removeThreadOcrAttivo(this.puntoFoto.getId_puntoFoto()); } public void run() { String TAG_THREAD_MSG = "OCR GARA " + this.puntoFoto.getId_gara() + " " + this.puntoFoto.getGara().getDescrizione() + " PUNTOFOTO " + this.puntoFoto.getId_puntoFoto() + " " + this.puntoFoto.getDescrizionePuntoFoto(); Timer timer = new Timer(); timer.start(); ResParm rp = new ResParm(true); StringBuffer err = new StringBuffer(); String COMANDOSH = "/bin/sh "; String COMANDO_PRODUZIONE = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_PRODUZIONE").getTesto() + " "; String COMANDO_TEST = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_TEST").getTesto() + " "; String END_COMMAND_TAG = "******* fine indicizzazione OCR"; String COMANDO = COMANDO_PRODUZIONE; String dirDaIndicizzare = PuntoFoto.this.getPathCompletoFoto(); String pathRisultato = PuntoFoto.this.getDocBase() + "_csv/"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); String dataFile = df.format(DBAdapter.getToday()); String nomeFileCsv = "" + PuntoFoto.this.getId_gara() + "-" + PuntoFoto.this.getId_gara() + "_" + PuntoFoto.this.getId_puntoFoto() + ".csv"; String TMP_PRODUZIONE_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto(); String TMP_TEST_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto(); String fineNameFinale = pathRisultato + pathRisultato; String LINEE_FILE = TMP_PRODUZIONE_LINEE_FILE; LINEE_FILE = fineNameFinale; String comandoParm = nomeFileCsv + " " + nomeFileCsv + " " + dirDaIndicizzare; String comandoCompleto = COMANDO + COMANDO; System.out.println(TAG_THREAD_MSG + "\n**** eseguo " + TAG_THREAD_MSG); PuntoFoto.this.getApFull().writeLog(comandoCompleto); long lineCount = 0L; try { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "eseguo " + comandoCompleto); System.out.println(TAG_THREAD_MSG + " eseguo " + TAG_THREAD_MSG); DefaultExecutor defaultExecutor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(300000L); defaultExecutor.setWatchdog(watchdog); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); defaultExecutor.setStreamHandler((ExecuteStreamHandler)psh); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); CommandLine cl = new CommandLine(COMANDO.trim()); cl.addArgument(nomeFileCsv); cl.addArgument(dirDaIndicizzare); cl.addArgument(pathRisultato); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv); defaultExecutor.execute(cl, (ExecuteResultHandler)resultHandler); String temp = ""; int numeroCicli = 0; File tmpFile = new File(LINEE_FILE); int numTentativi = 0; while (!tmpFile.exists() && numTentativi < 20) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... aspetto il file: " + nomeFileCsv + DBAdapter.charRight("", numTentativi, ".")); System.out.println(TAG_THREAD_MSG + " Chiamata OCR Python in corso..... aspetto il file: " + TAG_THREAD_MSG + nomeFileCsv); sleep(2000L); numTentativi++; } Stream stream = null; long currentLineCount = 0L; long numLineCountUguali = 0L; long maxNumlineCountUguali = 20L; while (temp.indexOf("******* fine indicizzazione OCR") < 0) { String[] arr; numeroCicli++; temp = stdout.toString().trim(); if (temp.indexOf("\n") > 0) { arr = temp.split("\n"); } else { arr = temp.split("\r"); } if (arr != null) temp = arr[arr.length - 1]; if (tmpFile.exists()) { try { stream = Files.lines(Paths.get(LINEE_FILE), StandardCharsets.UTF_8); lineCount = stream.count() - 1L; } catch (Exception e) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Eccezione file " + nomeFileCsv + ": " + e.getMessage()); } finally { if (stream != null) { stream.close(); stream = null; } } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " ciclo " + numeroCicli + " - Trovate " + lineCount + " Foto con numeri - " + temp); System.out.println(TAG_THREAD_MSG + " ciclo " + TAG_THREAD_MSG + " - Trovate " + numeroCicli + " Foto con numeri: " + lineCount); } if (currentLineCount == lineCount) { numLineCountUguali++; } else { numLineCountUguali = 0L; currentLineCount = lineCount; } if (numLineCountUguali >= maxNumlineCountUguali) { System.out.println("BREAK!!!"); break; } sleep(1000L); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " Fuori dal ciclo lettura OCR principale..... Foto con numeri - " + temp + " waitfor script ocr......"); System.out.println("waitfor...."); resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); System.out.println(" fine waitfor: exit val= " + exitVal); if (exitVal == 0); } catch (Exception e) { err.append("ERRORE!! " + e.getMessage()); err.append("\n"); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "fine OCR. Trovate " + lineCount + " foto con numeri. Importazione indici in corso..."); System.out.println(TAG_THREAD_MSG + " fine OCR. Trovate " + TAG_THREAD_MSG + " foto con numeri. Importazione indici in corso..."); PuntoFoto.this.getApFull().writeLog("\nfine OCR. Importazione indici in corso..."); if (new File(fineNameFinale).exists()) { rp = this.puntoFoto.getGara().indexFotoPisa(fineNameFinale, TAG_THREAD_MSG); } else { err.append("ERRORE!! file " + nomeFileCsv + " NON trovato."); err.append("\n"); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "ERRORE!! file " + nomeFileCsv + " NON trovato."); PuntoFoto.this.getApFull().writeLog("\nERRORE!! file " + nomeFileCsv + " NON trovato."); } timer.stop(); PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + " Risultato import:\n" + timer.getDurataHourMin() + "\n\n ocr concluso\n****************"); if (err.length() > 0) { PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " **************************************** ERRORI ocr ****************************************\n" + TAG_THREAD_MSG); rp.appendMsg(" " + err.toString()); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " concluso: DURATA: " + timer.getDurataHourMin() + "\n" + rp.getMsg()); System.out.println(TAG_THREAD_MSG + " concluso: DURATA: " + TAG_THREAD_MSG + "\n" + timer.getDurataHourMin()); try { sleep(4000L); } catch (Exception e) {} StatusMsg.deleteMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG); PuntoFoto.removeThreadOcrAttivo(this.puntoFoto.getId_puntoFoto()); } public void runExecSh() { String TAG_THREAD_MSG = "OCR GARA " + this.puntoFoto.getId_gara() + " " + this.puntoFoto.getGara().getDescrizione() + " PUNTOFOTO " + this.puntoFoto.getId_puntoFoto() + " " + this.puntoFoto.getDescrizionePuntoFoto(); Timestamp start = new Timestamp(Calendar.getInstance().getTimeInMillis()); ResParm rp = new ResParm(true); StringBuffer err = new StringBuffer(); String COMANDOSH = "/bin/sh "; String COMANDO_PRODUZIONE = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_PRODUZIONE").getTesto() + " "; String COMANDO_TEST = PuntoFoto.this.getParm("OCR_COMANDO_SHELL_TEST").getTesto() + " "; String END_COMMAND_TAG = "******* fine indicizzazione OCR"; String COMANDO = COMANDO_PRODUZIONE; String dirDaIndicizzare = PuntoFoto.this.getPathCompletoFoto(); String pathRisultato = PuntoFoto.this.getDocBase() + "_csv/"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); String dataFile = df.format(DBAdapter.getToday()); String nomeFileCsv = "" + PuntoFoto.this.getId_gara() + "-" + PuntoFoto.this.getId_gara() + "_" + PuntoFoto.this.getId_puntoFoto() + ".csv"; String TMP_PRODUZIONE_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_PRODUZIONE").getTesto(); String TMP_TEST_LINEE_FILE = PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto() + PuntoFoto.this.getParm("OCR_LINEE_FILE_TEST").getTesto(); String LINEE_FILE = TMP_PRODUZIONE_LINEE_FILE; String fineNameFinale = pathRisultato + pathRisultato; String comandoParm = nomeFileCsv + " " + nomeFileCsv + " " + dirDaIndicizzare; String comandoCompleto = COMANDO + COMANDO; System.out.println(comandoCompleto); PuntoFoto.this.getApFull().writeLog(comandoCompleto); long lineCount = 0L; try { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "eseguo " + comandoCompleto); DefaultExecutor defaultExecutor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(300000L); defaultExecutor.setWatchdog(watchdog); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); defaultExecutor.setStreamHandler((ExecuteStreamHandler)psh); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); CommandLine cl = new CommandLine(COMANDO.trim()); cl.addArgument(nomeFileCsv); cl.addArgument(dirDaIndicizzare); cl.addArgument(pathRisultato); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv); defaultExecutor.execute(cl, (ExecuteResultHandler)resultHandler); String temp = ""; int numeroCicli = 0; File tmpFile = new File(LINEE_FILE); int numTentativi = 0; while (!tmpFile.exists() && numTentativi < 20) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Chiamata OCR Python in corso..... file: " + nomeFileCsv + DBAdapter.charRight("", numTentativi, ".")); sleep(2000L); numTentativi++; } Stream stream = null; while (tmpFile.exists()) { String[] arr; numeroCicli++; temp = stdout.toString().trim(); if (temp.indexOf("\n") > 0) { arr = temp.split("\n"); } else { arr = temp.split("\r"); } System.out.println(arr.length); if (arr != null) temp = arr[arr.length - 1]; System.out.println("ultima riga: " + temp); if (tmpFile.exists()) { try { stream = Files.lines(Paths.get(LINEE_FILE), StandardCharsets.UTF_8); lineCount = stream.count() - 1L; } catch (Exception e) { StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "Eccezione file " + nomeFileCsv + ": " + e.getMessage()); } finally { if (stream != null) { stream.close(); stream = null; } } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " waiting.... " + numeroCicli + " Trovate " + lineCount + " Foto con numeri - " + temp); System.out.println(TAG_THREAD_MSG + " waiting.... " + TAG_THREAD_MSG + " Trovate " + numeroCicli + " Foto con numeri: " + lineCount); } sleep(2000L); } resultHandler.waitFor(); int exitVal = resultHandler.getExitValue(); System.out.println(" fine waitfor: exit val= " + exitVal); if (exitVal == 0); } catch (Exception e) { err.append("ERRORE!! " + e.getMessage()); err.append("\n"); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "fine OCR. Trovate " + lineCount + " foto con numeri. Importazione indici in corso..."); PuntoFoto.this.getApFull().writeLog("\nfine OCR. Importazione indici in corso..."); if (new File(fineNameFinale).exists()) { rp = this.puntoFoto.getGara().indexFotoPisa(fineNameFinale, TAG_THREAD_MSG); } else { err.append("ERRORE!! file " + nomeFileCsv + " NON trovato."); err.append("\n"); StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, "ERRORE!! file " + nomeFileCsv + " NON trovato."); PuntoFoto.this.getApFull().writeLog("\nERRORE!! file " + nomeFileCsv + " NON trovato."); } Timestamp stop = new Timestamp(Calendar.getInstance().getTimeInMillis()); SimpleDateFormat dfMS = new SimpleDateFormat("HH:mm:ss "); PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " DURATA: " + TAG_THREAD_MSG + "Risultato import:\n" + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n\n ocr concluso\n****************"); if (err.length() > 0) { PuntoFoto.this.getApFull().writeLog(TAG_THREAD_MSG + " **************************************** ERRORI ocr ****************************************\n" + TAG_THREAD_MSG); rp.appendMsg(" " + err.toString()); } StatusMsg.updateMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG, " concluso: DURATA: " + dfMS.format(new Time(stop.getTime() - start.getTime() - 3600000L)) + "\n" + rp.getMsg()); try { sleep(4000L); } catch (Exception e) {} StatusMsg.deleteMsgByTag(PuntoFoto.this.getApFull(), TAG_THREAD_MSG); PuntoFoto.removeThreadOcrAttivo(this.puntoFoto.getId_puntoFoto()); } } public static boolean threadPuntoFoto = false; public PuntoFoto(ApplParmFull newApplParmFull) { super(newApplParmFull); } public PuntoFoto() {} public void setId_puntoFoto(long newId_puntoFoto) { this.id_puntoFoto = newId_puntoFoto; } public void setId_gara(long newId_gara) { this.id_gara = newId_gara; setGara(null); } public void setPathRelativoFoto(String newPathRelativoFoto) { this.pathRelativoFoto = newPathRelativoFoto; } public void setTipoPuntoFoto(String newTipoPuntoFoto) { this.tipoPuntoFoto = newTipoPuntoFoto; } public long getId_puntoFoto() { return this.id_puntoFoto; } public long getId_gara() { return this.id_gara; } public String getPathRelativoFoto() { return (this.pathRelativoFoto == null) ? "" : this.pathRelativoFoto.trim(); } public String getTipoPuntoFoto() { return (this.tipoPuntoFoto == null) ? "" : this.tipoPuntoFoto.trim(); } public void setGara(Gara newGara) { this.gara = newGara; } public Gara getGara() { this.gara = (Gara)getSecondaryObject(this.gara, Gara.class, getId_gara()); return this.gara; } protected ResParm checkDeleteCascade() { return new ResParm(true); } protected void deleteCascade() { ResParm rp = noIndexFoto(); if (!rp.getStatus()) System.out.println("Attenzione. Cancellazione punto foto: " + rp.getMsg()); } public Vectumerator findByCR(PuntoFotoCR CR, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from PUNTO_FOTO AS A"; String s_Sql_Order = " order by A.descrizionePuntoFoto"; WcString wc = new WcString(); if (CR.getId_gara() > 0L) wc.addWc("A.id_gara=" + CR.getId_gara()); if (CR.getFlgIndexOk() == 0L) { wc.addWc("(A.flgIndexOk is null or A.flgIndexOk =0)"); } else if (CR.getFlgIndexOk() > 0L) { wc.addWc("A.flgIndexOk =" + CR.getFlgIndexOk()); } try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public Vectumerator findPuntiFotoByGara(long l_id_gara, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from PUNTO_FOTO AS A"; String s_Sql_Order = " order by A.descrizionePuntoFoto"; WcString wc = new WcString(); wc.addWc("A.id_gara=" + l_id_gara); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public Vectumerator findTipiPuntoFoto(long l_id_gara) { String s_Sql_Find = "select distinct B.tipoPuntoFoto from (select A.tipoPuntoFoto, A.pathRelativoFoto from PUNTO_FOTO AS A "; String s_Sql_OrderBy = " order by A.pathRelativoFoto, A.tipoPuntoFoto"; WcString wc = new WcString(); wc.addWc("A.id_gara=" + l_id_gara); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString() + ") as B"); return findRows(stmt); } catch (Exception e) { handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public String getDescrizioneCompleta() { return getTipoPuntoFoto() + " " + getTipoPuntoFoto() + "-" + getGara().getDescrizione(); } public String getPathCompletoFoto() { String temp = getPathBaseFoto() + getPathBaseFoto() + getGara().getPathBase(); if (!temp.endsWith(DBAdapter.SEPARATOR)) temp = temp + temp; return DBAdapter.convertPathToCurrentFileSystemSeparator(temp); } public String getPathCompletoFotoRidotta() { String temp = getPathBaseFotoRidotte() + getPathBaseFotoRidotte() + getGara().getPathBase(); if (!temp.endsWith(DBAdapter.SEPARATOR)) temp = temp + temp; return DBAdapter.convertPathToCurrentFileSystemSeparator(temp); } public ResParm indexFoto(long l_id_user) { ResParm rp = new ResParm(); int se1 = 10; int se2 = 100; int nuovefoto = 0; int fotoErrate = 0; int totFoto = 0; StringBuffer errMsg = new StringBuffer(); if (getId_gara() == 0L) { rp.setMsg("Indicizzazione foto: Bean gara non caricato"); rp.setStatus(false); handleDebug("Indicizzazione foto: Bean gara non caricato", 2); return rp; } String dirFoto = getPathCompletoFoto(); if (new File(dirFoto).exists()) { File dir = new File(dirFoto); File[] fotos = dir.listFiles(); Foto foto = new Foto(getApFull()); Foto currentFoto = new Foto(getApFull()); File theFoto = null; try { for (int i = 0; i < fotos.length; i++) { theFoto = fotos[i]; if (!theFoto.getName().toLowerCase().startsWith("tn_") && theFoto.getName().toLowerCase().endsWith(".jpg")) { totFoto++; foto.findByFilenamePuntoFoto(theFoto.getName(), getId_puntoFoto()); if (foto.getDBState() == 0) nuovefoto++; foto.setId_gara(getId_gara()); foto.setId_puntoFoto(getId_puntoFoto()); foto.setFile(theFoto.getName()); foto.setLastUpdId_user(l_id_user); rp = foto.save(); if (!rp.getStatus()) { System.out.println("ERRORE INDICIZZAZIONE: " + theFoto.getName() + ": " + rp.getMsg()); fotoErrate++; errMsg.append("
Attenzione " + theFoto.getName() + ": " + rp.getMsg()); } } else { handleDebug("File non jpg:" + theFoto.getName(), 1); } } System.out.println(" fine indicizzazione..."); } catch (Exception e) { handleDebug(e); errMsg.append("\nIndicizzazione fallita sul file " + theFoto.getName() + "
" + e.getMessage()); } rp.setMsg("Indicizzazione completata.
Numero foto indicizzate:" + totFoto + "
numero di foto nuove indicizzate: " + nuovefoto + "
numero di foto NON indicizzate: " + fotoErrate + "
" + errMsg.toString()); rp.setStatus(true); } else { rp.setMsg("Errore! Percorso foto errato: " + dirFoto); rp.setStatus(false); } return rp; } public ResParm noIndexFoto() { ResParm rp = new ResParm(); int se1 = 10; int se2 = 100; int nuovefoto = 0; int fotoErrate = 0; int totFoto = 0; StringBuffer errMsg = new StringBuffer(); if (getId_gara() == 0L || getId_puntoFoto() == 0L) { rp.setMsg("Indicizzazione foto: Bean gara non caricato"); rp.setStatus(false); handleDebug("Indicizzazione foto: Bean gara non caricato", 2); return rp; } rp = new Foto(getApFull()).deleteFotoByPuntoFoto(getId_puntoFoto()); return rp; } public String getTipoPuntoFotoNew() { return (this.tipoPuntoFotoNew == null) ? "" : this.tipoPuntoFotoNew.trim(); } public void setTipoPuntoFotoNew(String tipoPuntoFotoNew) { this.tipoPuntoFotoNew = tipoPuntoFotoNew; } public String getDescrizionePuntoFoto() { return (this.descrizionePuntoFoto == null) ? "" : this.descrizionePuntoFoto.trim(); } public void setDescrizionePuntoFoto(String descrizionePuntoFoto) { this.descrizionePuntoFoto = descrizionePuntoFoto; } protected void prepareSave(PreparedStatement ps) throws SQLException { if (!getTipoPuntoFotoNew().isEmpty()) setTipoPuntoFoto(getTipoPuntoFotoNew()); if (!getPathRelativoFoto().isEmpty() && !getPathRelativoFoto().endsWith(DBAdapter.SEPARATOR)) setPathRelativoFoto(getPathRelativoFoto() + getPathRelativoFoto()); super.prepareSave(ps); } public void findByGaraPath(long l_id_gara, String l_pathRelativoFoto) { String s_Sql_Find = "select A.* from PUNTO_FOTO AS A"; String s_Sql_Order = " order by A.pathRelativoFoto"; WcString wc = new WcString(); wc.addWc("A.id_gara=" + l_id_gara); if (!l_pathRelativoFoto.isEmpty() && !l_pathRelativoFoto.endsWith(DBAdapter.SEPARATOR)) l_pathRelativoFoto = l_pathRelativoFoto + l_pathRelativoFoto; wc.addWc("A.pathRelativoFoto='" + l_pathRelativoFoto.trim() + "'"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); findFirstRecord(stmt); } catch (SQLException e) { handleDebug(e); } } public static final String getIndexOk(long l_flgIndexOk) { if (l_flgIndexOk == 2L) return "Manuale"; if (l_flgIndexOk == 0L) return "Nessuna"; if (l_flgIndexOk == 1L) return "Tempi"; return "??"; } public long getFlgIndexOk() { return this.flgIndexOk; } public void setFlgIndexOk(long flgIndexOk) { this.flgIndexOk = flgIndexOk; } public String getIndexOk() { return getIndexOk(getFlgIndexOk()); } public long getId_puntoFotoExport() { return this.id_puntoFotoExport; } public void setId_puntoFotoExport(long id_puntoFotoExport) { this.id_puntoFotoExport = id_puntoFotoExport; } public final ResParm startCreaPreview() { if (!isThreadAttivo()) { new ThreadIndicizzaPuntofoto(this); return new ResParm(true, "Thread INDICIZZA FOTO avviato"); } return new ResParm(false, "ATTENZIONE!! Thread INDICIZZA FOTO gia' in esecuzione!!!"); } public final synchronized ResParm startOcrThread() { if (getId_puntoFoto() > 0L) { if (!isThreadOcrAttivo(getId_puntoFoto())) { new ThreadOcrPuntofoto(this); return new ResParm(true, "Thread OCR FOTO avviato"); } return new ResParm(false, "ATTENZIONE!! Thread OCR per questo punto foto gia' in esecuzione!!!"); } return new ResParm(false, "ATTENZIONE!! Punto foto non valido!!!"); } public static boolean isThreadAttivo() { return threadPuntoFoto; } public static boolean isThreadOcrAttivo(long l_id_puntofoto) { if (hsOcrThread.contains(new Long(l_id_puntofoto))) return true; return false; } public static boolean isThreadOcrAttivo() { if (hsOcrThread.size() > 0) return true; return false; } public static boolean setThreadOcrAttivo(long l_id_puntofoto) { Long key = new Long(l_id_puntofoto); if (hsOcrThread.contains(key)) return false; hsOcrThread.add(key); return true; } public static boolean removeThreadOcrAttivo(long l_id_puntofoto) { Long key = new Long(l_id_puntofoto); if (hsOcrThread.contains(key)) { hsOcrThread.remove(key); return true; } return false; } public ResParm callOcr() { ResParm rp = new ResParm(); if (getId_gara() > 0L) { String COMANDO = "sh /home/piero/Documenti/sites/bin/indicizzaR.sh "; String dirDaIndicizzare = getPathCompletoFoto(); String pathRisultato = getDocBase() + "_csv/"; String comando = "sh /home/piero/Documenti/sites/bin/indicizzaR.sh " + getId_puntoFoto() + " " + dirDaIndicizzare + " " + pathRisultato; System.out.println(comando); getApFull().writeLog(comando); rp.setMsg(comando); } return rp; } public Vectumerator findPuntiFotoConFotoOPettoraleNonInviatoByGara(long l_id_gara) { String s_Sql_Find = "select A.* from PUNTO_FOTO AS A INNER JOIN FOTO AS B ON A.id_puntofoto=B.id_puntoFoto"; String s_Sql_Order = " "; WcString wc = new WcString(); wc.addWc("A.id_gara=" + l_id_gara); wc.addWc("(B.flgFotoInviata is null or B.flgFotoInviata=0 or B.flgPettoraleInviato is null or B.flgPettoraleInviato=0)"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt); } catch (SQLException e) { return AB_EMPTY_VECTUMERATOR; } } public ResParm resetPreview() { ResParm rp = new ResParm(); if (getId_gara() == 0L || getId_puntoFoto() == 0L) { rp.setMsg("Indicizzazione foto: Bean gara non caricato"); rp.setStatus(false); handleDebug("resetPreview pinto foto: Bean gara o punto foto non caricato", 2); return rp; } rp = update("update FOTO set flgFotoInviata=0 where id_puntoFoto=" + getId_puntoFoto()); boolean resDel = DBAdapter.deleteDir(new File(getPathCompletoFotoRidotta())); if (!resDel) { rp.setStatus(false); rp.setMsg("Errore! impossibile cancellare " + getPathCompletoFotoRidotta() + " - " + rp.getMsg()); } else { rp.setMsg(rp.getMsg() + " - cancellata dir " + rp.getMsg()); } return rp; } }