Skip to content

Commit

Permalink
optimize mqttclient
Browse files Browse the repository at this point in the history
  • Loading branch information
passy1977 committed Aug 12, 2021
1 parent 94b7679 commit 9a759b4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 48 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
cmake_minimum_required(VERSION 3.16)

PROJECT(hgardenpi VERSION "0.40.0" LANGUAGES CXX C)
PROJECT(hgardenpi VERSION "0.41.0" LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic")
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
#set(RASPBIAN_ROOTFS "$ENV{HOME}/raspberrypi/rootfs")
set(FILE_LOCK_PATH "/tmp/hgardenpi.pid")
set(FILE_CONFIG "./config.json")
set(BROKER_HOST "localhost")
set(BROKER_PORT 1883)
set(BROKER_USER "hgardepi")
set(BROKER_PASSWD "hgardepi")
set(BROKER_USER "hgardenpi")
set(BROKER_PASSWD "hgardenpi")
set(DB_FILE "./hgardenpi.db")

#date param
set(BUILD_TZ_LIB ON)


Expand Down Expand Up @@ -44,7 +45,6 @@ FetchContent_MakeAvailable(date_src)


configure_file(config.h.in ../src/config.h)
#include_directories("3thparty/include/date")
include_directories("${RASPBIAN_ROOTFS}/usr/local/include")
include_directories("${RASPBIAN_ROOTFS}/usr/include")
link_directories("${RASPBIAN_ROOTFS}/usr/local/lib")
Expand Down
48 changes: 17 additions & 31 deletions src/clients/mqttclientmosquitto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <sstream>

#include "../threadengine.hpp"
#include "../config.h"

//todo: place here in global
static hgardenpi::v1::MQTTClient::MessageCallback onMessageCallback;
Expand All @@ -37,8 +38,10 @@ namespace hgardenpi
inline namespace v1
{

static LogService *logService = nullptr;

MQTTClientMosquitto::MQTTClientMosquitto(const string &serial, const string &host, const string &user, const string &passwd, uint16_t port, uint16_t keepAlive)
: topic("/HappyGardenPI/" + serial),
: topic(move("/HappyGardenPI/" + serial)),
host(host),
port(port),
keepAlive(keepAlive),
Expand All @@ -49,6 +52,8 @@ namespace hgardenpi
* new instance client
*/
ostringstream ss;
ss << HGARDENPI_NAME;
ss << "_";
ss << pidMain;

mosq = mosquitto_new(ss.str().c_str(), true, nullptr);
Expand All @@ -74,7 +79,7 @@ namespace hgardenpi
{
if (!initalizated)
{
throw runtime_error(_("MQTTClientMosquitto not initialized"));
HGARDENPI_ERROR_LOG_AMD_THROW("MQTTClientMosquitto not initialized")
}

//check il moquitto client is allocated
Expand All @@ -88,9 +93,10 @@ namespace hgardenpi
{
if (result != MOSQ_ERR_SUCCESS)
{
string err("connection error: ");
string err("mosquitto client connection error: ");
err.append(mosquitto_strerror(result));
cerr << err << endl;
HGARDENPI_ERROR_LOG_AMD_THROW(err.c_str())
}
});

Expand All @@ -100,17 +106,7 @@ namespace hgardenpi
*/
mosquitto_message_callback_set(mosq, [](mosquitto *mosq, void *obj, const mosquitto_message *message)
{
// bool match = 0;
// printf("received message '%.*s' for topic '%s'\n", message->payloadlen, (char *)message->payload, message->topic);

::onMessageCallback((uint8_t *)message->payload);

//todo
// mosquitto_topic_matches_sub(, message->topic, &match);
// if (match)
// {
// printf("received message for Telemetry topic\n");
// }
});

//set username and passwd
Expand All @@ -120,8 +116,7 @@ namespace hgardenpi
{
string err("set user and password: ");
err.append(mosquitto_strerror(rc));
logService->write(LOG_ERR, "%s", err.c_str());
throw runtime_error(err);
HGARDENPI_ERROR_LOG_AMD_THROW(err.c_str())
}

//set protocol version
Expand All @@ -134,8 +129,7 @@ namespace hgardenpi
{
string err("connection: ");
err.append(mosquitto_strerror(rc));
logService->write(LOG_ERR, "%s", err.c_str());
throw runtime_error(err);
HGARDENPI_ERROR_LOG_AMD_THROW(err.c_str())
}

logService->write(LOG_INFO, "broker %s: %s", "topic", topic.c_str());
Expand All @@ -150,28 +144,20 @@ namespace hgardenpi
{
threadSleep(Time::TICK);
}
}

// int rc = mosquitto_loop_forever(mosq, static_cast<int>(Time::TICK), 1);
// if (!wiringPiRunningThread && rc)
// {
// logService->write(LOG_WARNING, "%s", "connection error! Try to reconnect");
// threadSleep(5'000);
// rc = mosquitto_reconnect(mosq);
// if (rc != MOSQ_ERR_SUCCESS)
// {
// HGARDENPI_ERROR_LOG_AMD_THROW("reconnection fail")
// }
// }
inline void MQTTClientMosquitto::setLogService(const LogService *logService) noexcept
{
hgardenpi::v1::logService = const_cast<LogService *>(logService);
}

void MQTTClientMosquitto::initialize()
{
//initialize mosquittopp
if (mosquitto_lib_init() != MOSQ_ERR_SUCCESS)
{
string msg("mosquitto_lib_init() error");
logService->write(LOG_ERR, "%d", msg.c_str());
throw runtime_error(_(msg.c_str()));
HGARDENPI_ERROR_LOG_AMD_THROW("mosquitto_lib_init() error")
initalizated = true;
}
initalizated = true;
}
Expand Down
6 changes: 1 addition & 5 deletions src/clients/mqttclientmosquitto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace hgardenpi
const string &user;
const string &passwd;

LogService *logService = nullptr;
mosquitto *mosq = nullptr;

string id;
Expand Down Expand Up @@ -107,10 +106,7 @@ namespace hgardenpi
* @brief Set log
* @param logService instance
*/
inline void setLogService(const LogService *logService) noexcept override
{
this->logService = const_cast<LogService *>(logService);
}
void setLogService(const LogService *logService) noexcept override;

/**
* @brief Initialize lib
Expand Down
10 changes: 4 additions & 6 deletions src/components/buttonconcrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
#include "buttonconcrete.hpp"

#include <wiringPi.h>
#include <unistd.h>
#include <sys/syscall.h>


#include "../threadengine.hpp"
using namespace std;


Expand All @@ -46,8 +42,8 @@ static ButtonConcrete::OnClick internalCallback;
ButtonConcrete::ButtonConcrete(int lcdRS) noexcept
{
pinMode(lcdRS, INPUT);
::callback = [](){};
::internalCallback = [](){};
::callback = []{};
::internalCallback = []{};
::lcdRS = lcdRS;
}

Expand All @@ -60,11 +56,13 @@ void ButtonConcrete::setInternalOnClick(OnClick onClick) const
{
::internalCallback = move(onClick);

//set timeout for interrupt
if (waitForInterrupt(::lcdRS, static_cast<int>(Time::TICK)) == -1)
{
throw runtime_error("waitForInterrupt not run, the hw is not ready");
}

//set callback on click button
if (wiringPiISR(::lcdRS, INT_EDGE_RISING, []
{
::callback();
Expand Down
3 changes: 2 additions & 1 deletion src/services/schedulerconcrete.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#include <mutex>
#include <functional>

#include "scheduler.hpp"
#include "../threadengine.hpp"
#include "../constants.hpp"
#include "scheduler.hpp"


namespace hgardenpi
{
Expand Down

0 comments on commit 9a759b4

Please sign in to comment.