ENABLE_MQTT c++ macro definition (#1546)

* macro

* 2

* 2

* delete jomjol_mqtt from CMakeLists

* mqtt macro

* final
This commit is contained in:
Nicolas Liaudat
2022-12-11 16:04:56 +01:00
committed by GitHub
parent 3f85f9b755
commit 286915b647
15 changed files with 115 additions and 38 deletions

View File

@@ -25,7 +25,9 @@
#include "ClassLogFile.h"
#include "configFile.h"
#include "Helper.h"
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#endif //ENABLE_MQTT
static const char *TAG = "GPIO";
QueueHandle_t gpio_queue_handle = NULL;
@@ -83,12 +85,14 @@ static void gpioHandlerTask(void *arg) {
}
void GpioPin::gpioInterrupt(int value) {
#ifdef ENABLE_MQTT
if (_mqttTopic != "") {
ESP_LOGD(TAG, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
MQTTPublish(_mqttTopic, value ? "true" : "false");
currentState = value;
}
#endif //ENABLE_MQTT
currentState = value;
}
void GpioPin::init()
@@ -114,10 +118,12 @@ void GpioPin::init()
gpio_isr_handler_add(_gpio, gpio_isr_handler, (void*)&_gpio);
}
#ifdef ENABLE_MQTT
if ((_mqttTopic != "") && ((_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_OUTPUT_PWM) || (_mode == GPIO_PIN_MODE_BUILT_IN_FLASH_LED))) {
std::function<bool(std::string, char*, int)> f = std::bind(&GpioPin::handleMQTT, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
MQTTregisterSubscribeFunction(_mqttTopic, f);
}
#endif //ENABLE_MQTT
}
bool GpioPin::getValue(std::string* errorText)
@@ -138,9 +144,11 @@ void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* error
} else {
gpio_set_level(_gpio, value);
#ifdef ENABLE_MQTT
if ((_mqttTopic != "") && (setSource != GPIO_SET_SOURCE_MQTT)) {
MQTTPublish(_mqttTopic, value ? "true" : "false");
}
#endif //ENABLE_MQTT
}
}
@@ -148,11 +156,14 @@ void GpioPin::publishState() {
int newState = gpio_get_level(_gpio);
if (newState != currentState) {
ESP_LOGD(TAG,"publish state of GPIO %d new state %d", _gpio, newState);
#ifdef ENABLE_MQTT
MQTTPublish(_mqttTopic, newState ? "true" : "false");
#endif //ENABLE_MQTT
currentState = newState;
}
}
#ifdef ENABLE_MQTT
bool GpioPin::handleMQTT(std::string, char* data, int data_len) {
ESP_LOGD(TAG, "GpioPin::handleMQTT data %.*s", data_len, data);
@@ -174,7 +185,7 @@ bool GpioPin::handleMQTT(std::string, char* data, int data_len) {
return (errorText == "");
}
#endif //ENABLE_MQTT
esp_err_t callHandleHttpRequest(httpd_req_t *req)
{
@@ -236,8 +247,10 @@ void GpioHandler::init()
it->second->init();
}
#ifdef ENABLE_MQTT
std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
MQTTregisterConnectFunction("gpio-handler", f);
#endif //ENABLE_MQTT
if (xHandleTaskGpio == NULL) {
gpio_queue_handle = xQueueCreate(10,sizeof(GpioResult));
@@ -261,7 +274,7 @@ void GpioHandler::taskHandler() {
}
}
#ifdef ENABLE_MQTT
void GpioHandler::handleMQTTconnect()
{
if (gpioMap != NULL) {
@@ -271,9 +284,12 @@ void GpioHandler::handleMQTTconnect()
}
}
}
#endif //ENABLE_MQTT
void GpioHandler::deinit() {
#ifdef ENABLE_MQTT
MQTTunregisterConnectFunction("gpio-handler");
#endif //ENABLE_MQTT
clear();
if (xHandleTaskGpio != NULL) {
vTaskDelete(xHandleTaskGpio);
@@ -316,6 +332,7 @@ bool GpioHandler::readConfig()
// ESP_LOGD(TAG, "readConfig - Start 3");
#ifdef ENABLE_MQTT
// std::string mainTopicMQTT = "";
std::string mainTopicMQTT = GetMQTTMainTopic();
if (mainTopicMQTT.length() > 0)
@@ -323,7 +340,7 @@ bool GpioHandler::readConfig()
mainTopicMQTT = mainTopicMQTT + "/GPIO";
ESP_LOGD(TAG, "MAINTOPICMQTT found");
}
#endif // ENABLE_MQTT
bool registerISR = false;
while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
{
@@ -345,7 +362,9 @@ bool GpioHandler::readConfig()
gpio_pin_mode_t pinMode = resolvePinMode(toLower(zerlegt[1]));
gpio_int_type_t intType = resolveIntType(toLower(zerlegt[2]));
uint16_t dutyResolution = (uint8_t)atoi(zerlegt[3].c_str());
#ifdef ENABLE_MQTT
bool mqttEnabled = toLower(zerlegt[4]) == "true";
#endif // ENABLE_MQTT
bool httpEnabled = toLower(zerlegt[5]) == "true";
char gpioName[100];
if (zerlegt.size() >= 7) {
@@ -353,7 +372,11 @@ bool GpioHandler::readConfig()
} else {
sprintf(gpioName, "GPIO%d", gpioNr);
}
#ifdef ENABLE_MQTT
std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
#else // ENABLE_MQTT
std::string mqttTopic = "";
#endif // ENABLE_MQTT
GpioPin* gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType,dutyResolution, mqttTopic, httpEnabled);
(*gpioMap)[gpioNr] = gpioPin;

View File

@@ -45,7 +45,9 @@ public:
void init();
bool getValue(std::string* errorText);
void setValue(bool value, gpio_set_source setSource, std::string* errorText);
#ifdef ENABLE_MQTT
bool handleMQTT(std::string, char* data, int data_len);
#endif //ENABLE_MQTT
void publishState();
void gpioInterrupt(int value);
gpio_int_type_t getInterruptType() { return _interruptType; }
@@ -77,7 +79,9 @@ public:
void gpioInterrupt(GpioResult* gpioResult);
void flashLightEnable(bool value);
bool isEnabled() { return _isEnabled; }
#ifdef ENABLE_MQTT
void handleMQTTconnect();
#endif //ENABLE_MQTT
private:
std::string _configFile;

View File

@@ -39,7 +39,9 @@ extern "C" {
#include "server_tflite.h"
#include "server_help.h"
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#endif //ENABLE_MQTT
#include "server_GPIO.h"
#include "Helper.h"

View File

@@ -19,9 +19,10 @@ extern "C" {
#include "time_sntp.h"
#include "Helper.h"
#include "server_ota.h"
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#include "server_mqtt.h"
#endif //ENABLE_MQTT
//#include "CImg.h"
@@ -50,9 +51,11 @@ std::string ClassFlowControll::doSingleStep(std::string _stepname, std::string _
if ((_stepname.compare("[Analog]") == 0) || (_stepname.compare(";[Analog]") == 0)){
_classname = "ClassFlowCNNGeneral";
}
#ifdef ENABLE_MQTT
if ((_stepname.compare("[MQTT]") == 0) || (_stepname.compare(";[MQTT]") == 0)){
_classname = "ClassFlowMQTT";
}
#endif //ENABLE_MQTT
if ((_stepname.compare("[InfluxDB]") == 0) || (_stepname.compare(";[InfluxDB]") == 0)){
_classname = "ClassFlowInfluxDB";
}
@@ -78,8 +81,10 @@ std::string ClassFlowControll::TranslateAktstatus(std::string _input)
return ("Aligning");
if (_input.compare("ClassFlowCNNGeneral") == 0)
return ("Digitalization of ROIs");
#ifdef ENABLE_MQTT
if (_input.compare("ClassFlowMQTT") == 0)
return ("Sending MQTT");
#endif //ENABLE_MQTT
if (_input.compare("ClassFlowInfluxDB") == 0)
return ("Sending InfluxDB");
if (_input.compare("ClassFlowPostProcessing") == 0)
@@ -130,7 +135,7 @@ t_CNNType ClassFlowControll::GetTypeAnalog()
#ifdef ENABLE_MQTT
string ClassFlowControll::GetMQTTMainTopic()
{
for (int i = 0; i < FlowControll.size(); ++i)
@@ -149,7 +154,7 @@ bool ClassFlowControll::StartMQTTService() {
}
return false;
}
#endif //ENABLE_MQTT
void ClassFlowControll::SetInitialParameter(void)
{
@@ -196,8 +201,10 @@ ClassFlow* ClassFlowControll::CreateClassFlow(std::string _type)
cfc = new ClassFlowCNNGeneral(flowalignment);
flowdigit = (ClassFlowCNNGeneral*) cfc;
}
#ifdef ENABLE_MQTT
if (toUpper(_type).compare("[MQTT]") == 0)
cfc = new ClassFlowMQTT(&FlowControll);
#endif //ENABLE_MQTT
if (toUpper(_type).compare("[INFLUXDB]") == 0)
cfc = new ClassFlowInfluxDB(&FlowControll);
@@ -287,8 +294,9 @@ void ClassFlowControll::doFlowMakeImageOnly(string time){
zw_time = gettimestring("%H:%M:%S");
std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
aktstatus = flowStatus + " (" + zw_time + ")";
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, flowStatus);
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
#endif //ENABLE_MQTT
FlowControll[i]->doFlow(time);
}
@@ -318,8 +326,9 @@ bool ClassFlowControll::doFlow(string time)
zw_time = gettimestring("%H:%M:%S");
std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
aktstatus = flowStatus + " (" + zw_time + ")";
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, flowStatus);
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
#endif //ENABLE_MQTT
string zw = "FlowControll.doFlow - " + FlowControll[i]->name();
#ifdef DEBUG_DETAIL_ON
@@ -350,8 +359,9 @@ bool ClassFlowControll::doFlow(string time)
zw_time = gettimestring("%H:%M:%S");
std::string flowStatus = "Flow finished";
aktstatus = flowStatus + " (" + zw_time + ")";
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, flowStatus);
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
#endif //ENABLE_MQTT
return result;
}

View File

@@ -1,3 +1,5 @@
#ifndef __FLOWCONTROLL__
#define __FLOWCONTROLL__
@@ -8,7 +10,9 @@
#include "ClassFlowAlignment.h"
#include "ClassFlowCNNGeneral.h"
#include "ClassFlowPostProcessing.h"
#ifdef ENABLE_MQTT
#include "ClassFlowMQTT.h"
#endif //ENABLE_MQTT
#include "ClassFlowInfluxDB.h"
#include "ClassFlowCNNGeneral.h"
#include "ClassFlowWriteList.h"
@@ -54,8 +58,9 @@ public:
string getNumbersName();
string TranslateAktstatus(std::string _input);
#ifdef ENABLE_MQTT
string GetMQTTMainTopic();
#endif //ENABLE_MQTT
esp_err_t GetJPGStream(std::string _fn, httpd_req_t *req);
esp_err_t SendRawJPG(httpd_req_t *req);
@@ -71,7 +76,9 @@ public:
t_CNNType GetTypeDigital();
t_CNNType GetTypeAnalog();
#ifdef ENABLE_MQTT
bool StartMQTTService();
#endif //ENABLE_MQTT
int CleanTempFolder();
@@ -81,3 +88,4 @@ public:
#endif

View File

@@ -1,3 +1,5 @@
#ifdef ENABLE_MQTT
#include <sstream>
#include <iomanip>
#include "ClassFlowMQTT.h"
@@ -314,3 +316,6 @@ bool ClassFlowMQTT::doFlow(string zwtime)
return true;
}
#endif //ENABLE_MQTT

View File

@@ -1,3 +1,4 @@
#ifdef ENABLE_MQTT
#pragma once
#include "ClassFlow.h"
@@ -33,3 +34,4 @@ public:
string name(){return "ClassFlowMQTT";};
};
#endif //ENABLE_MQTT

View File

@@ -1,3 +1,4 @@
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
//#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
@@ -346,3 +347,4 @@ void MQTTdestroySubscribeFunction(){
subscribeFunktionMap = NULL;
}
}
#endif //ENABLE_MQTT

View File

@@ -1,3 +1,4 @@
#ifdef ENABLE_MQTT
#ifndef INTERFACE_MQTT_H
#define INTERFACE_MQTT_H
@@ -22,5 +23,5 @@ void MQTTdestroySubscribeFunction();
void MQTTconnected();
void MQTTdisable();
#endif //INTERFACE_MQTT_H
#endif //#ENABLE_MQTT

View File

@@ -1,3 +1,4 @@
#ifdef ENABLE_MQTT
#include <string>
#include <sstream>
#include <iomanip>
@@ -243,3 +244,5 @@ void mqttServer_setMainTopic( std::string _maintopic) {
std::string mqttServer_getMainTopic() {
return maintopic;
}
#endif //ENABLE_MQTT

View File

@@ -1,3 +1,5 @@
#ifdef ENABLE_MQTT
#include "ClassFlowDefineTypes.h"
#define LWT_TOPIC "connection"
@@ -18,3 +20,5 @@ void publishSystemData();
std::string getTimeUnit(void);
void GotConnected(std::string maintopic, int SetRetainFlag);
#endif //ENABLE_MQTT

View File

@@ -104,7 +104,9 @@ void doInit(void)
ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
#endif
#ifdef ENABLE_MQTT
tfliteflow.StartMQTTService();
#endif //ENABLE_MQTT
}
@@ -803,11 +805,12 @@ void TFliteDoAutoStart()
}
#ifdef ENABLE_MQTT
std::string GetMQTTMainTopic()
{
return tfliteflow.GetMQTTMainTopic();
}
#endif//ENABLE_MQTT
void register_server_tflite_uri(httpd_handle_t server)

View File

@@ -15,7 +15,9 @@ void TFliteDoAutoStart();
bool isSetupModusActive();
#ifdef ENABLE_MQTT
std::string GetMQTTMainTopic();
#endif //ENABLE_MQTT
int getCountFlowRounds();

View File

@@ -27,7 +27,9 @@
#include "ClassControllCamera.h"
#include "server_main.h"
#include "server_camera.h"
#ifdef ENABLE_MQTT
#include "server_mqtt.h"
#endif //ENABLE_MQTT
#include "Helper.h"
extern const char* GIT_TAG;
@@ -280,7 +282,9 @@ extern "C" void app_main(void)
register_server_tflite_uri(server);
register_server_file_uri(server, "/sdcard");
register_server_ota_sdcard_uri(server);
#ifdef ENABLE_MQTT
register_server_mqtt_uri(server);
#endif //ENABLE_MQTT
gpio_handler_create(server);

View File

@@ -18,24 +18,28 @@ board = esp32cam
;board = m5stack-core-esp32
framework = espidf
;Add macro definition ENABLE_MQTT
build_flags = -D ENABLE_MQTT
;board_build.partitions = partitions_singleapp.csv
board_build.partitions = partitions.csv
lib_deps =
jomjol_configfile
jomjol_helper
jomjol_wlan
jomjol_image_proc
jomjol_controlcamera
jomjol_flowcontroll
jomjol_tfliteclass
tflite-lib
jomjol_fileserver_ota
jomjol_time_sntp
jomjol_logfile
jomjol_mqtt
jomjol_influxdb
jomjol_controlGPIO
;lib_deps not needed
;lib_deps =
; jomjol_configfile
; jomjol_helper
; jomjol_wlan
; jomjol_image_proc
; jomjol_controlcamera
; jomjol_flowcontroll
; jomjol_tfliteclass
; tflite-lib
; jomjol_fileserver_ota
; jomjol_time_sntp
; jomjol_logfile
; jomjol_mqtt
; jomjol_influxdb
; jomjol_controlGPIO
monitor_speed = 115200