2323
2424package com .esp32 .mkspiffs ;
2525
26- import java .io .File ;
27- import java .io .FileReader ;
28- import java .io .BufferedReader ;
29- import java .io .InputStreamReader ;
30- import java .io .IOException ;
26+ import java .util .*;
27+ import java .io .*;
3128
3229import java .text .SimpleDateFormat ;
33- import java .util .Date ;
3430import java .lang .reflect .Field ;
3531import java .lang .reflect .InvocationTargetException ;
3632import javax .swing .JOptionPane ;
5147
5248import cc .arduino .files .DeleteFilesOnShutdown ;
5349
50+ /**
51+ * Taken from https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html?page=3
52+ */
53+ class StreamGobbler extends Thread {
54+ InputStream is ;
55+ String type ;
56+
57+ StreamGobbler (InputStream is , String type ) {
58+ this .is = is ;
59+ this .type = type ;
60+ }
61+
62+ public void run () {
63+ try {
64+ InputStreamReader isr = new InputStreamReader (is );
65+ BufferedReader br = new BufferedReader (isr );
66+ String line =null ;
67+ while ( (line = br .readLine ()) != null )
68+ System .out .println (type + ">" + line );
69+ } catch (IOException ioe ) {
70+ ioe .printStackTrace ();
71+ }
72+ }
73+ }
74+
75+
5476/**
5577 * Example Tools menu entry.
5678 */
@@ -71,31 +93,26 @@ public String getMenuTitle() {
7193
7294 private int listenOnProcess (String [] arguments ){
7395 try {
74- final Process p = ProcessUtils .exec (arguments );
75- Thread thread = new Thread () {
76- public void run () {
77- try {
78- InputStreamReader reader = new InputStreamReader (p .getInputStream ());
79- int c ;
80- while ((c = reader .read ()) != -1 )
81- System .out .print ((char ) c );
82- reader .close ();
83-
84- reader = new InputStreamReader (p .getErrorStream ());
85- while ((c = reader .read ()) != -1 )
86- System .err .print ((char ) c );
87- reader .close ();
88- } catch (Exception e ){}
89- }
90- };
91- thread .start ();
92- int res = p .waitFor ();
93- thread .join ();
94- return res ;
96+ Runtime rt = Runtime .getRuntime ();
97+ Process proc = rt .exec (arguments );
98+ // any error message?
99+ StreamGobbler errorGobbler = new StreamGobbler (proc .getErrorStream (), "E" );
100+
101+ // any output?
102+ StreamGobbler outputGobbler = new StreamGobbler (proc .getInputStream (), "O" );
103+
104+ // kick them off
105+ errorGobbler .start ();
106+ outputGobbler .start ();
107+
108+ // any error???
109+ int exitVal = proc .waitFor ();
110+
111+ return exitVal ;
95112 } catch (Exception e ){
96113 return -1 ;
97114 }
98- }
115+ }
99116
100117 private void sysExec (final String [] arguments ){
101118 Thread thread = new Thread () {
@@ -328,9 +345,12 @@ private void createAndUpload(){
328345 isNetwork = true ;
329346 espota = new File (platform .getFolder ()+"/tools" , espotaCmd );
330347 if (!espota .exists () || !espota .isFile ()){
331- System .err .println ();
332- editor .statusError (typefs + " Error: espota not found!" );
333- return ;
348+ espota = new File (platform .getFolder ()+"/tools" , "espota.py" ); //fall-back to .py
349+ if (!espota .exists () || !espota .isFile ()){
350+ System .err .println ();
351+ editor .statusError (typefs + " Error: espota not found!" );
352+ return ;
353+ }
334354 }
335355 System .out .println ("espota : " +espota .getAbsolutePath ());
336356 System .out .println ();
@@ -354,11 +374,10 @@ private void createAndUpload(){
354374 }
355375 }
356376 }
377+ System .out .println ("esptool : " +esptool .getAbsolutePath ());
378+ System .out .println ();
357379 }
358- System .out .println ("esptool : " +esptool .getAbsolutePath ());
359- System .out .println ();
360-
361-
380+
362381 //load a list of all files
363382 int fileCount = 0 ;
364383 File dataFolder = new File (editor .getSketch ().getFolder (), "data" );
@@ -426,9 +445,10 @@ private void createAndUpload(){
426445
427446 if (isNetwork ){
428447 System .out .println ("[" + typefs + "] IP : " +serialPort );
429- System .out .println ();
448+ System .out .println ("Running: " + espota .getAbsolutePath () + " -i " + serialPort + " -p 3232 -s -f " + imagePath );
449+ System .out .println ();
430450 if (espota .getAbsolutePath ().endsWith (".py" ))
431- sysExec (new String []{pythonCmd , espota .getAbsolutePath (), "-i" , serialPort , "-p" , "3232" , "-s" , "-f" , imagePath });
451+ sysExec (new String []{pythonCmd , espota .getAbsolutePath (), "-i" , serialPort , "-p" , "3232" , "-s" , "-f" , imagePath }); // other flags , "-d", "-r", "-t", "50"
432452 else
433453 sysExec (new String []{espota .getAbsolutePath (), "-i" , serialPort , "-p" , "3232" , "-s" , "-f" , imagePath });
434454 } else {
@@ -507,9 +527,9 @@ private void eraseFlash(){
507527 }
508528 }
509529 }
530+ System .out .println ("esptool : " +esptool .getAbsolutePath ());
531+ System .out .println ();
510532 }
511- System .out .println ("esptool : " +esptool .getAbsolutePath ());
512- System .out .println ();
513533
514534 Object [] options = { "Yes" , "No" };
515535 String title = "Erase All Flash" ;
0 commit comments