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
2 changes: 1 addition & 1 deletion Docs/Sensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ The handler reads NMEA sentences from any `Stream*` (hardware UART, `SoftwareSer
| Bearing | `S2` | Course in degrees |
| GPS Distance | `S14` | Cumulative distance travelled in km |

**Messages Published (MessageBus)** *(requires `MESSAGE_BUS` define)*
**Messages Published (MessageBus)**

- `GpsLocationUpdated` – latitude and longitude on each new fix.
- `GpsAltitudeUpdated` – altitude on each new fix.
Expand Down
155 changes: 76 additions & 79 deletions SmartFuseBox/AckCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,12 @@
#include "SystemFunctions.h"
#include "ConfigController.h"

#if defined(NEXTION_DISPLAY_DEVICE)
#include <NextionControl.h>
#include "BasePage.h"
#endif

const char AckCommand[] = "ACK";

AckCommandHandler::AckCommandHandler(BroadcastManager* broadcastManager,
#if defined(NEXTION_DISPLAY_DEVICE)
NextionControl* nextionControl,
#endif
MessageBus* messageBus,
WarningManager* warningManager)
: BaseNextionCommandHandler(broadcastManager,
#if defined(NEXTION_DISPLAY_DEVICE)
nextionControl,
#endif
warningManager)
: BaseNextionCommandHandler(broadcastManager, messageBus, warningManager)
{

}
Expand Down Expand Up @@ -100,80 +89,88 @@ bool AckCommandHandler::handleCommand(SerialCommandManager* sender, const char*
// only process known ACK keys if you need to take action

#if defined(NEXTION_DISPLAY_DEVICE)
if (strcmp(params[0].key, RelayRetrieveStates) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
// Relay state acknowledgement - handle both formats:
// 1. ACK:R2=ok (just acknowledgement, no relay state - paramCount == 1)
// 2. ACK:R2=ok:0=0 (acknowledgement with relay state - paramCount == 2)

if (paramCount >= 2)
{
// Format: ACK:R2=ok:0=0 (with relay index and state)
if (!SystemFunctions::isAllDigits(params[1].key) || !SystemFunctions::isAllDigits(params[1].value))
{
sendDebugMessage(F("Invalid R2 Ack response"), F("AckCommandHandler"));
return true;
}
if (strcmp(params[0].key, RelayRetrieveStates) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
if (!SystemFunctions::isAllDigits(params[1].key) || !SystemFunctions::isAllDigits(params[1].value))
{
sendDebugMessage(F("Invalid R2 Ack response"), F("AckCommandHandler"));
return true;
}

uint8_t relayIndex = static_cast<uint8_t>(strtoul(params[1].key, nullptr, 0));
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);

RelayStateUpdate update = { relayIndex, isOn };
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::RelayState), &update);
}
}
else if (strcmp(params[0].key, RelayStatusGet) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
if (relayIndex < ConfigRelayCount)
{
uint8_t changedMask = 1 << relayIndex;

if (_messageBus)
_messageBus->publish<RelayStatusChanged>(changedMask);
}
else
{
_broadcaster->sendDebug("ACK relay index out of range", AckCommand);
}
}
}
else if (strcmp(params[0].key, RelayStatusGet) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
uint8_t relayIndex = static_cast<uint8_t>(strtoul(params[1].key, nullptr, 0));
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);

RelayStateUpdate update = { relayIndex, isOn };
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::RelayState), &update);
}
else
{
if (relayIndex < ConfigRelayCount)
{
uint8_t changedMask = 1 << relayIndex;

if (_messageBus)
_messageBus->publish<RelayStatusChanged>(changedMask);
}
else
{
_broadcaster->sendDebug("ACK relay index out of range", AckCommand);
}
}
else
{
sendDebugMessage(F("Invalid R4 ACK format RelayStatusGet"), AckCommand);
}
}
else if (strcmp(params[0].key, SoundSignalActive) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);

BoolStateUpdate update = { isOn };
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::SoundSignal), &update);
}
else
{
}
}
else if (strcmp(params[0].key, SoundSignalActive) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);
if (_messageBus)
_messageBus->publish<SoundSignalUpdated>(isOn);
}
else
{
sendDebugMessage(F("Invalid R4 ACK format Sound Signal Active"), AckCommand);
}
}
else if (strcmp(params[0].key, SystemCpuUsage) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
uint8_t cpuUsage = static_cast<uint8_t>(strtoul(params[1].value, nullptr, 0));
UInt8Update update = { cpuUsage };
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::CpuUsage), &update);
}
else
{
sendDebugMessage(F("Invalid F3 ACK format: cpu"), AckCommand);
}
}
else if (strcmp(params[0].key, SystemFreeMemory) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
uint16_t freeMemory = static_cast<uint16_t>(strtoul(params[1].value, nullptr, 0));
UInt16Update update = { freeMemory };
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::MemoryUsage), &update);
}
else
{
{
if (paramCount >= 2)
{
uint8_t cpuUsage = static_cast<uint8_t>(strtoul(params[1].value, nullptr, 0));
if (_messageBus)
_messageBus->publish<CpuUsageUpdated>(cpuUsage);
}
else
{
sendDebugMessage(F("Invalid F3 ACK format: cpu"), AckCommand);
}
}
else if (strcmp(params[0].key, SystemFreeMemory) == 0 && strcmp(params[0].value, AckSuccess) == 0)
{
if (paramCount >= 2)
{
uint16_t freeMemory = static_cast<uint16_t>(strtoul(params[1].value, nullptr, 0));
if (_messageBus)
_messageBus->publish<MemoryUsageUpdated>(freeMemory);
}
else
{
sendDebugMessage(F("Invalid F2 ACK format free memory"), AckCommand);
}
}
Expand Down
8 changes: 1 addition & 7 deletions SmartFuseBox/AckCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
#include "ConfigManager.h"
#include "BaseNextionCommandHandler.h"

#if defined(NEXTION_DISPLAY_DEVICE)
#include <NextionControl.h>
#endif

// Forward declarations
class ConfigController;

Expand All @@ -40,9 +36,7 @@ class AckCommandHandler : public BaseNextionCommandHandler

public:
explicit AckCommandHandler(BroadcastManager* broadcastManager,
#if defined(NEXTION_DISPLAY_DEVICE)
NextionControl* nextionControl,
#endif
MessageBus* messageBus,
WarningManager* warningManager);

// Set the config sync manager (optional - needed for config sync feature)
Expand Down
10 changes: 3 additions & 7 deletions SmartFuseBox/BaseNextionCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@

BaseNextionCommandHandler::BaseNextionCommandHandler(
BroadcastManager* broadcaster,
#if defined(NEXTION_DISPLAY_DEVICE)
NextionControl* nextionControl,
#endif
MessageBus* messageBus,
WarningManager* warningManager)
: SharedBaseCommandHandler(broadcaster, warningManager)
#if defined(NEXTION_DISPLAY_DEVICE)
, _nextionControl(nextionControl)
#endif
: SharedBaseCommandHandler(broadcaster, warningManager),
_messageBus(messageBus)
{
}
40 changes: 4 additions & 36 deletions SmartFuseBox/BaseNextionCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@

#include <Arduino.h>

#if defined(NEXTION_DISPLAY_DEVICE)
#include <NextionControl.h>
#endif

#include "SharedBaseCommandHandler.h"
#include "WarningManager.h"
#include "MessageBus.h"

/**
* @brief Base class for command handlers that interact with boat-specific systems.
Expand Down Expand Up @@ -56,39 +53,10 @@ class BaseNextionCommandHandler : public SharedBaseCommandHandler
*/
BaseNextionCommandHandler(
BroadcastManager* broadcaster,
#if defined(NEXTION_DISPLAY_DEVICE)
NextionControl* nextionControl,
#endif
MessageBus* messageBus,
WarningManager* warningManager = nullptr
);

/**
* @brief Notify the current display page of an external update.
*
* This is a convenience wrapper that safely gets the current page from
* NextionControl and calls handleExternalUpdate on it.
*
* @param updateType Type of update (cast to uint8_t from PageUpdateType enum)
* @param data Optional pointer to update-specific data structure
*/
void notifyCurrentPage(uint8_t updateType, const void* data)
{
#if defined(NEXTION_DISPLAY_DEVICE)
if (!_nextionControl)
return;

BaseDisplayPage* p = _nextionControl->getCurrentPage();

if (!p)
return;

p->handleExternalUpdate(updateType, data);
#endif
}

#if defined(NEXTION_DISPLAY_DEVICE)
// Protected member variables for derived classes to access
// Note: _broadcaster is inherited from SharedBaseCommandHandler
NextionControl* _nextionControl;
#endif
protected:
MessageBus* _messageBus;
};
6 changes: 4 additions & 2 deletions SmartFuseBox/BasePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

BasePage::BasePage(Stream* serialPort,
WarningManager* warningMgr,
SerialCommandManager* commandMgrComputer)
SerialCommandManager* commandMgrComputer,
MessageBus* messageBus)
: BaseDisplayPage(serialPort),
_config(nullptr),
_commandMgrComputer(commandMgrComputer),
_warningManager(warningMgr)
_warningManager(warningMgr),
_messageBus(messageBus)
{
}

Expand Down
Loading
Loading