Skip to content

Commit

Permalink
Fix Realtime Database push issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Feb 12, 2025
1 parent fd11508 commit cc55104
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.5.9-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.5.10-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2025-02-11`
Revision `2025-02-12`

## Table of Contents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void setup()
Serial.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);

WiFi.onEvent(WiFiEvent);
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_POWER, ETH_CLK_MODE);
}

void setConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@

#include <LwipEthernet.h>

/** Don't gorget to define this in src/Config.h or src/UserConfig.h
/** Don't gorget to define this in src/FirebaseConfig.h or src/UserConfig.h
#define ENABLE_ESP8266_ENC28J60_ETH
*/

/** For PlatformIO IDE, please set the lib_ldf_mode in platformio.ini as the following.
/** For PlatformIO IDE, please set the lib_ldf_mode or lib_deps in platformio.ini as the following.
lib_ldf_mode = chain+
lib_deps = arduino-libraries/Ethernet @ 2.0.2
*/

// The API key can be obtained from Firebase console > Project Overview > Project settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions

// 📍######### IMPORTANT ! #########
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/Config.h or user sefined config at src/UserConfig.h
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/FirebaseConfig.h or user sefined config at src/UserConfig.h

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ void checkConnection(AsyncClientClass &a_client)
if (!a_client.networkStatus() && millis() - last_change_ms > change_timeout && millis() - a_client.networkLastSeen() > lastseen_timeout)
{
last_change_ms = millis();
if (a_client.getNetworkType() == firebase_network_data_default_network)
if (a_client.getNetworkType() == firebase_network_default)
{
Serial.println("Switch to Ethernet network...");
a_client.setNetwork(ethernet_ssl_client, getNetwork(ethernet_network));
}
else if (a_client.getNetworkType() == firebase_network_data_ethernet_network)
else if (a_client.getNetworkType() == firebase_network_ethernet)
{
Serial.println("Switch to WiFi network...");
connectWiFi();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include <LwipEthernet.h>

/** Don't gorget to define this in src/Config.h or src/UserConfig.h
/** Don't gorget to define this in src/FirebaseConfig.h or src/UserConfig.h
#define ENABLE_ESP8266_ENC28J60_ETH
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions

// 📍######### IMPORTANT ! #########
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/Config.h or user sefined config at src/UserConfig.h
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/FirebaseConfig.h or user sefined config at src/UserConfig.h

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ void checkConnection(AsyncClientClass &a_client)
if (!a_client.networkStatus() && millis() - last_change_ms > change_timeout && millis() - a_client.networkLastSeen() > lastseen_timeout)
{
last_change_ms = millis();
if (a_client.getNetworkType() == firebase_network_data_default_network)
if (a_client.getNetworkType() == firebase_network_default)
{
Serial.println("Switch to Ethernet network...");
a_client.setNetwork(ethernet_ssl_client, getNetwork(ethernet_network));
}
else if (a_client.getNetworkType() == firebase_network_data_ethernet_network)
else if (a_client.getNetworkType() == firebase_network_ethernet)
{
Serial.println("Switch to WiFi network...");
connectWiFi();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include <LwipEthernet.h>

/** Don't gorget to define this in src/Config.h or src/UserConfig.h
/** Don't gorget to define this in src/FirebaseConfig.h or src/UserConfig.h
#define ENABLE_ESP8266_ENC28J60_ETH
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions

// 📍######### IMPORTANT ! #########
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/Config.h or user sefined config at src/UserConfig.h
// The macro TINY_GSM_MODEM_SIM7600 should be defined in src/FirebaseConfig.h or user sefined config at src/UserConfig.h

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ void checkConnection(AsyncClientClass &a_client)
if (!a_client.networkStatus() && millis() - last_change_ms > change_timeout && millis() - a_client.networkLastSeen() > lastseen_timeout)
{
last_change_ms = millis();
if (a_client.getNetworkType() == firebase_network_data_default_network)
if (a_client.getNetworkType() == firebase_network_default)
{
Serial.println("Switch to Ethernet network...");
a_client.setNetwork(ethernet_ssl_client, getNetwork(ethernet_network));
}
else if (a_client.getNetworkType() == firebase_network_data_ethernet_network)
else if (a_client.getNetworkType() == firebase_network_ethernet)
{
Serial.println("Switch to WiFi network...");
connectWiFi();
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FirebaseClient",
"version": "1.5.9",
"version": "1.5.10",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Async Firebase Client library for Arduino.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=FirebaseClient

version=1.5.9
version=1.5.10

author=Mobizt

Expand Down
8 changes: 8 additions & 0 deletions src/core/AsyncClient/AsyncClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
return sman.connErrorHandler(sData, sData->state);

sman.conn.sse = sData->sse;
sman.conn.async = sData->async;
sData->auth_ts = auth_ts;
}

Expand Down Expand Up @@ -1045,6 +1046,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
sData->state = astate_send_header;
}

// Resume async task from previously stopped.
if (!sman.conn.async && sData->async)
{
sData->state = astate_send_header;
sman.conn.async = sData->async;
}

bool sending = false;
if (sData->state == astate_undefined || sData->state == astate_send_header || sData->state == astate_send_payload)
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/AsyncClient/AsyncData.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct async_data
req_handler request;
res_handler response;
async_error_t error;
bool to_remove = false, auth_used = false, complete = false, async = false, sse = false, path_not_existed = false;
bool to_remove = false, auth_used = false, complete = false, async = false, stop_current_async = false, sse = false, path_not_existed = false;
bool download = false, upload_progress_enabled = false, upload = false;
uint32_t auth_ts = 0, addr = 0, ref_result_addr = 0;
AsyncResult aResult;
Expand Down Expand Up @@ -62,6 +62,7 @@ struct async_data
auth_used = false;
complete = false;
async = false;
stop_current_async = false;
sse = false;
path_not_existed = false;
cb = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/core/AsyncClient/ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct conn_handler : public ConnBase
int netErrState = 0;

public:
bool sse = false;
bool sse = false, async = false;
String host;
uint16_t port;

Expand Down
15 changes: 13 additions & 2 deletions src/core/AsyncClient/SlotManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SlotManager : public ResultBase, RTDBResultBase
int availableSlot(slot_options_t &options)
{
int slot = -1;
if (options.auth_used)
if (options.auth_used /* highest priority */ || !options.async)
slot = 0;
else
{
Expand Down Expand Up @@ -101,8 +101,17 @@ class SlotManager : public ResultBase, RTDBResultBase
// Only one SSE mode is allowed
if (slot_index == -2)
return nullptr;

bool prev_async = getData(0) && getData(0)->async && conn.async;

async_data *sData = addSlot(slot_index);
sData->reset();

// If new task is sync while current async task is running then stop it.
// The stopped async task will be resumed later.
if (prev_async && !options.auth_used && !options.async)
sData->stop_current_async = true;

return sData;
}

Expand Down Expand Up @@ -231,9 +240,11 @@ class SlotManager : public ResultBase, RTDBResultBase
sData->request.setClient(client_type, client, atcp_config);
sData->response.setClient(client_type, client, atcp_config);

if ((!sData->sse && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC && session_timer.remaining() == 0) || (conn.sse && !sData->sse) || (!conn.sse && sData->sse) || (sData->auth_used && sData->state == astate_undefined) ||
if ((!sData->sse && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC && session_timer.remaining() == 0) || sData->stop_current_async ||
(conn.sse && !sData->sse) || (!conn.sse && sData->sse) || (sData->auth_used && sData->state == astate_undefined) ||
strcmp(conn.host.c_str(), host) != 0 || conn.port != port)
{
sData->stop_current_async = false;
stop();
getResult()->clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#undef FIREBASE_CLIENT_VERSION
#endif

#define FIREBASE_CLIENT_VERSION "1.5.9"
#define FIREBASE_CLIENT_VERSION "1.5.10"

static void sys_idle()
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/Network/Data/NetworkConfigData.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "./core/Network/NetworkOptions.h"
#include "./core/Network/Data/SPIEthernet.h"
#include "./core/Network/Data/FirebaseWiFi.h"

#include "./core/Network/Data/StaticAddress.h"
typedef enum
{
firebase_network_undefined,
Expand Down Expand Up @@ -109,6 +109,7 @@ struct network_config_data
struct ethernet_data
{
friend class AsyncClientClass;
friend class conn_handler;

private:
int ethernet_reset_pin = -1, ethernet_cs_pin = -1;
Expand Down
1 change: 1 addition & 0 deletions src/core/Network/Data/StaticAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct firebase_client_static_address
{
friend class AsyncClientClass;
friend class network_config_data;
friend class conn_handler;

public:
/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/Network/EthernetNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EthernetNetwork : public DefaultNetwork
network_data.ethernet.setMac(macAddress);
network_data.ethernet.setCs(csPin);
network_data.ethernet.setReset(resetPin);
network_data.network_type = firebase_network_ethernet_network;
network_data.network_type = firebase_network_ethernet;
}

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ class EthernetNetwork : public DefaultNetwork
network_data.ethernet.setCs(csPin);
network_data.ethernet.setReset(resetPin);
network_data.ethernet.setStaticIP(staticIP);
network_data.network_type = firebase_network_ethernet_network;
network_data.network_type = firebase_network_ethernet;
}
~EthernetNetwork() { clear(); }
};
Expand Down
6 changes: 3 additions & 3 deletions src/database/RealtimeDatabase.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* 2025-02-11
* 2025-02-12
*
* The MIT License (MIT)
* Copyright (c) 2025 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -520,7 +520,7 @@ class RealtimeDatabase : public RTDBResultBase, AppBase
*
*/
template <typename T = const char *>
String push(AsyncClientClass &aClient, const String &path, T value) { return sendRequest(&aClient, path, reqns::http_post, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL, "", convert(value).c_str())->rtdbResult.name(); }
String push(AsyncClientClass &aClient, const String &path, T value) { return sendRequest(&aClient, path, reqns::http_post, slot_options_t(), nullptr, nullptr, aClient.getResult(), NULL, "", convert(value).c_str())-> rtdbResult.name(); }

/**
* Push value to database.
Expand Down Expand Up @@ -823,7 +823,7 @@ class RealtimeDatabase : public RTDBResultBase, AppBase
return sendRequest(&aClient, path, mode, slot_options_t(false, false, async, payload.indexOf("\".sv\"") > -1, false, false), &options, nullptr, aResult, cb, uid, payload.c_str())->lastError.code() == 0;
}
template <typename T = object_t>
const char *convert(T value)
String convert(T value)
{
ValueConverter vcon;
String payload;
Expand Down

0 comments on commit cc55104

Please sign in to comment.