-
Notifications
You must be signed in to change notification settings - Fork 1
/
VariPass.cpp
263 lines (217 loc) · 8.77 KB
/
VariPass.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Library: VariPass for ESP8266 and ESP32
Programmed by: Thorinair
Version: v1.3.0
Description: Provides an API for easily exchanging data with the VariPass (varipass.org) website.
Usage:
First add this library to the "libraries" folder in your Arduino workspace.
Then, include the library in your program by calling #include <VariPass.h>.
Please use VariPass.h file as reference for different functions and values.
Requirements:
This library requires you to have an ESP8266 or ESP32 based board.
Your program needs to be connected to a WiFi for this library to work.
*/
#include "VariPass.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#elif ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#endif
#define HOST "http://api.varipass.org"
static String splitString(String data, char separator, int index);
static String request(String path);
static void varipassWrite(String key, String id, String value, int* status);
static String varipassRead(String key, String id, int* status);
static String splitString(String data, char separator, int index) {
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for(int i = 0; i <= maxIndex && found <= index; i++) {
if(data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
static String request(String path) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(HOST + path);
int httpCode = http.GET();
if (httpCode != 200)
return "error_connect";
String body = http.getString();
http.end();
return body;
}
else {
return "error_wifi";
}
}
static void varipassWrite(String key, String id, String value, int* result) {
String response = request("/?key=" + key + "&action=swrite&id=" + id + "&value=" + value);
if (response == "success")
*result = VARIPASS_RESULT_SUCCESS;
else if (response == "error_invalid_key")
*result = VARIPASS_RESULT_ERROR_INVALID_KEY;
else if (response == "error_invalid_id")
*result = VARIPASS_RESULT_ERROR_INVALID_ID;
else if (response == "error_cooldown")
*result = VARIPASS_RESULT_ERROR_COOLDOWN;
else if (response == "error_unconfirmed")
*result = VARIPASS_RESULT_ERROR_UNCONFIRMED;
else if (response == "error_banned")
*result = VARIPASS_RESULT_ERROR_BANNED;
else if (response == "error_empty_variable")
*result = VARIPASS_RESULT_ERROR_EMPTY_VARIABLE;
else if (response == "error_db")
*result = VARIPASS_RESULT_ERROR_DB;
else if (response == "error_wifi")
*result = VARIPASS_RESULT_ERROR_WIFI;
else if (response == "error_connect")
*result = VARIPASS_RESULT_ERROR_CONNECT;
else
*result = VARIPASS_RESULT_ERROR_UNKNOWN;
}
void varipassWriteInt(String key, String id, long value, int* result) {
varipassWrite(key, id, String(value), result);
}
void varipassWriteFloat(String key, String id, double value, int* result, char decimals) {
varipassWrite(key, id, String(value, decimals), result);
}
void varipassWriteBool(String key, String id, bool value, int* result) {
String boolval = "false";
if (value)
boolval = "true";
varipassWrite(key, id, boolval, result);
}
void varipassWriteString(String key, String id, String value, int* result) {
varipassWrite(key, id, value, result);
}
static String varipassRead(String key, String id, int* result) {
String response = request("/?key=" + key + "&action=sread&id=" + id);
String responseResult = splitString(response, '|', 0);
if (responseResult == "success") {
*result = VARIPASS_RESULT_SUCCESS;
return splitString(response, '|', 1);
}
else {
if (response == "error_invalid_key")
*result = VARIPASS_RESULT_ERROR_INVALID_KEY;
else if (response == "error_invalid_id")
*result = VARIPASS_RESULT_ERROR_INVALID_ID;
else if (response == "error_cooldown")
*result = VARIPASS_RESULT_ERROR_COOLDOWN;
else if (response == "error_unconfirmed")
*result = VARIPASS_RESULT_ERROR_UNCONFIRMED;
else if (response == "error_banned")
*result = VARIPASS_RESULT_ERROR_BANNED;
else if (response == "error_empty_variable")
*result = VARIPASS_RESULT_ERROR_EMPTY_VARIABLE;
else if (response == "error_db")
*result = VARIPASS_RESULT_ERROR_DB;
else if (response == "error_wifi")
*result = VARIPASS_RESULT_ERROR_WIFI;
else if (response == "error_connect")
*result = VARIPASS_RESULT_ERROR_CONNECT;
else
*result = VARIPASS_RESULT_ERROR_UNKNOWN;
return "";
}
}
long varipassReadInt(String key, String id, int* result) {
return atol(varipassRead(key, id, result).c_str());
}
double varipassReadFloat(String key, String id, int* result) {
return atof(varipassRead(key, id, result).c_str());
}
bool varipassReadBool(String key, String id, int* result) {
String value = varipassRead(key, id, result);
if (value == "true")
return true;
else
return false;
}
String varipassReadString(String key, String id, int* result) {
return varipassRead(key, id, result);
}
String varipassGetResultDescription(int result) {
String description;
switch (result) {
case VARIPASS_RESULT_ERROR_UNKNOWN:
description = "An unknown error has occurred. Usually caused by timeouts.";
break;
case VARIPASS_RESULT_SUCCESS:
description = "Data has been successfully written or read from VariPass.";
break;
case VARIPASS_RESULT_ERROR_INVALID_KEY:
description = "Your API key is invalid.";
break;
case VARIPASS_RESULT_ERROR_INVALID_ID:
description = "Your variable ID is invalid.";
break;
case VARIPASS_RESULT_ERROR_COOLDOWN:
description = "You are sending data too often. Check the cooldown for this variable on your Dashboard.";
break;
case VARIPASS_RESULT_ERROR_UNCONFIRMED:
description = "You email address is not yet confirmed. Click the link you have received in your inbox.";
break;
case VARIPASS_RESULT_ERROR_BANNED:
description = "Your account has been banned from VariPass.";
break;
case VARIPASS_RESULT_ERROR_EMPTY_VARIABLE:
description = "The variable you are trying to read or write to is empty. Please contact support.";
break;
case VARIPASS_RESULT_ERROR_DB:
description = "There is an error with the VariPass database. Please contact support.";
break;
case VARIPASS_RESULT_ERROR_WIFI:
description = "The WiFi is not connected.";
break;
case VARIPASS_RESULT_ERROR_CONNECT:
description = "There was an error while connecting to VariPass.";
break;
}
return description;
}
Variable * varipassLatest(String key, int* count, int* result) {
String response = request("/?key=" + key + "&action=slatest");
String responseResult = splitString(response, '|', 0);
if (responseResult == "success") {
*result = VARIPASS_RESULT_SUCCESS;
*count = atoi(splitString(response, '|', 1).c_str());
Variable *variables = (Variable*) malloc(*count * sizeof(Variable));
for (int i = 0; i < *count; i++) {
splitString(response, '|', 2 + i*2).toCharArray(variables[i].id, 9);
splitString(response, '|', 3 + i*2).toCharArray(variables[i].value, 256);
}
return variables;
}
else {
if (response == "error_invalid_key")
*result = VARIPASS_RESULT_ERROR_INVALID_KEY;
else if (response == "error_invalid_id")
*result = VARIPASS_RESULT_ERROR_INVALID_ID;
else if (response == "error_cooldown")
*result = VARIPASS_RESULT_ERROR_COOLDOWN;
else if (response == "error_unconfirmed")
*result = VARIPASS_RESULT_ERROR_UNCONFIRMED;
else if (response == "error_banned")
*result = VARIPASS_RESULT_ERROR_BANNED;
else if (response == "error_empty_variable")
*result = VARIPASS_RESULT_ERROR_EMPTY_VARIABLE;
else if (response == "error_db")
*result = VARIPASS_RESULT_ERROR_DB;
else if (response == "error_wifi")
*result = VARIPASS_RESULT_ERROR_WIFI;
else if (response == "error_connect")
*result = VARIPASS_RESULT_ERROR_CONNECT;
else
*result = VARIPASS_RESULT_ERROR_UNKNOWN;
return NULL;
}
}