Skip to content

Commit 4de78c2

Browse files
authored
Convert nextion to message bus (#181)
* Convert nextion to message bus * Updates from code review
1 parent bc6981a commit 4de78c2

31 files changed

Lines changed: 395 additions & 590 deletions

Docs/Sensors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ The handler reads NMEA sentences from any `Stream*` (hardware UART, `SoftwareSer
262262
| Bearing | `S2` | Course in degrees |
263263
| GPS Distance | `S14` | Cumulative distance travelled in km |
264264

265-
**Messages Published (MessageBus)** *(requires `MESSAGE_BUS` define)*
265+
**Messages Published (MessageBus)**
266266

267267
- `GpsLocationUpdated` – latitude and longitude on each new fix.
268268
- `GpsAltitudeUpdated` – altitude on each new fix.

SmartFuseBox/AckCommandHandler.cpp

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,12 @@
1919
#include "SystemFunctions.h"
2020
#include "ConfigController.h"
2121

22-
#if defined(NEXTION_DISPLAY_DEVICE)
23-
#include <NextionControl.h>
24-
#include "BasePage.h"
25-
#endif
26-
2722
const char AckCommand[] = "ACK";
2823

2924
AckCommandHandler::AckCommandHandler(BroadcastManager* broadcastManager,
30-
#if defined(NEXTION_DISPLAY_DEVICE)
31-
NextionControl* nextionControl,
32-
#endif
25+
MessageBus* messageBus,
3326
WarningManager* warningManager)
34-
: BaseNextionCommandHandler(broadcastManager,
35-
#if defined(NEXTION_DISPLAY_DEVICE)
36-
nextionControl,
37-
#endif
38-
warningManager)
27+
: BaseNextionCommandHandler(broadcastManager, messageBus, warningManager)
3928
{
4029

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

10291
#if defined(NEXTION_DISPLAY_DEVICE)
103-
if (strcmp(params[0].key, RelayRetrieveStates) == 0 && strcmp(params[0].value, AckSuccess) == 0)
104-
{
105-
// Relay state acknowledgement - handle both formats:
106-
// 1. ACK:R2=ok (just acknowledgement, no relay state - paramCount == 1)
107-
// 2. ACK:R2=ok:0=0 (acknowledgement with relay state - paramCount == 2)
108-
109-
if (paramCount >= 2)
110-
{
111-
// Format: ACK:R2=ok:0=0 (with relay index and state)
112-
if (!SystemFunctions::isAllDigits(params[1].key) || !SystemFunctions::isAllDigits(params[1].value))
113-
{
114-
sendDebugMessage(F("Invalid R2 Ack response"), F("AckCommandHandler"));
115-
return true;
116-
}
92+
if (strcmp(params[0].key, RelayRetrieveStates) == 0 && strcmp(params[0].value, AckSuccess) == 0)
93+
{
94+
if (paramCount >= 2)
95+
{
96+
if (!SystemFunctions::isAllDigits(params[1].key) || !SystemFunctions::isAllDigits(params[1].value))
97+
{
98+
sendDebugMessage(F("Invalid R2 Ack response"), F("AckCommandHandler"));
99+
return true;
100+
}
117101

118102
uint8_t relayIndex = static_cast<uint8_t>(strtoul(params[1].key, nullptr, 0));
119-
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);
120-
121-
RelayStateUpdate update = { relayIndex, isOn };
122-
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::RelayState), &update);
123-
}
124-
}
125-
else if (strcmp(params[0].key, RelayStatusGet) == 0 && strcmp(params[0].value, AckSuccess) == 0)
126-
{
127-
if (paramCount >= 2)
128-
{
103+
if (relayIndex < ConfigRelayCount)
104+
{
105+
uint8_t changedMask = 1 << relayIndex;
106+
107+
if (_messageBus)
108+
_messageBus->publish<RelayStatusChanged>(changedMask);
109+
}
110+
else
111+
{
112+
_broadcaster->sendDebug("ACK relay index out of range", AckCommand);
113+
}
114+
}
115+
}
116+
else if (strcmp(params[0].key, RelayStatusGet) == 0 && strcmp(params[0].value, AckSuccess) == 0)
117+
{
118+
if (paramCount >= 2)
119+
{
129120
uint8_t relayIndex = static_cast<uint8_t>(strtoul(params[1].key, nullptr, 0));
130-
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);
131-
132-
RelayStateUpdate update = { relayIndex, isOn };
133-
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::RelayState), &update);
134-
}
135-
else
136-
{
121+
if (relayIndex < ConfigRelayCount)
122+
{
123+
uint8_t changedMask = 1 << relayIndex;
124+
125+
if (_messageBus)
126+
_messageBus->publish<RelayStatusChanged>(changedMask);
127+
}
128+
else
129+
{
130+
_broadcaster->sendDebug("ACK relay index out of range", AckCommand);
131+
}
132+
}
133+
else
134+
{
137135
sendDebugMessage(F("Invalid R4 ACK format RelayStatusGet"), AckCommand);
138-
}
139-
}
140-
else if (strcmp(params[0].key, SoundSignalActive) == 0 && strcmp(params[0].value, AckSuccess) == 0)
141-
{
142-
if (paramCount >= 2)
143-
{
144-
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);
145-
146-
BoolStateUpdate update = { isOn };
147-
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::SoundSignal), &update);
148-
}
149-
else
150-
{
136+
}
137+
}
138+
else if (strcmp(params[0].key, SoundSignalActive) == 0 && strcmp(params[0].value, AckSuccess) == 0)
139+
{
140+
if (paramCount >= 2)
141+
{
142+
bool isOn = SystemFunctions::parseBooleanValue(params[1].value);
143+
if (_messageBus)
144+
_messageBus->publish<SoundSignalUpdated>(isOn);
145+
}
146+
else
147+
{
151148
sendDebugMessage(F("Invalid R4 ACK format Sound Signal Active"), AckCommand);
152149
}
153150
}
154151
else if (strcmp(params[0].key, SystemCpuUsage) == 0 && strcmp(params[0].value, AckSuccess) == 0)
155-
{
156-
if (paramCount >= 2)
157-
{
158-
uint8_t cpuUsage = static_cast<uint8_t>(strtoul(params[1].value, nullptr, 0));
159-
UInt8Update update = { cpuUsage };
160-
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::CpuUsage), &update);
161-
}
162-
else
163-
{
164-
sendDebugMessage(F("Invalid F3 ACK format: cpu"), AckCommand);
165-
}
166-
}
167-
else if (strcmp(params[0].key, SystemFreeMemory) == 0 && strcmp(params[0].value, AckSuccess) == 0)
168-
{
169-
if (paramCount >= 2)
170-
{
171-
uint16_t freeMemory = static_cast<uint16_t>(strtoul(params[1].value, nullptr, 0));
172-
UInt16Update update = { freeMemory };
173-
notifyCurrentPage(static_cast<uint8_t>(PageUpdateType::MemoryUsage), &update);
174-
}
175-
else
176-
{
152+
{
153+
if (paramCount >= 2)
154+
{
155+
uint8_t cpuUsage = static_cast<uint8_t>(strtoul(params[1].value, nullptr, 0));
156+
if (_messageBus)
157+
_messageBus->publish<CpuUsageUpdated>(cpuUsage);
158+
}
159+
else
160+
{
161+
sendDebugMessage(F("Invalid F3 ACK format: cpu"), AckCommand);
162+
}
163+
}
164+
else if (strcmp(params[0].key, SystemFreeMemory) == 0 && strcmp(params[0].value, AckSuccess) == 0)
165+
{
166+
if (paramCount >= 2)
167+
{
168+
uint16_t freeMemory = static_cast<uint16_t>(strtoul(params[1].value, nullptr, 0));
169+
if (_messageBus)
170+
_messageBus->publish<MemoryUsageUpdated>(freeMemory);
171+
}
172+
else
173+
{
177174
sendDebugMessage(F("Invalid F2 ACK format free memory"), AckCommand);
178175
}
179176
}

SmartFuseBox/AckCommandHandler.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
#include "ConfigManager.h"
2424
#include "BaseNextionCommandHandler.h"
2525

26-
#if defined(NEXTION_DISPLAY_DEVICE)
27-
#include <NextionControl.h>
28-
#endif
29-
3026
// Forward declarations
3127
class ConfigController;
3228

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

4137
public:
4238
explicit AckCommandHandler(BroadcastManager* broadcastManager,
43-
#if defined(NEXTION_DISPLAY_DEVICE)
44-
NextionControl* nextionControl,
45-
#endif
39+
MessageBus* messageBus,
4640
WarningManager* warningManager);
4741

4842
// Set the config sync manager (optional - needed for config sync feature)

SmartFuseBox/BaseNextionCommandHandler.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@
2020

2121
BaseNextionCommandHandler::BaseNextionCommandHandler(
2222
BroadcastManager* broadcaster,
23-
#if defined(NEXTION_DISPLAY_DEVICE)
24-
NextionControl* nextionControl,
25-
#endif
23+
MessageBus* messageBus,
2624
WarningManager* warningManager)
27-
: SharedBaseCommandHandler(broadcaster, warningManager)
28-
#if defined(NEXTION_DISPLAY_DEVICE)
29-
, _nextionControl(nextionControl)
30-
#endif
25+
: SharedBaseCommandHandler(broadcaster, warningManager),
26+
_messageBus(messageBus)
3127
{
3228
}

SmartFuseBox/BaseNextionCommandHandler.h

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020
#include <Arduino.h>
2121

22-
#if defined(NEXTION_DISPLAY_DEVICE)
23-
#include <NextionControl.h>
24-
#endif
25-
2622
#include "SharedBaseCommandHandler.h"
2723
#include "WarningManager.h"
24+
#include "MessageBus.h"
2825

2926
/**
3027
* @brief Base class for command handlers that interact with boat-specific systems.
@@ -56,39 +53,10 @@ class BaseNextionCommandHandler : public SharedBaseCommandHandler
5653
*/
5754
BaseNextionCommandHandler(
5855
BroadcastManager* broadcaster,
59-
#if defined(NEXTION_DISPLAY_DEVICE)
60-
NextionControl* nextionControl,
61-
#endif
56+
MessageBus* messageBus,
6257
WarningManager* warningManager = nullptr
6358
);
6459

65-
/**
66-
* @brief Notify the current display page of an external update.
67-
*
68-
* This is a convenience wrapper that safely gets the current page from
69-
* NextionControl and calls handleExternalUpdate on it.
70-
*
71-
* @param updateType Type of update (cast to uint8_t from PageUpdateType enum)
72-
* @param data Optional pointer to update-specific data structure
73-
*/
74-
void notifyCurrentPage(uint8_t updateType, const void* data)
75-
{
76-
#if defined(NEXTION_DISPLAY_DEVICE)
77-
if (!_nextionControl)
78-
return;
79-
80-
BaseDisplayPage* p = _nextionControl->getCurrentPage();
81-
82-
if (!p)
83-
return;
84-
85-
p->handleExternalUpdate(updateType, data);
86-
#endif
87-
}
88-
89-
#if defined(NEXTION_DISPLAY_DEVICE)
90-
// Protected member variables for derived classes to access
91-
// Note: _broadcaster is inherited from SharedBaseCommandHandler
92-
NextionControl* _nextionControl;
93-
#endif
60+
protected:
61+
MessageBus* _messageBus;
9462
};

SmartFuseBox/BasePage.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
BasePage::BasePage(Stream* serialPort,
2626
WarningManager* warningMgr,
27-
SerialCommandManager* commandMgrComputer)
27+
SerialCommandManager* commandMgrComputer,
28+
MessageBus* messageBus)
2829
: BaseDisplayPage(serialPort),
2930
_config(nullptr),
3031
_commandMgrComputer(commandMgrComputer),
31-
_warningManager(warningMgr)
32+
_warningManager(warningMgr),
33+
_messageBus(messageBus)
3234
{
3335
}
3436

0 commit comments

Comments
 (0)