-
Notifications
You must be signed in to change notification settings - Fork 493
FirebaseArduino: simpler Arduino wrapper #98
Changes from all commits
84df631
11b148f
2f9b0ca
be1a7da
13ce951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// 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" | ||
|
||
void FirebaseArduino::begin(const char* host, const char* auth) { | ||
http_.reset(FirebaseHttpClient::create()); | ||
http_->setReuseConnection(true); | ||
host_ = host; | ||
auth_ = auth; | ||
} | ||
|
||
String FirebaseArduino::FirebaseArduino::push(const String& path, const JsonVariant& value) { | ||
String buf; | ||
value.printTo(buf); | ||
auto push = FirebasePush(host_, auth_, path, buf, http_.get()); | ||
error_ = push.error(); | ||
return push.name(); | ||
} | ||
|
||
void FirebaseArduino::set(const String& path, const JsonVariant& value) { | ||
String buf; | ||
value.printTo(buf); | ||
auto set = FirebaseSet(host_, auth_, path, buf, http_.get()); | ||
error_ = set.error(); | ||
} | ||
|
||
FirebaseObject FirebaseArduino::get(const char* path) { | ||
auto get = FirebaseGet(host_, auth_, path, http_.get()); | ||
error_ = get.error(); | ||
if (!error_) { | ||
return FirebaseObject{""}; | ||
} | ||
return FirebaseObject(get.response()); | ||
} | ||
|
||
void FirebaseArduino::remove(const char* path) { | ||
auto remove = FirebaseRemove(host_, auth_, path, http_.get()); | ||
error_ = remove.error(); | ||
} | ||
|
||
void FirebaseArduino::stream(const char* path) { | ||
auto stream = FirebaseStream(host_, auth_, path, http_.get()); | ||
error_ = stream.error(); | ||
} | ||
|
||
bool FirebaseArduino::available() { | ||
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); | ||
obj["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(); | ||
} | ||
|
||
FirebaseArduino Firebase; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// 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. | ||
// | ||
|
||
#ifndef FIREBASE_ARDUINO_H | ||
#define FIREBASE_ARDUINO_H | ||
|
||
#include "Firebase.h" | ||
#include "FirebaseObject.h" | ||
|
||
#ifndef FIREBASE_JSONBUFFER_SIZE | ||
#define FIREBASE_JSONBUFFER_SIZE 200 | ||
#endif // FIREBASE_JSONBUFFER_SIZE | ||
|
||
class FirebaseArduino { | ||
public: | ||
void begin(const char* host, const char* auth = ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are presenting this as the super easy way to get into firebase I think we should have a lot of comments on all of the methods here. Also maybe some simple usage examples in the header comment at the top of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I'd like to do #30. maybe in a different PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||
String push(const String& path, const JsonVariant& value); | ||
void set(const String& path, const JsonVariant& value); | ||
FirebaseObject get(const char* path); | ||
void remove(const char* path); | ||
void stream(const char* path); | ||
bool available(); | ||
FirebaseObject readEvent(); | ||
bool success(); | ||
bool failed(); | ||
const String& error(); | ||
private: | ||
String host_; | ||
String auth_; | ||
FirebaseError error_; | ||
std::unique_ptr<FirebaseHttpClient> http_; | ||
}; | ||
|
||
extern FirebaseArduino Firebase; | ||
|
||
#endif // FIREBASE_ARDUINO_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// 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 "FirebaseObject.h" | ||
|
||
namespace { | ||
template<typename T> | ||
T decodeJsonLiteral(const String& json) { | ||
return JsonVariant{ArduinoJson::RawJson{json.c_str()}}; | ||
} | ||
|
||
// ugly workaround to https://github.com/bblanchon/ArduinoJson/issues/265 | ||
template<> | ||
String decodeJsonLiteral<String>(const String& json) { | ||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> buf; | ||
String array = "[" + json + "]"; | ||
return buf.parseArray(&array[0])[0]; | ||
} | ||
} // namespace | ||
|
||
FirebaseObject::FirebaseObject(const String& data) : data_{data} { | ||
if (data_[0] == '{') { | ||
json_ = &buffer_.parseObject(&data_[0]); | ||
} else if (data_[0] == '"') { | ||
data_ = decodeJsonLiteral<String>(data_); | ||
} | ||
} | ||
|
||
FirebaseObject::operator bool() { | ||
return decodeJsonLiteral<bool>(data_); | ||
} | ||
|
||
FirebaseObject::operator int() { | ||
return decodeJsonLiteral<int>(data_); | ||
} | ||
|
||
FirebaseObject::operator float() { | ||
return decodeJsonLiteral<float>(data_); | ||
} | ||
|
||
FirebaseObject::operator const String&() { | ||
return data_; | ||
} | ||
|
||
FirebaseObject::operator const JsonObject&() { | ||
return *json_; | ||
} | ||
|
||
JsonObjectSubscript<const char*> FirebaseObject::operator[](const char* key) { | ||
return json_->operator[](key); | ||
} | ||
|
||
JsonObjectSubscript<const String&> FirebaseObject::operator[](const String& key) { | ||
return json_->operator[](key); | ||
} | ||
|
||
JsonVariant FirebaseObject::operator[](JsonObjectKey key) const { | ||
return json_->operator[](key); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Copyright 2015 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. | ||
// | ||
|
||
#ifndef FIREBASE_OBJECT_H | ||
#define FIREBASE_OBJECT_H | ||
|
||
#include "third-party/arduino-json-5.2/include/ArduinoJson.h" | ||
|
||
#define FIREBASE_JSONBUFFER_SIZE 200 | ||
|
||
class FirebaseObject { | ||
public: | ||
FirebaseObject(const String& data); | ||
operator bool(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar point here about comments on everything. |
||
operator int(); | ||
operator float(); | ||
operator const String&(); | ||
operator const JsonObject&(); | ||
JsonObjectSubscript<const char*> operator[](const char* key); | ||
JsonObjectSubscript<const String&> operator[](const String& key); | ||
JsonVariant operator[](JsonObjectKey key) const; | ||
private: | ||
String data_; | ||
StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE> buffer_; | ||
JsonObject* json_; | ||
}; | ||
|
||
#endif // FIREBASE_OBJECT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// 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 "FirebaseObject.h" | ||
#include "gtest/gtest.h" | ||
|
||
TEST(FirebaseObjectTest, JsonLiteral) { | ||
EXPECT_EQ(bool(FirebaseObject("true")), true); | ||
EXPECT_EQ(bool(FirebaseObject("false")), false); | ||
EXPECT_EQ(int(FirebaseObject("42")), 42); | ||
EXPECT_EQ(float(FirebaseObject("43.0")), 43.0); | ||
EXPECT_EQ(String(FirebaseObject("\"foo\"")), "foo"); | ||
} | ||
|
||
TEST(FirebaseObjectTest, JsonObject) { | ||
{ | ||
const JsonObject& obj = FirebaseObject("{\"foo\":\"bar\"}"); | ||
String foo = obj["foo"]; | ||
EXPECT_EQ(foo, "bar"); | ||
} | ||
{ | ||
String foo = FirebaseObject("{\"foo\":\"bar\"}")["foo"]; | ||
EXPECT_EQ(foo, "bar"); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it is kind of pointless I still like having a Get command in here too to show you the timestamp that was actually written and to demonstrate using get and jsonobject since we don't have a get example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think I will adapt the same to show all the api (but stream) #99. Maybe in a different PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.