Skip to content

Commit d011a38

Browse files
authored
Add initial home page to control/view relay statuses and view sensor … (#183)
* Add initial home page to control/view relay statuses and view sensor data * fixes from code review
1 parent a80b665 commit d011a38

14 files changed

Lines changed: 533 additions & 62 deletions

SmartFuseBox/Dht11SensorHandler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ class Dht11SensorHandler : public BaseSensor, public BroadcastLoggerSupport
186186

187187
uint64_t update() override
188188
{
189-
sendDebug("Reading DHT11 sensor...", _name);
190-
191189
float temperature = 0.0f;
192190
float humidity = 0.0f;
193191
int result = readDht11(_sensorPin, temperature, humidity);

SmartFuseBox/INetworkCommandHandler.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,27 @@ class INetworkCommandHandler : public NetworkJsonVisitor
4747
* @param bufferSize Size of response buffer
4848
* @return CommandResult with success/status
4949
*/
50-
virtual CommandResult handleRequest(const char* method,
50+
virtual CommandResult handleRequest(const char* method,
5151
const char* command,
52-
StringKeyValue* params,
52+
StringKeyValue* params,
5353
uint8_t paramCount,
54-
char* responseBuffer,
55-
size_t bufferSize) = 0;
56-
57-
virtual ~INetworkCommandHandler() = default;
54+
char* responseBuffer,
55+
size_t bufferSize) = 0;
56+
57+
/**
58+
* @brief Optionally stream an HTML response directly to the client.
59+
*
60+
* Handlers that produce HTML (rather than JSON) should override this method,
61+
* write a complete HTTP response (headers + body) to @p client and return true.
62+
* The default implementation returns false, which causes the caller to fall
63+
* back to the standard JSON path via handleRequest().
64+
*
65+
* @param client The WiFi client to write to.
66+
* @return true if the response was handled and written; false otherwise.
67+
*/
68+
virtual bool generateHtml(IWifiClient& client) const { return false; }
69+
70+
virtual ~INetworkCommandHandler() = default;
5871

5972
uint8_t formatJsonResponse(char* buffer, size_t size, bool success, const char* message = nullptr)
6073
{

SmartFuseBox/Local.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,30 @@
6161

6262

6363
// ─── Display Support ───────────────────────────────────────────────────────
64-
// Enable support for Nextion displays.
64+
// Enable support for Nextion displays (approx 52kb).
6565
#define NEXTION_DISPLAY_DEVICE
6666

6767
// ─── Optional Features ────────────────────────────────────────────────────────
6868
// Remove the trailing underscore to enable each feature.
6969

70-
// SD card support (experimental)
70+
// SD card support (experimental) (approx 32kb)
7171
#define SD_CARD_SUPPORT
7272

7373
#if defined(SD_CARD_SUPPORT)
7474
// Read config.txt from SD card at boot. Requires SD_CARD_SUPPORT.
7575
#define CARD_CONFIG_LOADER_
7676
#endif
7777

78-
// MQTT home-assistant discovery (requires WIFI_SUPPORT — enforced in BoardConfig.h)
78+
// MQTT home-assistant discovery (requires WIFI_SUPPORT — enforced in BoardConfig.h) (approx 40kb)
7979
#define MQTT_SUPPORT
8080

8181
// OTA auto-update via GitHub releases (requires ESP32 + WIFI_SUPPORT — enforced in BoardConfig.h).
8282
// When enabled, the device checks for a new release every 24 hours and broadcasts the result.
8383
// Auto-applying the update is OFF by default; use the F12:apply=1 command or set the
84-
// OtaFlagAutoApply bit in SystemHeader::reserved[0] to enable automatic installs.
84+
// OtaFlagAutoApply bit in SystemHeader::reserved[0] to enable automatic installs. (approx 165kb)
8585
#define OTA_AUTO_UPDATE
8686

87-
// Bluetooth BLE (mutually exclusive with WIFI_SUPPORT on Arduino Uno R4)
87+
// Bluetooth BLE (mutually exclusive with WIFI_SUPPORT on Arduino Uno R4) (approx 144kb)
8888
#define BLUETOOTH_SUPPORT_
8989

9090
// Led Manager on arduino R4 Wifi, experimental at present time

SmartFuseBox/PageSystem.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ void PageSystem::refresh(unsigned long now)
7373

7474
if (now - _lastRefreshTime >= RefreshSystemIntervalMs)
7575
{
76-
SerialCommandManager* compMgr = getCommandMgrComputer();
77-
78-
if (compMgr)
79-
{
80-
compMgr->sendDebug(F("Sending F2/F3"), F("SystemPage"));
81-
}
82-
8376
_lastRefreshTime = now;
8477

8578
setFuseBoxCpu(SystemCpuMonitor::getCpuUsage());

SmartFuseBox/RelayCommandHandler.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,24 @@ bool RelayCommandHandler::handleCommand(SerialCommandManager* sender, const char
112112
}
113113

114114
CommandResult result = _relayController->setRelayState(relayIndex, state > 0);
115-
RelayResult status = static_cast<RelayResult>(result.status);
116115

117-
if (status == RelayResult::InvalidIndex)
118-
{
119-
sendAckErr(sender, command, F("Invalid relay index"), &params[0]);
120-
return true;
121-
}
122-
else if (status == RelayResult::Reserved)
123-
{
124-
sendAckErr(sender, command, F("Relay is reserved for sound system"), &params[0]);
125-
return true;
116+
if (!result.success)
117+
{
118+
RelayResult status = static_cast<RelayResult>(result.status);
119+
120+
if (status == RelayResult::InvalidIndex)
121+
{
122+
sendAckErr(sender, command, F("Invalid relay index"), &params[0]);
123+
return true;
124+
}
125+
else if (status == RelayResult::Reserved)
126+
{
127+
sendAckErr(sender, command, F("Relay is reserved for sound system"), &params[0]);
128+
return true;
129+
}
130+
131+
sendAckErr(sender, command, F("Failed to set relay state"), &params[0]);
132+
return true;
126133
}
127134

128135
broadcastRelayStatus(command, &params[0]);

SmartFuseBox/RelayNetworkHandler.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ CommandResult RelayNetworkHandler::handleRequest(const char* method,
6666
}
6767

6868
CommandResult result = _relayController->setRelayState(relayIndex, state > 0);
69-
RelayResult status = static_cast<RelayResult>(result.status);
7069

71-
if (status == RelayResult::InvalidIndex)
72-
{
73-
return CommandResult::error(InvalidCommandParameters);
74-
}
75-
else if (status == RelayResult::Reserved)
70+
71+
if (!result.success)
7672
{
7773
return CommandResult::error(InvalidCommandParameters);
7874
}

SmartFuseBox/SmartFuseBox.vcxproj

Lines changed: 3 additions & 2 deletions
Large diffs are not rendered by default.

SmartFuseBox/SmartFuseBoxApp.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ SmartFuseBoxApp::SmartFuseBoxApp(SerialCommandManager* commandMgrComputer)
7575
_soundNetworkHandler(&_soundController),
7676
_warningNetworkHandler(&_warningManager),
7777
_systemNetworkHandler(&_wifiController),
78-
_sensorNetworkHandler(nullptr)
78+
_sensorNetworkHandler(nullptr),
79+
_webIndexNetworkHandler(nullptr)
7980

8081
#if defined(SD_CARD_SUPPORT)
81-
, _sdCardLogger(&_sensorCommandHandler, &_warningManager)
82+
, _sdCardLogger(&_sensorCommandHandler, &_warningManager)
8283
#endif
8384

8485
, _sensorManager(nullptr)
@@ -235,6 +236,8 @@ void SmartFuseBoxApp::setup(RemoteSensor** remoteSensors, uint8_t remoteSensorCo
235236
_sensorManager = new SensorManager(allHandlers, totalCount);
236237

237238
_sensorNetworkHandler = new SensorNetworkHandler(_sensorController);
239+
_webIndexNetworkHandler = new WebIndexNetworkHandler(&_relayController, _sensorController);
240+
238241
_relayController.setup();
239242

240243
// middleware
@@ -443,7 +446,7 @@ void SmartFuseBoxApp::configureWifiSupport(Config* config)
443446
// network command handlers
444447
INetworkCommandHandler* networkHandlers[] = { &_relayNetworkHandler, &_soundNetworkHandler, &_warningNetworkHandler,
445448
&_systemNetworkHandler, _sensorNetworkHandler, &_configNetworkHandler, &_schedulerNetworkHandler,
446-
&_externalSensorNetworkHandler,
449+
&_externalSensorNetworkHandler, _webIndexNetworkHandler,
447450
&_wifiCommandBridge
448451
};
449452
size_t networkHandlerCount = sizeof(networkHandlers) / sizeof(networkHandlers[0]);

SmartFuseBox/SmartFuseBoxApp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "SensorNetworkHandler.h"
5656
#include "WarningNetworkHandler.h"
5757
#include "SchedulerNetworkHandler.h"
58+
#include "WebIndexNetworkHandler.h"
5859
#include "WifiCommandBridge.h"
5960

6061
#if defined(SD_CARD_SUPPORT)
@@ -129,6 +130,7 @@ class SmartFuseBoxApp
129130
WarningNetworkHandler _warningNetworkHandler;
130131
SystemNetworkHandler _systemNetworkHandler;
131132
SensorNetworkHandler* _sensorNetworkHandler;
133+
WebIndexNetworkHandler* _webIndexNetworkHandler;
132134

133135
#if defined(SD_CARD_SUPPORT)
134136
SdCardLogger _sdCardLogger;

SmartFuseBox/SystemFunctions.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,82 @@ void SystemFunctions::sanitizeJsonString(const char* input, char* output, size_t
472472
output[outPos] = '\0';
473473
}
474474

475+
void SystemFunctions::escapeHtml(const char* input, char* output, size_t outputSize)
476+
{
477+
if (!output || outputSize == 0)
478+
return;
479+
480+
output[0] = '\0';
481+
482+
if (!input)
483+
return;
484+
485+
size_t outPos = 0;
486+
const size_t limit = outputSize - 1;
487+
488+
while (*input != '\0' && outPos < limit)
489+
{
490+
char c = *input++;
491+
492+
// Escape special HTML characters
493+
if (c == '&')
494+
{
495+
// &amp; (5 chars)
496+
if (outPos + 5 > limit) break;
497+
output[outPos++] = '&';
498+
output[outPos++] = 'a';
499+
output[outPos++] = 'm';
500+
output[outPos++] = 'p';
501+
output[outPos++] = ';';
502+
}
503+
else if (c == '<')
504+
{
505+
// &lt; (4 chars)
506+
if (outPos + 4 > limit) break;
507+
output[outPos++] = '&';
508+
output[outPos++] = 'l';
509+
output[outPos++] = 't';
510+
output[outPos++] = ';';
511+
}
512+
else if (c == '>')
513+
{
514+
// &gt; (4 chars)
515+
if (outPos + 4 > limit) break;
516+
output[outPos++] = '&';
517+
output[outPos++] = 'g';
518+
output[outPos++] = 't';
519+
output[outPos++] = ';';
520+
}
521+
else if (c == '"')
522+
{
523+
// &quot; (6 chars)
524+
if (outPos + 6 > limit) break;
525+
output[outPos++] = '&';
526+
output[outPos++] = 'q';
527+
output[outPos++] = 'u';
528+
output[outPos++] = 'o';
529+
output[outPos++] = 't';
530+
output[outPos++] = ';';
531+
}
532+
else if (c == '\'')
533+
{
534+
// &#39; (5 chars)
535+
if (outPos + 5 > limit) break;
536+
output[outPos++] = '&';
537+
output[outPos++] = '#';
538+
output[outPos++] = '3';
539+
output[outPos++] = '9';
540+
output[outPos++] = ';';
541+
}
542+
else
543+
{
544+
output[outPos++] = c;
545+
}
546+
}
547+
548+
output[outPos] = '\0';
549+
}
550+
475551
bool SystemFunctions::progmemToBuffer(const char* progmemStr, char* buffer, size_t bufferSize)
476552
{
477553
if (!progmemStr || !buffer || bufferSize == 0)

0 commit comments

Comments
 (0)