Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 committed Nov 1, 2024
1 parent b34534d commit 508db52
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
29 changes: 12 additions & 17 deletions code/components/mqtt_ctrl/interface_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ static std::string TLSClientKey;
static int keepAlive;

static struct strMqttState {
bool mqttConfigured = false;
bool mqttStartEnabled = false;
bool mqttEnabled = false;
bool mqttInitialized = false;
bool mqttConnected = false;

Expand All @@ -49,11 +48,11 @@ static std::map<std::string, std::function<bool(std::string, char*, int)>>* subs

bool publishMqttData(std::string _key, std::string _content, int _qos, bool _retainFlag)
{
if (!mqttState.mqttStartEnabled) { // MQTT client preconfigured, but not initialized / started (startMqttClient not called before)
if (!mqttState.mqttEnabled) { // MQTT service disabled
return false;
}

if (mqttState.failedOnCycle == getFlowCycleCounter()) { // we already failed in this cycle, do not retry until the next cycle
if (mqttState.failedOnCycle == getFlowCycleCounter()) { // Already a failed transmission in this cycle
return true; // Fail quietly
}

Expand Down Expand Up @@ -283,7 +282,7 @@ bool configureMqttClient(const CfgData::SectionMqtt *_param, int keepAlive)
}
}

mqttState.mqttConfigured = true;
mqttState.mqttEnabled = true;
return true;
}

Expand All @@ -294,16 +293,13 @@ int startMqttClient(void)
return 0;
}

if (mqttState.mqttConfigured) {
mqttState.mqttStartEnabled = true;
}
else {
LogFile.writeToFile(ESP_LOG_DEBUG, TAG, "Init called, but client is not yet configured.");
if (!mqttState.mqttEnabled) {
LogFile.writeToFile(ESP_LOG_DEBUG, TAG, "Init called, but service is not configured");
return 0;
}

if (!getWifiIsConnected()) {
LogFile.writeToFile(ESP_LOG_DEBUG, TAG, "Init called, but wlan is not yet connected.");
LogFile.writeToFile(ESP_LOG_DEBUG, TAG, "Init called, but wlan is not yet connected");
return 0;
}

Expand Down Expand Up @@ -383,11 +379,10 @@ int startMqttClient(void)
}


void deinitMqttClient(bool discardConfig)
void deinitMqttClient(bool disable)
{
if (discardConfig) {
mqttState.mqttConfigured = false;
mqttState.mqttStartEnabled = false;
if (disable) {
mqttState.mqttEnabled = false;
}

mqttState.mqttInitialized = false;
Expand All @@ -402,9 +397,9 @@ void deinitMqttClient(bool discardConfig)
}


bool getMqttStartEnabled(void)
bool getMqttIsEnabled(void)
{
return mqttState.mqttConfigured && mqttState.mqttStartEnabled;
return mqttState.mqttEnabled;
}


Expand Down
4 changes: 2 additions & 2 deletions code/components/mqtt_ctrl/interface_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool publishMqttData(std::string _key, std::string _content, int qos, bool _reta
bool configureMqttClient(const CfgData::SectionMqtt *cfgDataPtr, int keepAlive);
int startMqttClient(void);

bool getMqttStartEnabled(void);
bool getMqttIsEnabled(void);
bool getMqttIsConnected(void);
bool getMqttIsEncrypted(void);

Expand All @@ -25,7 +25,7 @@ void registerMqttSubscribeFunction(std::string topic, std::function<bool(std::st
void unregisterMqttSubscribeFunction();
void isConnectedState(void);

void deinitMqttClient(bool discardConfig = false);
void deinitMqttClient(bool disable = false);

#endif //INTERFACE_MQTT_H
#endif //#ENABLE_MQTT
4 changes: 2 additions & 2 deletions code/components/webserver_softap/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ esp_err_t handler_get_info(httpd_req_t *req)
retVal = ESP_FAIL;

#ifdef ENABLE_MQTT
if (cJSON_AddStringToObject(cJSONObject, "mqtt_status", getMqttStartEnabled() ? (getMqttIsConnected() ? (getMqttIsEncrypted() ?
if (cJSON_AddStringToObject(cJSONObject, "mqtt_status", getMqttIsEnabled() ? (getMqttIsConnected() ? (getMqttIsEncrypted() ?
"Connected (Encrypted)" : "Connected") : "Disconnected") : "Disabled") == NULL)
retVal = ESP_FAIL;
#else
Expand Down Expand Up @@ -248,7 +248,7 @@ esp_err_t handler_get_info(httpd_req_t *req)

#ifdef ENABLE_MQTT
else if (type.compare("mqtt_status") == 0) {
httpd_resp_sendstr(req, getMqttStartEnabled() ? (getMqttIsConnected() ? (getMqttIsEncrypted() ?
httpd_resp_sendstr(req, getMqttIsEnabled() ? (getMqttIsConnected() ? (getMqttIsEncrypted() ?
"Connected (Encrypted)" : "Connected") : "Disconnected") : "Disabled");
return ESP_OK;
}
Expand Down
5 changes: 3 additions & 2 deletions code/components/wlan_ctrl/connect_wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_

#ifdef ENABLE_MQTT
// Start MQTT serivce
if (!getMqttStartEnabled()) {
if (getMqttIsEnabled()) {
vTaskDelay(pdMS_TO_TICKS(500));
startMqttClient();
}
#endif //ENABLE_MQTT
Expand Down Expand Up @@ -216,7 +217,7 @@ bool suspendWifiConnection(void)
setStatusLed(WLAN_CONN, 5, false);

// Stop MQTT client
if (getMqttStartEnabled()) {
if (getMqttIsEnabled()) {
deinitMqttClient();
}

Expand Down

0 comments on commit 508db52

Please sign in to comment.