@@ -58,11 +58,36 @@ const char Helper[] = R"(
5858)" ;
5959const char Header[] = " HTTP/1.1 303 OK\r\n Location:FSexplorer.html\r\n Cache-Control: no-cache\r\n " ;
6060
61+
62+
6163// =====================================================================================
62- void setupFSexplorer () // Funktionsaufruf "LittleFS();" muss im Setup eingebunden werden
63- {
64+ void startWebserver (){
65+ if (!LittleFS.exists (" /index.html" )) {
66+ httpServer.serveStatic (" /" , LittleFS, " /FSexplorer.html" );
67+ httpServer.serveStatic (" /index" , LittleFS, " /FSexplorer.html" );
68+ httpServer.serveStatic (" /index.html" , LittleFS, " /FSexplorer.html" );
69+ } else {
70+ httpServer.serveStatic (" /" , LittleFS, " /index.html" );
71+ httpServer.serveStatic (" /index" , LittleFS, " /index.html" );
72+ httpServer.serveStatic (" /index.html" , LittleFS, " /index.html" );
73+ }
74+ httpServer.serveStatic (" /FSexplorer.png" , LittleFS, " /FSexplorer.png" );
75+ httpServer.serveStatic (" /index.css" , LittleFS, " /index.css" );
76+ httpServer.serveStatic (" /index.js" , LittleFS, " /index.js" );
77+ // otgw pic functions
78+ httpServer.on (" /pic" , upgradepic);
79+ // all other api calls are catched in FSexplorer onNotFounD!
80+ httpServer.on (" /api" , HTTP_ANY, processAPI); // was only HTTP_GET (20210110)
81+
82+ httpServer.begin ();
83+ // Set up first message as the IP address
84+ OTGWSerial.println (" \n HTTP Server started\r " );
85+ sprintf (cMsg, " %03d.%03d.%d.%d" , WiFi.localIP ()[0 ], WiFi.localIP ()[1 ], WiFi.localIP ()[2 ], WiFi.localIP ()[3 ]);
86+ OTGWSerial.printf (" \n Assigned IP=%s\r\n " , cMsg);
87+ }
88+ // =====================================================================================
89+ void setupFSexplorer (){
6490 LittleFS.begin ();
65-
6691 if (LittleFS.exists (" /FSexplorer.html" ))
6792 {
6893 httpServer.serveStatic (" /FSexplorer.html" , LittleFS, " /FSexplorer.html" );
@@ -72,12 +97,12 @@ void setupFSexplorer() // Funktionsaufruf "LittleFS();" muss im Setup eingebu
7297 {
7398 httpServer.send (200 , " text/html" , Helper); // Upload the FSexplorer.html
7499 }
75- httpServer.on (" /api/listfiles" , APIlistFiles);
100+ httpServer.on (" /api/firmwarefilelist" , apifirmwarefilelist);
101+ httpServer.on (" /api/listfiles" , apilistfiles);
76102 httpServer.on (" /LittleFSformat" , formatLittleFS);
77103 httpServer.on (" /upload" , HTTP_POST, []() {}, handleFileUpload);
78104 httpServer.on (" /ReBoot" , reBootESP);
79- httpServer.on (" /update" , updateFirmware);
80- httpServer.on (" /upgradepic" , upgradePIC);
105+
81106 httpServer.onNotFound ([]()
82107 {
83108 if (Verbose) DebugTf (" in 'onNotFound()'!! [%s] => \r\n " , String (httpServer.uri ()).c_str ());
@@ -104,9 +129,48 @@ void setupFSexplorer() // Funktionsaufruf "LittleFS();" muss im Setup eingebu
104129
105130} // setupFSexplorer()
106131
132+ // =====================================================================================
133+ void apifirmwarefilelist () {
134+ char *s, buffer[400 ];
135+ String version, fwversion;
136+ Dir dir;
137+ File f;
138+
139+ s = buffer;
140+ s += sprintf (buffer, " [" );
141+ dir = LittleFS.openDir (" /" );
142+ while (dir.next ()) {
143+ if (dir.fileName ().endsWith (" .hex" )) {
144+ version=" " ;fwversion=" " ;
145+ String verfile = " /" + dir.fileName ();
146+ verfile.replace (" .hex" , " .ver" );
147+ f = LittleFS.open (verfile, " r" );
148+ if (f) {
149+ version = f.readStringUntil (' \n ' );
150+ version.trim ();
151+ f.close ();
152+ }
153+ fwversion = GetVersion (" /" +dir.fileName ()); // only check if gateway firmware
154+ DebugTf (" GetVersion(%s) returned %s\n " , dir.fileName ().c_str (), fwversion.c_str ());
155+ if (fwversion.length () && strcmp (fwversion.c_str (),version.c_str ())) { // versions do not match
156+ version=fwversion; // assign hex file version to version
157+ if (f = LittleFS.open (verfile, " w" )) { // write to .ver file
158+ DebugTf (" writing %s to %s\n " ,version.c_str (),verfile.c_str ());
159+ f.print (version + " \n " );
160+ f.close ();
161+ }
162+ }
163+ s += snprintf ( s, sizeof (buffer), " {\" name\" :\" %s\" ,\" version\" :\" %s\" ,\" size\" :%d}," , CSTR (dir.fileName ()), CSTR (version), dir.fileSize ());
164+ }
165+ }
166+ s += sprintf (--s, " ]\n " );
167+ DebugTf (" filelist response: [%s]\r\n " , buffer);
168+ httpServer.send (200 , " application/json" , buffer);
169+ }
170+
107171
108172// =====================================================================================
109- void APIlistFiles () // Senden aller Daten an den Client
173+ void apilistfiles () // Senden aller Daten an den Client
110174{
111175 FSInfo LittleFSinfo;
112176
@@ -118,7 +182,7 @@ void APIlistFiles() // Senden aller Daten an den Client
118182
119183 _fileMeta dirMap[MAX_FILES_IN_LIST+1 ];
120184 int fileNr = 0 ;
121-
185+
122186 Dir dir = LittleFS.openDir (" /" ); // List files on LittleFS
123187 while (dir.next () && (fileNr < MAX_FILES_IN_LIST))
124188 {
@@ -160,18 +224,17 @@ void APIlistFiles() // Senden aller Daten an den Client
160224 for (int f=0 ; f < fileNr; f++)
161225 {
162226 DebugTf (" [%3d] >> [%s]\r\n " , f, dirMap[f].Name );
163- if (temp != " [" ) temp += " ," ;
164- temp += R"( {"name":")" + String (dirMap[f].Name ) + R"( ","size":")" + formatBytes (dirMap[f].Size ) + R"( "})" ;
227+ temp += R"( {"name":")" + String (dirMap[f].Name ) + R"( ","size":")" + formatBytes (dirMap[f].Size ) + R"( "},)" ;
165228 }
166229
167230 LittleFS.info (LittleFSinfo);
168- temp += R"( , {"usedBytes":")" + formatBytes (LittleFSinfo.usedBytes * 1.05 ) + R"( ",)" + // Berechnet den verwendeten Speicherplatz + 5% Sicherheitsaufschlag
231+ temp += R"( {"usedBytes":")" + formatBytes (LittleFSinfo.usedBytes * 1.05 ) + R"( ",)" + // Berechnet den verwendeten Speicherplatz + 5% Sicherheitsaufschlag
169232 R"( "totalBytes":")" + formatBytes (LittleFSinfo.totalBytes ) + R"( ","freeBytes":")" + // Zeigt die Größe des Speichers
170233 (LittleFSinfo.totalBytes - (LittleFSinfo.usedBytes * 1.05 )) + R"( "}])" ; // Berechnet den freien Speicherplatz + 5% Sicherheitsaufschlag
171234
172235 httpServer.send (200 , " application/json" , temp);
173236
174- } // APIlistFiles ()
237+ } // apilistfiles ()
175238
176239
177240// =====================================================================================
@@ -269,28 +332,11 @@ bool freeSpace(uint16_t const& printsize)
269332
270333} // freeSpace()
271334
272- // =====================================================================================
273- void upgradePIC ()
274- {
275- DebugTln (F (" Redirect to upgrade PIC .." ));
276- doRedirect (" Upgrade OTGW PIC " , 120 , " /FSexplorer" , false );
277- upgradenow ();
278- } // upgradePIC()
279-
280-
281- // =====================================================================================
282- void updateFirmware ()
283- {
284- DebugTln (F (" Redirect to updateIndex .." ));
285- doRedirect (" wait ... " , 1 , " /updateIndex" , false );
286- } // updateFirmware()
287-
288-
289335// =====================================================================================
290336void reBootESP ()
291337{
292338 DebugTln (F (" Redirect and ReBoot .." ));
293- doRedirect (" Reboot OTGW firmware .." , 60 , " /" , true );
339+ doRedirect (" Reboot OTGW firmware .." , 120 , " /" , true );
294340} // reBootESP()
295341
296342// =====================================================================================
0 commit comments