10
10
#include " Builtin_Pages.h"
11
11
#include " HTTP_Server_Basic.h"
12
12
#include " cert.h"
13
+ #include " SS2KLog.h"
13
14
#include < WebServer.h>
14
15
#include < HTTPClient.h>
15
16
#include < HTTPUpdate.h>
@@ -47,7 +48,7 @@ void startWifi() {
47
48
int i = 0 ;
48
49
49
50
// Trying Station mode first:
50
- debugDirector ( " Connecting to: " + String ( userConfig.getSsid () ));
51
+ SS2K_LOG ( " HTTP_Server " , " Connecting to: %s " , userConfig.getSsid ());
51
52
if (String (WiFi.SSID ()) != userConfig.getSsid ()) {
52
53
WiFi.mode (WIFI_STA);
53
54
WiFi.setTxPower (WIFI_POWER_19_5dBm);
@@ -56,11 +57,11 @@ void startWifi() {
56
57
57
58
while (WiFi.status () != WL_CONNECTED) {
58
59
vTaskDelay (1000 / portTICK_RATE_MS);
59
- debugDirector ( " . " , false );
60
+ SS2K_LOG ( " HTTP_Server " , " Waiting for connection to be established... " );
60
61
i++;
61
62
if (i > WIFI_CONNECT_TIMEOUT || (String (userConfig.getSsid ()) == DEVICE_NAME)) {
62
63
i = 0 ;
63
- debugDirector ( " Couldn't Connect. Switching to AP mode" );
64
+ SS2K_LOG ( " HTTP_Server " , " Couldn't Connect. Switching to AP mode" );
64
65
WiFi.disconnect ();
65
66
WiFi.mode (WIFI_AP);
66
67
break ;
@@ -89,30 +90,32 @@ void startWifi() {
89
90
}
90
91
91
92
if (!MDNS.begin (userConfig.getDeviceName ())) {
92
- debugDirector ( " Error setting up MDNS responder!" );
93
+ SS2K_LOG ( " HTTP_Server " , " Error setting up MDNS responder!" );
93
94
}
94
95
95
96
MDNS.addService (" http" , " _tcp" , 80 );
96
97
MDNS.addServiceTxt (" http" , " _tcp" , " lf" , " 0" );
97
- debugDirector (" Connected to " + String (userConfig.getSsid ()) + " IP address: " + myIP.toString (), true , true );
98
- debugDirector (String (" Open http://" ) + userConfig.getDeviceName () + " .local/" );
98
+ SS2K_LOG (" HTTP_Server" , " Connected to %s IP address: %s" , userConfig.getSsid (), myIP.toString ().c_str ());
99
+ SEND_TO_TELEGRAM (" Connected to " + String (userConfig.getSsid ()) + " IP address: " + myIP.toString ());
100
+ SS2K_LOG (" HTTP_Server" , " Open http://%s.local/" , userConfig.getDeviceName ());
99
101
WiFi.setTxPower (WIFI_POWER_19_5dBm);
100
102
101
103
if (WiFi.getMode () == WIFI_STA) {
102
- Serial. print ( " Retrieving time: " );
104
+ SS2K_LOG ( " HTTP_Server " , " Syncing clock... " );
103
105
configTime (0 , 0 , " pool.ntp.org" ); // get UTC time via NTP
104
106
time_t now = time (nullptr );
105
107
while (now < 10 ) { // wait 10 seconds
106
- Serial. print ( " ." );
108
+ SS2K_LOG ( " HTTP_Server " , " Waiting for clock sync.. ." );
107
109
delay (100 );
108
110
now = time (nullptr );
109
111
}
112
+ SS2K_LOG (" HTTP_Server" , " Clock synced to: %.f" , difftime (now, (time_t )0 ));
110
113
Serial.println (now);
111
114
}
112
115
}
113
116
114
117
void startHttpServer () {
115
- server.onNotFound ([]() { debugDirector ( " Link Not Found: " + server.uri ()); });
118
+ server.onNotFound ([]() { SS2K_LOG ( " HTTP_Server " , " Link Not Found: %s " , server.uri (). c_str ()); });
116
119
117
120
/* *******************************************Begin
118
121
* Handlers***********************************/
@@ -132,7 +135,7 @@ void startHttpServer() {
132
135
server.on (" /jquery.js.gz" , handleSpiffsFile);
133
136
134
137
server.on (" /BLEScan" , []() {
135
- debugDirector ( " Scanning from web request" );
138
+ SS2K_LOG ( " HTTP_Server " , " Scanning from web request" );
136
139
String response =
137
140
" <!DOCTYPE html><html><body>Scanning for BLE Devices. Please wait "
138
141
" 15 seconds.</body><script> setTimeout(\" location.href = 'http://" +
@@ -143,7 +146,7 @@ void startHttpServer() {
143
146
});
144
147
145
148
server.on (" /load_defaults.html" , []() {
146
- debugDirector ( " Setting Defaults from Web Request" );
149
+ SS2K_LOG ( " HTTP_Server " , " Setting Defaults from Web Request" );
147
150
SPIFFS.format ();
148
151
userConfig.setDefaults ();
149
152
userConfig.saveToSPIFFS ();
@@ -157,7 +160,7 @@ void startHttpServer() {
157
160
});
158
161
159
162
server.on (" /reboot.html" , []() {
160
- debugDirector ( " Rebooting from Web Request" );
163
+ SS2K_LOG ( " HTTP_Server " , " Rebooting from Web Request" );
161
164
String response = " Rebooting....<script> setTimeout(\" location.href = 'http://" + myIP.toString () + " /index.html';\" ,500); </script>" ;
162
165
server.send (200 , " text/html" , response);
163
166
vTaskDelay (100 / portTICK_PERIOD_MS);
@@ -169,14 +172,14 @@ void startHttpServer() {
169
172
if (value == " enable" ) {
170
173
userConfig.setSimulateHr (true );
171
174
server.send (200 , " text/plain" , " OK" );
172
- debugDirector ( " HR Simulator turned on" );
175
+ SS2K_LOG ( " HTTP_Server " , " HR Simulator turned on" );
173
176
} else if (value == " disable" ) {
174
177
userConfig.setSimulateHr (false );
175
178
server.send (200 , " text/plain" , " OK" );
176
- debugDirector ( " HR Simulator turned off" );
179
+ SS2K_LOG ( " HTTP_Server " , " HR Simulator turned off" );
177
180
} else {
178
181
userConfig.setSimulatedHr (value.toInt ());
179
- debugDirector ( " HR is now: " + String ( userConfig.getSimulatedHr () ));
182
+ SS2K_LOG ( " HTTP_Server " , " HR is now: %d " , userConfig.getSimulatedHr ());
180
183
server.send (200 , " text/plain" , " OK" );
181
184
}
182
185
});
@@ -186,14 +189,14 @@ void startHttpServer() {
186
189
if (value == " enable" ) {
187
190
userConfig.setSimulateWatts (true );
188
191
server.send (200 , " text/plain" , " OK" );
189
- debugDirector ( " Watt Simulator turned on" );
192
+ SS2K_LOG ( " HTTP_Server " , " Watt Simulator turned on" );
190
193
} else if (value == " disable" ) {
191
194
userConfig.setSimulateWatts (false );
192
195
server.send (200 , " text/plain" , " OK" );
193
- debugDirector ( " Watt Simulator turned off" );
196
+ SS2K_LOG ( " HTTP_Server " , " Watt Simulator turned off" );
194
197
} else {
195
198
userConfig.setSimulatedWatts (value.toInt ());
196
- debugDirector ( " Watts are now: " + String ( userConfig.getSimulatedWatts () ));
199
+ SS2K_LOG ( " HTTP_Server " , " Watts are now: %d " , userConfig.getSimulatedWatts ());
197
200
server.send (200 , " text/plain" , " OK" );
198
201
}
199
202
});
@@ -203,32 +206,28 @@ void startHttpServer() {
203
206
if (value == " enable" ) {
204
207
userConfig.setSimulateCad (true );
205
208
server.send (200 , " text/plain" , " OK" );
206
- debugDirector ( " CAD Simulator turned on" );
209
+ SS2K_LOG ( " HTTP_Server " , " CAD Simulator turned on" );
207
210
} else if (value == " disable" ) {
208
211
userConfig.setSimulateCad (false );
209
212
server.send (200 , " text/plain" , " OK" );
210
- debugDirector ( " CAD Simulator turned off" );
213
+ SS2K_LOG ( " HTTP_Server " , " CAD Simulator turned off" );
211
214
} else {
212
215
userConfig.setSimulatedCad (value.toInt ());
213
- debugDirector ( " CAD is now: " + String ( userConfig.getSimulatedCad () ));
216
+ SS2K_LOG ( " HTTP_Server " , " CAD is now: %f " , userConfig.getSimulatedCad ());
214
217
server.send (200 , " text/plain" , " OK" );
215
218
}
216
219
});
217
220
218
221
server.on (" /configJSON" , []() {
219
222
String tString;
220
- tString = userConfig.returnJSON ();
221
- tString.remove (tString.length () - 1 , 1 );
222
- tString += String (" ,\" debug\" :\" " ) + debugToHTML + " \" ,\" firmwareVersion\" :\" " + String (FIRMWARE_VERSION) + " \" }" ;
223
+ tString = userConfig.returnJSON (!server.arg (" includeDebugLog" ).isEmpty ());
223
224
server.send (200 , " text/plain" , tString);
224
- debugToHTML = " " ;
225
225
});
226
226
227
227
server.on (" /PWCJSON" , []() {
228
228
String tString;
229
229
tString = userPWC.returnJSON ();
230
230
server.send (200 , " text/plain" , tString);
231
- debugToHTML = " " ;
232
231
});
233
232
234
233
server.on (" /login" , HTTP_GET, []() {
@@ -253,7 +252,7 @@ void startHttpServer() {
253
252
HTTPUpload &upload = server.upload ();
254
253
if (upload.filename == String (" firmware.bin" ).c_str ()) {
255
254
if (upload.status == UPLOAD_FILE_START) {
256
- debugDirector ( " Update: " + upload.filename );
255
+ SS2K_LOG ( " HTTP_Server " , " Update: %s " , upload.filename . c_str () );
257
256
if (!Update.begin (UPDATE_SIZE_UNKNOWN)) { // start with max
258
257
// available size
259
258
Update.printError (Serial);
@@ -279,7 +278,7 @@ void startHttpServer() {
279
278
if (!filename.startsWith (" /" )) {
280
279
filename = " /" + filename;
281
280
}
282
- debugDirector ( " handleFileUpload Name: " + filename);
281
+ SS2K_LOG ( " HTTP_Server " , " handleFileUpload Name: %s " , filename. c_str () );
283
282
fsUploadFile = SPIFFS.open (filename, " w" );
284
283
filename = String ();
285
284
} else if (upload.status == UPLOAD_FILE_WRITE) {
@@ -290,7 +289,7 @@ void startHttpServer() {
290
289
if (fsUploadFile) {
291
290
fsUploadFile.close ();
292
291
}
293
- debugDirector ( String ( " handleFileUpload Size: " ) + String ( upload.totalSize ) );
292
+ SS2K_LOG ( " HTTP_Server " , " handleFileUpload Size: %zu " , upload.totalSize );
294
293
server.send (200 , " text/plain" , String (upload.filename + " Uploaded Sucessfully." ));
295
294
}
296
295
}
@@ -299,13 +298,13 @@ void startHttpServer() {
299
298
/* *******************************************End Server
300
299
* Handlers*******************************/
301
300
302
- xTaskCreatePinnedToCore (webClientUpdate, /* Task function. */
303
- " webClientUpdate" , /* name of task. */
304
- 4500 , /* Stack size of task Used to be 3000*/
305
- NULL , /* parameter of the task */
306
- 1 , /* priority of the task - 29 worked*/
307
- &webClientTask, /* Task handle to keep track of created task */
308
- 1 ); /* pin task to core 1 */
301
+ xTaskCreatePinnedToCore (webClientUpdate, /* Task function. */
302
+ " webClientUpdate" , /* name of task. */
303
+ 4500 + (DEBUG_LOG_BUFFER_SIZE * 2 ), /* Stack size of task Used to be 3000*/
304
+ NULL , /* parameter of the task */
305
+ 1 , /* priority of the task - 29 worked*/
306
+ &webClientTask, /* Task handle to keep track of created task */
307
+ 1 ); /* pin task to core 1 */
309
308
310
309
#ifdef USE_TELEGRAM
311
310
xTaskCreatePinnedToCore (telegramUpdate, /* Task function. */
@@ -316,9 +315,8 @@ void startHttpServer() {
316
315
&telegramTask, /* Task handle to keep track of created task */
317
316
1 ); /* pin task to core 1 */
318
317
#endif
319
-
320
318
server.begin ();
321
- debugDirector ( " HTTP server started" );
319
+ SS2K_LOG ( " HTTP_Server " , " HTTP server started" );
322
320
}
323
321
324
322
void webClientUpdate (void *pvParameters) {
@@ -343,8 +341,9 @@ void handleIndexFile() {
343
341
File file = SPIFFS.open (filename, FILE_READ);
344
342
server.streamFile (file, " text/html" );
345
343
file.close ();
344
+ SS2K_LOG (" HTTP_Server" , " Served %s" , filename.c_str ());
346
345
} else {
347
- debugDirector (filename + " not found. Sending builtin Index.html" );
346
+ SS2K_LOG ( " HTTP_Server " , " %s not found. Sending builtin Index.html" , filename. c_str () );
348
347
server.send (200 , " text/html" , noIndexHTML);
349
348
}
350
349
}
@@ -360,9 +359,9 @@ void handleSpiffsFile() {
360
359
}
361
360
server.streamFile (file, " text/" + fileType);
362
361
file.close ();
363
- debugDirector ( " Served " + filename);
362
+ SS2K_LOG ( " HTTP_Server " , " Served %s " , filename. c_str () );
364
363
} else {
365
- debugDirector (filename + " not found. Sending builtin Index.html " );
364
+ SS2K_LOG ( " HTTP_Server " , " %s not found. Sending 404. " , filename. c_str () );
366
365
server.send (404 , " text/html" ,
367
366
" <html><body><h1>ERROR 404 <br> FILE NOT "
368
367
" FOUND!</h1></body></html>" );
@@ -482,7 +481,7 @@ void settingsProcessor() {
482
481
myIP.toString () + " /index.html';\" ,1000);</script></html>" ;
483
482
}
484
483
server.send (200 , " text/html" , response);
485
- debugDirector ( " Config Updated From Web" );
484
+ SS2K_LOG ( " HTTP_Server " , " Config Updated From Web" );
486
485
userConfig.saveToSPIFFS ();
487
486
userConfig.printFile ();
488
487
userPWC.saveToSPIFFS ();
@@ -497,7 +496,7 @@ void FirmwareUpdate() {
497
496
// WiFiClientSecure client;
498
497
499
498
client.setCACert (rootCACertificate);
500
- debugDirector ( " Checking for newer firmware:" );
499
+ SS2K_LOG ( " HTTP_Server " , " Checking for newer firmware:" );
501
500
http.begin (userConfig.getFirmwareUpdateURL () + String (FW_VERSIONFILE),
502
501
rootCACertificate); // check version URL
503
502
delay (100 );
@@ -507,10 +506,10 @@ void FirmwareUpdate() {
507
506
if (httpCode == HTTP_CODE_OK) { // if version received
508
507
payload = http.getString (); // save received version
509
508
payload.trim ();
510
- debugDirector ( " - Server version: " + payload);
509
+ SS2K_LOG ( " HTTP_Server " , " - Server version: %s " , payload. c_str () );
511
510
internetConnection = true ;
512
511
} else {
513
- debugDirector ( " error downloading " + String ( FW_VERSIONFILE) + " " + String ( httpCode) );
512
+ SS2K_LOG ( " HTTP_Server " , " error downloading %s %d " , FW_VERSIONFILE, httpCode);
514
513
internetConnection = false ;
515
514
}
516
515
@@ -519,54 +518,54 @@ void FirmwareUpdate() {
519
518
bool updateAnyway = false ;
520
519
if (!SPIFFS.exists (" /index.html" )) {
521
520
updateAnyway = true ;
522
- debugDirector ( " -index.html not found. Forcing update" );
521
+ SS2K_LOG ( " HTTP_Server " , " -index.html not found. Forcing update" );
523
522
}
524
523
Version availiableVer (payload.c_str ());
525
524
Version currentVer (FIRMWARE_VERSION);
526
525
527
526
if ((availiableVer > currentVer) || (updateAnyway)) {
528
- debugDirector ( " New firmware detected!" );
529
- debugDirector ( " Upgrading from " + String (FIRMWARE_VERSION) + " to " + payload);
527
+ SS2K_LOG ( " HTTP_Server " , " New firmware detected!" );
528
+ SS2K_LOG ( " HTTP_Server " , " Upgrading from %s to %s " , FIRMWARE_VERSION, payload. c_str () );
530
529
531
530
// Update Spiffs
532
531
httpUpdate.setLedPin (LED_BUILTIN, LOW);
533
- debugDirector ( " Updating FileSystem" );
532
+ SS2K_LOG ( " HTTP_Server " , " Updating FileSystem" );
534
533
t_httpUpdate_return ret = httpUpdate.updateSpiffs (client, userConfig.getFirmwareUpdateURL () + String (FW_SPIFFSFILE));
535
534
vTaskDelay (100 / portTICK_PERIOD_MS);
536
535
switch (ret) {
537
536
case HTTP_UPDATE_OK:
538
- debugDirector ( " Saving Config.txt" );
537
+ SS2K_LOG ( " HTTP_Server " , " Saving Config.txt" );
539
538
userConfig.saveToSPIFFS ();
540
539
userPWC.saveToSPIFFS ();
541
- debugDirector ( " Updating Program" );
540
+ SS2K_LOG ( " HTTP_Server " , " Updating Program" );
542
541
break ;
543
542
544
543
case HTTP_UPDATE_NO_UPDATES:
545
- debugDirector ( " HTTP_UPDATE_NO_UPDATES" );
544
+ SS2K_LOG ( " HTTP_Server " , " HTTP_UPDATE_NO_UPDATES" );
546
545
break ;
547
546
548
547
case HTTP_UPDATE_FAILED:
549
- debugDirector ( " SPIFFS Update Failed: " + String ( httpUpdate.getLastError ()) + " : " + httpUpdate.getLastErrorString ());
548
+ SS2K_LOG ( " HTTP_Server " , " SPIFFS Update Failed: %d : %s " , httpUpdate.getLastError (), httpUpdate.getLastErrorString (). c_str ());
550
549
break ;
551
550
}
552
551
553
552
// Update Firmware
554
553
ret = httpUpdate.update (client, userConfig.getFirmwareUpdateURL () + String (FW_BINFILE));
555
554
switch (ret) {
556
555
case HTTP_UPDATE_FAILED:
557
- debugDirector ( " HTTP_UPDATE_FAILD Error " + String ( httpUpdate.getLastError ()) + " : " + httpUpdate.getLastErrorString ());
556
+ SS2K_LOG ( " HTTP_Server " , " HTTP_UPDATE_FAILD Error %d : %s " , httpUpdate.getLastError (), httpUpdate.getLastErrorString (). c_str ());
558
557
break ;
559
558
560
559
case HTTP_UPDATE_NO_UPDATES:
561
- debugDirector ( " HTTP_UPDATE_NO_UPDATES" );
560
+ SS2K_LOG ( " HTTP_Server " , " HTTP_UPDATE_NO_UPDATES" );
562
561
break ;
563
562
564
563
case HTTP_UPDATE_OK:
565
- debugDirector ( " HTTP_UPDATE_OK" );
564
+ SS2K_LOG ( " HTTP_Server " , " HTTP_UPDATE_OK" );
566
565
break ;
567
566
}
568
567
} else { // don't update
569
- debugDirector ( " - Current Version: " + String ( FIRMWARE_VERSION) );
568
+ SS2K_LOG ( " HTTP_Server " , " - Current Version: %s " , FIRMWARE_VERSION);
570
569
}
571
570
}
572
571
}
@@ -601,7 +600,7 @@ void telegramUpdate(void *pvParameters) {
601
600
bool rm = (bot.sendMessage (TELEGRAM_CHAT_ID, telegramMessage, " " ));
602
601
if (!rm) {
603
602
telegramFailures++;
604
- debugDirector ( " Telegram failed to send!" , + TELEGRAM_CHAT_ID);
603
+ SS2K_LOG ( " HTTP_Server " , " Telegram failed to send! %s " , TELEGRAM_CHAT_ID);
605
604
if (telegramFailures > 2 ) {
606
605
internetConnection = false ;
607
606
}
0 commit comments