Skip to content

Commit

Permalink
Fixes to get rid of mqtt log topic
Browse files Browse the repository at this point in the history
  • Loading branch information
cziter15 committed Oct 10, 2023
1 parent 1267659 commit 5c9c927
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
34 changes: 19 additions & 15 deletions firmware/src/apps/raesp/comps/RadioCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace std::placeholders;
namespace apps::raesp::comps
{
RadioCommander::RadioCommander(uint8_t ssPin, uint8_t dio0pin, uint8_t rstPin, uint8_t dio2pin, ksLedWP& wifiLedWp, ksLedWP& radioLedWp)
: radioLedWp(radioLedWp), wifiLedWp(wifiLedWp)
: radioLedWp(radioLedWp), wifiLedWp(wifiLedWp), rfTopicPrefix(PSTR("rfswitch/"))
{
/* Instantiate radio PHY/Module. */
radioPhy = std::make_unique<Module>(ssPin, dio0pin, rstPin, dio2pin);
Expand All @@ -35,6 +35,7 @@ namespace apps::raesp::comps

bool RadioCommander::postInit(ksf::ksApplication* app)
{
this->app = app;
mqttConnWp = app->findComponent<ksf::comps::ksMqttConnector>();

if (auto mqttConnSp{mqttConnWp.lock()})
Expand All @@ -52,12 +53,6 @@ namespace apps::raesp::comps
mqttConnSp->subscribe(rfTopicPrefix + '#');
}

void RadioCommander::sendMqttInfo(const std::string& info) const
{
if (auto mqttConnSp{mqttConnWp.lock()})
mqttConnSp->publish(PSTR("log"), info);
}

void RadioCommander::onMqttDevMessage(const std::string_view& topic, const std::string_view& payload)
{
if (topic.find(rfTopicPrefix) == std::string_view::npos)
Expand All @@ -75,8 +70,12 @@ namespace apps::raesp::comps
if (!wifiLedSp->isBlinking())
wifiLedSp->setBlinking(100, 5);

/* ... and tell MQTT via log channel about that. */
sendMqttInfo(PSTR("RadioCmd: Queue is full - discarding!"));
/* ... and send log about that. */
#if APP_LOG_ENABLED
app->log([](std::string& out){
out += PSTR("RadioCmd: Queue is full - discarding!");
});
#endif
return;
}

Expand Down Expand Up @@ -173,12 +172,17 @@ namespace apps::raesp::comps
/* Check if it's last repeat. */
if (currentCommand.repeats <= 0)
{
sendMqttInfo(
PSTR("RadioCmd: Sent! [ A: ") + ksf::to_string(currentCommand.address) +
PSTR(" | U: ") + ksf::to_string(currentCommand.unit) +
PSTR(" | V: ") + ksf::to_string(currentCommand.enable) + PSTR(" ]")
);

#if APP_LOG_ENABLED
app->log([&](std::string& out){
out += PSTR("RadioCmd: Sent! [ A: ");
out += ksf::to_string(currentCommand.address);
out += PSTR(" | U: ");
out += ksf::to_string(currentCommand.unit);
out += PSTR(" | V: ");
out += ksf::to_string(currentCommand.enable);
out += PSTR(" ]");
});
#endif
/* Pop current request (remove) from queue, coz we are done with it. */
commandQueue.pop();

Expand Down
10 changes: 2 additions & 8 deletions firmware/src/apps/raesp/comps/RadioCommander.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace apps::raesp::comps
using ksLedWP = std::weak_ptr<ksf::comps::ksLed>;

protected:
ksf::ksApplication* app{nullptr}; // Application ptr.

static constexpr uint16_t MAX_TX_QUEUE_SIZE{20}; // Maximum queue size for RC requests.
static constexpr int16_t RC_UNIT_NONE{-1}; // Value indicating no unit presence.
Expand All @@ -54,7 +55,7 @@ namespace apps::raesp::comps
std::weak_ptr<ksf::comps::ksMqttConnector> mqttConnWp; // Weak pointer to mqtt connector.
std::weak_ptr<ksf::comps::ksLed> radioLedWp, wifiLedWp; // Weak pointers to LEDs.

const std::string rfTopicPrefix{"rfswitch/"}; // RF command topic prefix.
const std::string rfTopicPrefix; // RF command topic prefix.
std::unique_ptr<ksf::evt::ksEventHandle> connEventHandleSp, msgEventHandleSp; // Shared ptrs to events.

double cachedFrequency{0.0}; // Cached frequency value.
Expand All @@ -72,13 +73,6 @@ namespace apps::raesp::comps
*/
void onMqttConnected();

/*
Wrapper function for publishin device log to specific MQTT topic.
@param info Message to publish.
*/
void sendMqttInfo(const std::string& info) const;

/*
This funciton will handle one repeat of RadioCommand and will block to drive data pin.
Expand Down

0 comments on commit 5c9c927

Please sign in to comment.