Skip to content

Commit b1da2b0

Browse files
committed
Refactor includes and function declarations
Moved LittleFS include to maintain alphabetical order. Updated function declarations to avoid duplication and improve organization. Improved HTTP response handling in respondJson by explicitly handling non-200 status codes. Replaced deprecated JConvertToJson with JConvertToJSONString. Removed unnecessary notecard.deleteRequest calls after failed JSON parsing.
1 parent 282a54a commit b1da2b0

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

TankAlarm-112025-Server-BluesOpta/TankAlarm-112025-Server-BluesOpta.ino

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#include <Arduino.h>
2121
#include <Wire.h>
2222
#include <ArduinoJson.h>
23-
#include <LittleFS.h>
2423
#include <Notecard.h>
2524
#include <Ethernet.h>
25+
#include <LittleFS.h>
2626
#include <math.h>
2727
#include <string.h>
2828
#include <ctype.h>
@@ -39,6 +39,7 @@
3939

4040
// Watchdog support for STM32H7 (Arduino Opta)
4141
#if defined(ARDUINO_OPTA) || defined(STM32H7xx)
42+
4243
#include <IWatchdog.h>
4344
#define WATCHDOG_AVAILABLE
4445
#define WATCHDOG_TIMEOUT_SECONDS 30
@@ -197,8 +198,9 @@ static unsigned long gLastPollMillis = 0;
197198
static double gLastDailyEmailSentEpoch = 0.0;
198199
#define MIN_DAILY_EMAIL_INTERVAL_SECONDS 3600 // Minimum 1 hour between daily emails
199200

200-
#ifndef strlcpy
201-
static size_t strlcpy(char *dst, const char *src, size_t size) {
201+
#ifndef ARDUINO_TANKALARM_HAS_STRLCPY
202+
#define ARDUINO_TANKALARM_HAS_STRLCPY
203+
size_t strlcpy(char *dst, const char *src, size_t size) {
202204
if (!dst || !src || size == 0) {
203205
return 0;
204206
}
@@ -2540,6 +2542,22 @@ static void sendClientDataJson(EthernetClient &client);
25402542
static void handleConfigPost(EthernetClient &client, const String &body);
25412543
static void handlePinPost(EthernetClient &client, const String &body);
25422544
static void handleRefreshPost(EthernetClient &client, const String &body);
2545+
static ConfigDispatchStatus dispatchClientConfig(const char *clientUid, JsonVariantConst cfgObj);
2546+
static void pollNotecard();
2547+
static void processNotefile(const char *fileName, void (*handler)(JsonDocument &, double));
2548+
static void handleTelemetry(JsonDocument &doc, double epoch);
2549+
static void handleAlarm(JsonDocument &doc, double epoch);
2550+
static void handleDaily(JsonDocument &doc, double epoch);
2551+
static TankRecord *upsertTankRecord(const char *clientUid, uint8_t tankNumber);
2552+
static void sendSmsAlert(const char *message);
2553+
static void sendDailyEmail();
2554+
static void loadClientConfigSnapshots();
2555+
static void saveClientConfigSnapshots();
2556+
static void cacheClientConfigFromBuffer(const char *clientUid, const char *buffer);
2557+
static ClientConfigSnapshot *findClientConfigSnapshot(const char *clientUid);
2558+
static bool checkSmsRateLimit(TankRecord *rec);
2559+
static void publishViewerSummary();
2560+
static double computeNextAlignedEpoch(double epoch, uint8_t baseHour, uint32_t intervalSeconds);
25432561
enum class ConfigDispatchStatus : uint8_t {
25442562
Ok = 0,
25452563
PayloadTooLarge,
@@ -2568,22 +2586,6 @@ static void handleRefreshPost(EthernetClient &client, const String &body) {
25682586
pollNotecard();
25692587
sendClientDataJson(client);
25702588
}
2571-
static ConfigDispatchStatus dispatchClientConfig(const char *clientUid, JsonVariantConst cfgObj);
2572-
static void pollNotecard();
2573-
static void processNotefile(const char *fileName, void (*handler)(JsonDocument &, double));
2574-
static void handleTelemetry(JsonDocument &doc, double epoch);
2575-
static void handleAlarm(JsonDocument &doc, double epoch);
2576-
static void handleDaily(JsonDocument &doc, double epoch);
2577-
static TankRecord *upsertTankRecord(const char *clientUid, uint8_t tankNumber);
2578-
static void sendSmsAlert(const char *message);
2579-
static void sendDailyEmail();
2580-
static void loadClientConfigSnapshots();
2581-
static void saveClientConfigSnapshots();
2582-
static void cacheClientConfigFromBuffer(const char *clientUid, const char *buffer);
2583-
static ClientConfigSnapshot *findClientConfigSnapshot(const char *clientUid);
2584-
static bool checkSmsRateLimit(TankRecord *rec);
2585-
static void publishViewerSummary();
2586-
static double computeNextAlignedEpoch(double epoch, uint8_t baseHour, uint32_t intervalSeconds);
25872589

25882590
void setup() {
25892591
Serial.begin(115200);
@@ -3057,7 +3059,11 @@ static void respondHtml(EthernetClient &client, const String &body) {
30573059
static void respondJson(EthernetClient &client, const String &body, int status) {
30583060
client.print(F("HTTP/1.1 "));
30593061
client.print(status);
3060-
client.println(status == 200 ? F(" OK") : "");
3062+
if (status == 200) {
3063+
client.println(F(" OK"));
3064+
} else {
3065+
client.println();
3066+
}
30613067
client.println(F("Content-Type: application/json"));
30623068
client.print(F("Content-Length: "));
30633069
client.println(body.length());
@@ -3396,7 +3402,6 @@ static ConfigDispatchStatus dispatchClientConfig(const char *clientUid, JsonVari
33963402

33973403
J *body = JParse(buffer);
33983404
if (!body) {
3399-
notecard.deleteRequest(req);
34003405
return ConfigDispatchStatus::PayloadTooLarge;
34013406
}
34023407
JAddItemToObject(req, "body", body);
@@ -3438,7 +3443,7 @@ static void processNotefile(const char *fileName, void (*handler)(JsonDocument &
34383443
break;
34393444
}
34403445

3441-
char *json = JConvertToJson(body);
3446+
char *json = JConvertToJSONString(body);
34423447
double epoch = JGetNumber(rsp, "time");
34433448
if (json) {
34443449
DynamicJsonDocument doc(4096);
@@ -3635,7 +3640,6 @@ static void sendSmsAlert(const char *message) {
36353640
JAddBoolToObject(req, "sync", true);
36363641
J *body = JParse(buffer);
36373642
if (!body) {
3638-
notecard.deleteRequest(req);
36393643
return;
36403644
}
36413645
JAddItemToObject(req, "body", body);
@@ -3691,7 +3695,6 @@ static void sendDailyEmail() {
36913695
JAddBoolToObject(req, "sync", true);
36923696
J *body = JParse(buffer);
36933697
if (!body) {
3694-
notecard.deleteRequest(req);
36953698
return;
36963699
}
36973700
JAddItemToObject(req, "body", body);
@@ -3738,7 +3741,6 @@ static void publishViewerSummary() {
37383741
JAddBoolToObject(req, "sync", true);
37393742
J *body = JParse(json.c_str());
37403743
if (!body) {
3741-
notecard.deleteRequest(req);
37423744
Serial.println(F("Viewer summary JSON parse failed"));
37433745
return;
37443746
}

0 commit comments

Comments
 (0)