This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFirebaseArduino.cpp
173 lines (145 loc) · 4.7 KB
/
FirebaseArduino.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// 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) {
http_.reset(FirebaseHttpClient::create());
http_->setReuseConnection(true);
host_ = host.c_str();
auth_ = auth.c_str();
}
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) {
String buf;
value.printTo(buf);
auto push = FirebasePush(host_, auth_, path.c_str(), buf.c_str(), http_.get());
error_ = push.error();
return push.name().c_str();
}
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) {
String buf;
value.printTo(buf);
auto set = FirebaseSet(host_, auth_, path.c_str(), buf.c_str(), http_.get());
error_ = set.error();
}
void FirebaseArduino::update(const String& path, const JsonVariant& value) {
String buf;
value.printTo(buf);
auto set = FirebaseUpdate(host_, auth_, path.c_str(), buf.c_str(), http_.get());
error_ = set.error();
}
FirebaseObject FirebaseArduino::get(const String& path) {
auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get());
error_ = get.error();
if (failed()) {
return FirebaseObject{""};
}
return FirebaseObject(get.response().c_str());
}
int FirebaseArduino::getInt(const String& path) {
auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get());
error_ = get.error();
if (failed()) {
return 0;
}
return FirebaseObject(get.response().c_str()).getInt();
}
float FirebaseArduino::getFloat(const String& path) {
auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get());
error_ = get.error();
if (failed()) {
return 0.0f;
}
return FirebaseObject(get.response().c_str()).getFloat();
}
String FirebaseArduino::getString(const String& path) {
auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get());
error_ = get.error();
if (failed()) {
return "";
}
return FirebaseObject(get.response().c_str()).getString();
}
bool FirebaseArduino::getBool(const String& path) {
auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get());
error_ = get.error();
if (failed()) {
return "";
}
return FirebaseObject(get.response().c_str()).getBool();
}
void FirebaseArduino::remove(const String& path) {
auto remove = FirebaseRemove(host_, auth_, path.c_str(), http_.get());
error_ = remove.error();
}
void FirebaseArduino::stream(const String& path) {
auto stream = FirebaseStream(host_, auth_, path.c_str(), http_.get());
error_ = stream.error();
}
bool FirebaseArduino::available() {
if (http_->getStreamPtr() == nullptr) {
return false;
}
return http_->getStreamPtr()->available();
}
FirebaseObject FirebaseArduino::readEvent() {
auto client = http_->getStreamPtr();
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;
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();
}
const FirebaseError& FirebaseArduino::errorObj() {
return error_;
}
FirebaseArduino Firebase;