Skip to content

Commit 792eb4b

Browse files
Initial commit
0 parents  commit 792eb4b

File tree

6 files changed

+387
-0
lines changed

6 files changed

+387
-0
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Change Log
2+
3+
## [Unreleased]
4+
### Added
5+
- CLIENT: Create requester and main method
6+
7+
## [0.3] - 2016-09-15
8+
### Added
9+
- CLIENT: Create readResponse() method in SlicingDice class
10+
11+
## [0.2] - 2016-09-14
12+
### Added
13+
- CLIENT: Create makeRequest() method in SlicingDice class
14+
15+
### Removed
16+
- CLIENT: Create ApiClient class
17+
18+
## [0.1] - 2016-09-07
19+
### Added
20+
- CLIENT: Create SlicingDice class
21+
- CLIENT: Create ApiClient class
22+
- CLIENT: Create index() main method in class SlicingDice

LICENSE.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 SlicingDice LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# SlicingDice Official Arduino Client (v1.0)
2+
3+
Official Arduino client for [SlicingDice](http://www.slicingdice.com/), Data Warehouse and Analytics Database as a Service.
4+
5+
## Documentation
6+
7+
If you are new to SlicingDice, check our [quickstart guide](http://panel.slicingdice.com/docs/#quickstart-guide) and learn to use it in 15 minutes.
8+
9+
Please refer to the [SlicingDice official documentation](http://panel.slicingdice.com/docs/) for more information on [analytics databases](http://panel.slicingdice.com/docs/#analytics-concepts), [data modeling](http://panel.slicingdice.com/docs/#data-modeling), [indexing](http://panel.slicingdice.com/docs/#data-indexing), [querying](http://panel.slicingdice.com/docs/#data-querying), [limitations](http://panel.slicingdice.com/docs/#current-slicingdice-limitations) and [API details](http://panel.slicingdice.com/docs/#api-details).
10+
11+
## Installing
12+
13+
[Click here]() to download our Arduino client as a `zip` file. After downloading it, you only need to import the zipped contents into your project path.
14+
15+
## Usage
16+
17+
```c
18+
#include <SlicingDice.h>
19+
#include <ArduinoJson.h>
20+
21+
#define BUFFER_SIZE 256
22+
23+
SlicingDice sd = SlicingDice("API_KEY");
24+
25+
void setup() {
26+
// Open serial communication and wait for port to open
27+
Serial.begin(9600);
28+
while (!Serial) {
29+
}
30+
31+
// Arduino network settings
32+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
33+
byte ip[] = { 192, 168, 1, 10 };
34+
byte gateway[] = { 192, 168, 1, 1 };
35+
byte subnet[] = { 255, 255, 255, 0 };
36+
byte dns[] = { 8, 8, 8, 8 };
37+
Ethernet.begin(mac, ip, dns, gateway, subnet);
38+
}
39+
40+
void loop() {
41+
// Create JSON object
42+
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
43+
JsonObject& queryIndex = jsonBuffer.createObject();
44+
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("[email protected]");
45+
nestedQueryIndex["age"] = 22;
46+
47+
// Index object
48+
sd.index(queryIndex);
49+
50+
// Print response for debugging
51+
Serial.print("Status code: ");
52+
Serial.println(sd.statusCode);
53+
Serial.println(sd.response);
54+
}
55+
```
56+
57+
## Tests and examples
58+
59+
Whether you want to test the client installation or simply check more examples on how the client works, take a look at [tests directory](test/).
60+
61+
## Reference
62+
63+
`SlicingDice` encapsulates logic for sending requests to the [index endpoint](http://panel.slicingdice.com/docs/#api-details-api-endpoints-post-index).
64+
65+
### Attributes
66+
67+
* `statusCode (int)` - HTTP status code after indexing to SlicingDice. Should be `200` in ordinary circumstances or one of the HTTP requests defined at the [API errors](http://panel.slicingdice.com/docs/#api-details-api-errors) section.
68+
* `response (String)` - Response after indexing data. Useful for debugging purposes.
69+
70+
### Constructors
71+
72+
`SlicingDice(const char* apiKey)`
73+
* `apiKey (const char*)` - [API key](http://panel.slicingdice.com/docs/#api-details-api-connection-api-keys) to authenticate requests with the SlicingDice API.
74+
75+
`SlicingDice(const char* apiKey, const char* host)`
76+
* `apiKey (const char*)` - [API key](http://panel.slicingdice.com/docs/#api-details-api-connection-api-keys) to authenticate requests with the SlicingDice API.
77+
* `host (const char*)` - [Connection endpoint](http://panel.slicingdice.com/docs/#api-details-api-connection-connection-endpoints) to use when generating requests to SlicingDice.
78+
79+
`SlicingDice(const char* apiKey, const char* host, int port)`
80+
* `apiKey (const char*)` - [API key](http://panel.slicingdice.com/docs/#api-details-api-connection-api-keys) to authenticate requests with the SlicingDice API.
81+
* `host (const char*)` - [Connection endpoint](http://panel.slicingdice.com/docs/#api-details-api-connection-connection-endpoints) to use when generating requests to SlicingDice.
82+
* `port (int)` - Port to connect to when generating requests. Particularly useful when connect to `http://localhost`.
83+
84+
### `void index(JsonObject& query)`
85+
Index data to existing entities or create new entities, if necessary. This method corresponds to a [POST request at /index](http://panel.slicingdice.com/docs/#api-details-api-endpoints-post-index).
86+
87+
#### Request example
88+
89+
```c
90+
#include <SlicingDice.h>
91+
#include <ArduinoJson.h>
92+
93+
#define BUFFER_SIZE 256
94+
95+
SlicingDice sd = SlicingDice("API_KEY");
96+
97+
void setup() {
98+
// Open serial communication and wait for port to open
99+
Serial.begin(9600);
100+
while (!Serial) {
101+
}
102+
103+
// Arduino network settings
104+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
105+
byte ip[] = { 192, 168, 1, 10 };
106+
byte gateway[] = { 192, 168, 1, 1 };
107+
byte subnet[] = { 255, 255, 255, 0 };
108+
byte dns[] = { 8, 8, 8, 8 };
109+
Ethernet.begin(mac, ip, dns, gateway, subnet);
110+
}
111+
112+
void loop() {
113+
// Create JSON object
114+
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
115+
JsonObject& queryIndex = jsonBuffer.createObject();
116+
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("[email protected]");
117+
nestedQueryIndex["age"] = 22;
118+
119+
// Index object
120+
sd.index(queryIndex);
121+
122+
// Print response for debugging
123+
Serial.print("Status code: ");
124+
Serial.println(sd.statusCode);
125+
Serial.println(sd.response);
126+
}
127+
```
128+
129+
#### Output example
130+
131+
```
132+
Status code: 200
133+
{
134+
"status": "success",
135+
"indexed-entities": 1,
136+
"indexed-fields": 1,
137+
"took": 0.023
138+
}
139+
```
140+
141+
### `void index(JsonObject& query, boolean autoCreateFields)`
142+
Index data to existing entities or create new entities, if necessary. This method corresponds to a [POST request at /index](http://panel.slicingdice.com/docs/#api-details-api-endpoints-post-index).
143+
144+
#### Request example
145+
146+
```c
147+
#include <SlicingDice.h>
148+
#include <ArduinoJson.h>
149+
150+
#define BUFFER_SIZE 256
151+
152+
SlicingDice sd = SlicingDice("API_KEY");
153+
154+
void setup() {
155+
// Open serial communication and wait for port to open
156+
Serial.begin(9600);
157+
while (!Serial) {
158+
}
159+
160+
// Arduino network settings
161+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
162+
byte ip[] = { 192, 168, 1, 10 };
163+
byte gateway[] = { 192, 168, 1, 1 };
164+
byte subnet[] = { 255, 255, 255, 0 };
165+
byte dns[] = { 8, 8, 8, 8 };
166+
Ethernet.begin(mac, ip, dns, gateway, subnet);
167+
}
168+
169+
void loop() {
170+
// Create JSON object
171+
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
172+
JsonObject& queryIndex = jsonBuffer.createObject();
173+
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("[email protected]");
174+
nestedQueryIndex["age"] = 22;
175+
176+
// Index object
177+
sd.index(queryIndex, true);
178+
179+
// Print response for debugging
180+
Serial.print("Status code: ");
181+
Serial.println(sd.statusCode);
182+
Serial.println(sd.response);
183+
}
184+
```
185+
186+
#### Output example
187+
188+
```
189+
Status code: 200
190+
{
191+
"status": "success",
192+
"indexed-entities": 1,
193+
"indexed-fields": 1,
194+
"took": 0.023
195+
}
196+
```
197+
198+
## License
199+
200+
[MIT](https://opensource.org/licenses/MIT)

SlicingDice.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "SlicingDice.h"
2+
3+
4+
SlicingDice::SlicingDice(const char* apiUserKey) {
5+
host = "api.slicingdice.com/v1";
6+
port = 80;
7+
apiKey = apiUserKey;
8+
}
9+
10+
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost) {
11+
host = customHost;
12+
port = 80;
13+
apiKey = apiUserKey;
14+
}
15+
16+
SlicingDice::SlicingDice(const char* apiUserKey, const char* customHost, int customPort) {
17+
host = customHost;
18+
port = customPort;
19+
apiKey = apiUserKey;
20+
}
21+
22+
void SlicingDice::index(JsonObject& query) {
23+
char queryConverted[query.measureLength() + 1];
24+
query.printTo(queryConverted, sizeof(queryConverted));
25+
makeRequest(queryConverted);
26+
}
27+
28+
void SlicingDice::makeRequest(const char* query){
29+
client.connect(host, port);
30+
while (!client.connected()) {
31+
client.connect(host, port);
32+
}
33+
34+
client.println(F("POST /index/ HTTP/1.1"));
35+
client.println(F("Content-Type: application/json"));
36+
client.println(F("Connection: close"));
37+
client.print(F("Content-Length: "));
38+
client.println(strlen(query));
39+
client.println();
40+
client.print(query);
41+
readResponse();
42+
client.stop();
43+
}
44+
45+
void SlicingDice::readResponse(){
46+
response = " ";
47+
boolean currentLineIsBlank = true;
48+
boolean hasBody = false;
49+
boolean inStatus = false;
50+
51+
char statusCodeRequest[4];
52+
int i = 0;
53+
54+
while (client.connected()){
55+
if (client.available()) {
56+
char c = client.read();
57+
// Set status code request
58+
if(c == ' ' && !inStatus){
59+
inStatus = true;
60+
}
61+
62+
if(inStatus && i < 3 && c != ' '){
63+
statusCodeRequest[i] = c;
64+
i++;
65+
}
66+
if(i == 3){
67+
statusCodeRequest[i] = '\0';
68+
statusCode = atoi(statusCodeRequest);
69+
}
70+
71+
// Set response request
72+
if(hasBody){
73+
if(response != NULL) response.concat(c);
74+
} else {
75+
if (c == '\n' && currentLineIsBlank) {
76+
hasBody = true;
77+
}
78+
79+
else if (c == '\n') {
80+
currentLineIsBlank = true;
81+
}
82+
else if (c != '\r') {
83+
currentLineIsBlank = false;
84+
}
85+
}
86+
}
87+
}
88+
response.trim();
89+
}

SlicingDice.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <Arduino.h>
2+
#include <SPI.h>
3+
#include <ArduinoJson.h>
4+
#include <Ethernet.h>
5+
6+
7+
class SlicingDice {
8+
9+
public:
10+
SlicingDice(const char* apiUserKey);
11+
SlicingDice(const char* apiUserKey, const char* customHost);
12+
SlicingDice(const char* apiUserKey, const char* customHost, int customPort);
13+
14+
void index(JsonObject& query);
15+
int statusCode;
16+
String response;
17+
18+
private:
19+
void makeRequest(const char* query);
20+
void readResponse();
21+
22+
const char* apiKey;
23+
const char* host;
24+
int port;
25+
EthernetClient client;
26+
};

tests_and_examples/Simple.ino

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <SlicingDice.h>
2+
#include <ArduinoJson.h>
3+
4+
SlicingDice sd = SlicingDice("mytoken");
5+
6+
void setup() {
7+
// Open serial communications and wait for port to open:
8+
Serial.begin(9600);
9+
while (!Serial) {
10+
}
11+
12+
// Arduino network settings
13+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
14+
byte ip[] = { 192, 168, 1, 10 };
15+
byte gateway[] = { 192, 168, 1, 1 };
16+
byte subnet[] = { 255, 255, 255, 0 };
17+
byte dnxs[] = { 8, 8, 8, 8 };
18+
Ethernet.begin(mac, ip, dnxs, gateway, subnet);
19+
}
20+
21+
void loop() {
22+
StaticJsonBuffer<200> jsonBuffer;
23+
JsonObject& queryIndex = jsonBuffer.createObject();
24+
JsonObject& nestedQueryIndex = queryIndex.createNestedObject("[email protected]");
25+
nestedQueryIndex["age"] = 22;
26+
sd.index(queryIndex);
27+
Serial.print("Status code: ");
28+
Serial.println(sd.statusCode);
29+
Serial.println(sd.response);
30+
}

0 commit comments

Comments
 (0)