Skip to content

Commit a7be1ff

Browse files
committed
Added support for atomic operations
1 parent c3974cc commit a7be1ff

4 files changed

Lines changed: 117 additions & 5 deletions

File tree

keywords.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ tryConnect KEYWORD2
1414
setCredentials KEYWORD2
1515
put KEYWORD2
1616
get KEYWORD2
17-
add KEYWORD2
17+
add KEYWORD2
18+
pop KEYWORD2
19+
inc KEYWORD2
20+
dec KEYWORD2
21+
put_min KEYWORD2
22+
put_max KEYWORD2
23+
datetime KEYWORD2

src/CloudStorage.h

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,33 @@ class BaseCloudStorage {
127127
!root["result"]["empty"]
128128
);
129129
}
130+
131+
//Increment
132+
cloud_storage_utils::ResultWrapper<int> inc(String key, int value = 1) {
133+
return atomic<int>(key, "inc", value);
134+
}
135+
136+
//Decrement
137+
cloud_storage_utils::ResultWrapper<int> dec(String key, int value = 1) {
138+
return atomic<int>(key, "dec", value);
139+
}
140+
141+
//Replace an item if it is a new minimum
142+
template <class Ty>
143+
cloud_storage_utils::ResultWrapper<Ty> put_min(String key, Ty value) {
144+
return atomic<Ty>(key, "min", value);
145+
}
146+
147+
//Replace an item if it is a new maximum
148+
template <class Ty>
149+
cloud_storage_utils::ResultWrapper<Ty> put_max(String key, Ty value) {
150+
return atomic<Ty>(key, "max", value);
151+
}
152+
153+
//Replace an item if it is a new maximum
154+
cloud_storage_utils::ResultWrapper<String> datetime(String key) {
155+
return atomic<String>(key, "date", "");
156+
}
130157

131158
private:
132159
String _username, _password;
@@ -197,6 +224,24 @@ class BaseCloudStorage {
197224
return jsonString;
198225
}
199226

227+
// Utility method for constructing generic *Atomic* request json string
228+
template <class Ty>
229+
String buildAtomicRequestJson(String key, String action, Ty value) {
230+
// Compose request json object
231+
StaticJsonBuffer<200> jsonBuffer;
232+
JsonObject& root = jsonBuffer.createObject();
233+
root["username"] = _username;
234+
root["password"] = _password;
235+
root["key"] = key;
236+
root["action"] = action;
237+
root["value"] = value;
238+
239+
// Return string form of the object
240+
String jsonString;
241+
root.printTo(jsonString);
242+
return jsonString;
243+
}
244+
200245
// Method for accessing nested json objects with '.' seperated keys.
201246
// for examples "name.first.english" for accessing name:{.., first:{ english: "MyName", .... }}
202247
template <class Type>
@@ -211,7 +256,34 @@ class BaseCloudStorage {
211256

212257
// traverse the parentKey object
213258
return getValueByKey<Type>(root[parentKey], remainder);
214-
}
259+
}
260+
261+
//Generic Atomic Request
262+
template <class Ty>
263+
cloud_storage_utils::ResultWrapper<Ty> atomic(String key, String action, Ty value) {
264+
// Build request json
265+
String jsonString = buildAtomicRequestJson<Ty>(key, action, value);
266+
267+
// Construct http request
268+
RequestType request(
269+
_baseServerUrl + "/data/object/atomic",
270+
http::Method::GET,
271+
jsonString
272+
);
273+
request.addHeader("Content-Type", "application/json; charset=utf-8");
274+
275+
//Execute request
276+
http::Response response = request.execute();
277+
if(response.statusCode != 200) return false;
278+
279+
// Parse response body and extract the wanted value
280+
StaticJsonBuffer<300> jsonBuffer;
281+
JsonObject& root = jsonBuffer.parseObject(response.body);
282+
return cloud_storage_utils::ResultWrapper<Ty>(
283+
!root["error"],
284+
getValueByKey<Ty>(root["result"], key)
285+
);
286+
}
215287
};
216288

217289
typedef BaseCloudStorage<http::GenericEspRequest> CloudStorage;

src/Http/EspClient.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#pragma once
22

33
#include "Client.h"
4-
#include "GenericEspRequestImpl.h"
54

5+
#ifdef ARDUINO_ARCH_ESP32
6+
7+
#include "GenericEspRequestImpl.h"
68
namespace http {
79
typedef Request<GenericEspRequestImpl> GenericEspRequest;
8-
};
10+
};
11+
12+
#else
13+
14+
#include "Esp8266RequestImpl.h"
15+
namespace http {
16+
typedef Request<Esp8266RequestImpl> GenericEspRequest;
17+
};
18+
19+
#endif

src/WifiConnection.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include <WiFi.h>
3+
#ifdef ARDUINO_ARCH_ESP32
44

5+
#include <WiFi.h>
56
namespace WifiConnection {
67
// Sets wifi mode to STA and tries to connect to a network
78
void tryConnect(String ssid, String pass) {
@@ -14,3 +15,25 @@ namespace WifiConnection {
1415
return WiFi.status() == WL_CONNECTED;
1516
}
1617
};
18+
19+
#else
20+
21+
#include <ESP8266WiFi.h>
22+
#include <ESP8266WiFiMulti.h>
23+
24+
namespace WifiConnection {
25+
ESP8266WiFiMulti WiFiMulti;
26+
27+
// Sets wifi mode to STA and tries to connect to a network
28+
void tryConnect(String ssid, String pass) {
29+
WiFi.mode(WIFI_STA);
30+
WiFiMulti.addAP(ssid.c_str(), pass.c_str());
31+
}
32+
33+
// Method for checking if currently connected to wifi
34+
boolean isConnected() {
35+
return WiFiMulti.run() == WL_CONNECTED;
36+
}
37+
};
38+
39+
#endif

0 commit comments

Comments
 (0)