Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab6b537

Browse files
committed
FirebaseObject: simplify API
- remove cast operator- remove indexing method- accept a subpath
1 parenta516bb2 commitab6b537

File tree

6 files changed

+249
-91
lines changed

6 files changed

+249
-91
lines changed

‎examples/FirebaseRoom_ESP8266/FirebaseRoom_ESP8266.ino‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
#include<ESP8266WiFi.h>
2121
#include<FirebaseArduino.h>
2222

23-
constintpinGrove =15;
24-
constintpinVibrator =5;
25-
constintpinLightSensor = A0;
26-
constintpinLed =12;
27-
constintpinButton =14;
28-
constintpinFan =13;
23+
constintgrovePowerPin =15;
24+
constintvibratorPin =5;
25+
constintlightSensorPin = A0;
26+
constintledPin =12;
27+
constintbuttonPin =14;
28+
constintfanPin =13;
2929

3030
voidsetup() {
3131
Serial.begin(9600);
3232

33-
pinMode(pinGrove, OUTPUT);
34-
digitalWrite(pinGrove, HIGH);
33+
pinMode(grovePowerPin, OUTPUT);
34+
digitalWrite(grovePowerPin, HIGH);
3535

36-
pinMode(pinVibrator, OUTPUT);
37-
pinMode(pinLightSensor, INPUT);
38-
pinMode(pinLed, OUTPUT);
39-
pinMode(pinButton, INPUT);
40-
pinMode(pinFan, OUTPUT);
36+
pinMode(vibratorPin, OUTPUT);
37+
pinMode(lightSensorPin, INPUT);
38+
pinMode(ledPin, OUTPUT);
39+
pinMode(buttonPin, INPUT);
40+
pinMode(fanPin, OUTPUT);
4141

4242
// connect to wifi.
4343
WiFi.begin("SSID","PASSWORD");
@@ -62,15 +62,15 @@ int button = 0;
6262
float light =0.0;
6363

6464
voidloop() {
65-
digitalWrite(pinLed, (int)Firebase.get("redlight"));
66-
digitalWrite(pinFan, (int)Firebase.get("cooldown"));
67-
digitalWrite(pinVibrator, (int)Firebase.get("brrr"));
68-
int newButton =digitalRead(pinButton);
65+
digitalWrite(ledPin,Firebase.getInt("redlight"));
66+
digitalWrite(fanPin,Firebase.getInt("cooldown"));
67+
digitalWrite(vibratorPin,Firebase.getInt("brrr"));
68+
int newButton =digitalRead(buttonPin);
6969
if (newButton != button) {
7070
button = newButton;
7171
Firebase.set("pushbutton", button);
7272
}
73-
float newLight =analogRead(pinLightSensor);
73+
float newLight =analogRead(lightSensorPin);
7474
if (abs(newLight - light) >100) {
7575
light = newLight;
7676
Firebase.set("sunlight", light);

‎src/FirebaseArduino.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void FirebaseArduino::set(const String& path, const JsonVariant& value) {
3838
error_ = set.error();
3939
}
4040

41+
4142
FirebaseObjectFirebaseArduino::get(constchar* path) {
4243
auto get =FirebaseGet(host_, auth_, path, http_.get());
4344
error_ = get.error();
@@ -47,6 +48,44 @@ FirebaseObject FirebaseArduino::get(const char* path) {
4748
returnFirebaseObject(get.response());
4849
}
4950

51+
intFirebaseArduino::getInt(constchar* path) {
52+
auto get =FirebaseGet(host_, auth_, path, http_.get());
53+
error_ = get.error();
54+
if (failed()) {
55+
return0;
56+
}
57+
returnFirebaseObject(get.response()).getInt();
58+
}
59+
60+
61+
floatFirebaseArduino::getFloat(constchar* path) {
62+
auto get =FirebaseGet(host_, auth_, path, http_.get());
63+
error_ = get.error();
64+
if (failed()) {
65+
return0.0f;
66+
}
67+
returnFirebaseObject(get.response()).getFloat();
68+
}
69+
70+
StringFirebaseArduino::getString(constchar* path) {
71+
auto get =FirebaseGet(host_, auth_, path, http_.get());
72+
error_ = get.error();
73+
if (failed()) {
74+
return"";
75+
}
76+
returnFirebaseObject(get.response()).getString();
77+
}
78+
79+
boolFirebaseArduino::getBool(constchar* path) {
80+
auto get =FirebaseGet(host_, auth_, path, http_.get());
81+
error_ = get.error();
82+
if (failed()) {
83+
return"";
84+
}
85+
returnFirebaseObject(get.response()).getBool();
86+
}
87+
88+
5089
voidFirebaseArduino::remove(constchar* path) {
5190
auto remove =FirebaseRemove(host_, auth_, path, http_.get());
5291
error_ = remove.error();

‎src/FirebaseArduino.h‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,44 @@ class FirebaseArduino {
5858

5959

6060
/**
61-
* Gets the value located at path.
61+
* Gets theintegervalue located at path.
6262
* You should check success() after calling.
6363
* \param path The path to the node you wish to retrieve.
64-
* \return The data located at that path. This may either be a single element
65-
* or it may be a json structure. Will only be populated if success() is true.
64+
* \return The integer value located at that path. Will only be populated if success() is true.
6665
*/
67-
FirebaseObjectget(constchar* path);
66+
intgetInt(constchar* path);
67+
68+
/**
69+
* Gets the float value located at path.
70+
* You should check success() after calling.
71+
* \param path The path to the node you wish to retrieve.
72+
* \return The float value located at that path. Will only be populated if success() is true.
73+
*/
74+
intgetFloat(constchar* path);
75+
76+
/**
77+
* Gets the string value located at path.
78+
* You should check success() after calling.
79+
* \param path The path to the node you wish to retrieve.
80+
* \return The string value located at that path. Will only be populated if success() is true.
81+
*/
82+
intgetString(constchar* path);
83+
84+
/**
85+
* Gets the boolean value located at path.
86+
* You should check success() after calling.
87+
* \param path The path to the node you wish to retrieve.
88+
* \return The boolean value located at that path. Will only be populated if success() is true.
89+
*/
90+
intgetBool(constchar* path);
91+
92+
/**
93+
* Gets the json object value located at path.
94+
* You should check success() after calling.
95+
* \param path The path to the node you wish to retrieve.
96+
* \return a FirebaseObject value located at that path. Will only be populated if success() is true.
97+
*/
98+
FirebaseObjectgetObject(constchar* path);
6899

69100
/**
70101
* Remove the node, and possibly entire tree, located at path.
@@ -94,7 +125,7 @@ class FirebaseArduino {
94125
/**
95126
* Reads the next event in a stream. This is only meaningful once stream() has
96127
* been called.
97-
* \returnObject will have ["type"] that describes the event type, ["path"]
128+
* \returnFirebaseObject will have ["type"] that describes the event type, ["path"]
98129
* that describes the effected path and ["data"] that was updated.
99130
*/
100131
FirebaseObjectreadEvent();

‎src/FirebaseObject.cpp‎

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,76 @@
1616

1717
#include"FirebaseObject.h"
1818

19-
namespace {
20-
template<typename T>
21-
TdecodeJsonLiteral(const String& json) {
22-
return JsonVariant{ArduinoJson::RawJson{json.c_str()}};
23-
}
24-
25-
// ugly workaround to https://github.com/bblanchon/ArduinoJson/issues/265
26-
template<>
27-
String decodeJsonLiteral<String>(const String& json) {
28-
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> buf;
29-
String array ="[" + json +"]";
30-
return buf.parseArray(&array[0])[0];
31-
}
32-
}// namespace
3319

3420
FirebaseObject::FirebaseObject(const String& data) : data_{data} {
35-
if (data_[0] =='{') {
36-
json_ = &buffer_.parseObject(&data_[0]);
37-
}elseif (data_[0] =='"') {
38-
data_ = decodeJsonLiteral<String>(data_);
39-
}
21+
json_ = buffer_.parse(&data_[0]);
22+
// TODO(proppy): find a way to check decoding error, tricky because
23+
// ArduinoJson doesn't surface error for variant parsing.
24+
// See: https://github.com/bblanchon/ArduinoJson/issues/279
4025
}
4126

42-
FirebaseObject::operatorbool() {
43-
return decodeJsonLiteral<bool>(data_);
27+
boolFirebaseObject::getBool(const String& path) {
28+
JsonVariant variant =getJsonVariant(path);
29+
if (!variant.is<bool>()) {
30+
error_ ="failed to convert to bool";
31+
return0;
32+
}
33+
returnstatic_cast<bool>(variant);
4434
}
4535

46-
FirebaseObject::operatorint() {
47-
return decodeJsonLiteral<int>(data_);
36+
intFirebaseObject::getInt(const String& path) {
37+
JsonVariant variant =getJsonVariant(path);
38+
if (!variant.is<int>()) {
39+
error_ ="failed to convert to int";
40+
return0;
41+
}
42+
returnstatic_cast<int>(variant);
4843
}
4944

50-
FirebaseObject::operatorfloat() {
51-
return decodeJsonLiteral<float>(data_);
45+
floatFirebaseObject::getFloat(const String& path) {
46+
JsonVariant variant =getJsonVariant(path);
47+
if (!variant.is<float>()) {
48+
error_ ="failed to convert to float";
49+
return0;
50+
}
51+
returnstatic_cast<float>(variant);
5252
}
5353

54-
FirebaseObject::operatorconst String&() {
55-
return data_;
54+
StringFirebaseObject::getString(const String& path) {
55+
JsonVariant variant =getJsonVariant(path);
56+
if (!variant.is<constchar *>()) {
57+
error_ ="failed to convert to string";
58+
return"";
59+
}
60+
returnstatic_cast<constchar*>(variant);
5661
}
5762

58-
FirebaseObject::operatorconst JsonObject&() {
59-
return *json_;
63+
JsonVariantFirebaseObject::getJsonVariant(const String& path) {
64+
Stringkey(path);
65+
char* start = &key[0];
66+
char* end = start + key.length();
67+
if (*start =='/') {
68+
start++;
69+
}
70+
JsonVariant json = json_;
71+
while (start < end) {
72+
char* p = start;
73+
while (*p && (*p !='/')) p++;
74+
*p =0;
75+
json = json.asObject().get(start);
76+
start = p +1;
77+
}
78+
return json;
6079
}
6180

62-
JsonObjectSubscript<constchar*>FirebaseObject::operator[](constchar* key) {
63-
returnjson_->operator[](key);
81+
boolFirebaseObject::failed()const {
82+
returnerror_.length() >0;
6483
}
6584

66-
JsonObjectSubscript<const String&>FirebaseObject::operator[](const String& key) {
67-
returnjson_->operator[](key);
85+
boolFirebaseObject::success()const {
86+
returnerror_.length() ==0;
6887
}
6988

70-
JsonVariantFirebaseObject::operator[](JsonObjectKey key)const {
71-
returnjson_->operator[](key);
89+
const String&FirebaseObject::error()const {
90+
returnerror_;
7291
}

‎src/FirebaseObject.h‎

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,61 @@ class FirebaseObject {
3131
public:
3232
/**
3333
* Construct from json.
34-
* \param dataJson formatted string.
34+
* \param dataJSON formatted string.
3535
*/
3636
FirebaseObject(const String& data);
3737

3838
/**
39-
* Interpret result as a bool, only applicable if result is a single element
40-
* and not a tree.
39+
* Return the value as a boolean.
40+
* \param optional path in the JSON object.
41+
* \return result as a bool.
4142
*/
42-
operatorbool();
43+
boolgetBool(const String& path ="");
4344

4445
/**
45-
* Interpret result as a int, only applicable if result is a single element
46-
* and not a tree.
46+
* Return the value as an int.
47+
* \param optional path in the JSON object.
48+
* \return result as an integer.
4749
*/
48-
operatorint();
50+
intgetInt(const String& path ="");
4951

5052
/**
51-
* Interpret result as a float, only applicable if result is a single element
52-
* and not a tree.
53+
* Return the value as a float.
54+
* \param optional path in the JSON object.
55+
* \return result as a float.
5356
*/
54-
operatorfloat();
57+
floatgetFloat(const String& path ="");
5558

5659
/**
57-
* Interpret result as a String, only applicable if result is a single element
58-
* and not a tree.
60+
* Return the value as a String.
61+
* \param optional path in the JSON object.
62+
* \return result as a String.
5963
*/
60-
operatorconst String&();
64+
StringgetString(const String& path ="");
6165

6266
/**
63-
* Interpret result as a JsonObject, if the result is a tree use this or the
64-
*operator[] methods below.
67+
*
68+
*\return Whether there was an error decoding or accessing the JSON object.
6569
*/
66-
operatorconst JsonObject&();
70+
boolsuccess()const;
6771

68-
//TODO(proppy): Add comments to these.
69-
JsonObjectSubscript<constchar*>operator[](constchar* key);
70-
JsonObjectSubscript<const String&>operator[](const String& key);
71-
JsonVariantoperator[](JsonObjectKey key)const;
72+
/**
73+
*
74+
* \return Whether there was an error decoding or accessing the JSON object.
75+
*/
76+
boolfailed()const;
77+
78+
/**
79+
*
80+
* \return Error message if failed() is true.
81+
*/
82+
const String&error()const;
7283
private:
84+
JsonVariantgetJsonVariant(const String& path ="");
7385
String data_;
7486
StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE> buffer_;
75-
JsonObject* json_;
87+
JsonVariant json_;
88+
String error_;
7689
};
7790

7891
#endif// FIREBASE_OBJECT_H

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp