Skip to content

Commit cac9e8a

Browse files
Adjust the file headers for log files on day change. (#3)
Set the headers for the current log file. Set the headers for other files from sd.
1 parent 8595274 commit cac9e8a

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

smartMeterLogger-esp32.ino

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//#define SH1106_OLED /* uncomment to compile for SH1106 instead of SSD1306 */
1+
#define SH1106_OLED /* uncomment to compile for SH1106 instead of SSD1306 */
22

33
#include <SD.h>
44
#include <FS.h>
@@ -18,13 +18,13 @@
1818
#include <SSD1306.h> /* In same library as SH1106 */
1919
#endif
2020

21-
#define USE_WS_BRIDGE true /* true = use a dsmr websocket bridge - false = use a dsmr smartmeter */
21+
#define USE_WS_BRIDGE true /* true = connect to a dsmr websocket bridge - false = connect to a dsmr smartmeter */
2222

2323
const char* WS_BRIDGE_HOST = "192.168.0.177"; /* bridge adress */
2424
const uint16_t WS_BRIDGE_PORT = 80; /* bridge port */
2525
const char* WS_BRIDGE_URL = "/raw"; /* bridge url */
2626

27-
#define SAVE_TIME_MIN (1) /* data save interval in minutes */
27+
#define SAVE_TIME_MIN (1) /* data save interval in minutes */
2828

2929
/* settings for smartMeter */
3030
#define RXD_PIN (36)
@@ -69,8 +69,8 @@ struct {
6969
uint32_t gas;
7070
} current;
7171

72-
time_t bootTime;
73-
bool oledFound{false};
72+
time_t bootTime;
73+
bool oledFound{false};
7474

7575
const char* HEADER_MODIFIED_SINCE = "If-Modified-Since";
7676

@@ -83,6 +83,24 @@ void connectToWebSocketBridge() {
8383
ws_bridge.begin(WS_BRIDGE_HOST, WS_BRIDGE_PORT, WS_BRIDGE_URL);
8484
}
8585

86+
void updateFileHandlers(const tm& now) {
87+
static char path[16];
88+
snprintf(path, sizeof(path), "/%i/%i/%i.log", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday);
89+
90+
static AsyncCallbackWebHandler* currentLogFileHandler;
91+
http_server.removeHandler(currentLogFileHandler);
92+
currentLogFileHandler = &http_server.on(path, HTTP_GET, [] (AsyncWebServerRequest * request) {
93+
if (!SD.exists(path)) return request->send(404);
94+
AsyncWebServerResponse *response = request->beginResponse(SD, path);
95+
response->addHeader("Cache-Control", "no-store, max-age=0");
96+
request->send(response);
97+
});
98+
99+
static AsyncStaticWebHandler* staticFilesHandler;
100+
http_server.removeHandler(staticFilesHandler);
101+
staticFilesHandler = &http_server.serveStatic("/", SD, "/").setCacheControl("public, max-age=604800, immutable");
102+
}
103+
86104
void setup() {
87105
Serial.begin(115200);
88106
Serial.printf("\n\nsmartMeterLogger-esp32\n\nconnecting to %s...\n", WIFI_NETWORK);
@@ -128,15 +146,13 @@ void setup() {
128146
/* sync the clock with ntp */
129147
configTzTime(TIMEZONE, NTP_POOL);
130148

131-
struct tm timeinfo {
149+
tm now {
132150
0
133151
};
134152

135-
while (!getLocalTime(&timeinfo, 0))
153+
while (!getLocalTime(&now, 0))
136154
delay(10);
137155

138-
time(&bootTime);
139-
140156
/* websocket setup */
141157
ws_server_raw.onEvent(ws_server_onEvent);
142158
http_server.addHandler(&ws_server_raw);
@@ -145,6 +161,7 @@ void setup() {
145161
http_server.addHandler(&ws_server_events);
146162

147163
/* webserver setup */
164+
time(&bootTime);
148165
static char modifiedDate[30];
149166
strftime(modifiedDate, sizeof(modifiedDate), "%a, %d %b %Y %X GMT", gmtime(&bootTime));
150167

@@ -201,7 +218,8 @@ void setup() {
201218
request->send(response);
202219
});
203220
*/
204-
http_server.serveStatic("/", SD, "/").setCacheControl("no-store, no-cache, must-revalidate, max-age=0");
221+
222+
updateFileHandlers(now);
205223

206224
http_server.onNotFound([](AsyncWebServerRequest * request) {
207225
request->send(404);
@@ -274,11 +292,17 @@ void loop() {
274292
ws_server_raw.cleanupClients();
275293
ws_server_events.cleanupClients();
276294

277-
static struct tm now;
295+
static tm now;
278296
getLocalTime(&now);
279297
if ((59 == now.tm_sec) && !(now.tm_min % SAVE_TIME_MIN) && (numberOfSamples > 2))
280298
saveAverage(now);
281299

300+
static uint8_t currentMonthDay = now.tm_mday;
301+
if (currentMonthDay != now.tm_mday) {
302+
updateFileHandlers(now);
303+
currentMonthDay = now.tm_mday;
304+
}
305+
282306
if (USE_WS_BRIDGE) {
283307
ws_bridge.loop();
284308
}

0 commit comments

Comments
 (0)