Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions PowerControlHub/ExternalSensorNetworkHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ CommandResult ExternalSensorNetworkHandler::handleRequest(const char* method,
}

if (result == ConfigResult::Success)
{
formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

return CommandResult::error(static_cast<uint8_t>(result));
}
Expand Down
23 changes: 22 additions & 1 deletion PowerControlHub/PowerControlHub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ SerialCommandManager commandMgrComputer(&COMPUTER_SERIAL, onComputerCommandRecei

PowerControlHubApp app(&commandMgrComputer);

void powerControlHubTask(void* pvParameters)
{
PowerControlHubApp* app = (PowerControlHubApp*)pvParameters;
while (true)
{
app->loop();
vTaskDelay(1);
}
}

void setup()
{
// Serial initialization is performed first to ensure that any logging or error messages
Expand All @@ -59,11 +69,22 @@ void setup()

// configure app
app.setup(nullptr, 0);

// Create a dedicated task with 16KB stack
xTaskCreatePinnedToCore(
powerControlHubTask,
"PowerControlHub",
16384, // stack size
&app, // parameters
1, // priority
NULL, // task handle
1 // core 1
);
}

void loop()
{
app.loop();
vTaskDelay(portMAX_DELAY);
}

// Called when a command arrives on COMPUTER_SERIAL that no registered handler
Expand Down
10 changes: 4 additions & 6 deletions PowerControlHub/PowerControlHub.vcxproj

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions PowerControlHub/PowerControlHub.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
</ClCompile>
<ClCompile Include="PowerControlHub.ino" />
<ClCompile Include="SensorConfigNetworkHandler.cpp">
<Filter>Source Files</Filter>
<Filter>Source Files\NetworkCommandHandlers</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -672,9 +672,6 @@
<ClInclude Include="NavigationController.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="build_opt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ExternalSensorConfigCommandHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -714,8 +711,5 @@
<ClInclude Include="SensorConfigNetworkHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SensorConfig.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions PowerControlHub/SensorConfigNetworkHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ CommandResult SensorConfigNetworkHandler::handleRequest(const char* method,
}

if (result == ConfigResult::Success)
{
formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

return CommandResult::error(static_cast<uint8_t>(result));
}
Expand Down
62 changes: 50 additions & 12 deletions PowerControlHub/SensorNetworkHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
e.options2[0],
e.options2[1],
e.enabled ? 1u : 0u);

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
}
}
Expand All @@ -83,11 +85,13 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
int n = snprintf(responseBuffer + written, bufferSize - written,
",\"meta\":{\"count\":%u,\"descriptors\":[",
static_cast<unsigned>(SensorIdList::Count));

if (n < 0 || written + n >= static_cast<int>(bufferSize))
{
responseBuffer[bufferSize - 1] = '\0';
return CommandResult::ok();
}

written += n;

for (unsigned di = 0; di < static_cast<unsigned>(SensorIdList::Count); di++)
Expand All @@ -97,63 +101,91 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
"%s{\"id\":%u,\"name\":\"%s\","
"\"pins\":[",
di > 0 ? "," : "", di, d.name);
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;
written += n;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
bool firstPin = true;

for (unsigned p = 0; p < ConfigMaxSensorPins; p++)
{
if (strcmp(d.pins[p].type, "none") == 0) continue;
if (strcmp(d.pins[p].type, "none") == 0)
continue;

n = snprintf(responseBuffer + written, bufferSize - written,
"%s{\"label\":\"%s\",\"type\":\"%s\",\"min\":%d,\"max\":%d,\"default\":%d}",
firstPin ? "" : ",",
d.pins[p].label, d.pins[p].type,
d.pins[p].minVal, d.pins[p].maxVal, d.pins[p].defaultValue);
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
firstPin = false;
}

n = snprintf(responseBuffer + written, bufferSize - written,
"],\"options1\":[");
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;

bool firstOpt1 = true;

for (unsigned o = 0; o < 2; o++)
{
if (strcmp(d.options1[o].type, "none") == 0) continue;
if (strcmp(d.options1[o].type, "none") == 0)
continue;

n = snprintf(responseBuffer + written, bufferSize - written,
"%s{\"label\":\"%s\",\"type\":\"%s\",\"min\":%d,\"max\":%d,\"default\":%d}",
firstOpt1 ? "" : ",",
d.options1[o].label, d.options1[o].type,
d.options1[o].minVal, d.options1[o].maxVal, d.options1[o].defaultValue);
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
firstOpt1 = false;
}

n = snprintf(responseBuffer + written, bufferSize - written,
"],\"options2\":[");
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;
written += n;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
bool firstOpt2 = true;

for (unsigned o = 0; o < 2; o++)
{
if (strcmp(d.options2[o].type, "none") == 0) continue;
if (strcmp(d.options2[o].type, "none") == 0)
continue;

n = snprintf(responseBuffer + written, bufferSize - written,
"%s{\"label\":\"%s\",\"type\":\"%s\",\"min\":%d,\"max\":%d,\"default\":%d}",
firstOpt2 ? "" : ",",
d.options2[o].label, d.options2[o].type,
d.options2[o].minVal, d.options2[o].maxVal, d.options2[o].defaultValue);
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
firstOpt2 = false;
}

n = snprintf(responseBuffer + written, bufferSize - written, "]}");
if (n < 0 || written + n >= static_cast<int>(bufferSize)) break;

if (n < 0 || written + n >= static_cast<int>(bufferSize))
break;

written += n;
}

Expand Down Expand Up @@ -193,6 +225,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
if (idx >= config->sensors.count)
config->sensors.count = idx + 1;

formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand Down Expand Up @@ -228,6 +261,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
memset(&config->sensors.sensors[config->sensors.count], 0, sizeof(SensorEntry));
}

formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand Down Expand Up @@ -255,6 +289,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
SensorEntry& entry = config->sensors.sensors[idx];
strncpy(entry.name, params[0].value, sizeof(entry.name) - 1);
entry.name[sizeof(entry.name) - 1] = '\0';
formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand All @@ -279,6 +314,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
}

config->sensors.sensors[idx].pins[slot] = pin;
formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand All @@ -305,6 +341,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
}

config->sensors.sensors[idx].enabled = enabled;
formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand Down Expand Up @@ -358,6 +395,7 @@ CommandResult SensorNetworkHandler::handleRequest(const char* method,
config->sensors.sensors[idx].options2[slot] = val16;
}

formatJsonResponse(responseBuffer, bufferSize, true);
return CommandResult::ok();
}

Expand Down
2 changes: 1 addition & 1 deletion PowerControlHub/SystemDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ constexpr const char* compassDirections[16] = {
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
};

constexpr uint16_t MaximumJsonResponseBufferSize = 1024;
constexpr uint16_t MaximumJsonResponseBufferSize = 4096;

/*
* Unique IDs for different sensor types in the system.
Expand Down
20 changes: 8 additions & 12 deletions PowerControlHub/WifiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1112,16 +1112,14 @@ bool WifiServer::dispatchToHandler(IWifiClient& client, INetworkCommandHandler*
// Log response buffer for debugging
if (responseBuffer[0] != '\0')
{
char dbg[1051];
snprintf(dbg, sizeof(dbg), "Handler success response: %s", responseBuffer);
sendDebug(dbg, F("WifiServer"));
sendDebug("Handler success response", F("WifiServer"));
}

// Log parameters passed to handler
char paramDbg[512];
char paramDbg[128];
for (uint8_t p = 0; p < paramCount; p++)
{
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %s=%s", p, params[p].key, params[p].value);
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %*s=%*s", p, (int)sizeof(params[p].key) - 1, params[p].key, (int)sizeof(params[p].value) - 1, params[p].value);
sendDebug(paramDbg, F("WifiServer"));
}

Expand All @@ -1135,15 +1133,13 @@ bool WifiServer::dispatchToHandler(IWifiClient& client, INetworkCommandHandler*
if (responseBuffer[0] != '\0')
{
// Log error response for debugging
char dbg[1050];
snprintf(dbg, sizeof(dbg), "Handler error response: %s", responseBuffer);
sendDebug(dbg, F("WifiServer"));
sendDebug("Handler error response", F("WifiServer"));

// Also log parameters to help reproduce the error
char paramDbg[1024];
char paramDbg[128];
for (uint8_t p = 0; p < paramCount; p++)
{
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %s=%s", p, params[p].key, params[p].value);
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %*s=%*s", p, (int)sizeof(params[p].key) - 1, params[p].key, (int)sizeof(params[p].value) - 1, params[p].value);
sendDebug(paramDbg, F("WifiServer"));
}

Expand All @@ -1160,8 +1156,8 @@ bool WifiServer::dispatchToHandler(IWifiClient& client, INetworkCommandHandler*

for (uint8_t p = 0; p < paramCount; p++)
{
char paramDbg[400];
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %s=%s", p, params[p].key, params[p].value);
char paramDbg[128];
snprintf(paramDbg, sizeof(paramDbg), "Param[%d] %*s=%*s", p, (int)sizeof(params[p].key) - 1, params[p].key, (int)sizeof(params[p].value) - 1 , params[p].value);
sendDebug(paramDbg, F("WifiServer"));
}

Expand Down
4 changes: 2 additions & 2 deletions PowerControlHubApp/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

<ShellContent
x:Name="SensorConfigPage"
Title="Local Sens."
Title="Local Sensors"
Route="SensorConfigPage"
ContentTemplate="{DataTemplate views:LocalSensorConfigPage}" />

<ShellContent
x:Name="ExternalSensorConfigPage"
Title="Ext. Sens."
Title="External Sensors"
Route="ExternalSensorConfigPage"
ContentTemplate="{DataTemplate views:ExternalSensorConfigPage}" />

Expand Down
Loading
Loading