Hi. I’m using an ESP8266 Wi-Fi module to get data from the Firebase Realtime Database and I would like to know how this is being done in the code. Since my ESP is retrieving string from the Firebase, I’m using the getString() function in FirebaseArduino.cpp. When I was trying to understand the code, I found out that there is some function that is not within that file. For example, the reset() function that is used in the initRequest function is not found in that file. Also, the function printTo() used in set() function is also not defined in that file. May I know where are all these function being defined?
Here is the code for FirebaseArduino.cpp:
//
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "FirebaseArduino.h"
// This is needed to compile std::string on esp8266.
template class std::basic_string<char>;
void FirebaseArduino::begin(const String& host, const String& auth) {
host_ = host.c_str();
auth_ = auth.c_str();
}
void FirebaseArduino::initStream() {
if (stream_http_.get() == nullptr) {
stream_http_.reset(FirebaseHttpClient::create());
stream_http_->setReuseConnection(true);
stream_.reset(new FirebaseStream(stream_http_));
}
}
void FirebaseArduino::initRequest() {
if (req_http_.get() == nullptr) {
req_http_.reset(FirebaseHttpClient::create());
req_http_->setReuseConnection(true);
req_.reset(new FirebaseRequest(req_http_));
}
}
String FirebaseArduino::pushInt(const String& path, int value) {
return push(path, value);
}
String FirebaseArduino::pushFloat(const String& path, float value) {
return push(path, value);
}
String FirebaseArduino::pushBool(const String& path, bool value) {
return push(path, value);
}
String FirebaseArduino::pushString(const String& path, const String& value) {
JsonVariant json(value.c_str());
return push(path, json);
}
String FirebaseArduino::push(const String& path, const JsonVariant& value) {
int size = value.measureLength()+1;
char * buf = new char[size];
value.printTo(buf, size);
initRequest();
int status = req_.get()->sendRequest(host_, auth_, "POST", path.c_str(), buf);
error_ = req_.get()->error();
const char* name = req_.get()->json()["name"].as<const char*>();
delete buf;
return name;
}
void FirebaseArduino::setInt(const String& path, int value) {
set(path, value);
}
void FirebaseArduino::setFloat(const String& path, float value) {
set(path, value);
}
void FirebaseArduino::setBool(const String& path, bool value) {
set(path, value);
}
void FirebaseArduino::setString(const String& path, const String& value) {
JsonVariant json(value.c_str());
set(path, json);
}
void FirebaseArduino::set(const String& path, const JsonVariant& value) {
int size = value.measureLength()+1;
char* buf= new char[size];
value.printTo(buf, size);
initRequest();
req_.get()->sendRequest(host_, auth_, "PUT", path.c_str(), buf);
error_ = req_.get()->error();
delete buf;
}
void FirebaseArduino::getRequest(const String& path) {
initRequest();
req_.get()->sendRequest(host_, auth_, "GET", path.c_str());
error_ = req_.get()->error();
}
FirebaseObject FirebaseArduino::get(const String& path) {
getRequest(path);
if (failed()) {
return FirebaseObject{""};
}
return FirebaseObject(req_.get()->response().c_str());
}
int FirebaseArduino::getInt(const String& path) {
getRequest(path);
if (failed()) {
return 0;
}
return FirebaseObject(req_.get()->response().c_str()).getInt();
}
float FirebaseArduino::getFloat(const String& path) {
getRequest(path);
if (failed()) {
return 0.0f;
}
return FirebaseObject(req_.get()->response().c_str()).getFloat();
}
String FirebaseArduino::getString(const String& path) {
getRequest(path);
if (failed()) {
return "";
}
return FirebaseObject(req_.get()->response().c_str()).getString();
}
bool FirebaseArduino::getBool(const String& path) {
getRequest(path);
if (failed()) {
return "";
}
return FirebaseObject(req_.get()->response().c_str()).getBool();
}
void FirebaseArduino::remove(const String& path) {
initRequest();
req_.get()->sendRequest(host_, auth_, "DELETE", path.c_str());
error_ = req_.get()->error();
}
void FirebaseArduino::stream(const String& path) {
initStream();
stream_.get()->startStreaming(host_, auth_, path.c_str());
error_ = stream_.get()->error();
}
bool FirebaseArduino::available() {
if (stream_http_.get() == nullptr) {
return 0;
}
auto client = stream_http_.get()->getStreamPtr();
return (client == nullptr) ? false : client->available();
}
FirebaseObject FirebaseArduino::readEvent() {
if (stream_http_.get() == nullptr) {
return FirebaseObject("");
}
auto client = stream_http_.get()->getStreamPtr();
if (client == nullptr) {
return FirebaseObject("");
}
String type = client->readStringUntil('\n').substring(7);;
String event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
FirebaseObject obj = FirebaseObject(event.c_str());
obj.getJsonVariant().asObject()["type"] = type.c_str();
return obj;
}
bool FirebaseArduino::success() {
return error_.code() == 0;
}
bool FirebaseArduino::failed() {
return error_.code() != 0;
}
const String& FirebaseArduino::error() {
return error_.message().c_str();
}
FirebaseArduino Firebase;
Any help would be appreciated.