Skip to content

Commit a4d381a

Browse files
authored
Implement admin PIN functionality and SMS alert configuration options
1 parent fa27b7f commit a4d381a

File tree

2 files changed

+616
-40
lines changed

2 files changed

+616
-40
lines changed

TankAlarm-112025-Client-BluesOpta/TankAlarm-112025-Client-BluesOpta.ino

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ static size_t strlcpy(char *dst, const char *src, size_t size) {
8383
#define NOTE_BUFFER_TEMP_PATH "/pending_notes.tmp"
8484
#endif
8585

86+
#ifndef NOTE_BUFFER_MAX_BYTES
87+
#define NOTE_BUFFER_MAX_BYTES 16384
88+
#endif
89+
90+
#ifndef NOTE_BUFFER_MIN_HEADROOM
91+
#define NOTE_BUFFER_MIN_HEADROOM 2048
92+
#endif
93+
8694
#ifndef MAX_TANKS
8795
#define MAX_TANKS 8
8896
#endif
@@ -236,6 +244,7 @@ static void sendDailyReport();
236244
static void publishNote(const char *fileName, const JsonDocument &doc, bool syncNow);
237245
static void bufferNoteForRetry(const char *fileName, const char *payload, bool syncNow);
238246
static void flushBufferedNotes();
247+
static void pruneNoteBufferIfNeeded();
239248
static void ensureTimeSync();
240249
static void updateDailyScheduleIfNeeded();
241250
static bool checkNotecardHealth();
@@ -1360,7 +1369,8 @@ static bool appendDailyTank(DynamicJsonDocument &doc, JsonArray &array, uint8_t
13601369
static void publishNote(const char *fileName, const JsonDocument &doc, bool syncNow) {
13611370
// Build target file string and serialized payload once for both live send and buffering
13621371
char targetFile[80];
1363-
snprintf(targetFile, sizeof(targetFile), "fleet.%s:%s", gConfig.serverFleet, fileName);
1372+
const char *fleetName = (strlen(gConfig.serverFleet) > 0) ? gConfig.serverFleet : "tankalarm-server";
1373+
snprintf(targetFile, sizeof(targetFile), "fleet:%s:%s", fleetName, fileName);
13641374

13651375
char buffer[1024];
13661376
size_t len = serializeJson(doc, buffer, sizeof(buffer));
@@ -1418,6 +1428,7 @@ static void bufferNoteForRetry(const char *fileName, const char *payload, bool s
14181428
file.println(payload);
14191429
file.close();
14201430
Serial.println(F("Note buffered for retry"));
1431+
pruneNoteBufferIfNeeded();
14211432
}
14221433

14231434
static void flushBufferedNotes() {
@@ -1493,3 +1504,52 @@ static void flushBufferedNotes() {
14931504
LittleFS.remove(NOTE_BUFFER_TEMP_PATH);
14941505
}
14951506
}
1507+
1508+
static void pruneNoteBufferIfNeeded() {
1509+
if (!LittleFS.exists(NOTE_BUFFER_PATH)) {
1510+
return;
1511+
}
1512+
1513+
File file = LittleFS.open(NOTE_BUFFER_PATH, "r");
1514+
if (!file) {
1515+
return;
1516+
}
1517+
1518+
size_t size = file.size();
1519+
if (size <= NOTE_BUFFER_MAX_BYTES) {
1520+
file.close();
1521+
return;
1522+
}
1523+
1524+
size_t targetSize = NOTE_BUFFER_MAX_BYTES > NOTE_BUFFER_MIN_HEADROOM ? (NOTE_BUFFER_MAX_BYTES - NOTE_BUFFER_MIN_HEADROOM) : (NOTE_BUFFER_MAX_BYTES / 2);
1525+
if (targetSize == 0) {
1526+
targetSize = NOTE_BUFFER_MAX_BYTES / 2;
1527+
}
1528+
size_t startOffset = (size > targetSize) ? (size - targetSize) : 0;
1529+
1530+
if (!file.seek(startOffset)) {
1531+
file.close();
1532+
LittleFS.remove(NOTE_BUFFER_PATH);
1533+
return;
1534+
}
1535+
1536+
if (startOffset > 0) {
1537+
file.readStringUntil('\n');
1538+
}
1539+
1540+
File tmp = LittleFS.open(NOTE_BUFFER_TEMP_PATH, "w");
1541+
if (!tmp) {
1542+
file.close();
1543+
return;
1544+
}
1545+
1546+
while (file.available()) {
1547+
tmp.write(file.read());
1548+
}
1549+
1550+
file.close();
1551+
tmp.close();
1552+
LittleFS.remove(NOTE_BUFFER_PATH);
1553+
LittleFS.rename(NOTE_BUFFER_TEMP_PATH, NOTE_BUFFER_PATH);
1554+
Serial.println(F("Note buffer pruned"));
1555+
}

0 commit comments

Comments
 (0)