-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKintoForIoT_Data.cpp
191 lines (163 loc) · 4.69 KB
/
KintoForIoT_Data.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// KintoForIoT - Arduino library for prototyping connected devices with Kinto
// Copyright Julien Lebunetel 2018
// MIT License
// include Arduino core library
#include "Arduino.h"
// include this library's description file
#include "KintoForIoT_Data.h"
// include description files for other libraries used
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include "ESP8266HTTPClient.h"
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
Data::Data()
{
strcpy(_server, "");
strcpy(_token, "");
strcpy(_secret, "");
strcpy(_collection, "");
strcpy(_json, "{}");
};
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in KintoForIoT sketches, this library, and other libraries
void Data::connect(char* server, char* token, char* secret, char* collection)
{
strcpy(_server, server);
strcpy(_token, token);
strcpy(_secret, secret);
strcpy(_collection, collection);
_connected = true;
};
void Data::read(char* key, char* value)
{
// By design, the parser needs to alter the input string.
// We work on a copy to keep _properties unaltered.
char json[JSON_BUFFER_SIZE];
strcpy(json, _json);
DynamicJsonBuffer jsonBuffer;
JsonObject& data = jsonBuffer.parseObject(json);
if (data.success()) {
JsonVariant property = data[String(key)];
if (property.success()) {
strcpy(value, property.as<char*>());
}
else {
#ifdef DEBUG
Serial.print(key);
Serial.println(" does not exists");
#endif
strcpy(value, "");
}
}
else {
#ifdef DEBUG
Serial.println("parseObject() failed");
#endif
strcpy(value, "");
}
};
void Data::write(char* key, char* value)
{
// By design, the parser needs to alter the input string.
// We work on a copy to keep _properties unaltered.
char json[JSON_BUFFER_SIZE];
strcpy(json, _json);
DynamicJsonBuffer jsonBuffer;
JsonObject& data = jsonBuffer.parseObject(json);
if (data.success()) {
data[key] = value;
data.printTo(_json);
}
#ifdef DEBUG
Serial.println(_json);
#endif
};
void Data::get()
{
if (!isConnected()) { return; }
String jsonMessageBuffer;
char url[URL_BUFFER_SIZE] = "";
strcat(url, _server);
strcat(url, "/buckets/default/collections/");
strcat(url, _collection);
strcat(url, "/records?_limit=1");
#ifdef DEBUG
Serial.println(url);
#endif
HTTPClient http;
http.begin(url);
http.addHeader("Accept", "application/json");
http.setAuthorization(_token, _secret);
int httpCode = http.GET();
if (httpCode > 0 ) {
// HTTP header has been send and Server response header has been handled
// file found at server
if (httpCode == HTTP_CODE_OK) {
jsonMessageBuffer = http.getString();
#ifdef DEBUG
Serial.println(jsonMessageBuffer);
#endif
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(jsonMessageBuffer);
// Test if parsing succeeds.
if (root.success()) {
JsonObject& data = root["data"][0];
if (data.success()) {
size_t n = data.printTo(_json, sizeof(_json));
#ifdef DEBUG
Serial.println(_json);
#endif
}
else {
#ifdef DEBUG
Serial.println("data does not exist");
#endif
}
}
else {
#ifdef DEBUG
Serial.println("parseObject() failed");
#endif
}
}
}
else {
#ifdef DEBUG
Serial.println(httpCode); //Print HTTP return code
#endif
}
http.end();
};
void Data::post()
{
if (!isConnected()) { return; }
DynamicJsonBuffer jsonBufferRoot;
JsonObject& root = jsonBufferRoot.createObject();
root["data"] = RawJson(_json);
char output[JSON_BUFFER_SIZE];
size_t n = root.printTo(output, sizeof(output));
#ifdef DEBUG
Serial.println(output);
#endif
char url[URL_BUFFER_SIZE] = "";
strcat(url, _server);
strcat(url, "/buckets/default/collections/");
strcat(url, _collection);
strcat(url, "/records");
#ifdef DEBUG
Serial.println(url);
#endif
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
http.setAuthorization(_token, _secret);
int httpCode = http.POST(output);
#ifdef DEBUG
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
#endif
http.end(); //Close connection
};
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library